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