Create Card Reader page for admin UI#1652
Conversation
| router.get('/listen', (req, res) => { | ||
| res.setHeader('Content-Type', 'text/event-stream'); | ||
| res.setHeader('Cache-Control', 'no-cache'); |
There was a problem hiding this comment.
can we add verification to this api, where the user has to have a valid token
| const response = { | ||
| endpoint: '/verify', | ||
| statusCode: 200, | ||
| message: 'Card authorized!' | ||
| }; | ||
|
|
||
| const writeRequestResponse = () => { |
There was a problem hiding this comment.
response shouldnt be global, instead something you initialize before you call writeRequestResponse and pass as a parameter
| const response = { | |
| endpoint: '/verify', | |
| statusCode: 200, | |
| message: 'Card authorized!' | |
| }; | |
| const writeRequestResponse = () => { | |
| const writeRequestResponse = (response) => { |
|
|
||
| const cardExists = await checkIfCardExists(cardBytes); | ||
| if (cardExists) { | ||
| response.endpoint = '/verify'; // if user tried to add a card that already existed, ignore the "add" parameter |
There was a problem hiding this comment.
we dont need this line?
evanugarte
left a comment
There was a problem hiding this comment.
i think we should call writeRequestResponse with the object that we want to send back to the client, like
const defaultResponse = {
cardWasAdded: false,
message: null,
endpoint: '/verify',
};
const writeRequestResponse = ({ statusCode, ...rest }) => {
const response = {
statusCode,
...defaultResponse,
...rest, // override any default values
};
clients.forEach(client => {
client.res.write(`data: ${JSON.stringify(response)}\n\n`);
});
};that way we can call on it like
writeRequestResponse({ statusCode: OK });
writeRequestResponse({ statusCode: OK, cardWasAdded: true });
writeRequestResponse({ statusCode: NOT_FOUND });
writeRequestResponse({
statusCode: BAD_REQUEST,
message: `${missingValue.title} missing from request`,
});| } | ||
| }); | ||
|
|
||
| router.get('/delete', async (req, res) => { |
There was a problem hiding this comment.
lets move this to another pr? something that would be merged after this
| }); | ||
| } | ||
|
|
||
| function deleteCard(cardBytes) { |
There was a problem hiding this comment.
this can also be in another pr
ef5f60f to
f087c18
Compare
| let endpoint = data.endpoint; | ||
| if (!endpoint.includes('?add=1')) { | ||
| endpoint += ' '; | ||
| } |
There was a problem hiding this comment.
i think we can use the field cardWasAdded?
| if (!endpoint.includes('?add=1')) { | ||
| endpoint += ' '; | ||
| } | ||
| return [new Date().toISOString(), endpoint, data.statusCode, data.message].join(' '); |
There was a problem hiding this comment.
up to you, what if we use '\t' instead? or does it not look as good
There was a problem hiding this comment.
\t is inconsistent for some reason so I used padEnd() instead
| let data = JSON.parse(event.data); | ||
| let newLog = buildLog(data); | ||
| setLogs(currLogs => [newLog, ...currLogs]); // prepend the new log |
There was a problem hiding this comment.
lets put a try/catch in here, so if for some reason the data cant be parsed we do
try {
let data = JSON.parse(event.data);
let newLog = buildLog(data);
setLogs(currLogs => [newLog, ...currLogs]);
} catch (e) {
console.error('unable to format event data from /listen', e);
setLogs(
(currLogs) => [
'[error] unable to format response, check browser logs',
...currLogs,
]
);
}There was a problem hiding this comment.
also the above code likely needs to be linted
| await new OfficeAccessCard({ | ||
| cardBytes | ||
| }).save(); | ||
| writeRequestResponse({ statusCode: OK, message: 'Card added!', endpoint: '/verify?add=1' }); |
There was a problem hiding this comment.
is there a way to dynamically get the endpoint from req?
I created a card reader page only visibly to admins/officers for Step 2 of the SCE Badge Reader project