payservedb 8.9.9 → 9.0.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.
Files changed (196) hide show
  1. package/.env +2 -2
  2. package/ZOHO_INTEGRATION_SCHEMA.md +644 -644
  3. package/index.js +330 -329
  4. package/package.json +17 -17
  5. package/src/models/InvoiceWithholdingTax.js +68 -0
  6. package/src/models/account.js +52 -52
  7. package/src/models/agent_departments.js +59 -59
  8. package/src/models/agent_notifications.js +53 -53
  9. package/src/models/agent_performance.js +127 -127
  10. package/src/models/agent_roles.js +77 -77
  11. package/src/models/agents.js +154 -154
  12. package/src/models/apilog.js +18 -18
  13. package/src/models/approvalsWorkflows.js +49 -49
  14. package/src/models/archivedapilog.js +18 -18
  15. package/src/models/asset.js +92 -92
  16. package/src/models/assetsAssignment.js +64 -64
  17. package/src/models/auditTrail.js +346 -346
  18. package/src/models/bankdetails.js +47 -47
  19. package/src/models/billerAddress.js +124 -124
  20. package/src/models/booking_invoice.js +165 -165
  21. package/src/models/bookinganalytics.js +63 -63
  22. package/src/models/bookingconfig.js +45 -45
  23. package/src/models/bookingproperty.js +179 -179
  24. package/src/models/bookingreservation.js +239 -239
  25. package/src/models/bookingrevenuerecord.js +84 -84
  26. package/src/models/budget.js +95 -95
  27. package/src/models/budgetCategory.js +19 -19
  28. package/src/models/campaigns.js +108 -108
  29. package/src/models/cashpayment.js +290 -290
  30. package/src/models/combinedUnits.js +62 -62
  31. package/src/models/combined_invoice.js +424 -424
  32. package/src/models/common_area_electricity.js +38 -38
  33. package/src/models/common_area_generator.js +41 -41
  34. package/src/models/common_area_utility_alert.js +37 -37
  35. package/src/models/common_area_water.js +39 -39
  36. package/src/models/communication_status.js +33 -33
  37. package/src/models/communication_user_opt.js +32 -32
  38. package/src/models/community_guidelines.js +35 -35
  39. package/src/models/company.js +53 -53
  40. package/src/models/coreBaseSettings.js +16 -16
  41. package/src/models/coreInvoiceSettings.js +100 -100
  42. package/src/models/counter_schema.js +21 -21
  43. package/src/models/country_tax.js +42 -42
  44. package/src/models/currency_settings.js +39 -39
  45. package/src/models/customer.js +214 -214
  46. package/src/models/customer_preference.js +52 -52
  47. package/src/models/customer_satisfaction_survey.js +297 -297
  48. package/src/models/customer_surveys.js +139 -139
  49. package/src/models/customer_tickets.js +237 -237
  50. package/src/models/dailyChecklist.js +312 -312
  51. package/src/models/default_payment_details.js +17 -17
  52. package/src/models/deliveryTimeMarks.js +18 -18
  53. package/src/models/document_type.js +19 -19
  54. package/src/models/dutyRosterChecklist.js +250 -250
  55. package/src/models/dutyroster.js +136 -136
  56. package/src/models/email.js +37 -37
  57. package/src/models/email_sms_queue.js +61 -61
  58. package/src/models/email_thread.js +35 -35
  59. package/src/models/entry_exit.js +53 -53
  60. package/src/models/expense.js +99 -99
  61. package/src/models/expense_category.js +45 -45
  62. package/src/models/facility.js +76 -76
  63. package/src/models/facilityBillingPrices.js +29 -29
  64. package/src/models/facilityInvoice.js +240 -240
  65. package/src/models/facilityInvoicePayment.js +52 -52
  66. package/src/models/facilityInvoiceRecipient.js +32 -32
  67. package/src/models/facilityWalletTransactionsMetadata.js +236 -236
  68. package/src/models/facility_departements.js +20 -20
  69. package/src/models/facility_etims_config.js +116 -116
  70. package/src/models/facility_payment_details.js +20 -20
  71. package/src/models/facility_rating.js +78 -78
  72. package/src/models/facilityasset.js +25 -25
  73. package/src/models/faq.js +14 -14
  74. package/src/models/gl_account_double_entries.js +25 -25
  75. package/src/models/gl_accounts.js +56 -56
  76. package/src/models/gl_entries.js +49 -49
  77. package/src/models/goodsReceivedNotes.js +115 -115
  78. package/src/models/guard.js +47 -47
  79. package/src/models/handover.js +258 -258
  80. package/src/models/inspection_category.js +38 -38
  81. package/src/models/invoice.js +514 -514
  82. package/src/models/invoice_edit_log.js +81 -81
  83. package/src/models/invoice_generation_approval.js +86 -86
  84. package/src/models/invoicing_schedule.js +40 -40
  85. package/src/models/item_inspection.js +96 -96
  86. package/src/models/knowledge_base.js +109 -109
  87. package/src/models/knowledge_base_rating.js +44 -44
  88. package/src/models/leaseagreement.js +243 -243
  89. package/src/models/leasetemplate.js +17 -17
  90. package/src/models/levy.js +212 -212
  91. package/src/models/levy_invoice_settings.js +26 -26
  92. package/src/models/levycontract.js +215 -215
  93. package/src/models/levytype.js +23 -23
  94. package/src/models/maintenance_service_vendor.js +38 -38
  95. package/src/models/maintenance_services.js +17 -17
  96. package/src/models/maintenancerequisition.js +31 -31
  97. package/src/models/master_workplan.js +32 -32
  98. package/src/models/master_workplan_child.js +34 -34
  99. package/src/models/message.js +38 -38
  100. package/src/models/module.js +21 -21
  101. package/src/models/movein_application.js +29 -29
  102. package/src/models/movein_landlord.js +18 -18
  103. package/src/models/movein_user.js +15 -15
  104. package/src/models/notification.js +44 -44
  105. package/src/models/paymentTermsMarks.js +19 -19
  106. package/src/models/penalty.js +76 -76
  107. package/src/models/pendingCredentials.js +32 -32
  108. package/src/models/powerMeterCommunicationProtocol.js +17 -17
  109. package/src/models/powerMeterCustomerAccount.js +78 -78
  110. package/src/models/powerMeterCustomerBand.js +14 -14
  111. package/src/models/powerMeterDailyReading.js +30 -30
  112. package/src/models/powerMeterGateways.js +40 -40
  113. package/src/models/powerMeterMonthlyReading.js +34 -34
  114. package/src/models/powerMeterPowerCharges.js +85 -85
  115. package/src/models/powerMeterSettings.js +159 -159
  116. package/src/models/powerMeterSingleDayReading.js +32 -32
  117. package/src/models/powerMeters.js +116 -116
  118. package/src/models/powerMetersManufacturer.js +14 -14
  119. package/src/models/power_invoice.js +470 -470
  120. package/src/models/power_meter_account.js +81 -81
  121. package/src/models/power_meter_command_logs.js +30 -30
  122. package/src/models/power_meter_command_queue.js +33 -33
  123. package/src/models/power_meter_negative_balance.js +44 -44
  124. package/src/models/power_prepaid_credits.js +47 -47
  125. package/src/models/power_prepaid_debits.js +53 -53
  126. package/src/models/power_prepaid_orders.js +78 -78
  127. package/src/models/power_sms_notification.js +26 -26
  128. package/src/models/privacy_policy.js +19 -19
  129. package/src/models/propertyManagerContract.js +556 -556
  130. package/src/models/propertyManagerRevenue.js +195 -195
  131. package/src/models/purchaseOrderInvoice.js +74 -74
  132. package/src/models/purchase_order.js +213 -213
  133. package/src/models/purchase_request.js +110 -110
  134. package/src/models/quickbooks_config.js +52 -52
  135. package/src/models/refresh_token.js +23 -23
  136. package/src/models/reminder.js +197 -197
  137. package/src/models/report.js +13 -13
  138. package/src/models/resident.js +121 -121
  139. package/src/models/rfq_details.js +131 -131
  140. package/src/models/rfq_response.js +153 -153
  141. package/src/models/service_charge_invoice_upload.js +42 -42
  142. package/src/models/service_charge_payments.js +27 -27
  143. package/src/models/servicerequest.js +55 -55
  144. package/src/models/settings.js +62 -62
  145. package/src/models/short_urls.js +21 -21
  146. package/src/models/smart_meter_daily_consumption.js +44 -44
  147. package/src/models/sms_africastalking.js +20 -20
  148. package/src/models/sms_balance_notification.js +26 -26
  149. package/src/models/sms_meliora.js +20 -20
  150. package/src/models/staff.js +36 -36
  151. package/src/models/stocksandspare.js +161 -161
  152. package/src/models/suppliers.js +74 -74
  153. package/src/models/terms_and_conditions.js +19 -19
  154. package/src/models/tickets.js +186 -186
  155. package/src/models/tickets_category.js +72 -72
  156. package/src/models/unitManagementTemplate.js +44 -44
  157. package/src/models/unitasset.js +25 -25
  158. package/src/models/units.js +130 -130
  159. package/src/models/user.js +186 -186
  160. package/src/models/valueaddedservices.js +21 -21
  161. package/src/models/vas_invoices_upload.js +50 -50
  162. package/src/models/vas_payments.js +24 -24
  163. package/src/models/vasinvoice.js +196 -196
  164. package/src/models/vasvendor.js +52 -52
  165. package/src/models/visitLog.js +95 -95
  166. package/src/models/visitor.js +67 -67
  167. package/src/models/waitlist.js +45 -45
  168. package/src/models/wallet.js +44 -44
  169. package/src/models/wallet_transactions.js +50 -50
  170. package/src/models/water_invoice.js +351 -351
  171. package/src/models/water_meter_Command_Queue.js +33 -33
  172. package/src/models/water_meter_account.js +86 -86
  173. package/src/models/water_meter_billing.js +58 -58
  174. package/src/models/water_meter_combined_accounts.js +92 -92
  175. package/src/models/water_meter_communication.js +17 -17
  176. package/src/models/water_meter_communication_logs.js +39 -39
  177. package/src/models/water_meter_concentrator.js +70 -70
  178. package/src/models/water_meter_daily_history.js +32 -32
  179. package/src/models/water_meter_high_risk.js +36 -36
  180. package/src/models/water_meter_iot_cards.js +34 -34
  181. package/src/models/water_meter_loan_deduction.js +134 -134
  182. package/src/models/water_meter_manufacturer.js +35 -35
  183. package/src/models/water_meter_monthly_history.js +36 -36
  184. package/src/models/water_meter_negative_amounts.js +44 -44
  185. package/src/models/water_meter_settings.js +290 -290
  186. package/src/models/water_meter_single_day_history.js +34 -34
  187. package/src/models/water_meter_size.js +15 -15
  188. package/src/models/water_meters.js +176 -176
  189. package/src/models/water_meters_delivery.js +76 -76
  190. package/src/models/water_prepaid_credit.js +47 -47
  191. package/src/models/water_prepaid_debit.js +50 -50
  192. package/src/models/whatsapp_conversation.js +23 -23
  193. package/src/models/workorder.js +49 -49
  194. package/src/models/zohoAccount.js +453 -453
  195. package/src/models/zohoIntegration.js +262 -262
  196. package/src/models/zohoItem.js +504 -504
@@ -1,346 +1,346 @@
1
- const mongoose = require("mongoose");
2
-
3
- // User details sub-schema (from JWT token)
4
- const userDetailsSchema = new mongoose.Schema(
5
- {
6
- userId: {
7
- type: String,
8
- required: true,
9
- },
10
- email: {
11
- type: String,
12
- required: true,
13
- },
14
- fullName: {
15
- type: String,
16
- required: true,
17
- },
18
- role: {
19
- type: String,
20
- required: true,
21
- },
22
- phoneNumber: {
23
- type: String,
24
- required: false,
25
- },
26
- facilityId: {
27
- type: String,
28
- required: false,
29
- },
30
- permissions: {
31
- type: mongoose.Schema.Types.Mixed,
32
- required: false,
33
- },
34
- tokenExpiry: {
35
- type: Date,
36
- required: false,
37
- },
38
- tokenIssued: {
39
- type: Date,
40
- required: false,
41
- },
42
- },
43
- { _id: false },
44
- );
45
-
46
- // Main audit trail schema
47
- const auditTrailSchema = new mongoose.Schema(
48
- {
49
- // Basic request information
50
- timestamp: {
51
- type: Date,
52
- required: true,
53
- default: Date.now,
54
- },
55
- method: {
56
- type: String,
57
- enum: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"],
58
- required: true,
59
- },
60
- url: {
61
- type: String,
62
- required: true,
63
- },
64
- ip_address: {
65
- type: String,
66
- required: true,
67
- },
68
- platform: {
69
- type: String,
70
- required: true,
71
- },
72
-
73
- // Authorization and user information
74
- is_authorized: {
75
- type: Boolean,
76
- required: true,
77
- default: false,
78
- },
79
- user_details: {
80
- type: userDetailsSchema,
81
- required: false,
82
- },
83
-
84
- // Device and browser information
85
- browser: {
86
- type: String,
87
- required: true,
88
- default: "unknown",
89
- },
90
- operating_system: {
91
- type: String,
92
- required: true,
93
- default: "unknown",
94
- },
95
- device_type: {
96
- type: String,
97
- enum: ["desktop", "mobile", "tablet", "unknown"],
98
- required: true,
99
- default: "unknown",
100
- },
101
- user_agent: {
102
- type: String,
103
- required: false,
104
- },
105
-
106
- // Request data
107
- request_data: {
108
- type: mongoose.Schema.Types.Mixed,
109
- required: false,
110
- },
111
- query_params: {
112
- type: mongoose.Schema.Types.Mixed,
113
- required: false,
114
- default: {},
115
- },
116
- route_params: {
117
- type: mongoose.Schema.Types.Mixed,
118
- required: false,
119
- default: {},
120
- },
121
-
122
- // Activity information
123
- activity: {
124
- type: String,
125
- required: true,
126
- },
127
-
128
- // response status
129
- response_status: {
130
- type: String,
131
- required: true,
132
- enum: ["success", "error", "unknown"],
133
- default: "unknown",
134
- },
135
-
136
- // Method-specific data
137
- previous_data: {
138
- type: mongoose.Schema.Types.Mixed,
139
- required: false,
140
- },
141
- deleted_data: {
142
- type: mongoose.Schema.Types.Mixed,
143
- required: false,
144
- },
145
-
146
- // Custom additional data
147
- custom_data: {
148
- type: mongoose.Schema.Types.Mixed,
149
- required: false,
150
- },
151
-
152
- // Request tracking
153
- request_id: {
154
- type: String,
155
- required: false,
156
- },
157
- },
158
- {
159
- timestamps: true,
160
- },
161
- );
162
-
163
- // Pre-save middleware for data validation and cleanup
164
- auditTrailSchema.pre("save", function (next) {
165
- // Ensure timestamp is set
166
- if (!this.timestamp) {
167
- this.timestamp = new Date();
168
- }
169
-
170
- // Validate that authorized requests have user details
171
- if (this.is_authorized && !this.user_details) {
172
- console.warn("Authorized request without user details detected");
173
- }
174
-
175
- // Ensure query_params and route_params are objects
176
- if (!this.query_params || typeof this.query_params !== "object") {
177
- this.query_params = {};
178
- }
179
- if (!this.route_params || typeof this.route_params !== "object") {
180
- this.route_params = {};
181
- }
182
-
183
- // Set browser to unknown if not provided
184
- if (!this.browser) {
185
- this.browser = "unknown";
186
- }
187
-
188
- // Set operating_system to unknown if not provided
189
- if (!this.operating_system) {
190
- this.operating_system = "unknown";
191
- }
192
-
193
- next();
194
- });
195
-
196
- // Static methods for common queries
197
- auditTrailSchema.statics.findByUser = function (userId, startDate, endDate) {
198
- const query = { "user_details.userId": userId };
199
- if (startDate || endDate) {
200
- query.timestamp = {};
201
- if (startDate) query.timestamp.$gte = startDate;
202
- if (endDate) query.timestamp.$lte = endDate;
203
- }
204
- return this.find(query).sort({ timestamp: -1 });
205
- };
206
-
207
- auditTrailSchema.statics.findByActivity = function (activity, limit = 100) {
208
- return this.find({ activity: new RegExp(activity, "i") })
209
- .sort({ timestamp: -1 })
210
- .limit(limit);
211
- };
212
-
213
- auditTrailSchema.statics.findByFacility = function (
214
- facilityId,
215
- startDate,
216
- endDate,
217
- ) {
218
- const query = { "route_params.facilityId": facilityId };
219
- if (startDate || endDate) {
220
- query.timestamp = {};
221
- if (startDate) query.timestamp.$gte = startDate;
222
- if (endDate) query.timestamp.$lte = endDate;
223
- }
224
- return this.find(query).sort({ timestamp: -1 });
225
- };
226
-
227
- auditTrailSchema.statics.findByAsset = function (assetId, startDate, endDate) {
228
- const query = { "route_params.assetId": assetId };
229
- if (startDate || endDate) {
230
- query.timestamp = {};
231
- if (startDate) query.timestamp.$gte = startDate;
232
- if (endDate) query.timestamp.$lte = endDate;
233
- }
234
- return this.find(query).sort({ timestamp: -1 });
235
- };
236
-
237
- auditTrailSchema.statics.findByIP = function (ipAddress, startDate, endDate) {
238
- const query = { ip_address: ipAddress };
239
- if (startDate || endDate) {
240
- query.timestamp = {};
241
- if (startDate) query.timestamp.$gte = startDate;
242
- if (endDate) query.timestamp.$lte = endDate;
243
- }
244
- return this.find(query).sort({ timestamp: -1 });
245
- };
246
-
247
- auditTrailSchema.statics.findByMethod = function (method, startDate, endDate) {
248
- const query = { method: method.toUpperCase() };
249
- if (startDate || endDate) {
250
- query.timestamp = {};
251
- if (startDate) query.timestamp.$gte = startDate;
252
- if (endDate) query.timestamp.$lte = endDate;
253
- }
254
- return this.find(query).sort({ timestamp: -1 });
255
- };
256
-
257
- // Instance methods
258
- auditTrailSchema.methods.getActivityType = function () {
259
- if (this.activity.includes("Create")) return "CREATE";
260
- if (this.activity.includes("Update")) return "UPDATE";
261
- if (this.activity.includes("Delete")) return "DELETE";
262
- if (this.activity.includes("Login")) return "AUTH";
263
- return "OTHER";
264
- };
265
-
266
- auditTrailSchema.methods.isSuccessful = function () {
267
- return (
268
- this.activity.includes("Success") ||
269
- (!this.activity.includes("Failed") &&
270
- !this.activity.includes("Error") &&
271
- !this.activity.includes("Not Found"))
272
- );
273
- };
274
-
275
- auditTrailSchema.methods.hasDataChanges = function () {
276
- return !!(this.previous_data || this.deleted_data);
277
- };
278
-
279
- // Create indexes for efficient queries
280
- auditTrailSchema.index({ timestamp: -1 }); // Recent activities first
281
- auditTrailSchema.index({ "user_details.userId": 1, timestamp: -1 }); // User activity history
282
- auditTrailSchema.index({ "user_details.email": 1, timestamp: -1 }); // User by email
283
- auditTrailSchema.index({ ip_address: 1, timestamp: -1 }); // IP-based queries
284
- auditTrailSchema.index({ method: 1, timestamp: -1 }); // Method-based queries
285
- auditTrailSchema.index({ activity: 1, timestamp: -1 }); // Activity-based queries
286
- auditTrailSchema.index({ platform: 1, timestamp: -1 }); // Platform-based queries
287
- auditTrailSchema.index({ "route_params.facilityId": 1, timestamp: -1 }); // Facility-based queries
288
- auditTrailSchema.index({ "route_params.assetId": 1, timestamp: -1 }); // Asset-based queries
289
- auditTrailSchema.index({ is_authorized: 1, timestamp: -1 }); // Authorization status
290
- auditTrailSchema.index({ device_type: 1, timestamp: -1 }); // Device type queries
291
- auditTrailSchema.index({ browser: 1, timestamp: -1 }); // Browser-based queries
292
-
293
- // Compound indexes for common query patterns
294
- auditTrailSchema.index({
295
- "user_details.userId": 1,
296
- activity: 1,
297
- timestamp: -1,
298
- }); // User + activity
299
- auditTrailSchema.index({
300
- "route_params.facilityId": 1,
301
- method: 1,
302
- timestamp: -1,
303
- }); // Facility + method
304
- auditTrailSchema.index({
305
- ip_address: 1,
306
- "user_details.userId": 1,
307
- timestamp: -1,
308
- }); // IP + user correlation
309
-
310
- // Text search index for searching activities and user details
311
- auditTrailSchema.index({
312
- activity: "text",
313
- "user_details.email": "text",
314
- "user_details.fullName": "text",
315
- url: "text",
316
- });
317
-
318
- // Activity type constants
319
- auditTrailSchema.statics.ACTIVITY_TYPES = {
320
- CREATE: "CREATE",
321
- UPDATE: "UPDATE",
322
- DELETE: "DELETE",
323
- AUTH: "AUTH",
324
- OTHER: "OTHER",
325
- };
326
-
327
- // Method constants
328
- auditTrailSchema.statics.METHODS = {
329
- GET: "GET",
330
- POST: "POST",
331
- PUT: "PUT",
332
- PATCH: "PATCH",
333
- DELETE: "DELETE",
334
- OPTIONS: "OPTIONS",
335
- HEAD: "HEAD",
336
- };
337
-
338
- // Device type constants
339
- auditTrailSchema.statics.DEVICE_TYPES = {
340
- DESKTOP: "desktop",
341
- MOBILE: "mobile",
342
- TABLET: "tablet",
343
- UNKNOWN: "unknown",
344
- };
345
-
346
- module.exports = mongoose.model("AuditTrail", auditTrailSchema);
1
+ const mongoose = require("mongoose");
2
+
3
+ // User details sub-schema (from JWT token)
4
+ const userDetailsSchema = new mongoose.Schema(
5
+ {
6
+ userId: {
7
+ type: String,
8
+ required: true,
9
+ },
10
+ email: {
11
+ type: String,
12
+ required: true,
13
+ },
14
+ fullName: {
15
+ type: String,
16
+ required: true,
17
+ },
18
+ role: {
19
+ type: String,
20
+ required: true,
21
+ },
22
+ phoneNumber: {
23
+ type: String,
24
+ required: false,
25
+ },
26
+ facilityId: {
27
+ type: String,
28
+ required: false,
29
+ },
30
+ permissions: {
31
+ type: mongoose.Schema.Types.Mixed,
32
+ required: false,
33
+ },
34
+ tokenExpiry: {
35
+ type: Date,
36
+ required: false,
37
+ },
38
+ tokenIssued: {
39
+ type: Date,
40
+ required: false,
41
+ },
42
+ },
43
+ { _id: false },
44
+ );
45
+
46
+ // Main audit trail schema
47
+ const auditTrailSchema = new mongoose.Schema(
48
+ {
49
+ // Basic request information
50
+ timestamp: {
51
+ type: Date,
52
+ required: true,
53
+ default: Date.now,
54
+ },
55
+ method: {
56
+ type: String,
57
+ enum: ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"],
58
+ required: true,
59
+ },
60
+ url: {
61
+ type: String,
62
+ required: true,
63
+ },
64
+ ip_address: {
65
+ type: String,
66
+ required: true,
67
+ },
68
+ platform: {
69
+ type: String,
70
+ required: true,
71
+ },
72
+
73
+ // Authorization and user information
74
+ is_authorized: {
75
+ type: Boolean,
76
+ required: true,
77
+ default: false,
78
+ },
79
+ user_details: {
80
+ type: userDetailsSchema,
81
+ required: false,
82
+ },
83
+
84
+ // Device and browser information
85
+ browser: {
86
+ type: String,
87
+ required: true,
88
+ default: "unknown",
89
+ },
90
+ operating_system: {
91
+ type: String,
92
+ required: true,
93
+ default: "unknown",
94
+ },
95
+ device_type: {
96
+ type: String,
97
+ enum: ["desktop", "mobile", "tablet", "unknown"],
98
+ required: true,
99
+ default: "unknown",
100
+ },
101
+ user_agent: {
102
+ type: String,
103
+ required: false,
104
+ },
105
+
106
+ // Request data
107
+ request_data: {
108
+ type: mongoose.Schema.Types.Mixed,
109
+ required: false,
110
+ },
111
+ query_params: {
112
+ type: mongoose.Schema.Types.Mixed,
113
+ required: false,
114
+ default: {},
115
+ },
116
+ route_params: {
117
+ type: mongoose.Schema.Types.Mixed,
118
+ required: false,
119
+ default: {},
120
+ },
121
+
122
+ // Activity information
123
+ activity: {
124
+ type: String,
125
+ required: true,
126
+ },
127
+
128
+ // response status
129
+ response_status: {
130
+ type: String,
131
+ required: true,
132
+ enum: ["success", "error", "unknown"],
133
+ default: "unknown",
134
+ },
135
+
136
+ // Method-specific data
137
+ previous_data: {
138
+ type: mongoose.Schema.Types.Mixed,
139
+ required: false,
140
+ },
141
+ deleted_data: {
142
+ type: mongoose.Schema.Types.Mixed,
143
+ required: false,
144
+ },
145
+
146
+ // Custom additional data
147
+ custom_data: {
148
+ type: mongoose.Schema.Types.Mixed,
149
+ required: false,
150
+ },
151
+
152
+ // Request tracking
153
+ request_id: {
154
+ type: String,
155
+ required: false,
156
+ },
157
+ },
158
+ {
159
+ timestamps: true,
160
+ },
161
+ );
162
+
163
+ // Pre-save middleware for data validation and cleanup
164
+ auditTrailSchema.pre("save", function (next) {
165
+ // Ensure timestamp is set
166
+ if (!this.timestamp) {
167
+ this.timestamp = new Date();
168
+ }
169
+
170
+ // Validate that authorized requests have user details
171
+ if (this.is_authorized && !this.user_details) {
172
+ console.warn("Authorized request without user details detected");
173
+ }
174
+
175
+ // Ensure query_params and route_params are objects
176
+ if (!this.query_params || typeof this.query_params !== "object") {
177
+ this.query_params = {};
178
+ }
179
+ if (!this.route_params || typeof this.route_params !== "object") {
180
+ this.route_params = {};
181
+ }
182
+
183
+ // Set browser to unknown if not provided
184
+ if (!this.browser) {
185
+ this.browser = "unknown";
186
+ }
187
+
188
+ // Set operating_system to unknown if not provided
189
+ if (!this.operating_system) {
190
+ this.operating_system = "unknown";
191
+ }
192
+
193
+ next();
194
+ });
195
+
196
+ // Static methods for common queries
197
+ auditTrailSchema.statics.findByUser = function (userId, startDate, endDate) {
198
+ const query = { "user_details.userId": userId };
199
+ if (startDate || endDate) {
200
+ query.timestamp = {};
201
+ if (startDate) query.timestamp.$gte = startDate;
202
+ if (endDate) query.timestamp.$lte = endDate;
203
+ }
204
+ return this.find(query).sort({ timestamp: -1 });
205
+ };
206
+
207
+ auditTrailSchema.statics.findByActivity = function (activity, limit = 100) {
208
+ return this.find({ activity: new RegExp(activity, "i") })
209
+ .sort({ timestamp: -1 })
210
+ .limit(limit);
211
+ };
212
+
213
+ auditTrailSchema.statics.findByFacility = function (
214
+ facilityId,
215
+ startDate,
216
+ endDate,
217
+ ) {
218
+ const query = { "route_params.facilityId": facilityId };
219
+ if (startDate || endDate) {
220
+ query.timestamp = {};
221
+ if (startDate) query.timestamp.$gte = startDate;
222
+ if (endDate) query.timestamp.$lte = endDate;
223
+ }
224
+ return this.find(query).sort({ timestamp: -1 });
225
+ };
226
+
227
+ auditTrailSchema.statics.findByAsset = function (assetId, startDate, endDate) {
228
+ const query = { "route_params.assetId": assetId };
229
+ if (startDate || endDate) {
230
+ query.timestamp = {};
231
+ if (startDate) query.timestamp.$gte = startDate;
232
+ if (endDate) query.timestamp.$lte = endDate;
233
+ }
234
+ return this.find(query).sort({ timestamp: -1 });
235
+ };
236
+
237
+ auditTrailSchema.statics.findByIP = function (ipAddress, startDate, endDate) {
238
+ const query = { ip_address: ipAddress };
239
+ if (startDate || endDate) {
240
+ query.timestamp = {};
241
+ if (startDate) query.timestamp.$gte = startDate;
242
+ if (endDate) query.timestamp.$lte = endDate;
243
+ }
244
+ return this.find(query).sort({ timestamp: -1 });
245
+ };
246
+
247
+ auditTrailSchema.statics.findByMethod = function (method, startDate, endDate) {
248
+ const query = { method: method.toUpperCase() };
249
+ if (startDate || endDate) {
250
+ query.timestamp = {};
251
+ if (startDate) query.timestamp.$gte = startDate;
252
+ if (endDate) query.timestamp.$lte = endDate;
253
+ }
254
+ return this.find(query).sort({ timestamp: -1 });
255
+ };
256
+
257
+ // Instance methods
258
+ auditTrailSchema.methods.getActivityType = function () {
259
+ if (this.activity.includes("Create")) return "CREATE";
260
+ if (this.activity.includes("Update")) return "UPDATE";
261
+ if (this.activity.includes("Delete")) return "DELETE";
262
+ if (this.activity.includes("Login")) return "AUTH";
263
+ return "OTHER";
264
+ };
265
+
266
+ auditTrailSchema.methods.isSuccessful = function () {
267
+ return (
268
+ this.activity.includes("Success") ||
269
+ (!this.activity.includes("Failed") &&
270
+ !this.activity.includes("Error") &&
271
+ !this.activity.includes("Not Found"))
272
+ );
273
+ };
274
+
275
+ auditTrailSchema.methods.hasDataChanges = function () {
276
+ return !!(this.previous_data || this.deleted_data);
277
+ };
278
+
279
+ // Create indexes for efficient queries
280
+ auditTrailSchema.index({ timestamp: -1 }); // Recent activities first
281
+ auditTrailSchema.index({ "user_details.userId": 1, timestamp: -1 }); // User activity history
282
+ auditTrailSchema.index({ "user_details.email": 1, timestamp: -1 }); // User by email
283
+ auditTrailSchema.index({ ip_address: 1, timestamp: -1 }); // IP-based queries
284
+ auditTrailSchema.index({ method: 1, timestamp: -1 }); // Method-based queries
285
+ auditTrailSchema.index({ activity: 1, timestamp: -1 }); // Activity-based queries
286
+ auditTrailSchema.index({ platform: 1, timestamp: -1 }); // Platform-based queries
287
+ auditTrailSchema.index({ "route_params.facilityId": 1, timestamp: -1 }); // Facility-based queries
288
+ auditTrailSchema.index({ "route_params.assetId": 1, timestamp: -1 }); // Asset-based queries
289
+ auditTrailSchema.index({ is_authorized: 1, timestamp: -1 }); // Authorization status
290
+ auditTrailSchema.index({ device_type: 1, timestamp: -1 }); // Device type queries
291
+ auditTrailSchema.index({ browser: 1, timestamp: -1 }); // Browser-based queries
292
+
293
+ // Compound indexes for common query patterns
294
+ auditTrailSchema.index({
295
+ "user_details.userId": 1,
296
+ activity: 1,
297
+ timestamp: -1,
298
+ }); // User + activity
299
+ auditTrailSchema.index({
300
+ "route_params.facilityId": 1,
301
+ method: 1,
302
+ timestamp: -1,
303
+ }); // Facility + method
304
+ auditTrailSchema.index({
305
+ ip_address: 1,
306
+ "user_details.userId": 1,
307
+ timestamp: -1,
308
+ }); // IP + user correlation
309
+
310
+ // Text search index for searching activities and user details
311
+ auditTrailSchema.index({
312
+ activity: "text",
313
+ "user_details.email": "text",
314
+ "user_details.fullName": "text",
315
+ url: "text",
316
+ });
317
+
318
+ // Activity type constants
319
+ auditTrailSchema.statics.ACTIVITY_TYPES = {
320
+ CREATE: "CREATE",
321
+ UPDATE: "UPDATE",
322
+ DELETE: "DELETE",
323
+ AUTH: "AUTH",
324
+ OTHER: "OTHER",
325
+ };
326
+
327
+ // Method constants
328
+ auditTrailSchema.statics.METHODS = {
329
+ GET: "GET",
330
+ POST: "POST",
331
+ PUT: "PUT",
332
+ PATCH: "PATCH",
333
+ DELETE: "DELETE",
334
+ OPTIONS: "OPTIONS",
335
+ HEAD: "HEAD",
336
+ };
337
+
338
+ // Device type constants
339
+ auditTrailSchema.statics.DEVICE_TYPES = {
340
+ DESKTOP: "desktop",
341
+ MOBILE: "mobile",
342
+ TABLET: "tablet",
343
+ UNKNOWN: "unknown",
344
+ };
345
+
346
+ module.exports = mongoose.model("AuditTrail", auditTrailSchema);