import mongoose, { Document, Schema } from 'mongoose'; export interface IExpense extends Document { category: string; description: string; amount: number; date: Date; taxDeductible: boolean; vendor?: string; reference?: string; receiptUrl?: string; notes?: string; userId: mongoose.Types.ObjectId; dateCreated: Date; dateUpdated: Date; } const ExpenseSchema: Schema = new Schema({ category: { type: String, required: true }, description: { type: String, required: true, trim: true }, amount: { type: Number, required: true, min: 0 }, date: { type: Date, required: true }, taxDeductible: { type: Boolean, default: false }, vendor: { type: String }, reference: { type: String }, receiptUrl: { type: String }, notes: { type: String }, userId: { type: Schema.Types.ObjectId, ref: 'User', required: true, index: true }, dateCreated: { type: Date, default: Date.now }, dateUpdated: { type: Date, default: Date.now }, }); ExpenseSchema.pre('save', function (next) { this.dateUpdated = new Date(); next(); }); // Create compound index to prevent duplicate expenses with same reference, vendor, and user ExpenseSchema.index({ reference: 1, vendor: 1, userId: 1, amount: 1, date: 1 }, { unique: true, partialFilterExpression: { reference: { $exists: true, $ne: null }, vendor: { $exists: true, $ne: null } } }); export default mongoose.model('Expense', ExpenseSchema);