payservedb 3.6.5 → 3.6.7

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": "3.6.5",
3
+ "version": "3.6.7",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -1,6 +1,5 @@
1
1
  const mongoose = require("mongoose");
2
2
 
3
- // Define the schema for Penalty
4
3
  const penaltySchema = new mongoose.Schema(
5
4
  {
6
5
  name: {
@@ -10,23 +9,36 @@ const penaltySchema = new mongoose.Schema(
10
9
  type: {
11
10
  type: String,
12
11
  required: true,
12
+ enum: ['percentage', 'fixed']
13
13
  },
14
14
  effectDays: {
15
15
  type: Number,
16
16
  required: true,
17
+ min: [1, "Effect days must be at least 1"]
17
18
  },
18
19
  percentage: {
19
20
  type: Number,
20
- required: false,
21
- min: [0, "Percentage must be at least 0"],
21
+ required: function () {
22
+ return this.type === 'percentage';
23
+ },
24
+ min: [0, "Percentage must be at least 0"]
22
25
  },
23
26
  amount: {
24
27
  type: Number,
25
- min: [0, "Amount must be a positive number"],
28
+ required: function () {
29
+ return this.type === 'fixed';
30
+ },
31
+ min: [0, "Amount must be a positive number"]
26
32
  },
27
33
  module: {
28
- type: String,
29
- required: true,
34
+ type: String,
35
+ required: true,
36
+ enum: ['levy', 'lease', 'utility']
37
+ },
38
+ moduleId: {
39
+ type: mongoose.Schema.Types.ObjectId,
40
+ required: true,
41
+ refPath: 'module'
30
42
  },
31
43
  isActive: {
32
44
  type: Boolean,
@@ -36,13 +48,30 @@ const penaltySchema = new mongoose.Schema(
36
48
  type: mongoose.Schema.Types.ObjectId,
37
49
  ref: "Facility",
38
50
  required: true,
39
- },
51
+ index: true
52
+ }
40
53
  },
41
54
  {
42
55
  timestamps: true,
56
+ indexes: [
57
+ { facilityId: 1, isActive: 1 },
58
+ { moduleId: 1, module: 1 },
59
+ { name: 1, facilityId: 1 }
60
+ ]
43
61
  }
44
62
  );
45
63
 
64
+ // Add any custom methods or middleware here if needed
65
+ penaltySchema.pre('save', function (next) {
66
+ // Clear the irrelevant field based on penalty type
67
+ if (this.type === 'percentage') {
68
+ this.amount = null;
69
+ } else if (this.type === 'fixed') {
70
+ this.percentage = null;
71
+ }
72
+ next();
73
+ });
74
+
46
75
  const Penalty = mongoose.model("Penalty", penaltySchema);
47
76
 
48
- module.exports = Penalty;
77
+ module.exports = Penalty;
@@ -17,9 +17,14 @@ const reminderSchema = new mongoose.Schema(
17
17
  module: {
18
18
  type: String,
19
19
  required: true,
20
- enum: ['levy', 'lease'],
20
+ enum: ['levy', 'lease', 'utility'],
21
21
  default: 'levy'
22
22
  },
23
+ moduleId: {
24
+ type: mongoose.Schema.Types.ObjectId,
25
+ required: true,
26
+ refPath: 'module'
27
+ },
23
28
  remindOn: {
24
29
  invoiceDate: {
25
30
  type: Boolean,
@@ -87,20 +92,6 @@ const reminderSchema = new mongoose.Schema(
87
92
  type: String,
88
93
  maxLength: [500, 'Message cannot exceed 500 characters']
89
94
  },
90
- levyId: {
91
- type: mongoose.Schema.Types.ObjectId,
92
- ref: "Levy",
93
- required: function() {
94
- return this.module === 'levy';
95
- }
96
- },
97
- leaseId: {
98
- type: mongoose.Schema.Types.ObjectId,
99
- ref: "LeaseAgreement",
100
- required: function() {
101
- return this.module === 'lease';
102
- }
103
- },
104
95
  facilityId: {
105
96
  type: mongoose.Schema.Types.ObjectId,
106
97
  ref: "Facility",
@@ -126,14 +117,13 @@ const reminderSchema = new mongoose.Schema(
126
117
  { facilityId: 1, isActive: 1, 'remindOn.invoiceDate': 1 },
127
118
  { facilityId: 1, isActive: 1, 'remindOn.dueDate': 1 },
128
119
  { facilityId: 1, isActive: 1, 'remindOn.afterOverdue.enabled': 1 },
129
- { levyId: 1, isActive: 1 },
130
- { leaseId: 1, isActive: 1 },
120
+ { moduleId: 1, isActive: 1 },
131
121
  { facilityId: 1, lastProcessed: 1 }
132
122
  ]
133
123
  }
134
124
  );
135
125
 
136
- // Add method to check if reminder should be processed
126
+ // Add methods to reminder schema
137
127
  reminderSchema.methods.shouldProcess = function (currentTime, invoice) {
138
128
  if (!this.isActive || !invoice) return false;
139
129
 
@@ -142,20 +132,16 @@ reminderSchema.methods.shouldProcess = function (currentTime, invoice) {
142
132
  const dueDate = moment(invoice.dueDate);
143
133
  const today = moment().startOf('day');
144
134
 
145
- // Check if we should process based on time
146
135
  if (!this.isTimeToProcess(now)) return false;
147
136
 
148
- // Check invoice date reminder
149
137
  if (this.remindOn.invoiceDate && invoiceDate.isSame(today, 'day')) {
150
138
  return true;
151
139
  }
152
140
 
153
- // Check due date reminder
154
141
  if (this.remindOn.dueDate && dueDate.isSame(today, 'day')) {
155
142
  return true;
156
143
  }
157
144
 
158
- // Check overdue reminders
159
145
  if (this.remindOn.afterOverdue.enabled && dueDate.isBefore(today, 'day')) {
160
146
  const daysOverdue = today.diff(dueDate, 'days');
161
147
  return this.remindOn.afterOverdue.days.includes(daysOverdue);
@@ -164,13 +150,10 @@ reminderSchema.methods.shouldProcess = function (currentTime, invoice) {
164
150
  return false;
165
151
  };
166
152
 
167
- // Helper method to check if current time matches reminder time
168
153
  reminderSchema.methods.isTimeToProcess = function (currentTime) {
169
154
  const reminderMoment = moment(this.time, 'HH:mm');
170
155
  const currentMoment = moment(currentTime).format('HH:mm');
171
156
  const diffMinutes = moment(currentMoment, 'HH:mm').diff(reminderMoment, 'minutes');
172
-
173
- // Allow processing within 5-minute window
174
157
  return Math.abs(diffMinutes) <= 5;
175
158
  };
176
159
 
@@ -18,7 +18,7 @@ const meterSchema = new mongoose.Schema({
18
18
  required: true,
19
19
  trim: true,
20
20
  unique: true,
21
- index: true
21
+ index: true
22
22
  },
23
23
  accountNumber: {
24
24
  type: String,
@@ -41,8 +41,8 @@ const meterSchema = new mongoose.Schema({
41
41
  valveType: {
42
42
  type: String,
43
43
  enum: ['automatic', 'manual'],
44
- default:'manual'
45
- },
44
+ default: 'manual'
45
+ },
46
46
  isInstalled: {
47
47
  type: Boolean,
48
48
  default: false
@@ -73,7 +73,7 @@ const meterSchema = new mongoose.Schema({
73
73
  readingHistory: [{
74
74
  reading: Number,
75
75
  date: Date,
76
- readBy: String
76
+ time: Date
77
77
  }],
78
78
  currentReading: {
79
79
  type: Number,