-
Notifications
You must be signed in to change notification settings - Fork 30
Description
there are some bugs and redundancies in the way that the card reader verifies, deletes, and updates cards. we need to adjust the logic of these operations a little bit.
relevant files for card reader:
/src/Pages/CardReader/CardReader.js- UI components/src/APIFunctions/CardReader.js- API functions (connect frontend with backend)/api/main_endpoints/routes/OfficeAccessCard.js- card reader API/api/main_endpoints/util/OfficeAccessCard.js- card reader utility functions (for database operations)/test/api/OfficeAccessCard.js- api tests for card reader
you should understand roughly how these files work together from the dessert problem, but here's a refresher:
user action on the UI -> function call to APIFunctions -> API request to the main API -> database query made using util function
there are a couple problems that we need to fix with how card reader works right now...
-
the
/verifyendpoint verifies/adds cards using thecheckIfCardExists()utility function. as it stands, this function takes in eithercardBytesoralias. but this is a bit excessive, as we don't ever need to use alias.- let's simplify the function to take in only one parameter
cardBytes, and query the database to check for a card with the same value. as such, we can delete line 6 which assigns a variablebody, as this is unnecessary now that we know we pass incardBytesevery time. we can also delete the variable definition ofdescriptionon line 23, and have the next line log thecardBytesinstead - OPTIONAL: since this utility function is actually verifying cards and not just checking if they exist, we can rename the function to
verifyCard()so the functionality is more clear
- let's simplify the function to take in only one parameter
-
the
/deleteendpoint deletes cards given requests from the frontend, and uses thedeleteCard()utility function. this function currently takes in analiasparameter, which is a bit weird.- we should just do this with the card
_idfield for simplicity. let's updatedeleteCard()to take in an_idparameter and use the MongoDBfindByIdAndDelete()function. this returns the deleted document if successful, and null if the document was not found. - we also need to modify what the
deleteCard()function returns, because there are 3 cases: the card exists and was successfully deleted, the card doesn't exist, or there was an error deleting. sodeleteCard()should return:- the deleted document if the deletion was successful (this is the normal return value from
findByIdAndDelete() -
falseif the deletion encountered an error -
nullif the document could not be found (this is the normal return value fromfindByIdAndDelete()if a document wasn't found)
- the deleted document if the deletion was successful (this is the normal return value from
- now we need to adjust the
/deleteendpoint too. first we need to make sure the endpoint expects_idas its body parameter, notalias - next we need to handle each of the three cases for deleting. you'll notice that the endpoint currently uses
checkIfCardExists()- it shouldn't, so we can delete lines 155-163 (fromconst cardExists...tores.sendStatus(NOT_FOUND);) - we can call
deleteCard(_id)and assign its return value to a variablecardDeletion- if
cardDeletion === nullwe can return a 404 not found error - if
cardDeletion === falsewe can return a 500 internal server error - else, it must be a successful deletion, so we return 200 ok status
- if
- ensure that all calls to
writeLogToClient()are updated to match with our revised logic here; since we return the deleted document when deletion is successful, we can just usecardDeletion.aliasas part of the message - we also need to adjust the creation of the AuditLog to use
cardDeletion.alias
- we should just do this with the card
-
now that we changed some stuff with the
/deleteendpoint, we also need to adjust how the frontend handles deletion- the APIFunctions file has a function called
deleteCardFromDb()that takes in analiasparameter - let's change this to be_idinstead - in the main frontend file on line 373, we call
deleteCardFromDb(token, cardToDelete.alias). here we can replacecardToDelete.aliaswithcardToDelete._id
- the APIFunctions file has a function called
-
finally, the changes to
/deletewill also fail some of the unit tests- update any unit tests for
/deletethat take in thealiasparameter to take in an_idparameter instead
- update any unit tests for
once these fixes are implemented, our card reader logic will be much more sound 😁