Skip to content

revise card reader operations #1969

@adarshm11

Description

@adarshm11

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.

    • 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 variable body, as this is unnecessary now that we know we pass in cardBytes every time. we can also delete the variable definition of description on line 23, and have the next line log the cardBytes instead
    • 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
  • 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.

    • we should just do this with the card _id field for simplicity. let's update deleteCard() to take in an _id parameter and use the MongoDB findByIdAndDelete() 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. so deleteCard() should return:
      • the deleted document if the deletion was successful (this is the normal return value from findByIdAndDelete()
      • false if the deletion encountered an error
      • null if the document could not be found (this is the normal return value from findByIdAndDelete() if a document wasn't found)
    • now we need to adjust the /delete endpoint too. first we need to make sure the endpoint expects _id as its body parameter, not alias
    • 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 (from const cardExists... to res.sendStatus(NOT_FOUND);)
    • we can call deleteCard(_id) and assign its return value to a variable cardDeletion
      • if cardDeletion === null we can return a 404 not found error
      • if cardDeletion === false we can return a 500 internal server error
      • else, it must be a successful deletion, so we return 200 ok status
    • 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 use cardDeletion.alias as part of the message
    • we also need to adjust the creation of the AuditLog to use cardDeletion.alias
  • now that we changed some stuff with the /delete endpoint, we also need to adjust how the frontend handles deletion

    • the APIFunctions file has a function called deleteCardFromDb() that takes in an alias parameter - let's change this to be _id instead
    • in the main frontend file on line 373, we call deleteCardFromDb(token, cardToDelete.alias). here we can replace cardToDelete.alias with cardToDelete._id
  • finally, the changes to /delete will also fail some of the unit tests

    • update any unit tests for /delete that take in the alias parameter to take in an _id parameter instead

once these fixes are implemented, our card reader logic will be much more sound 😁

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions