diff --git a/server/routers/dry-wash/model/order.js b/server/routers/dry-wash/model/order.js index 6fdaa25..8ee924f 100644 --- a/server/routers/dry-wash/model/order.js +++ b/server/routers/dry-wash/model/order.js @@ -27,6 +27,11 @@ const schema = new Schema({ type: String, required: true }, + orderNumber: { + type: String, + required: true, + unique: true + }, status: { type: String, required: true, diff --git a/server/routers/dry-wash/order.js b/server/routers/dry-wash/order.js index d258fa0..6cdb11f 100644 --- a/server/routers/dry-wash/order.js +++ b/server/routers/dry-wash/order.js @@ -74,6 +74,21 @@ const VALIDATION_MESSAGES = { }, } +/** + * Generates the next order number incrementing the last order number + * @returns {Promise} + */ +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(),