Skip to content

Commit 9e69b3d

Browse files
remove alias and use id instead to identify and remove cards (#1980)
* remove alias and use id instead to identify and remove cards * removed trailing space * change export to verifyCard * Co-authored-by: adarsh <adarshm11@users.noreply.github.com> * fix id type * change test stub to null * cleanup --------- Co-authored-by: adarshm11 <amlearning2023@gmail.com>
1 parent 0e9fc96 commit 9e69b3d

5 files changed

Lines changed: 41 additions & 43 deletions

File tree

api/main_endpoints/routes/OfficeAccessCard.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const { API_KEY = 'NOTHING_REALLY' } = officeAccessCard;
1717
const { decodeToken } = require('../util/token-functions.js');
1818
const ROWS_PER_PAGE = 25;
1919
const {
20-
checkIfCardExists,
20+
verifyCard,
2121
generateAlias,
2222
deleteCard,
2323
editAlias,
@@ -89,14 +89,14 @@ router.get('/verify', async (req, res) => {
8989
return res.sendStatus(FORBIDDEN);
9090
}
9191

92-
const cardExists = await checkIfCardExists({ cardBytes });
93-
if (cardExists) {
94-
const alias = cardExists.alias;
92+
const cardVerification = await verifyCard(cardBytes);
93+
if (cardVerification) {
94+
const alias = cardVerification.alias;
9595
AuditLog.create({
9696
action: AuditLogActions.VERIFY_CARD,
9797
details: { alias }
9898
});
99-
writeLogToClient(req.method, { alias: cardExists.alias, statusCode: OK });
99+
writeLogToClient(req.method, { alias: cardVerification.alias, statusCode: OK });
100100
return res.sendStatus(OK);
101101
}
102102
// if a card doesnt exist and we arent trying
@@ -143,30 +143,36 @@ router.post('/delete', async (req, res) => {
143143
return res.sendStatus(decoded.status);
144144
}
145145

146-
const { alias } = req.body;
147-
if (!alias) {
146+
const { _id } = req.body;
147+
if (!_id) {
148148
writeLogToClient(req.method, {
149149
statusCode: BAD_REQUEST,
150150
message: 'cardBytes missing from request',
151151
});
152152
return res.sendStatus(BAD_REQUEST);
153153
}
154+
const cardDeletion = await deleteCard(_id);
154155

155-
const cardExists = await checkIfCardExists({ alias });
156-
if (!await cardExists) {
156+
if (cardDeletion === null) {
157157
logger.info('Card does not exist');
158158
writeLogToClient(req.method, {
159159
statusCode: NOT_FOUND,
160160
message: 'Card does not exist',
161161
});
162162
return res.sendStatus(NOT_FOUND);
163-
}
164-
165-
if (await deleteCard(alias)) {
163+
} else if(cardDeletion === false){
164+
writeLogToClient(req.method, {
165+
statusCode: SERVER_ERROR,
166+
message: 'Error deleting card',
167+
});
168+
return res.sendStatus(SERVER_ERROR);
169+
} else {
170+
const alias = cardDeletion.alias;
166171
logger.info('Successfully deleted card');
167172
writeLogToClient(req.method, {
168-
alias,
173+
alias: cardDeletion.alias,
169174
statusCode: OK,
175+
_id
170176
});
171177
AuditLog.create({
172178
userId: decoded.token._id,
@@ -175,13 +181,6 @@ router.post('/delete', async (req, res) => {
175181
});
176182
return res.sendStatus(OK);
177183
}
178-
179-
writeLogToClient(req.method, {
180-
alias: cardExists.alias,
181-
statusCode: SERVER_ERROR,
182-
message: 'Error deleting card',
183-
});
184-
return res.sendStatus(SERVER_ERROR);
185184
});
186185

187186
router.post('/getAllCards', async (req, res) => {

api/main_endpoints/util/OfficeAccessCard.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ const OfficeAccessCard = require('../models/OfficeAccessCard.js');
22
const logger = require('../../util/logger');
33
const { ADJECTIVES, NOUNS } = require('../../util/CardReaderConstants.js');
44

5-
function checkIfCardExists({ cardBytes = null, alias = null } = {}) {
6-
const body = (cardBytes !== null) ? { cardBytes } : { alias };
5+
function verifyCard(cardBytes) {
76
return new Promise((resolve) => {
87
try {
98
OfficeAccessCard.findOneAndUpdate(
10-
body,
9+
{ cardBytes },
1110
{
1211
$inc: { verifiedCount: 1 },
1312
$set: { lastVerified: Date.now() }
@@ -16,7 +15,7 @@ function checkIfCardExists({ cardBytes = null, alias = null } = {}) {
1615
}
1716
, (error, result) => {
1817
if (error) {
19-
logger.error('checkIfCardExists got an error querying mongodb: ', error);
18+
logger.error('verifyCard got an error querying mongodb: ', error);
2019
return resolve(false);
2120
}
2221
if (!result) {
@@ -26,7 +25,7 @@ function checkIfCardExists({ cardBytes = null, alias = null } = {}) {
2625
return resolve(result); // return the document
2726
});
2827
} catch (error) {
29-
logger.error('checkIfCardExists caught an error: ', error);
28+
logger.error('verifyCard caught an error: ', error);
3029
return resolve(false);
3130
}
3231
});
@@ -64,20 +63,21 @@ async function generateAlias() {
6463
return new Date().toGMTString();
6564
}
6665

67-
function deleteCard(alias) {
66+
function deleteCard(_id) {
6867
return new Promise((resolve) => {
6968
try {
70-
OfficeAccessCard.findOneAndDelete(
71-
{ alias }
72-
, (error, result) => {
69+
OfficeAccessCard.findByIdAndDelete(
70+
_id,
71+
(error, result) => {
7372
if (error) {
7473
logger.error('deleteCard got an error querying mongodb: ', error);
7574
return resolve(false);
7675
}
7776
if (!result) {
78-
logger.info(`Card ${ alias } not found in the database`);
77+
logger.info(`Card with id: ${_id} not found in the database`);
78+
return resolve(null);
7979
}
80-
return resolve(!!result);
80+
return resolve(result);
8181
}
8282
);
8383
} catch (error) {
@@ -113,4 +113,4 @@ function editAlias(_id, newAlias) {
113113
});
114114
}
115115

116-
module.exports = { checkIfCardExists, generateAlias, deleteCard, editAlias };
116+
module.exports = { verifyCard, generateAlias, deleteCard, editAlias };

src/APIFunctions/CardReader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function getAllCardsFromDb({
3131
return status;
3232
}
3333

34-
export async function deleteCardFromDb(token, alias) {
34+
export async function deleteCardFromDb(token, _id) {
3535
let status = new ApiResponse();
3636
try {
3737
const url = new URL('/api/OfficeAccessCard/delete', BASE_API_URL);
@@ -41,7 +41,7 @@ export async function deleteCardFromDb(token, alias) {
4141
'Authorization': `Bearer ${token}`,
4242
'Content-Type': 'application/json',
4343
},
44-
body: JSON.stringify({ alias, }),
44+
body: JSON.stringify({ _id }),
4545
});
4646
if (res.ok) {
4747
const result = await res.json();

src/Pages/CardReader/CardReader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ export default function CardReader() {
370370
cancelText: 'No, keep the card',
371371
confirmClassAddons: 'bg-red-600 hover:bg-red-500',
372372
handleConfirmation: async () => {
373-
await deleteCardFromDb(token, cardToDelete.alias);
373+
await deleteCardFromDb(token, cardToDelete._id);
374374
await getAllCards();
375375
setToggleDelete(!toggleDelete);
376376
},

test/api/OfficeAccessCard.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const {
2020
SERVER_ERROR,
2121
FORBIDDEN,
2222
} = require('../../api/util/constants').STATUS_CODES;
23-
const { MEMBERSHIP_STATE } = require('../../api/util/constants');
2423
const {
2524
initializeTokenMock,
2625
setTokenStatus,
@@ -45,16 +44,15 @@ const token = '';
4544

4645
describe('OfficeAccessCard', () => {
4746
let deleteCardStub = null;
48-
let getAllCardsStub = null;
49-
let editAliasStub = null;
5047
let testCardId = null;
5148

5249
const VALID_CARD_BYTES = 'wesleys card';
5350
const NEW_CARD_BYTES = 'dials card';
5451
const INVALID_CARD_BYTES = 'evans card';
5552

53+
const VALID_ID = id.toString();
54+
const INVALID_ID = 'tiffanys id';
5655
const VALID_ALIAS = 'gauravs card';
57-
const INVALID_ALIAS = 'bobs card';
5856
const NEW_ALIAS = 'updated test card';
5957
const EMPTY_ALIAS = '';
6058
const WHITESPACE_ALIAS = ' ';
@@ -76,6 +74,7 @@ describe('OfficeAccessCard', () => {
7674
// Before each test we empty the database
7775
tools.emptySchema(OfficeAccessCard);
7876
const testOfficeAccessCard = new OfficeAccessCard({
77+
_id: VALID_ID,
7978
cardBytes: VALID_CARD_BYTES,
8079
alias: VALID_ALIAS,
8180
verifiedCount: INCREMENT_VERIFY_COUNT,
@@ -194,9 +193,9 @@ describe('OfficeAccessCard', () => {
194193

195194
it('Should return 404 if the card attempted to be deleted was not found', async () => {
196195
setTokenStatus(true);
197-
deleteCardStub.resolves(false);
196+
deleteCardStub.resolves(null);
198197
const result = await test.sendPostRequestWithToken(token,
199-
DELETE_API_PATH, { alias: INVALID_ALIAS },
198+
DELETE_API_PATH, { _id: INVALID_ID },
200199
);
201200
expect(result).to.have.status(NOT_FOUND);
202201
});
@@ -205,7 +204,7 @@ describe('OfficeAccessCard', () => {
205204
setTokenStatus(true);
206205
deleteCardStub.resolves(true);
207206
const result = await test.sendPostRequestWithToken(token,
208-
DELETE_API_PATH, { alias: VALID_ALIAS },
207+
DELETE_API_PATH, { _id: VALID_ID },
209208
);
210209
expect(result).to.have.status(OK);
211210
});
@@ -214,7 +213,7 @@ describe('OfficeAccessCard', () => {
214213
setTokenStatus(true);
215214
deleteCardStub.resolves(false);
216215
const result = await test.sendPostRequestWithToken(token,
217-
DELETE_API_PATH, { alias: VALID_ALIAS },
216+
DELETE_API_PATH, { _id: VALID_ID },
218217
);
219218
expect(result).to.have.status(SERVER_ERROR);
220219
});

0 commit comments

Comments
 (0)