NFC Reader Entries Controller
Overview
The nfcReaderEntries.controller.js file handles the creation of new NFC reader entries. Instead of directly processing the data, it enqueues two background jobs using BullMQ: one to process the NFC reader stream and another to persist it.
Dependencies
bullmq: A robust and fast message queue for Node.js, built on top of Redis.../../constants.config.js: Provides the Redis connection object for BullMQ.
Queues
Two BullMQ queues are initialized:
queueProcessNfcReaderStream: For processing NFC reader stream data (e.g., marking attendance).queuePersistNfcReaderStream: For persisting raw NFC reader stream data to the database.
Controller Functions
create(req, res)
- Description: Enqueues jobs to process and persist NFC reader entry data.
- Request Body: Expected to contain
scannerIdandnfcTag. - Functionality:
- Captures the current timestamp as
entryTime. - Adds a job to
queueProcessNfcReaderStreamwithscannerId,nfcTag, andentryTime. - Adds a job to
queuePersistNfcReaderStreamwithscannerId,nfcTag, andentryTime. - Uses
Promise.allto enqueue both jobs concurrently.
- Captures the current timestamp as
- Responses:
200 OK: If both jobs are successfully enqueued.500 Internal Server Error: If there's an issue enqueuing the jobs or any other unexpected error occurs.
Usage
This controller is designed to be called when an NFC reader detects a tag. It offloads the actual processing and persistence to background jobs, ensuring that the API response is fast and the main thread is not blocked.
Exports
create: The function to handle the creation of NFC reader entries.