Skip to content

Commit 003c142

Browse files
committed
revert backend changes
1 parent a3cec14 commit 003c142

8 files changed

Lines changed: 21 additions & 219 deletions

File tree

api/main_endpoints/routes/OfficeAccessCard.js

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const {
2020
checkIfCardExists,
2121
generateAlias,
2222
deleteCard,
23-
editAlias,
2423
} = require('../util/OfficeAccessCard.js');
2524
const AuditLogActions = require('../util/auditLogActions.js');
2625
const AuditLog = require('../models/AuditLog.js');
@@ -81,20 +80,12 @@ router.get('/verify', async (req, res) => {
8180
return res.status(BAD_REQUEST).send(`${missingValue.title} missing from request`);
8281
}
8382

84-
if (!apiKey) {
85-
writeLogToClient(req.method, {
86-
statusCode: UNAUTHORIZED,
87-
message: 'API key missing from request',
88-
});
89-
return res.sendStatus(UNAUTHORIZED);
90-
}
91-
9283
if (apiKey !== API_KEY) {
9384
writeLogToClient(req.method, {
94-
statusCode: FORBIDDEN,
85+
statusCode: UNAUTHORIZED,
9586
message: `Invalid API key: ${apiKey}`,
9687
});
97-
return res.sendStatus(FORBIDDEN);
88+
return res.sendStatus(UNAUTHORIZED);
9889
}
9990

10091
const cardExists = await checkIfCardExists({ cardBytes });
@@ -218,56 +209,6 @@ router.post('/getAllCards', async (req, res) => {
218209
}
219210
});
220211

221-
router.post('/edit', async (req, res) => {
222-
const decoded = await decodeToken(req);
223-
if (decoded.status !== OK) {
224-
return res.sendStatus(decoded.status);
225-
}
226-
227-
const { _id, alias } = req.body;
228-
229-
if (!_id || !alias) {
230-
return res.status(BAD_REQUEST).send('_id and alias are required in request body');
231-
}
232-
233-
// Validate alias is not empty or whitespace only
234-
if (!alias.trim()) {
235-
return res.status(BAD_REQUEST).send('alias cannot be empty or whitespace only');
236-
}
237-
238-
// Validate _id is a valid ObjectId format
239-
if (!/^[0-9a-fA-F]{24}$/.test(_id)) {
240-
return res.status(BAD_REQUEST).send('_id must be a valid ObjectId');
241-
}
242-
243-
try {
244-
const updatedCard = await editAlias(_id, alias);
245-
246-
if (!updatedCard) {
247-
return res.status(NOT_FOUND).send('Card not found');
248-
}
249-
250-
// Log the edit action
251-
AuditLog.create({
252-
userId: decoded.token._id,
253-
action: AuditLogActions.EDIT_CARD,
254-
details: {
255-
newAlias: alias,
256-
oldAlias: updatedCard.alias !== alias ? 'unknown' : alias
257-
}
258-
});
259-
260-
logger.info(`Card alias updated successfully for card ID: ${_id}`);
261-
return res.status(OK).json({
262-
message: 'Card alias updated successfully',
263-
card: updatedCard
264-
});
265-
} catch (error) {
266-
logger.error('Error updating card alias: ', error);
267-
return res.status(SERVER_ERROR).send('Error updating card alias');
268-
}
269-
});
270-
271212
router.get('/listen', async (req, res) => {
272213
const decoded = await decodeToken(req, membershipState.OFFICER);
273214
if (decoded.status !== OK) {

api/main_endpoints/util/OfficeAccessCard.js

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ function checkIfCardExists({ cardBytes = null, alias = null } = {}) {
2020
return resolve(false);
2121
}
2222
if (!result) {
23-
logger.info('Card not found in the database');
23+
const { description } = body;
24+
logger.info(`Card:${description} not found in the database`);
2425
}
2526
return resolve(result); // return the document
2627
});
@@ -86,30 +87,4 @@ function deleteCard(alias) {
8687
});
8788
}
8889

89-
function editAlias(_id, newAlias) {
90-
return new Promise((resolve) => {
91-
try {
92-
OfficeAccessCard.findByIdAndUpdate(
93-
_id,
94-
{ $set: { alias: newAlias } },
95-
{ new: true, useFindAndModify: false },
96-
(error, result) => {
97-
if (error) {
98-
logger.error('editAlias got an error querying mongodb: ', error);
99-
return resolve(false);
100-
}
101-
if (!result) {
102-
logger.info(`Card with id ${_id} not found in the database`);
103-
return resolve(false);
104-
}
105-
return resolve(result);
106-
}
107-
);
108-
} catch (error) {
109-
logger.error('editAlias caught an error: ', error);
110-
return resolve(false);
111-
}
112-
});
113-
}
114-
115-
module.exports = { checkIfCardExists, generateAlias, deleteCard, editAlias };
90+
module.exports = { checkIfCardExists, generateAlias, deleteCard };

api/main_endpoints/util/auditLogActions.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const AuditLogActions = {
1414
VERIFY_CARD: 'VERIFY_CARD',
1515
ADD_CARD: 'ADD_CARD',
1616
DELETE_CARD: 'DELETE_CARD',
17-
EDIT_CARD: 'EDIT_CARD',
1817
};
1918

2019
module.exports = AuditLogActions;

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/APIFunctions/CardReader.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,3 @@ export async function deleteCardFromDb(token, alias) {
5555
}
5656
return status;
5757
}
58-
59-
export async function editCardAlias(token, _id, alias) {
60-
let status = new ApiResponse();
61-
try {
62-
const url = new URL('/api/OfficeAccessCard/edit', BASE_API_URL);
63-
const res = await fetch(url.href, {
64-
method: 'POST',
65-
headers: {
66-
'Authorization': `Bearer ${token}`,
67-
'Content-Type': 'application/json',
68-
},
69-
body: JSON.stringify({ _id, alias }),
70-
});
71-
if (res.ok) {
72-
const result = await res.json();
73-
status.responseData = result;
74-
} else {
75-
status.error = true;
76-
}
77-
} catch (err) {
78-
status.error = true;
79-
status.responseData = err;
80-
}
81-
return status;
82-
}

src/Pages/Overview/SVG.js

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config/config.example.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
"TINYMCE_API_KEY": "XXXXXXXXXXXXXXXXXXXXX",
44
"GOOGLE_API_CLIENT_ID": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com"
55
}
6-

test/api/OfficeAccessCard.js

Lines changed: 3 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,17 @@ const token = '';
4545
describe('OfficeAccessCard', () => {
4646
let deleteCardStub = null;
4747
let getAllCardsStub = null;
48-
let editAliasStub = null;
49-
let testCardId = null;
5048

5149
const VALID_CARD_BYTES = 'wesleys card';
5250
const NEW_CARD_BYTES = 'dials card';
5351
const INVALID_CARD_BYTES = 'evans card';
5452

5553
const VALID_ALIAS = 'gauravs card';
5654
const INVALID_ALIAS = 'bobs card';
57-
const NEW_ALIAS = 'updated test card';
58-
const EMPTY_ALIAS = '';
59-
const WHITESPACE_ALIAS = ' ';
6055

6156
const VERIFY_API_PATH = '/api/OfficeAccessCard/verify';
6257
const DELETE_API_PATH = '/api/OfficeAccessCard/delete';
6358
const GET_ALL_CARDS_API_PATH = '/api/OfficeAccessCard/getAllCards';
64-
const EDIT_API_PATH = '/api/OfficeAccessCard/edit';
6559
const INCREMENT_VERIFY_COUNT = 0;
6660

6761
before(() => {
@@ -82,10 +76,7 @@ describe('OfficeAccessCard', () => {
8276
});
8377
return new Promise((resolve, reject) => {
8478
testOfficeAccessCard.save()
85-
.then(savedCard => {
86-
testCardId = savedCard._id.toString();
87-
resolve(savedCard);
88-
})
79+
.then(resolve)
8980
.catch(reject);
9081
});
9182
});
@@ -125,14 +116,14 @@ describe('OfficeAccessCard', () => {
125116
expect(result).to.have.status(BAD_REQUEST);
126117
});
127118

128-
it('Should return 403 with invalid api key', async () => {
119+
it('Should return 401 with invalid api key', async () => {
129120
const params = new URLSearchParams();
130121
params.append('cardBytes', VALID_CARD_BYTES);
131122
const path = VERIFY_API_PATH + '?' + params.toString();
132123
const invalidApiKey = API_KEY + '-invalid-suffix';
133124
const result = await test.sendGetRequestWithApiKey(
134125
invalidApiKey + '', path);
135-
expect(result).to.have.status(FORBIDDEN);
126+
expect(result).to.have.status(UNAUTHORIZED);
136127
});
137128

138129
it('Should return 404 with valid api key and unknown card', async () => {
@@ -250,94 +241,4 @@ describe('OfficeAccessCard', () => {
250241
});
251242
});
252243

253-
describe('POST edit', () => {
254-
it('Should return 401 when token is not sent', async () => {
255-
const result = await test.sendPostRequest(EDIT_API_PATH);
256-
expect(result).to.have.status(UNAUTHORIZED);
257-
});
258-
259-
it('Should return 401 when invalid token is sent', async () => {
260-
const result = await test.sendPostRequestWithToken(token,
261-
EDIT_API_PATH);
262-
expect(result).to.have.status(UNAUTHORIZED);
263-
});
264-
265-
it('Should return 400 when _id is missing from request body', async () => {
266-
setTokenStatus(true);
267-
const result = await test.sendPostRequestWithToken(token,
268-
EDIT_API_PATH, { alias: NEW_ALIAS });
269-
expect(result).to.have.status(BAD_REQUEST);
270-
});
271-
272-
it('Should return 400 when alias is missing from request body', async () => {
273-
setTokenStatus(true);
274-
const result = await test.sendPostRequestWithToken(token,
275-
EDIT_API_PATH, { _id: testCardId });
276-
expect(result).to.have.status(BAD_REQUEST);
277-
});
278-
279-
it('Should return 404 when trying to edit a non-existent card', async () => {
280-
setTokenStatus(true);
281-
const nonExistentId = new mongoose.Types.ObjectId().toString();
282-
const result = await test.sendPostRequestWithToken(token,
283-
EDIT_API_PATH, { _id: nonExistentId, alias: NEW_ALIAS });
284-
expect(result).to.have.status(NOT_FOUND);
285-
});
286-
287-
it('Should return 400 when _id is not a valid ObjectId', async () => {
288-
setTokenStatus(true);
289-
const result = await test.sendPostRequestWithToken(token,
290-
EDIT_API_PATH, { _id: 'invalid-id', alias: NEW_ALIAS });
291-
expect(result).to.have.status(BAD_REQUEST);
292-
});
293-
294-
it('Should return 200 and successfully update alias for valid request', async () => {
295-
setTokenStatus(true);
296-
const result = await test.sendPostRequestWithToken(token,
297-
EDIT_API_PATH, { _id: testCardId, alias: NEW_ALIAS });
298-
expect(result).to.have.status(OK);
299-
expect(result.body).to.have.property('message', 'Card alias updated successfully');
300-
expect(result.body).to.have.property('card');
301-
expect(result.body.card).to.have.property('alias', NEW_ALIAS);
302-
});
303-
304-
it('Should actually update the alias in the database', async () => {
305-
setTokenStatus(true);
306-
await test.sendPostRequestWithToken(token,
307-
EDIT_API_PATH, { _id: testCardId, alias: NEW_ALIAS });
308-
309-
const updatedCard = await OfficeAccessCard.findById(testCardId);
310-
expect(updatedCard.alias).to.equal(NEW_ALIAS);
311-
});
312-
313-
it('Should handle empty alias by returning 400', async () => {
314-
setTokenStatus(true);
315-
const result = await test.sendPostRequestWithToken(token,
316-
EDIT_API_PATH, { _id: testCardId, alias: EMPTY_ALIAS });
317-
expect(result).to.have.status(BAD_REQUEST);
318-
});
319-
320-
it('Should handle whitespace-only alias by returning 400', async () => {
321-
setTokenStatus(true);
322-
const result = await test.sendPostRequestWithToken(token,
323-
EDIT_API_PATH, { _id: testCardId, alias: WHITESPACE_ALIAS });
324-
expect(result).to.have.status(BAD_REQUEST);
325-
});
326-
327-
it('Should preserve other card properties when updating alias', async () => {
328-
setTokenStatus(true);
329-
const originalCard = await OfficeAccessCard.findById(testCardId);
330-
const originalCardBytes = originalCard.cardBytes;
331-
const originalVerifiedCount = originalCard.verifiedCount;
332-
333-
await test.sendPostRequestWithToken(token,
334-
EDIT_API_PATH, { _id: testCardId, alias: NEW_ALIAS });
335-
336-
const updatedCard = await OfficeAccessCard.findById(testCardId);
337-
expect(updatedCard.cardBytes).to.equal(originalCardBytes);
338-
expect(updatedCard.verifiedCount).to.equal(originalVerifiedCount);
339-
expect(updatedCard.alias).to.equal(NEW_ALIAS);
340-
});
341-
});
342-
343244
});

0 commit comments

Comments
 (0)