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 /verify endpoint verifies/adds cards using the checkIfCardExists() utility function. as it stands, this function takes in either cardBytes or alias. but this is a bit excessive, as we don't ever need to use alias.
-
the /delete endpoint deletes cards given requests from the frontend, and uses the deleteCard() utility function. this function currently takes in an alias parameter, which is a bit weird.
-
now that we changed some stuff with the /delete endpoint, we also need to adjust how the frontend handles deletion
-
finally, the changes to /delete will also fail some of the unit tests
once these fixes are implemented, our card reader logic will be much more sound 😁
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 readeryou 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 functionthere 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.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 thecardBytesinsteadverifyCard()so the functionality is more clearthe
/deleteendpoint deletes cards given requests from the frontend, and uses thedeleteCard()utility function. this function currently takes in analiasparameter, which is a bit weird._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.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:findByIdAndDelete()falseif the deletion encountered an errornullif the document could not be found (this is the normal return value fromfindByIdAndDelete()if a document wasn't found)/deleteendpoint too. first we need to make sure the endpoint expects_idas its body parameter, notaliascheckIfCardExists()- it shouldn't, so we can delete lines 155-163 (fromconst cardExists...tores.sendStatus(NOT_FOUND);)deleteCard(_id)and assign its return value to a variablecardDeletioncardDeletion === nullwe can return a 404 not found errorcardDeletion === falsewe can return a 500 internal server errorwriteLogToClient()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 messagecardDeletion.aliasnow that we changed some stuff with the
/deleteendpoint, we also need to adjust how the frontend handles deletiondeleteCardFromDb()that takes in analiasparameter - let's change this to be_idinsteaddeleteCardFromDb(token, cardToDelete.alias). here we can replacecardToDelete.aliaswithcardToDelete._idfinally, the changes to
/deletewill also fail some of the unit tests/deletethat take in thealiasparameter to take in an_idparameter insteadonce these fixes are implemented, our card reader logic will be much more sound 😁