feat: generate order number #69

Open
Rustam wants to merge 2 commits from feature/dry-wash-order-number into master
2 changed files with 22 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,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(),
})