feat: generate order number

This commit is contained in:
RustamRu 2025-01-26 16:29:27 +03:00
parent 3c89d8b9a8
commit 0c3cd7f475
2 changed files with 21 additions and 0 deletions

View File

@ -27,6 +27,11 @@ const schema = new Schema({
type: String,
required: true
},
orderNumber: {
type: String,
required: true,
unique: true
},
status: {
type: String,
required: true,

View File

@ -74,6 +74,21 @@ const VALIDATION_MESSAGES = {
},
}
/**
* Generates the next order number incrementing the last order number
* @returns {Promise<string>}
*/
const generateOrderNumber = async () => {
const lastOrder = await OrderModel.findOne().sort({ created: -1 })
if (lastOrder) {
const lastOrderNumber = parseInt(lastOrder.orderNumber ?? '0')
const nextOrderNumber = lastOrderNumber + 1
return String(nextOrderNumber)
}
return '1' // number of the very first order
}
router.post('/create', async (req, res, next) => {
const bodyErrors = []
@ -129,6 +144,7 @@ router.post('/create', async (req, res, next) => {
startWashTime: washing.begin,
endWashTime: washing.end,
location: washing.location,
orderNumber: await generateOrderNumber(),
status: orderStatus.PROGRESS,
notes: '',
created: new Date().toISOString(),