Let me tell you a bit about my application and the problem: This application will be used to register students in a particular course launched by an organization. A student will provide the following in the form besides other information:
- a course from available courses
- his photo
He can print his admit card after the form has been successfully submitted.
Now for admin:
- He can create as many courses as he want
- He can toggle the courses' availablibility
- He can choose where to
start
the roll number for a course - He can change the
current
roll number of a course
The steps taken after a student clicks submit are as follows:
- Get the
course_id
from the form, and get thecurrent
roll number of that course - Save the image with the
current
roll number as its filename at pathcourse_name/batch/roll_number.(png|jpg)
- Get the image if the image was successfully saved, reject otherwise
- Resize the image and overwrite the existing one, delete the image and reject if resizing failed
- Save the student's form in
forms
collection - increment the
current
roll number of that course incourses
collection - send the response containing the form data along with the image's base64 string (will be used in admit card)
The Problem:
The problem is that it can generate duplicate roll numbers if users submit form almost at the same time since they will be using the same current
roll number.
I am not being able to solve this problem as I can't simply auto-increment the form's roll_number
since it is coming from the courses
collection.
Here's how my database looks like:
{
"courses":[
{
"_id" : ObjectId,
"batches": [Object],
"course_name" : String,
"current": Number, // current roll number
"start" : Number, // starting roll number
"available": Boolean
}
],
"forms": [
{
"_id": ObjectId,
"roll_number": Number,
"course_id" : ObjectId
// other stuff
}
],
"settings": [ // always have one object
{
"_id" : ObjectId,
"accept_form": Boolean,
"courses": [ObjectId]
}
]
}
I am using:
multer for saving image : https://github.com/expressjs/multer
sharp for resizing image : https://github.com/lovell/sharp
expressjs for handling requests
Please tell me what should I do to solve this duplication problem.