const { Schema, model } = require('mongoose') const { orderStatus } = require('./const') const { OrderNumberModel } = require('./order.number') const schema = new Schema({ phone: { type: String, required: true }, carNumber: { type: String, required: true }, carBody: { type: Number, required: true }, carColor: String, startWashTime: { type: Date, required: true }, endWashTime: { type: Date, required: true }, location: { type: String, required: true }, orderNumber: { }, status: { type: String, required: true, enum: Object.values(orderStatus) }, master: { type: Schema.Types.ObjectId, ref: 'dry-wash-master' }, notes: String, created: { type: Date, default: () => new Date().toISOString(), }, updated: { type: Date, default: () => new Date().toISOString(), }, }) schema.pre('save', async function (next) { if (this.isNew) { const counter = await OrderNumberModel.findOneAndUpdate( { _id: 'orderNumber' }, { $inc: { sequenceValue: 1 } }, { new: true, upsert: true } ) this.orderNumber = counter.sequenceValue.toString() } next() }) schema.set('toJSON', { virtuals: true, versionKey: false, transform(_doc, ret) { delete ret._id } }) schema.virtual('id').get(function () { return this._id.toHexString() }) exports.OrderModel = model('dry-wash-order', schema)