File size: 2,082 Bytes
4dbde5b
1e65df5
4dbde5b
 
 
 
 
 
 
 
 
0159174
 
 
 
 
 
4dbde5b
 
 
 
 
 
 
1e65df5
 
 
 
 
4dbde5b
 
 
 
 
 
 
 
 
 
 
 
 
 
0159174
 
 
 
 
 
 
 
 
 
 
 
 
0330133
 
 
 
 
 
545ad7d
 
 
 
 
 
 
 
4dbde5b
 
 
 
 
 
0159174
1e65df5
0159174
 
1e65df5
 
 
 
0159174
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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);