MultiUsersSalesSystem / models /CustomerModel.js
Mahmoud7456's picture
Upload 71 files
1e65df5 verified
Raw History Blame Contribute Delete
2.08 kB
import mongoose from "mongoose";
import { normalizeCustomerName } from "../utils/customerNormalization.js";
const customerSchema = new mongoose.Schema(
{
tenant: {
type: mongoose.Schema.Types.ObjectId,
ref: "Tenant",
required: true,
index: true,
},
branch: {
type: mongoose.Schema.Types.ObjectId,
ref: "Branch",
default: null,
index: true,
},
name: {
type: String,
required: [true, "You must enter customer name"],
minlength: [3, "Customer Name must be at least 3"],
maxlength: [20, "Customer max 20"],
trim: true,
},
normalizedName: {
type: String,
default: "",
index: true,
},
phone: {
type: String,
default: "",
},
email: {
type: String,
trim: true,
lowercase: true,
default: "",
},
address: {
type: String,
default: "",
},
notes: {
type: String,
default: "",
},
isWalkIn: {
type: Boolean,
default: false,
},
createdBy: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
default: null,
},
// invoices: [
// {
// type: mongoose.Schema.Types.ObjectId,
// ref: "Invoice",
// },
// ],
isDeleted: {
type: Boolean,
default: false,
},
deletedAt: {
type: Date,
default: null,
},
},
{
timestamps: true,
}
);
customerSchema.index({ tenant: 1, branch: 1, name: 1 });
customerSchema.index({ tenant: 1, branch: 1, normalizedName: 1 });
customerSchema.index({ tenant: 1, branch: 1, isWalkIn: 1 });
customerSchema.pre("validate", function () {
this.normalizedName = normalizeCustomerName(this.name || "");
});
export default mongoose.model("Customer", customerSchema);