payservedb 5.2.2 → 5.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payservedb",
3
- "version": "5.2.2",
3
+ "version": "5.2.3",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -39,16 +39,26 @@ const reminderSchema = new mongoose.Schema(
39
39
  type: Boolean,
40
40
  default: false,
41
41
  },
42
+ daily: {
43
+ type: Boolean,
44
+ default: false,
45
+ },
42
46
  days: {
43
47
  type: [Number],
44
48
  validate: {
45
49
  validator: function (value) {
46
- if (!this.remindOn.afterOverdue.enabled) return true;
50
+ // If daily reminders are enabled, no need to validate days
51
+ if (this.remindOn?.afterOverdue?.daily === true) return true;
52
+
53
+ // If overdue reminders are disabled, no validation needed
54
+ if (!this.remindOn?.afterOverdue?.enabled) return true;
55
+
56
+ // If overdue is enabled but daily is false, then validate days array
47
57
  return Array.isArray(value) &&
48
58
  value.length > 0 &&
49
59
  value.every(day => [1, 3, 7].includes(day));
50
60
  },
51
- message: 'When overdue reminders are enabled, days can only be 1, 3, or 7'
61
+ message: 'When overdue reminders are enabled (and daily is false), days must include only values of 1, 3, or 7'
52
62
  }
53
63
  }
54
64
  }
@@ -117,13 +127,14 @@ const reminderSchema = new mongoose.Schema(
117
127
  { facilityId: 1, isActive: 1, 'remindOn.invoiceDate': 1 },
118
128
  { facilityId: 1, isActive: 1, 'remindOn.dueDate': 1 },
119
129
  { facilityId: 1, isActive: 1, 'remindOn.afterOverdue.enabled': 1 },
130
+ { facilityId: 1, isActive: 1, 'remindOn.afterOverdue.daily': 1 },
120
131
  { moduleId: 1, isActive: 1 },
121
132
  { facilityId: 1, lastProcessed: 1 }
122
133
  ]
123
134
  }
124
135
  );
125
136
 
126
- // Add methods to reminder schema
137
+ // Helper method to check if reminder should be processed
127
138
  reminderSchema.methods.shouldProcess = function (currentTime, invoice) {
128
139
  if (!this.isActive || !invoice) return false;
129
140
 
@@ -132,17 +143,27 @@ reminderSchema.methods.shouldProcess = function (currentTime, invoice) {
132
143
  const dueDate = moment(invoice.dueDate);
133
144
  const today = moment().startOf('day');
134
145
 
146
+ // Check if current time matches reminder time
135
147
  if (!this.isTimeToProcess(now)) return false;
136
148
 
149
+ // Check for invoice date reminder
137
150
  if (this.remindOn.invoiceDate && invoiceDate.isSame(today, 'day')) {
138
151
  return true;
139
152
  }
140
153
 
154
+ // Check for due date reminder
141
155
  if (this.remindOn.dueDate && dueDate.isSame(today, 'day')) {
142
156
  return true;
143
157
  }
144
158
 
159
+ // Check for overdue reminder
145
160
  if (this.remindOn.afterOverdue.enabled && dueDate.isBefore(today, 'day')) {
161
+ // If daily reminders are enabled, send reminder every day after due date
162
+ if (this.remindOn.afterOverdue.daily) {
163
+ return true;
164
+ }
165
+
166
+ // Otherwise, check if today is one of the specific days after due date
146
167
  const daysOverdue = today.diff(dueDate, 'days');
147
168
  return this.remindOn.afterOverdue.days.includes(daysOverdue);
148
169
  }
@@ -150,11 +171,26 @@ reminderSchema.methods.shouldProcess = function (currentTime, invoice) {
150
171
  return false;
151
172
  };
152
173
 
174
+ // Helper to check if current time is close to scheduled reminder time
153
175
  reminderSchema.methods.isTimeToProcess = function (currentTime) {
154
- const reminderMoment = moment(this.time, 'HH:mm');
176
+ const reminderTime = moment(this.time, 'HH:mm');
155
177
  const currentMoment = moment(currentTime).format('HH:mm');
156
- const diffMinutes = moment(currentMoment, 'HH:mm').diff(reminderMoment, 'minutes');
157
- return Math.abs(diffMinutes) <= 5;
178
+ const diffMinutes = Math.abs(
179
+ moment(currentMoment, 'HH:mm').diff(reminderTime, 'minutes')
180
+ );
181
+
182
+ // Allow processing within 5 minutes of scheduled time
183
+ return diffMinutes <= 5;
184
+ };
185
+
186
+ // Method to check if reminder has been processed today
187
+ reminderSchema.methods.hasBeenProcessedToday = function () {
188
+ if (!this.lastProcessed) return false;
189
+
190
+ const today = moment().startOf('day');
191
+ const processedDate = moment(this.lastProcessed).startOf('day');
192
+
193
+ return processedDate.isSame(today, 'day');
158
194
  };
159
195
 
160
196
  const Reminder = mongoose.model("Reminder", reminderSchema);