VREP is a user-friendly web application designed to streamline administrative tasks and enhance educational workflows for elementary schools. It empowers schools to effectively manage student enrollment, conduct and evaluate exams, track results, and stay informed about upcoming school events.
0
stars
0
forks
source link
update: Add cron job for deletion of token after 24hr and start the cron job on server startup #164
Imported the necessary dependencies: crypto, dotenv, resend, node-cron, and the PreRegistrationModel.
Created a function generateOtp to generate a 4-digit OTP using the crypto library.
Created an asynchronous function saveOtp to update the OTP and expiration time for a given email in the Teacher model.
Created an instance of the Resend class to send registration emails using the Resend API.
Created an asynchronous function sendRegistrationEmail to send a registration email to a given email address with a token link.
Created a function validateEmail to validate an email address using a regular expression.
Created a variable job to store the cron job instance.
Defined the cron job schedule using the cron.schedule function. The job runs every day at midnight.
Inside the cron job, the current date and time is obtained.
The cron job retrieves all the expired tokens from the PreRegistrationModel using the deleteMany method and the $lt operator to filter documents with an expiration time less than the current date.
The number of deleted tokens is logged to the console.
The cron job checks if there are any more expired tokens using the countDocuments method. If there are no more expired tokens, the cron job is stopped using the job.stop() method.
Finally, the sendRegistrationEmail, validateEmail, and job variables are exported for use in other parts of the application.
Imported the necessary dependencies: crypto, dotenv, resend, node-cron, and the PreRegistrationModel.
Created a function generateOtp to generate a 4-digit OTP using the crypto library.
Created an asynchronous function saveOtp to update the OTP and expiration time for a given email in the Teacher model.
Created an instance of the Resend class to send registration emails using the Resend API.
Created an asynchronous function sendRegistrationEmail to send a registration email to a given email address with a token link.
Created a function validateEmail to validate an email address using a regular expression.
Created a variable job to store the cron job instance.
Defined the cron job schedule using the cron.schedule function. The job runs every day at midnight.
Inside the cron job, the current date and time is obtained.
The cron job retrieves all the expired tokens from the PreRegistrationModel using the deleteMany method and the $lt operator to filter documents with an expiration time less than the current date.
The number of deleted tokens is logged to the console.
The cron job checks if there are any more expired tokens using the countDocuments method. If there are no more expired tokens, the cron job is stopped using the job.stop() method.
Finally, the sendRegistrationEmail, validateEmail, and job variables are exported for use in other parts of the application.