payservedb 8.3.8 → 8.4.0

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.
@@ -1,5 +1,5 @@
1
1
  const mongoose = require("mongoose");
2
- const {RecipientSchema} = require("./facilityInvoiceRecipient")
2
+ const { RecipientSchema } = require("./facilityInvoiceRecipient");
3
3
 
4
4
  const FacilityInvoiceSchema = new mongoose.Schema(
5
5
  {
@@ -13,11 +13,11 @@ const FacilityInvoiceSchema = new mongoose.Schema(
13
13
  ref: "FacilityInvoiceContract",
14
14
  required: true,
15
15
  },
16
- recipient: {
17
- type: RecipientSchema,
18
- required: true,
19
- },
20
- invoiceNumber: {
16
+ recipient: {
17
+ type: RecipientSchema,
18
+ required: true,
19
+ },
20
+ invoiceNumber: {
21
21
  type: String,
22
22
  required: true,
23
23
  trim: true,
@@ -32,7 +32,7 @@ const FacilityInvoiceSchema = new mongoose.Schema(
32
32
  },
33
33
  status: {
34
34
  type: String,
35
- enum: ["pending", "paid", "overdue"],
35
+ enum: ["pending", "paid", "partially_paid", "overdue"],
36
36
  default: "pending",
37
37
  },
38
38
  invoiceType: {
@@ -52,6 +52,7 @@ const FacilityInvoiceSchema = new mongoose.Schema(
52
52
  "powerMeters",
53
53
  "hotWaterMeters",
54
54
  "coldWaterMeters",
55
+ "sms",
55
56
  ],
56
57
  trim: true,
57
58
  },
@@ -88,10 +89,26 @@ const FacilityInvoiceSchema = new mongoose.Schema(
88
89
  },
89
90
  },
90
91
  ],
92
+ // Subtotal of all invoice items before tax and balance
93
+ subtotal: {
94
+ type: Number,
95
+ required: true,
96
+ min: [0, "Subtotal cannot be negative"],
97
+ default: 0,
98
+ },
99
+ balanceBroughtForward: {
100
+ type: Number,
101
+ default: 0,
102
+ },
103
+ // Tax amount calculated on subtotal
104
+ taxAmount: {
105
+ type: Number,
106
+ default: 0,
107
+ min: [0, "Tax amount cannot be negative"],
108
+ },
91
109
  amount: {
92
110
  type: Number,
93
111
  required: true,
94
- min: [0, "Total amount cannot be negative"],
95
112
  },
96
113
  taxRate: {
97
114
  type: Number,
@@ -23,6 +23,11 @@ const FacilityInvoicePaymentSchema = new mongoose.Schema({
23
23
  type: String,
24
24
  required: false,
25
25
  },
26
+ proofOfPaymentUrl: {
27
+ type: String,
28
+ required: false,
29
+ trim: true,
30
+ },
26
31
  status: {
27
32
  type: String,
28
33
  enum: ["pending", "paid", "overpaid", "rejected"],
@@ -40,6 +40,12 @@ const waterMeterAccountSchema = new mongoose.Schema({
40
40
  type: mongoose.Schema.Types.ObjectId,
41
41
  ref: 'Unit',
42
42
  },
43
+ unitName: {
44
+ type: String,
45
+ required: true,
46
+ trim: true,
47
+ index: true
48
+ },
43
49
  payment_type: {
44
50
  type: String,
45
51
  required: true,
@@ -79,4 +85,4 @@ const waterMeterAccountSchema = new mongoose.Schema({
79
85
 
80
86
  const WaterMeterAccount = mongoose.model('WaterMeterAccount', waterMeterAccountSchema);
81
87
 
82
- module.exports = WaterMeterAccount;
88
+ module.exports = WaterMeterAccount;
@@ -25,6 +25,13 @@ const waterMeterSettingsSchema = new mongoose.Schema({
25
25
  type: Number,
26
26
  default: 0
27
27
  },
28
+ freeWaterAllowance: {
29
+ type: Number,
30
+ required: true,
31
+ default: 0,
32
+ min: 0,
33
+ description: "Free water allowance in cubic meters (m³) - users won't be billed for consumption up to this amount"
34
+ },
28
35
  gracePeriod: {
29
36
  type: Number,
30
37
  required: true,
@@ -262,19 +269,16 @@ const waterMeterSettingsSchema = new mongoose.Schema({
262
269
  },
263
270
 
264
271
  //GL ACCOUNT DOUBLE ENTRIES
265
-
266
272
  invocieCreationDe: {
267
273
  type: mongoose.Schema.Types.ObjectId,
268
274
  ref: 'GLAccountDoubleEntries',
269
275
  unique: true
270
276
  },
271
-
272
277
  invoicePaymentDe: {
273
278
  type: mongoose.Schema.Types.ObjectId,
274
279
  ref: 'GLAccountDoubleEntries',
275
280
  unique: true
276
281
  },
277
-
278
282
  }, {
279
283
  timestamps: true
280
284
  });
@@ -0,0 +1,262 @@
1
+ const mongoose = require("mongoose");
2
+
3
+ const ZohoIntegrationSchema = new mongoose.Schema(
4
+ {
5
+ facilityId: {
6
+ type: mongoose.Schema.Types.ObjectId,
7
+ ref: "Facility",
8
+ required: [true, "Facility ID is required"],
9
+ unique: true,
10
+ index: true,
11
+ },
12
+
13
+ // Zoho API Credentials
14
+ // NOTE: clientSecret, refreshToken, and accessToken should be encrypted before storage
15
+ clientId: {
16
+ type: String,
17
+ required: [true, "Zoho Client ID is required"],
18
+ trim: true,
19
+ },
20
+ clientSecret: {
21
+ type: String,
22
+ required: [true, "Zoho Client Secret is required"],
23
+ trim: true,
24
+ // This field should be encrypted in the application layer
25
+ },
26
+ refreshToken: {
27
+ type: String,
28
+ required: [true, "Zoho Refresh Token is required"],
29
+ trim: true,
30
+ // This field should be encrypted in the application layer
31
+ },
32
+ accessToken: {
33
+ type: String,
34
+ trim: true,
35
+ default: null,
36
+ // This field should be encrypted in the application layer
37
+ // Auto-refreshed by the system
38
+ },
39
+ organizationId: {
40
+ type: String,
41
+ required: [true, "Zoho Organization ID is required"],
42
+ trim: true,
43
+ },
44
+
45
+ // Integration Status
46
+ isActive: {
47
+ type: Boolean,
48
+ default: true,
49
+ },
50
+ connectionStatus: {
51
+ type: String,
52
+ enum: ["connected", "disconnected", "error", "pending"],
53
+ default: "pending",
54
+ },
55
+ connectionError: {
56
+ type: String,
57
+ trim: true,
58
+ default: null,
59
+ },
60
+ lastConnectionTest: {
61
+ type: Date,
62
+ default: null,
63
+ },
64
+
65
+ // Token Management
66
+ tokenExpiresAt: {
67
+ type: Date,
68
+ default: null,
69
+ },
70
+ lastTokenRefreshAt: {
71
+ type: Date,
72
+ default: null,
73
+ },
74
+ tokenRefreshCount: {
75
+ type: Number,
76
+ default: 0,
77
+ min: [0, "Token refresh count cannot be negative"],
78
+ },
79
+
80
+ // Sync Statistics
81
+ lastSyncedAt: {
82
+ type: Date,
83
+ default: null,
84
+ },
85
+ totalInvoicesSynced: {
86
+ type: Number,
87
+ default: 0,
88
+ min: [0, "Total invoices synced cannot be negative"],
89
+ },
90
+ totalPaymentsSynced: {
91
+ type: Number,
92
+ default: 0,
93
+ min: [0, "Total payments synced cannot be negative"],
94
+ },
95
+ totalCustomersSynced: {
96
+ type: Number,
97
+ default: 0,
98
+ min: [0, "Total customers synced cannot be negative"],
99
+ },
100
+ successfulSyncs: {
101
+ type: Number,
102
+ default: 0,
103
+ min: [0, "Successful syncs cannot be negative"],
104
+ },
105
+ failedSyncs: {
106
+ type: Number,
107
+ default: 0,
108
+ min: [0, "Failed syncs cannot be negative"],
109
+ },
110
+
111
+ // Sync Configuration
112
+ autoSyncEnabled: {
113
+ type: Boolean,
114
+ default: true,
115
+ },
116
+ syncFrequency: {
117
+ type: String,
118
+ enum: ["realtime", "hourly", "daily", "manual"],
119
+ default: "realtime",
120
+ },
121
+ lastSyncError: {
122
+ type: String,
123
+ trim: true,
124
+ default: null,
125
+ },
126
+
127
+ // Invoice Sync Settings
128
+ syncInvoicesOnCreation: {
129
+ type: Boolean,
130
+ default: true,
131
+ },
132
+ syncPaymentsOnReceipt: {
133
+ type: Boolean,
134
+ default: true,
135
+ },
136
+ markInvoicesAsSent: {
137
+ type: Boolean,
138
+ default: true,
139
+ },
140
+
141
+ // API Configuration
142
+ apiBaseUrl: {
143
+ type: String,
144
+ trim: true,
145
+ default: "https://www.zohoapis.com/books/v3",
146
+ },
147
+ requestTimeout: {
148
+ type: Number,
149
+ default: 30000,
150
+ min: [1000, "Request timeout must be at least 1000ms"],
151
+ },
152
+ maxRetries: {
153
+ type: Number,
154
+ default: 3,
155
+ min: [0, "Max retries cannot be negative"],
156
+ max: [10, "Max retries cannot exceed 10"],
157
+ },
158
+
159
+ // Audit Fields
160
+ createdBy: {
161
+ type: mongoose.Schema.Types.ObjectId,
162
+ ref: "User",
163
+ default: null,
164
+ },
165
+ updatedBy: {
166
+ type: mongoose.Schema.Types.ObjectId,
167
+ ref: "User",
168
+ default: null,
169
+ },
170
+ lastModifiedBy: {
171
+ type: mongoose.Schema.Types.ObjectId,
172
+ ref: "User",
173
+ default: null,
174
+ },
175
+
176
+ // Notes and metadata
177
+ notes: {
178
+ type: String,
179
+ trim: true,
180
+ default: null,
181
+ },
182
+ metadata: {
183
+ type: mongoose.Schema.Types.Mixed,
184
+ default: {},
185
+ },
186
+ },
187
+ {
188
+ timestamps: true,
189
+ }
190
+ );
191
+
192
+ // Indexes for better query performance
193
+ ZohoIntegrationSchema.index({ facilityId: 1, isActive: 1 });
194
+ ZohoIntegrationSchema.index({ connectionStatus: 1 });
195
+ ZohoIntegrationSchema.index({ lastSyncedAt: -1 });
196
+
197
+ // Virtual for success rate
198
+ ZohoIntegrationSchema.virtual("syncSuccessRate").get(function () {
199
+ const total = this.successfulSyncs + this.failedSyncs;
200
+ if (total === 0) return 0;
201
+ return ((this.successfulSyncs / total) * 100).toFixed(2);
202
+ });
203
+
204
+ // Method to mask sensitive fields for API responses
205
+ ZohoIntegrationSchema.methods.toSafeObject = function () {
206
+ const obj = this.toObject();
207
+
208
+ // Mask sensitive fields
209
+ if (obj.clientSecret) {
210
+ obj.clientSecret = "••••••••" + obj.clientSecret.slice(-4);
211
+ }
212
+ if (obj.refreshToken) {
213
+ obj.refreshToken = "••••••••" + obj.refreshToken.slice(-4);
214
+ }
215
+ if (obj.accessToken) {
216
+ obj.accessToken = "••••••••" + (obj.accessToken ? obj.accessToken.slice(-4) : "");
217
+ }
218
+
219
+ return obj;
220
+ };
221
+
222
+ // Method to check if token needs refresh (5 minutes buffer)
223
+ ZohoIntegrationSchema.methods.needsTokenRefresh = function () {
224
+ if (!this.tokenExpiresAt) return true;
225
+ const bufferTime = 5 * 60 * 1000; // 5 minutes in milliseconds
226
+ return new Date().getTime() >= (this.tokenExpiresAt.getTime() - bufferTime);
227
+ };
228
+
229
+ // Method to increment sync counters
230
+ ZohoIntegrationSchema.methods.incrementSyncCounters = function (type, success = true) {
231
+ this.lastSyncedAt = new Date();
232
+
233
+ if (type === "invoice") {
234
+ this.totalInvoicesSynced += 1;
235
+ } else if (type === "payment") {
236
+ this.totalPaymentsSynced += 1;
237
+ } else if (type === "customer") {
238
+ this.totalCustomersSynced += 1;
239
+ }
240
+
241
+ if (success) {
242
+ this.successfulSyncs += 1;
243
+ this.lastSyncError = null;
244
+ } else {
245
+ this.failedSyncs += 1;
246
+ }
247
+
248
+ return this.save();
249
+ };
250
+
251
+ // Pre-save hook to update timestamps
252
+ ZohoIntegrationSchema.pre("save", function (next) {
253
+ this.updatedAt = new Date();
254
+ next();
255
+ });
256
+
257
+ const ZohoIntegration = mongoose.model(
258
+ "ZohoIntegration",
259
+ ZohoIntegrationSchema
260
+ );
261
+
262
+ module.exports = ZohoIntegration;
@@ -1,12 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="MaterialThemeProjectNewConfig">
4
- <option name="metadata">
5
- <MTProjectMetadataState>
6
- <option name="migrated" value="true" />
7
- <option name="pristineConfig" value="false" />
8
- <option name="userId" value="1756d2b0:198e001a643:-7ffe" />
9
- </MTProjectMetadataState>
10
- </option>
11
- </component>
12
- </project>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/psdb.iml" filepath="$PROJECT_DIR$/.idea/psdb.iml" />
6
- </modules>
7
- </component>
8
- </project>
package/.idea/psdb.iml DELETED
@@ -1,12 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="WEB_MODULE" version="4">
3
- <component name="NewModuleRootManager">
4
- <content url="file://$MODULE_DIR$">
5
- <excludeFolder url="file://$MODULE_DIR$/.tmp" />
6
- <excludeFolder url="file://$MODULE_DIR$/temp" />
7
- <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
- </content>
9
- <orderEntry type="inheritedJdk" />
10
- <orderEntry type="sourceFolder" forTests="false" />
11
- </component>
12
- </module>
package/.idea/vcs.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="" vcs="Git" />
5
- </component>
6
- </project>