dt-common-device 6.2.0 → 7.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.
@@ -89,9 +89,10 @@ let AlertService = (() => {
89
89
  /**
90
90
  * Create a readiness alert using AlertBuilder
91
91
  */
92
- async raiseReadinessAlert(propertyId, title, description, entityId, entityType, createdBy) {
92
+ async raiseReadinessAlert(propertyId, zoneId, title, description, entityId, entityType, createdBy) {
93
93
  const alertBuilder = AlertBuilder_1.AlertBuilder.createReadinessAlert()
94
94
  .setPropertyId(propertyId)
95
+ .setZoneId(zoneId)
95
96
  .setTitle(title)
96
97
  .setDescription(description);
97
98
  if (entityId)
@@ -105,9 +106,10 @@ let AlertService = (() => {
105
106
  /**
106
107
  * Create an operations alert using AlertBuilder
107
108
  */
108
- async raiseOperationsAlert(propertyId, title, description, entityId, entityType, createdBy) {
109
+ async raiseOperationsAlert(propertyId, zoneId, title, description, entityId, entityType, createdBy) {
109
110
  const alertBuilder = AlertBuilder_1.AlertBuilder.createOperationsAlert()
110
111
  .setPropertyId(propertyId)
112
+ .setZoneId(zoneId)
111
113
  .setTitle(title)
112
114
  .setDescription(description);
113
115
  if (entityId)
@@ -121,9 +123,10 @@ let AlertService = (() => {
121
123
  /**
122
124
  * Create a security alert using AlertBuilder
123
125
  */
124
- async raiseSecurityAlert(propertyId, title, description, entityId, entityType, createdBy) {
126
+ async raiseSecurityAlert(propertyId, zoneId, title, description, entityId, entityType, createdBy) {
125
127
  const alertBuilder = AlertBuilder_1.AlertBuilder.createSecurityAlert()
126
128
  .setPropertyId(propertyId)
129
+ .setZoneId(zoneId)
127
130
  .setTitle(title)
128
131
  .setDescription(description);
129
132
  if (entityId)
@@ -137,9 +140,10 @@ let AlertService = (() => {
137
140
  /**
138
141
  * Create an energy alert using AlertBuilder
139
142
  */
140
- async raiseEnergyAlert(propertyId, title, description, entityId, entityType, createdBy) {
143
+ async raiseEnergyAlert(propertyId, zoneId, title, description, entityId, entityType, createdBy) {
141
144
  const alertBuilder = AlertBuilder_1.AlertBuilder.createEnergyAlert()
142
145
  .setPropertyId(propertyId)
146
+ .setZoneId(zoneId)
143
147
  .setTitle(title)
144
148
  .setDescription(description);
145
149
  if (entityId)
@@ -153,8 +157,8 @@ let AlertService = (() => {
153
157
  /**
154
158
  * Create a device-specific alert using AlertBuilder
155
159
  */
156
- async raiseDeviceAlert(deviceId, propertyId, title, description, category, severity, source) {
157
- const alertBuilder = AlertBuilder_1.AlertBuilder.createDeviceAlert(deviceId, propertyId)
160
+ async raiseDeviceAlert(deviceId, propertyId, zoneId, title, description, category, severity, source) {
161
+ const alertBuilder = AlertBuilder_1.AlertBuilder.createDeviceAlert(deviceId, propertyId, zoneId)
158
162
  .setTitle(title)
159
163
  .setDescription(description);
160
164
  if (category)
@@ -168,8 +172,8 @@ let AlertService = (() => {
168
172
  /**
169
173
  * Create a hub-specific alert using AlertBuilder
170
174
  */
171
- async raiseHubAlert(hubId, propertyId, title, description, category, severity, createdBy) {
172
- const alertBuilder = AlertBuilder_1.AlertBuilder.createHubAlert(hubId, propertyId)
175
+ async raiseHubAlert(hubId, propertyId, zoneId, title, description, category, severity, createdBy) {
176
+ const alertBuilder = AlertBuilder_1.AlertBuilder.createHubAlert(hubId, propertyId, zoneId)
173
177
  .setTitle(title)
174
178
  .setDescription(description);
175
179
  if (category)
@@ -184,25 +188,25 @@ let AlertService = (() => {
184
188
  * Raise alert for device going offline (OPERATIONAL only)
185
189
  */
186
190
  async raiseDeviceOfflineAlert(device, source, reason) {
187
- return await this.raiseDeviceAlert(device.deviceId, device.propertyId, "Device Offline", `Device ${device.name} (${device.deviceId}) has gone offline. ${reason ? `Reason: ${reason}` : ""}`, [alert_types_1.AlertCategory.OPERATIONS], alert_types_1.AlertSeverity.HIGH, source);
191
+ return await this.raiseDeviceAlert(device.deviceId, device.propertyId, device.zoneId, "Device Offline", `Device ${device.name} (${device.deviceId}) has gone offline. ${reason ? `Reason: ${reason}` : ""}`, alert_types_1.AlertCategory.OPERATIONS, alert_types_1.AlertSeverity.HIGH, source);
188
192
  }
189
193
  /**
190
194
  * Raise alert for device coming online (OPERATIONAL only)
191
195
  */
192
196
  async raiseDeviceOnlineAlert(device, source, reason) {
193
- return await this.raiseDeviceAlert(device.deviceId, device.propertyId, "Device Online", `Device ${device.name} (${device.deviceId}) is now online. ${reason ? `Reason: ${reason}` : ""}`, [alert_types_1.AlertCategory.OPERATIONS], alert_types_1.AlertSeverity.INFO, source);
197
+ return await this.raiseDeviceAlert(device.deviceId, device.propertyId, device.zoneId, "Device Online", `Device ${device.name} (${device.deviceId}) is now online. ${reason ? `Reason: ${reason}` : ""}`, alert_types_1.AlertCategory.OPERATIONS, alert_types_1.AlertSeverity.INFO, source);
194
198
  }
195
199
  /**
196
200
  * Raise alert for device battery level below threshold (READINESS + OPERATIONAL + ENERGY)
197
201
  */
198
202
  async raiseDeviceBatteryAlert(device, batteryLevel, threshold, source) {
199
- return await this.raiseDeviceAlert(device.deviceId, device.propertyId, "Device Battery Low", `Device ${device.name} (${device.deviceId}) battery level is ${batteryLevel}%, which is below the property threshold of ${threshold}%.`, [alert_types_1.AlertCategory.READINESS, alert_types_1.AlertCategory.OPERATIONS, alert_types_1.AlertCategory.ENERGY], alert_types_1.AlertSeverity.MEDIUM, source);
203
+ return await this.raiseDeviceAlert(device.deviceId, device.propertyId, device.zoneId, "Device Battery Low", `Device ${device.name} (${device.deviceId}) battery level is ${batteryLevel}%, which is below the property threshold of ${threshold}%.`, alert_types_1.AlertCategory.ENERGY, alert_types_1.AlertSeverity.MEDIUM, source);
200
204
  }
201
205
  /**
202
206
  * Raise alert for device issue (jammed or malfunctioned) (READINESS + OPERATIONAL)
203
207
  */
204
208
  async raiseDeviceIssueAlert(device, issueType, source, reason) {
205
- return await this.raiseDeviceAlert(device.deviceId, device.propertyId, `Device Issue - ${issueType}`, `Device ${device.name} (${device.deviceId}) has an issue: ${issueType}. ${reason ? `Reason: ${reason}` : ""}`, [alert_types_1.AlertCategory.READINESS, alert_types_1.AlertCategory.OPERATIONS], alert_types_1.AlertSeverity.HIGH, source);
209
+ return await this.raiseDeviceAlert(device.deviceId, device.propertyId, device.zoneId, `Device Issue - ${issueType}`, `Device ${device.name} (${device.deviceId}) has an issue: ${issueType}. ${reason ? `Reason: ${reason}` : ""}`, alert_types_1.AlertCategory.READINESS, alert_types_1.AlertSeverity.HIGH, source);
206
210
  }
207
211
  /**
208
212
  * Create a new alert with business logic validation
@@ -401,8 +405,8 @@ let AlertService = (() => {
401
405
  /**
402
406
  * Get alert statistics with business logic
403
407
  */
404
- async getAlertStatistics(propertyId) {
405
- const stats = await this.alertRepository.getStatistics(propertyId);
408
+ async getAlertStatistics(propertyId, zoneId) {
409
+ const stats = await this.alertRepository.getStatistics(propertyId, zoneId);
406
410
  // Business logic: Add alerts for critical metrics
407
411
  if (stats.unread > 0) {
408
412
  console.warn(`Alert: ${stats.unread} unread alerts require attention`);
@@ -415,6 +419,83 @@ let AlertService = (() => {
415
419
  }
416
420
  return stats;
417
421
  }
422
+ /**
423
+ * Get alerts by zone ID
424
+ */
425
+ async getAlertsByZoneId(zoneId, includeDeleted = false) {
426
+ if (!zoneId) {
427
+ throw new Error("Zone ID is required");
428
+ }
429
+ return await this.alertRepository.findByZoneId(zoneId, includeDeleted);
430
+ }
431
+ /**
432
+ * Get alerts by zone ID and category
433
+ */
434
+ async getAlertsByZoneIdAndCategory(zoneId, category, includeDeleted = false) {
435
+ if (!zoneId) {
436
+ throw new Error("Zone ID is required");
437
+ }
438
+ if (!category) {
439
+ throw new Error("Alert category is required");
440
+ }
441
+ return await this.alertRepository.findByZoneIdAndCategory(zoneId, category, includeDeleted);
442
+ }
443
+ /**
444
+ * Get alerts by zone ID and severity
445
+ */
446
+ async getAlertsByZoneIdAndSeverity(zoneId, severity, includeDeleted = false) {
447
+ if (!zoneId) {
448
+ throw new Error("Zone ID is required");
449
+ }
450
+ if (!severity) {
451
+ throw new Error("Alert severity is required");
452
+ }
453
+ return await this.alertRepository.findByZoneIdAndSeverity(zoneId, severity, includeDeleted);
454
+ }
455
+ /**
456
+ * Get alerts by multiple zone IDs
457
+ */
458
+ async getAlertsByZoneIds(zoneIds, includeDeleted = false) {
459
+ if (!zoneIds || zoneIds.length === 0) {
460
+ throw new Error("Zone IDs array is required and cannot be empty");
461
+ }
462
+ return await this.alertRepository.findByZoneIds(zoneIds, includeDeleted);
463
+ }
464
+ /**
465
+ * Get alert statistics by zone ID and severity
466
+ */
467
+ async getAlertStatisticsByZoneAndSeverity(zoneId, severity) {
468
+ if (!zoneId) {
469
+ throw new Error("Zone ID is required");
470
+ }
471
+ const stats = await this.alertRepository.getStatisticsByZoneAndSeverity(zoneId, severity);
472
+ // Business logic: Add alerts for critical metrics
473
+ if (stats.unread > 0) {
474
+ console.warn(`Alert: ${stats.unread} unread alerts in zone ${zoneId} require attention`);
475
+ }
476
+ if (severity === alert_types_1.AlertSeverity.CRITICAL && stats.total > 0) {
477
+ console.error(`Alert: ${stats.total} critical alerts in zone ${zoneId} require immediate attention`);
478
+ }
479
+ return stats;
480
+ }
481
+ /**
482
+ * Get alerts by zone ID and active status
483
+ */
484
+ async getAlertsByZoneIdAndActiveStatus(zoneId, isActive, includeDeleted = false) {
485
+ if (!zoneId) {
486
+ throw new Error("Zone ID is required");
487
+ }
488
+ return await this.alertRepository.findByZoneIdAndActiveStatus(zoneId, isActive, includeDeleted);
489
+ }
490
+ /**
491
+ * Get alerts by zone ID and read status
492
+ */
493
+ async getAlertsByZoneIdAndReadStatus(zoneId, isRead, includeDeleted = false) {
494
+ if (!zoneId) {
495
+ throw new Error("Zone ID is required");
496
+ }
497
+ return await this.alertRepository.findByZoneIdAndReadStatus(zoneId, isRead, includeDeleted);
498
+ }
418
499
  // Private business logic methods
419
500
  validateAlertData(data) {
420
501
  if (!data.title || data.title.trim().length < 3) {
@@ -426,6 +507,9 @@ let AlertService = (() => {
426
507
  if (!data.propertyId) {
427
508
  throw new Error("Property ID is required");
428
509
  }
510
+ if (!data.zoneId) {
511
+ throw new Error("Zone ID is required");
512
+ }
429
513
  if (!data.entityType) {
430
514
  throw new Error("Entity type is required");
431
515
  }
@@ -460,19 +544,6 @@ let AlertService = (() => {
460
544
  [alert_types_1.AlertCategory.ENERGY]: alert_types_1.AlertSeverity.LOW,
461
545
  [alert_types_1.AlertCategory.OTHER]: alert_types_1.AlertSeverity.MEDIUM,
462
546
  };
463
- // If it's an array, use the highest severity category
464
- if (Array.isArray(category)) {
465
- const severities = category.map((cat) => categorySeverities[cat] || alert_types_1.AlertSeverity.MEDIUM);
466
- const severityOrder = [
467
- alert_types_1.AlertSeverity.CRITICAL,
468
- alert_types_1.AlertSeverity.HIGH,
469
- alert_types_1.AlertSeverity.MEDIUM,
470
- alert_types_1.AlertSeverity.LOW,
471
- alert_types_1.AlertSeverity.INFO,
472
- ];
473
- return (severityOrder.find((severity) => severities.includes(severity)) ||
474
- alert_types_1.AlertSeverity.MEDIUM);
475
- }
476
547
  return categorySeverities[category] || alert_types_1.AlertSeverity.MEDIUM;
477
548
  }
478
549
  applyBusinessRules(filters) {
@@ -23,11 +23,15 @@ export declare class AlertBuilder {
23
23
  /**
24
24
  * Sets the alert category
25
25
  */
26
- setCategory(category: AlertCategory | AlertCategory[]): this;
26
+ setCategory(category: AlertCategory): this;
27
27
  /**
28
28
  * Sets the property ID
29
29
  */
30
30
  setPropertyId(propertyId: string): this;
31
+ /**
32
+ * Sets the zone ID
33
+ */
34
+ setZoneId(zoneId: string): this;
31
35
  /**
32
36
  * Sets the alert title
33
37
  */
@@ -79,9 +83,9 @@ export declare class AlertBuilder {
79
83
  /**
80
84
  * Creates a device-specific alert builder
81
85
  */
82
- static createDeviceAlert(deviceId: string, propertyId: string): AlertBuilder;
86
+ static createDeviceAlert(deviceId: string, propertyId: string, zoneId: string): AlertBuilder;
83
87
  /**
84
88
  * Creates a hub-specific alert builder
85
89
  */
86
- static createHubAlert(hubId: string, propertyId: string): AlertBuilder;
90
+ static createHubAlert(hubId: string, propertyId: string, zoneId: string): AlertBuilder;
87
91
  }
@@ -29,7 +29,7 @@ class AlertBuilder {
29
29
  * Sets the alert category
30
30
  */
31
31
  setCategory(category) {
32
- this.data.category = Array.isArray(category) ? category : [category];
32
+ this.data.category = category;
33
33
  return this;
34
34
  }
35
35
  /**
@@ -42,6 +42,16 @@ class AlertBuilder {
42
42
  this.data.propertyId = propertyId;
43
43
  return this;
44
44
  }
45
+ /**
46
+ * Sets the zone ID
47
+ */
48
+ setZoneId(zoneId) {
49
+ if (!zoneId || zoneId.trim() === "") {
50
+ throw new Error("Zone ID is required and cannot be empty");
51
+ }
52
+ this.data.zoneId = zoneId;
53
+ return this;
54
+ }
45
55
  /**
46
56
  * Sets the alert title
47
57
  */
@@ -112,6 +122,7 @@ class AlertBuilder {
112
122
  const requiredFields = [
113
123
  "category",
114
124
  "propertyId",
125
+ "zoneId",
115
126
  "title",
116
127
  "description",
117
128
  "entityType",
@@ -167,20 +178,22 @@ class AlertBuilder {
167
178
  /**
168
179
  * Creates a device-specific alert builder
169
180
  */
170
- static createDeviceAlert(deviceId, propertyId) {
181
+ static createDeviceAlert(deviceId, propertyId, zoneId) {
171
182
  return new AlertBuilder()
172
183
  .setEntityType(alert_types_1.EntityType.DEVICE)
173
184
  .setEntityId(deviceId)
174
- .setPropertyId(propertyId);
185
+ .setPropertyId(propertyId)
186
+ .setZoneId(zoneId);
175
187
  }
176
188
  /**
177
189
  * Creates a hub-specific alert builder
178
190
  */
179
- static createHubAlert(hubId, propertyId) {
191
+ static createHubAlert(hubId, propertyId, zoneId) {
180
192
  return new AlertBuilder()
181
193
  .setEntityType(alert_types_1.EntityType.HUB)
182
194
  .setEntityId(hubId)
183
- .setPropertyId(propertyId);
195
+ .setPropertyId(propertyId)
196
+ .setZoneId(zoneId);
184
197
  }
185
198
  }
186
199
  exports.AlertBuilder = AlertBuilder;
@@ -132,7 +132,7 @@ class AlertServiceExample {
132
132
  */
133
133
  async createAlertWithLegacyData() {
134
134
  const alertData = {
135
- category: [alert_types_1.AlertCategory.OPERATIONS],
135
+ category: alert_types_1.AlertCategory.OPERATIONS,
136
136
  propertyId: "prop123",
137
137
  title: "Legacy Alert",
138
138
  description: "This alert was created using the old CreateAlertData format",
@@ -15,8 +15,9 @@ export declare enum AlertSeverity {
15
15
  }
16
16
  export interface AlertDocument {
17
17
  _id: string;
18
- category: AlertCategory[];
18
+ category: AlertCategory;
19
19
  propertyId: string;
20
+ zoneId: string;
20
21
  title: string;
21
22
  description: string;
22
23
  entityId?: string;
@@ -32,8 +33,9 @@ export interface AlertDocument {
32
33
  updatedAt: Date;
33
34
  }
34
35
  export interface CreateAlertData {
35
- category: AlertCategory[];
36
+ category: AlertCategory;
36
37
  propertyId: string;
38
+ zoneId: string;
37
39
  title: string;
38
40
  description: string;
39
41
  entityId?: string;
@@ -43,7 +45,7 @@ export interface CreateAlertData {
43
45
  snoozeUntil?: Date;
44
46
  }
45
47
  export interface UpdateAlertData {
46
- category?: AlertCategory[];
48
+ category?: AlertCategory;
47
49
  title?: string;
48
50
  description?: string;
49
51
  entityId?: string;
@@ -56,7 +58,8 @@ export interface UpdateAlertData {
56
58
  }
57
59
  export interface IAlertQuery {
58
60
  propertyId?: string;
59
- category?: AlertCategory | AlertCategory[];
61
+ zoneId?: string;
62
+ category?: AlertCategory;
60
63
  severity?: AlertSeverity;
61
64
  entityType?: EntityType;
62
65
  entityId?: string;
@@ -1,6 +1,5 @@
1
1
  import { IAccessGroup, IUser, IZone } from "./IAdmin";
2
2
  export declare class AdminRepository {
3
- private readonly axiosInstance;
4
3
  private readonly deviceRepository;
5
4
  private readonly postgres;
6
5
  constructor();
@@ -84,7 +84,6 @@ let AdminRepository = (() => {
84
84
  let _classThis;
85
85
  var AdminRepository = _classThis = class {
86
86
  constructor() {
87
- this.axiosInstance = (0, utils_1.getAdminServiceAxiosInstance)();
88
87
  this.deviceRepository = typedi_1.default.get(Device_repository_1.DeviceRepository);
89
88
  this.postgres = (0, db_1.getPostgresClient)();
90
89
  }
@@ -112,7 +111,7 @@ let AdminRepository = (() => {
112
111
  const collectionZone = [];
113
112
  for (let zone of response) {
114
113
  let zoneIds = [];
115
- const zones = (await this.axiosInstance.get(`/zones/child?zoneId=${zone.zoneId}`))?.data?.data;
114
+ const zones = (await (0, utils_1.getAdminServiceAxiosInstance)().get(`/zones/child?zoneId=${zone.zoneId}`))?.data?.data;
116
115
  zoneIds.push(zone.zoneId);
117
116
  if (zones.childZones?.length > 0) {
118
117
  const nestedZoneIds = new Set(_zones(zones.childZones));
@@ -65,11 +65,6 @@ let EventHandler = (() => {
65
65
  actionPayload: body,
66
66
  },
67
67
  });
68
- // const payload = {
69
- // eventType: DT_EVENT_TYPES.DEVICE.CREATE.SUCCESS,
70
- // properties: await this.auditUtils.buildAuditProperties(body),
71
- // };
72
- // await publishAudit(payload);
73
68
  }
74
69
  async onDeviceUpdate(deviceId, body, auditBody) {
75
70
  await (0, audit_1.pushAudit)({
@@ -80,15 +75,6 @@ let EventHandler = (() => {
80
75
  ...auditBody,
81
76
  },
82
77
  });
83
- // const payload = {
84
- // eventType: DT_EVENT_TYPES.DEVICE.UPDATE.SUCCESS,
85
- // properties: await this.auditUtils.buildAuditProperties({
86
- // ...auditBody,
87
- // deviceId,
88
- // ...body,
89
- // }),
90
- // };
91
- // await publishAudit(payload);
92
78
  }
93
79
  async onDeviceDelete(deviceId, auditBody) {
94
80
  await (0, audit_1.pushAudit)({
@@ -98,14 +84,6 @@ let EventHandler = (() => {
98
84
  ...auditBody,
99
85
  },
100
86
  });
101
- // const payload = {
102
- // eventType: DT_EVENT_TYPES.DEVICE.DELETE.SUCCESS,
103
- // properties: await this.auditUtils.buildAuditProperties({
104
- // ...auditBody,
105
- // deviceId,
106
- // }),
107
- // };
108
- // await publishAudit(payload);
109
87
  }
110
88
  async onStateChange(deviceId, state, auditProperties, eventType) {
111
89
  await (0, audit_1.pushAudit)({
@@ -116,15 +94,6 @@ let EventHandler = (() => {
116
94
  ...auditProperties,
117
95
  },
118
96
  });
119
- // const payload = {
120
- // eventType: eventType || DT_EVENT_TYPES.DEVICE.STATE.CHANGED,
121
- // properties: await this.auditUtils.buildAuditProperties({
122
- // ...auditProperties,
123
- // deviceId,
124
- // ...state,
125
- // }),
126
- // };
127
- // await publishAudit(payload);
128
97
  }
129
98
  async onStatusChange(deviceId, status, auditProperties, eventType) {
130
99
  await (0, audit_1.pushAudit)({
@@ -135,15 +104,6 @@ let EventHandler = (() => {
135
104
  ...auditProperties,
136
105
  },
137
106
  });
138
- // const payload = {
139
- // eventType: eventType || DT_EVENT_TYPES.DEVICE.STATUS.CHANGED,
140
- // properties: await this.auditUtils.buildAuditProperties({
141
- // ...auditProperties,
142
- // deviceId,
143
- // ...status,
144
- // }),
145
- // };
146
- // await publishAudit(payload);
147
107
  }
148
108
  async onStatusChangeMany(query, status, auditProperties, eventType) {
149
109
  await (0, audit_1.pushAudit)({
@@ -154,15 +114,6 @@ let EventHandler = (() => {
154
114
  ...auditProperties,
155
115
  },
156
116
  });
157
- // const payload = {
158
- // eventType: eventType || DT_EVENT_TYPES.DEVICE.STATUS.UPDATED,
159
- // properties: await this.auditUtils.buildAuditProperties({
160
- // ...auditProperties,
161
- // ...query,
162
- // ...status,
163
- // }),
164
- // };
165
- // await publishAudit(payload);
166
117
  }
167
118
  async onBatteryLevelChange(deviceId, batteryLevel, auditProperties) {
168
119
  await (0, audit_1.pushAudit)({
@@ -173,15 +124,6 @@ let EventHandler = (() => {
173
124
  ...auditProperties,
174
125
  },
175
126
  });
176
- // const payload = {
177
- // eventType: DT_EVENT_TYPES.DEVICE.BATTERY_LEVEL.UPDATED,
178
- // properties: await this.auditUtils.buildAuditProperties({
179
- // ...auditProperties,
180
- // deviceId,
181
- // batteryLevel,
182
- // }),
183
- // };
184
- // await publishAudit(payload);
185
127
  }
186
128
  async onDeviceMetaChange(deviceId, metaData, auditProperties) {
187
129
  await (0, audit_1.pushAudit)({
@@ -192,15 +134,6 @@ let EventHandler = (() => {
192
134
  ...auditProperties,
193
135
  },
194
136
  });
195
- // const payload = {
196
- // eventType: DT_EVENT_TYPES.DEVICE.METADATA.UPDATED,
197
- // properties: await this.auditUtils.buildAuditProperties({
198
- // ...auditProperties,
199
- // deviceId,
200
- // metaData,
201
- // }),
202
- // };
203
- // await publishAudit(payload);
204
137
  }
205
138
  };
206
139
  __setFunctionName(_classThis, "EventHandler");
@@ -56,6 +56,11 @@ const IssueSchema = new mongoose_1.Schema({
56
56
  required: true,
57
57
  index: true,
58
58
  },
59
+ zoneId: {
60
+ type: String,
61
+ required: true,
62
+ index: true,
63
+ },
59
64
  title: {
60
65
  type: String,
61
66
  required: true,
@@ -123,9 +128,13 @@ const IssueSchema = new mongoose_1.Schema({
123
128
  }, {
124
129
  timestamps: true,
125
130
  collection: "dt_issues",
131
+ toJSON: { virtuals: true },
132
+ toObject: { virtuals: true },
133
+ id: false, // Disable the virtual id field since we're handling it manually
126
134
  });
127
135
  exports.IssueSchema = IssueSchema;
128
136
  IssueSchema.index({ propertyId: 1, status: 1 });
137
+ IssueSchema.index({ zoneId: 1, status: 1 });
129
138
  IssueSchema.index({ assignedTo: 1, status: 1 });
130
139
  IssueSchema.index({ entityId: 1, entityType: 1 });
131
140
  // Pre-save middleware to update the updatedAt field
@@ -138,6 +147,35 @@ IssueSchema.pre(["updateOne", "findOneAndUpdate", "updateMany"], function (next)
138
147
  this.set({ updatedAt: new Date() });
139
148
  next();
140
149
  });
150
+ // Post middleware to transform all find results to plain objects
151
+ IssueSchema.post(/^find/, function (result) {
152
+ if (!result)
153
+ return;
154
+ // Handle array results (find)
155
+ if (Array.isArray(result)) {
156
+ result.forEach((doc) => {
157
+ if (doc && typeof doc.toObject === "function") {
158
+ const plainDoc = doc.toObject();
159
+ // Transform _id to id and remove __v
160
+ plainDoc.id = plainDoc._id ? plainDoc._id.toString() : plainDoc._id;
161
+ delete plainDoc._id;
162
+ delete plainDoc.__v;
163
+ // Replace the document with plain object
164
+ Object.assign(doc, plainDoc);
165
+ }
166
+ });
167
+ }
168
+ // Handle single document results (findOne, findById, etc.)
169
+ else if (result && typeof result.toObject === "function") {
170
+ const plainDoc = result.toObject();
171
+ // Transform _id to id and remove __v
172
+ plainDoc.id = plainDoc._id ? plainDoc._id.toString() : plainDoc._id;
173
+ delete plainDoc._id;
174
+ delete plainDoc.__v;
175
+ // Replace the document with plain object
176
+ Object.assign(result, plainDoc);
177
+ }
178
+ });
141
179
  // Instance methods
142
180
  IssueSchema.methods.addComment = function (commentData) {
143
181
  const comment = {
@@ -225,8 +263,24 @@ IssueSchema.statics.findUpcoming = function (days = 7, includeDeleted = false) {
225
263
  IssueSchema.virtual("isActive").get(function () {
226
264
  return !this.isDeleted;
227
265
  });
228
- // Ensure virtuals are serialized
229
- IssueSchema.set("toJSON", { virtuals: true });
230
- IssueSchema.set("toObject", { virtuals: true });
266
+ // Ensure virtuals are serialized and transform to plain objects
267
+ IssueSchema.set("toJSON", {
268
+ virtuals: true,
269
+ transform: function (doc, ret) {
270
+ ret.id = ret._id ? ret._id.toString() : ret._id;
271
+ delete ret._id;
272
+ delete ret.__v;
273
+ return ret;
274
+ },
275
+ });
276
+ IssueSchema.set("toObject", {
277
+ virtuals: true,
278
+ transform: function (doc, ret) {
279
+ ret.id = ret._id ? ret._id.toString() : ret._id;
280
+ delete ret._id;
281
+ delete ret.__v;
282
+ return ret;
283
+ },
284
+ });
231
285
  // Create and export the model
232
286
  exports.IssueModel = mongoose_1.default.model("Issue", IssueSchema);
@@ -47,6 +47,7 @@ export declare class IssueRepository {
47
47
  */
48
48
  search(searchTerm: string, filters?: {
49
49
  propertyId?: string;
50
+ zoneId?: string;
50
51
  includeDeleted?: boolean;
51
52
  limit?: number;
52
53
  skip?: number;
@@ -54,7 +55,7 @@ export declare class IssueRepository {
54
55
  /**
55
56
  * Get issue statistics
56
57
  */
57
- getStatistics(propertyId?: string): Promise<{
58
+ getStatistics(propertyId?: string, zoneId?: string): Promise<{
58
59
  total: number;
59
60
  pending: number;
60
61
  inProgress: number;
@@ -72,4 +73,16 @@ export declare class IssueRepository {
72
73
  * Bulk soft delete issues
73
74
  */
74
75
  bulkSoftDelete(ids: string[], deletedBy: string): Promise<number>;
76
+ /**
77
+ * Find issues by zone ID
78
+ */
79
+ findByZoneId(zoneId: string, includeDeleted?: boolean): Promise<IIssueDocument[]>;
80
+ /**
81
+ * Find issues by zone ID and status
82
+ */
83
+ findByZoneIdAndStatus(zoneId: string, status: string, includeDeleted?: boolean): Promise<IIssueDocument[]>;
84
+ /**
85
+ * Find issues by multiple zone IDs
86
+ */
87
+ findByZoneIds(zoneIds: string[], includeDeleted?: boolean): Promise<IIssueDocument[]>;
75
88
  }