File size: 2,385 Bytes
0330133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import mongoose from "mongoose";

const notificationSchema = new mongoose.Schema(
   {
      tenant: {
         type: mongoose.Schema.Types.ObjectId,
         ref: "Tenant",
         required: true,
         index: true,
      },
      invoice: {
         type: mongoose.Schema.Types.ObjectId,
         ref: "Invoice",
         required: true,
      },

      customer: {
         type: mongoose.Schema.Types.ObjectId,
         ref: "Customer",
         default: null,
      },

      // Phone number for WhatsApp (may not be linked to customer)
      phoneNumber: {
         type: String,
         required: true,
         trim: true,
      },

      notificationType: {
         type: String,
         enum: [
            "order-confirmed",
            "pending",
            "preparing",
            "ready_for_delivery",   // ✅ match invoiceController status strings
            "shipped",
            "out_for_delivery",     // ✅ underscore consistency
            "delivered",
            "dine-in-ready",        // ✅ for dine-in table-ready notifications
         ],
         required: true,
      },

      messageTemplate: {
         type: String,
         required: true,
      },

      channel: {
         type: String,
         enum: ["whatsapp", "sms", "email"],
         default: "whatsapp",
      },

      status: {
         type: String,
         enum: ["queued", "sent", "delivered", "failed"],
         default: "queued",
         index: true,
      },

      retryCount: {
         type: Number,
         default: 0,
      },

      maxRetries: {
         type: Number,
         default: 3,
      },

      externalId: {
         type: String,
         default: null,
      },

      errorMessage: {
         type: String,
         default: null,
      },

      sentAt: {
         type: Date,
         default: null,
      },

      scheduledFor: {
         type: Date,
         default: null,
      },
   },
   {
      timestamps: true,
   }
);

// Indexes for performance optimization
notificationSchema.index({ tenant: 1, status: 1 });
notificationSchema.index({ tenant: 1, order: 1 });
notificationSchema.index({ status: 1, scheduledFor: 1 });


const Notification = mongoose.model("Notification", notificationSchema);

export default Notification;