feat: generate order number #69
@ -27,6 +27,11 @@ const schema = new Schema({
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
orderNumber: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true
|
||||
},
|
||||
status: {
|
||||
type: String,
|
||||
required: true,
|
||||
|
@ -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,7 +144,9 @@ router.post('/create', async (req, res, next) => {
|
||||
startWashTime: washing.begin,
|
||||
endWashTime: washing.end,
|
||||
location: washing.location,
|
||||
orderNumber: await generateOrderNumber(),
|
||||
status: orderStatus.PROGRESS,
|
||||
master: '',
|
||||
notes: '',
|
||||
created: new Date().toISOString(),
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user