import mongoose, { Schema } from "mongoose"; const orderItemSchema = new Schema({ productId: { type: Schema.Types.ObjectId, ref: "Product", required: true, }, SKU: { type: String, required: true, }, name: { type: String, required: true, }, categoryName: { type: String, // Directly store category name required: true, }, brandName: { type: String, // Directly store brand name required: true, }, price: { type: Number, required: true, }, GST: { type: Number, required: true, }, HSN_Code: { type: Number, required: true, }, image: [ { public_id: String, url: String, }, ], processquantity: { //updated quantity type: Number, required: true, default: 1, }, }); const invoiceSchema = new mongoose.Schema({ invoiceId: { type: String, required: true, unique: true }, orderId: { type: mongoose.Schema.Types.ObjectId, ref: "RdOrder", required: true, }, items: [orderItemSchema], subtotal: { type: Number, required: true, }, gstTotal: { type: Number, required: true, }, invoiceAmount: { type: Number, required: true }, courier_name: { type: String }, courier_tracking_id: { type: String }, courierStatus: { type: String, enum: ["processing", "dispatched", "delivered"], default: "processing", }, courierstatus_timeline: { processing: { type: Date }, dispatched: { type: Date }, delivered: { type: Date }, }, }); // Middleware to set the processing date only when the invoice is created invoiceSchema.pre("save", function (next) { if (this.isNew && !this.courierstatus_timeline.processing) { this.courierstatus_timeline.processing = new Date(); } next(); }); export const InvoiceRd = mongoose.model("InvoiceRd", invoiceSchema);