dt-common-device 2.0.6 → 3.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 (187) hide show
  1. package/README.md +321 -99
  2. package/dist/alerts/Alert.model.d.ts +28 -0
  3. package/dist/alerts/Alert.model.js +222 -0
  4. package/dist/alerts/Alert.repository.d.ts +106 -0
  5. package/dist/alerts/Alert.repository.js +374 -0
  6. package/dist/alerts/Alert.service.d.ts +137 -0
  7. package/dist/alerts/Alert.service.js +476 -0
  8. package/dist/alerts/AlertBuilder.d.ts +87 -0
  9. package/dist/alerts/AlertBuilder.example.d.ts +11 -0
  10. package/dist/alerts/AlertBuilder.example.js +117 -0
  11. package/dist/alerts/AlertBuilder.js +185 -0
  12. package/dist/alerts/AlertService.example.d.ts +55 -0
  13. package/dist/alerts/AlertService.example.js +148 -0
  14. package/dist/alerts/alert.types.d.ts +57 -0
  15. package/dist/alerts/alert.types.js +22 -0
  16. package/dist/alerts/index.d.ts +3 -0
  17. package/dist/alerts/index.js +19 -0
  18. package/dist/config/config.d.ts +4 -4
  19. package/dist/config/config.js +3 -3
  20. package/dist/config/config.types.d.ts +19 -0
  21. package/dist/config/config.types.js +2 -0
  22. package/dist/connection/Connection.repository.d.ts +8 -0
  23. package/dist/connection/Connection.repository.js +92 -0
  24. package/dist/connection/Connection.service.d.ts +8 -0
  25. package/dist/connection/Connection.service.js +32 -0
  26. package/dist/connection/IConnection.d.ts +26 -0
  27. package/dist/connection/IConnection.js +14 -0
  28. package/dist/connection/index.d.ts +2 -0
  29. package/dist/connection/index.js +18 -0
  30. package/dist/device/cloud/entities/CloudDevice.d.ts +2 -2
  31. package/dist/device/cloud/entities/CloudDeviceService.d.ts +1 -1
  32. package/dist/device/cloud/entities/DeviceFactory.d.ts +1 -1
  33. package/dist/device/cloud/entities/DeviceFactory.js +1 -1
  34. package/dist/device/cloud/interfaces/ICloudDeviceService.d.ts +1 -1
  35. package/dist/device/cloud/interfaces/IRawDevice.d.ts +1 -1
  36. package/dist/device/local/interfaces/index.d.ts +2 -3
  37. package/dist/device/local/interfaces/index.js +2 -3
  38. package/dist/device/local/repository/Device.repository.d.ts +2 -0
  39. package/dist/device/local/repository/Device.repository.js +22 -3
  40. package/dist/device/local/repository/Hub.repository.js +4 -4
  41. package/dist/device/local/repository/Schedule.repository.js +2 -2
  42. package/dist/device/local/services/Device.service.d.ts +2 -2
  43. package/dist/device/local/services/Device.service.js +3 -1
  44. package/dist/device/local/services/index.d.ts +0 -4
  45. package/dist/device/local/services/index.js +0 -4
  46. package/dist/events/BaseEventHandler.d.ts +2 -2
  47. package/dist/events/BaseEventHandler.js +2 -2
  48. package/dist/events/BaseEventTransformer.d.ts +1 -1
  49. package/dist/events/BaseEventTransformer.js +1 -1
  50. package/dist/events/DeviceEventHandler.d.ts +1 -1
  51. package/dist/events/DeviceEventHandler.js +2 -2
  52. package/dist/events/EventHandler.js +1 -1
  53. package/dist/events/EventHandlerOrchestrator.js +1 -1
  54. package/dist/events/EventProcessingService.js +1 -1
  55. package/dist/events/InternalEventSubscription.js +1 -1
  56. package/dist/index.d.ts +7 -5
  57. package/dist/index.js +16 -13
  58. package/dist/issues/Issue.model.d.ts +28 -0
  59. package/dist/issues/Issue.model.js +260 -0
  60. package/dist/issues/Issue.repository.d.ts +113 -0
  61. package/dist/issues/Issue.repository.js +401 -0
  62. package/dist/issues/Issue.service.d.ts +168 -0
  63. package/dist/issues/Issue.service.js +642 -0
  64. package/dist/issues/IssueBuilder.d.ts +109 -0
  65. package/dist/issues/IssueBuilder.example.d.ts +16 -0
  66. package/dist/issues/IssueBuilder.example.js +196 -0
  67. package/dist/issues/IssueBuilder.js +237 -0
  68. package/dist/issues/IssueService.example.d.ts +68 -0
  69. package/dist/issues/IssueService.example.js +177 -0
  70. package/dist/issues/index.d.ts +2 -0
  71. package/dist/issues/index.js +18 -0
  72. package/dist/issues/issue.types.d.ts +90 -0
  73. package/dist/issues/issue.types.js +40 -0
  74. package/dist/property/IProperty.d.ts +29 -0
  75. package/dist/property/IProperty.js +2 -0
  76. package/dist/property/Property.repository.d.ts +8 -0
  77. package/dist/property/Property.repository.js +95 -0
  78. package/dist/property/Property.service.d.ts +8 -0
  79. package/dist/property/Property.service.js +36 -0
  80. package/dist/property/index.d.ts +2 -0
  81. package/dist/property/index.js +18 -0
  82. package/dist/queue/entities/HybridHttpQueue.d.ts +23 -0
  83. package/dist/queue/entities/HybridHttpQueue.js +189 -0
  84. package/dist/queue/entities/index.d.ts +1 -0
  85. package/dist/queue/entities/index.js +17 -0
  86. package/dist/queue/index.d.ts +5 -0
  87. package/dist/queue/index.js +22 -0
  88. package/dist/queue/interfaces/IHttpRequestJob.d.ts +9 -0
  89. package/dist/queue/interfaces/IHttpRequestJob.js +2 -0
  90. package/dist/queue/interfaces/IHybridHttpQueue.d.ts +16 -0
  91. package/dist/queue/interfaces/IHybridHttpQueue.js +2 -0
  92. package/dist/queue/interfaces/IJobResult.d.ts +6 -0
  93. package/dist/queue/interfaces/IJobResult.js +2 -0
  94. package/dist/queue/interfaces/IRateLimitConfig.d.ts +5 -0
  95. package/dist/queue/interfaces/IRateLimitConfig.js +2 -0
  96. package/dist/queue/interfaces/index.d.ts +4 -0
  97. package/dist/queue/interfaces/index.js +20 -0
  98. package/dist/queue/services/QueueService.d.ts +19 -0
  99. package/dist/queue/services/QueueService.js +73 -0
  100. package/dist/queue/services/index.d.ts +1 -0
  101. package/dist/queue/services/index.js +17 -0
  102. package/dist/queue/types/http.types.d.ts +21 -0
  103. package/dist/queue/types/http.types.js +2 -0
  104. package/dist/queue/types/index.d.ts +2 -0
  105. package/dist/queue/types/index.js +18 -0
  106. package/dist/queue/types/queue.types.d.ts +35 -0
  107. package/dist/queue/types/queue.types.js +2 -0
  108. package/dist/queue/utils/index.d.ts +3 -0
  109. package/dist/queue/utils/index.js +19 -0
  110. package/dist/queue/utils/jobUtils.d.ts +10 -0
  111. package/dist/queue/utils/jobUtils.js +64 -0
  112. package/dist/queue/utils/queueUtils.d.ts +5 -0
  113. package/dist/queue/utils/queueUtils.js +59 -0
  114. package/dist/queue/utils/rateLimit.utils.d.ts +6 -0
  115. package/dist/queue/utils/rateLimit.utils.js +44 -0
  116. package/package.json +2 -1
  117. package/src/{device/local/models → alerts}/Alert.model.ts +1 -1
  118. package/src/{device/local/repository → alerts}/Alert.repository.ts +2 -2
  119. package/src/{device/local/services → alerts}/Alert.service.ts +14 -7
  120. package/src/{device/local/entities → alerts}/AlertBuilder.example.ts +2 -2
  121. package/src/{device/local/entities → alerts}/AlertBuilder.ts +14 -8
  122. package/src/{device/local/services → alerts}/AlertService.example.ts +6 -5
  123. package/src/{types → alerts}/alert.types.ts +2 -2
  124. package/src/alerts/index.ts +3 -0
  125. package/src/config/config.ts +7 -7
  126. package/src/{types → config}/config.types.ts +1 -1
  127. package/src/{device/local/repository → connection}/Connection.repository.ts +2 -2
  128. package/src/{device/local/services → connection}/Connection.service.ts +2 -2
  129. package/src/connection/index.ts +3 -0
  130. package/src/device/cloud/entities/CloudDevice.ts +2 -2
  131. package/src/device/cloud/entities/CloudDeviceService.ts +1 -1
  132. package/src/device/cloud/entities/DeviceFactory.ts +2 -2
  133. package/src/device/cloud/interfaces/ICloudDeviceService.ts +1 -1
  134. package/src/device/cloud/interfaces/IRawDevice.ts +1 -1
  135. package/src/device/local/interfaces/index.ts +2 -3
  136. package/src/device/local/repository/Device.repository.ts +29 -3
  137. package/src/device/local/repository/Hub.repository.ts +4 -4
  138. package/src/device/local/repository/Schedule.repository.ts +2 -2
  139. package/src/device/local/services/Device.service.ts +5 -1
  140. package/src/device/local/services/index.ts +0 -4
  141. package/{TROUBLESHOOTING.md → src/docs/TROUBLESHOOTING.md} +2 -2
  142. package/src/events/BaseEventHandler.ts +3 -3
  143. package/src/events/BaseEventTransformer.ts +2 -2
  144. package/src/events/DeviceEventHandler.ts +3 -3
  145. package/src/events/EventHandler.ts +1 -1
  146. package/src/events/EventHandlerOrchestrator.ts +2 -2
  147. package/src/events/EventProcessingService.ts +2 -2
  148. package/src/events/InternalEventSubscription.ts +2 -2
  149. package/src/index.ts +19 -13
  150. package/src/{device/local/models → issues}/Issue.model.ts +1 -1
  151. package/src/{device/local/repository → issues}/Issue.repository.ts +2 -2
  152. package/src/{device/local/services → issues}/Issue.service.ts +4 -4
  153. package/src/{device/local/entities → issues}/IssueBuilder.example.ts +1 -1
  154. package/src/{device/local/entities → issues}/IssueBuilder.ts +1 -1
  155. package/src/{device/local/services → issues}/IssueService.example.ts +6 -5
  156. package/src/issues/index.ts +2 -0
  157. package/src/{device/local/repository → property}/Property.repository.ts +2 -2
  158. package/src/{device/local/services → property}/Property.service.ts +1 -1
  159. package/src/property/index.ts +2 -0
  160. package/src/queue/entities/HybridHttpQueue.ts +196 -0
  161. package/src/queue/entities/index.ts +1 -0
  162. package/src/queue/index.ts +6 -0
  163. package/src/queue/interfaces/IHttpRequestJob.ts +10 -0
  164. package/src/queue/interfaces/IHybridHttpQueue.ts +23 -0
  165. package/src/queue/interfaces/IJobResult.ts +6 -0
  166. package/src/queue/interfaces/IRateLimitConfig.ts +5 -0
  167. package/src/queue/interfaces/index.ts +4 -0
  168. package/src/queue/services/QueueService.ts +39 -0
  169. package/src/queue/services/index.ts +1 -0
  170. package/src/queue/types/http.types.ts +22 -0
  171. package/src/queue/types/index.ts +2 -0
  172. package/src/queue/types/queue.types.ts +22 -0
  173. package/src/queue/utils/index.ts +3 -0
  174. package/src/queue/utils/jobUtils.ts +80 -0
  175. package/src/queue/utils/queueUtils.ts +90 -0
  176. package/src/queue/utils/rateLimit.utils.ts +58 -0
  177. package/tsconfig.json +4 -0
  178. package/src/device/local/entities/README.md +0 -173
  179. package/src/device/local/entities/index.ts +0 -2
  180. package/src/types/index.ts +0 -3
  181. /package/src/{device/local/interfaces → connection}/IConnection.ts +0 -0
  182. /package/src/{device/local/models → docs}/Alert.model.md +0 -0
  183. /package/src/{device/local/models/README.md → docs/Alerts&IssuesModel.md} +0 -0
  184. /package/src/{device/local/models → docs}/Issue.model.md +0 -0
  185. /package/{SECURITY.md → src/docs/SECURITY.md} +0 -0
  186. /package/src/{types → issues}/issue.types.ts +0 -0
  187. /package/src/{device/local/interfaces → property}/IProperty.ts +0 -0
@@ -0,0 +1,137 @@
1
+ import { AlertRepository } from "./Alert.repository";
2
+ import { IAlertDocument } from "./Alert.model";
3
+ import { CreateAlertData, UpdateAlertData, AlertCategory, AlertSeverity, EntityType } from "./alert.types";
4
+ import { AlertBuilder } from "./AlertBuilder";
5
+ export declare class AlertService {
6
+ private readonly alertRepository;
7
+ constructor(alertRepository: AlertRepository);
8
+ /**
9
+ * Create a readiness alert using AlertBuilder
10
+ */
11
+ raiseReadinessAlert(propertyId: string, title: string, description: string, entityId?: string, entityType?: EntityType, createdBy?: string): Promise<IAlertDocument>;
12
+ /**
13
+ * Create an operations alert using AlertBuilder
14
+ */
15
+ raiseOperationsAlert(propertyId: string, title: string, description: string, entityId?: string, entityType?: EntityType, createdBy?: string): Promise<IAlertDocument>;
16
+ /**
17
+ * Create a security alert using AlertBuilder
18
+ */
19
+ raiseSecurityAlert(propertyId: string, title: string, description: string, entityId?: string, entityType?: EntityType, createdBy?: string): Promise<IAlertDocument>;
20
+ /**
21
+ * Create an energy alert using AlertBuilder
22
+ */
23
+ raiseEnergyAlert(propertyId: string, title: string, description: string, entityId?: string, entityType?: EntityType, createdBy?: string): Promise<IAlertDocument>;
24
+ /**
25
+ * Create a device-specific alert using AlertBuilder
26
+ */
27
+ raiseDeviceAlert(deviceId: string, propertyId: string, title: string, description: string, category?: AlertCategory, severity?: AlertSeverity, createdBy?: string): Promise<IAlertDocument>;
28
+ /**
29
+ * Create a hub-specific alert using AlertBuilder
30
+ */
31
+ raiseHubAlert(hubId: string, propertyId: string, title: string, description: string, category?: AlertCategory, severity?: AlertSeverity, createdBy?: string): Promise<IAlertDocument>;
32
+ /**
33
+ * Create a new alert with business logic validation
34
+ * Accepts either a CreateAlertData object or an AlertBuilder instance
35
+ */
36
+ createAlert(alertData: CreateAlertData | AlertBuilder): Promise<IAlertDocument>;
37
+ /**
38
+ * Get alert by ID with business logic
39
+ */
40
+ getAlertById(id: string, includeDeleted?: boolean): Promise<IAlertDocument | null>;
41
+ /**
42
+ * Get all alerts with business logic filtering
43
+ */
44
+ getAlerts(filters?: {
45
+ propertyId?: string;
46
+ category?: AlertCategory;
47
+ severity?: AlertSeverity;
48
+ entityType?: EntityType;
49
+ entityId?: string;
50
+ isActive?: boolean;
51
+ isRead?: boolean;
52
+ includeDeleted?: boolean;
53
+ limit?: number;
54
+ skip?: number;
55
+ }): Promise<IAlertDocument[]>;
56
+ /**
57
+ * Update an alert with business logic validation
58
+ */
59
+ updateAlert(id: string, updateData: UpdateAlertData): Promise<IAlertDocument | null>;
60
+ /**
61
+ * Soft delete an alert with business logic
62
+ */
63
+ deleteAlert(id: string, deletedBy: string): Promise<boolean>;
64
+ /**
65
+ * Mark alert as read with business logic
66
+ */
67
+ markAsRead(id: string, updatedBy: string): Promise<IAlertDocument | null>;
68
+ /**
69
+ * Mark alert as unread with business logic
70
+ */
71
+ markAsUnread(id: string, updatedBy: string): Promise<IAlertDocument | null>;
72
+ /**
73
+ * Activate an alert with business logic
74
+ */
75
+ activateAlert(id: string, updatedBy: string): Promise<IAlertDocument | null>;
76
+ /**
77
+ * Deactivate an alert with business logic
78
+ */
79
+ deactivateAlert(id: string, updatedBy: string): Promise<IAlertDocument | null>;
80
+ /**
81
+ * Snooze an alert with business logic
82
+ */
83
+ snoozeAlert(id: string, until: Date, updatedBy: string): Promise<IAlertDocument | null>;
84
+ /**
85
+ * Unsnooze an alert with business logic
86
+ */
87
+ unsnoozeAlert(id: string, updatedBy: string): Promise<IAlertDocument | null>;
88
+ /**
89
+ * Get alerts by property with business logic
90
+ */
91
+ getAlertsByProperty(propertyId: string, includeDeleted?: boolean): Promise<IAlertDocument[]>;
92
+ /**
93
+ * Get alerts by entity with business logic
94
+ */
95
+ getAlertsByEntity(entityId: string, entityType: EntityType, includeDeleted?: boolean): Promise<IAlertDocument[]>;
96
+ /**
97
+ * Get alerts by category with business logic
98
+ */
99
+ getAlertsByCategory(category: AlertCategory, includeDeleted?: boolean): Promise<IAlertDocument[]>;
100
+ /**
101
+ * Get alerts by severity with business logic
102
+ */
103
+ getAlertsBySeverity(severity: AlertSeverity, includeDeleted?: boolean): Promise<IAlertDocument[]>;
104
+ /**
105
+ * Get active alerts with business logic
106
+ */
107
+ getActiveAlerts(includeDeleted?: boolean): Promise<IAlertDocument[]>;
108
+ /**
109
+ * Get unread alerts with business logic
110
+ */
111
+ getUnreadAlerts(includeDeleted?: boolean): Promise<IAlertDocument[]>;
112
+ /**
113
+ * Get snoozed alerts with business logic
114
+ */
115
+ getSnoozedAlerts(includeDeleted?: boolean): Promise<IAlertDocument[]>;
116
+ /**
117
+ * Get expired snooze alerts with business logic
118
+ */
119
+ getExpiredSnoozeAlerts(includeDeleted?: boolean): Promise<IAlertDocument[]>;
120
+ /**
121
+ * Get alert statistics with business logic
122
+ */
123
+ getAlertStatistics(propertyId?: string): Promise<{
124
+ total: number;
125
+ active: number;
126
+ unread: number;
127
+ snoozed: number;
128
+ bySeverity: Record<AlertSeverity, number>;
129
+ byCategory: Record<AlertCategory, number>;
130
+ }>;
131
+ private validateAlertData;
132
+ private validateFilters;
133
+ private validateUpdateData;
134
+ private validateSnoozeDate;
135
+ private determineDefaultSeverity;
136
+ private applyBusinessRules;
137
+ }
@@ -0,0 +1,476 @@
1
+ "use strict";
2
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
3
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
4
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
5
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
6
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
7
+ var _, done = false;
8
+ for (var i = decorators.length - 1; i >= 0; i--) {
9
+ var context = {};
10
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
11
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
12
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
13
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
14
+ if (kind === "accessor") {
15
+ if (result === void 0) continue;
16
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
17
+ if (_ = accept(result.get)) descriptor.get = _;
18
+ if (_ = accept(result.set)) descriptor.set = _;
19
+ if (_ = accept(result.init)) initializers.unshift(_);
20
+ }
21
+ else if (_ = accept(result)) {
22
+ if (kind === "field") initializers.unshift(_);
23
+ else descriptor[key] = _;
24
+ }
25
+ }
26
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
27
+ done = true;
28
+ };
29
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
30
+ var useValue = arguments.length > 2;
31
+ for (var i = 0; i < initializers.length; i++) {
32
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
33
+ }
34
+ return useValue ? value : void 0;
35
+ };
36
+ var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
37
+ if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
38
+ return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
39
+ };
40
+ Object.defineProperty(exports, "__esModule", { value: true });
41
+ exports.AlertService = void 0;
42
+ const typedi_1 = require("typedi");
43
+ const Alert_model_1 = require("./Alert.model");
44
+ const alert_types_1 = require("./alert.types");
45
+ const AlertBuilder_1 = require("./AlertBuilder");
46
+ let AlertService = (() => {
47
+ let _classDecorators = [(0, typedi_1.Service)()];
48
+ let _classDescriptor;
49
+ let _classExtraInitializers = [];
50
+ let _classThis;
51
+ var AlertService = _classThis = class {
52
+ constructor(alertRepository) {
53
+ this.alertRepository = alertRepository;
54
+ }
55
+ /**
56
+ * Create a readiness alert using AlertBuilder
57
+ */
58
+ async raiseReadinessAlert(propertyId, title, description, entityId, entityType, createdBy) {
59
+ const alertBuilder = AlertBuilder_1.AlertBuilder.createReadinessAlert()
60
+ .setPropertyId(propertyId)
61
+ .setTitle(title)
62
+ .setDescription(description);
63
+ if (entityId)
64
+ alertBuilder.setEntityId(entityId);
65
+ if (entityType)
66
+ alertBuilder.setEntityType(entityType);
67
+ if (createdBy)
68
+ alertBuilder.setCreatedBy(createdBy);
69
+ return await this.createAlert(alertBuilder);
70
+ }
71
+ /**
72
+ * Create an operations alert using AlertBuilder
73
+ */
74
+ async raiseOperationsAlert(propertyId, title, description, entityId, entityType, createdBy) {
75
+ const alertBuilder = AlertBuilder_1.AlertBuilder.createOperationsAlert()
76
+ .setPropertyId(propertyId)
77
+ .setTitle(title)
78
+ .setDescription(description);
79
+ if (entityId)
80
+ alertBuilder.setEntityId(entityId);
81
+ if (entityType)
82
+ alertBuilder.setEntityType(entityType);
83
+ if (createdBy)
84
+ alertBuilder.setCreatedBy(createdBy);
85
+ return await this.createAlert(alertBuilder);
86
+ }
87
+ /**
88
+ * Create a security alert using AlertBuilder
89
+ */
90
+ async raiseSecurityAlert(propertyId, title, description, entityId, entityType, createdBy) {
91
+ const alertBuilder = AlertBuilder_1.AlertBuilder.createSecurityAlert()
92
+ .setPropertyId(propertyId)
93
+ .setTitle(title)
94
+ .setDescription(description);
95
+ if (entityId)
96
+ alertBuilder.setEntityId(entityId);
97
+ if (entityType)
98
+ alertBuilder.setEntityType(entityType);
99
+ if (createdBy)
100
+ alertBuilder.setCreatedBy(createdBy);
101
+ return await this.createAlert(alertBuilder);
102
+ }
103
+ /**
104
+ * Create an energy alert using AlertBuilder
105
+ */
106
+ async raiseEnergyAlert(propertyId, title, description, entityId, entityType, createdBy) {
107
+ const alertBuilder = AlertBuilder_1.AlertBuilder.createEnergyAlert()
108
+ .setPropertyId(propertyId)
109
+ .setTitle(title)
110
+ .setDescription(description);
111
+ if (entityId)
112
+ alertBuilder.setEntityId(entityId);
113
+ if (entityType)
114
+ alertBuilder.setEntityType(entityType);
115
+ if (createdBy)
116
+ alertBuilder.setCreatedBy(createdBy);
117
+ return await this.createAlert(alertBuilder);
118
+ }
119
+ /**
120
+ * Create a device-specific alert using AlertBuilder
121
+ */
122
+ async raiseDeviceAlert(deviceId, propertyId, title, description, category, severity, createdBy) {
123
+ const alertBuilder = AlertBuilder_1.AlertBuilder.createDeviceAlert(deviceId, propertyId)
124
+ .setTitle(title)
125
+ .setDescription(description);
126
+ if (category)
127
+ alertBuilder.setCategory(category);
128
+ if (severity)
129
+ alertBuilder.setSeverity(severity);
130
+ if (createdBy)
131
+ alertBuilder.setCreatedBy(createdBy);
132
+ return await this.createAlert(alertBuilder);
133
+ }
134
+ /**
135
+ * Create a hub-specific alert using AlertBuilder
136
+ */
137
+ async raiseHubAlert(hubId, propertyId, title, description, category, severity, createdBy) {
138
+ const alertBuilder = AlertBuilder_1.AlertBuilder.createHubAlert(hubId, propertyId)
139
+ .setTitle(title)
140
+ .setDescription(description);
141
+ if (category)
142
+ alertBuilder.setCategory(category);
143
+ if (severity)
144
+ alertBuilder.setSeverity(severity);
145
+ if (createdBy)
146
+ alertBuilder.setCreatedBy(createdBy);
147
+ return await this.createAlert(alertBuilder);
148
+ }
149
+ /**
150
+ * Create a new alert with business logic validation
151
+ * Accepts either a CreateAlertData object or an AlertBuilder instance
152
+ */
153
+ async createAlert(alertData) {
154
+ let processedAlertData;
155
+ // Handle AlertBuilder instance
156
+ if (alertData instanceof AlertBuilder_1.AlertBuilder) {
157
+ processedAlertData = alertData.build();
158
+ }
159
+ else {
160
+ processedAlertData = alertData;
161
+ }
162
+ // Business logic: Validate alert data
163
+ this.validateAlertData(processedAlertData);
164
+ // Business logic: Set default severity if not provided
165
+ if (!processedAlertData.severity) {
166
+ processedAlertData.severity = this.determineDefaultSeverity(processedAlertData.category);
167
+ }
168
+ // Business logic: Validate snooze date is in the future
169
+ if (processedAlertData.snoozeUntil &&
170
+ processedAlertData.snoozeUntil <= new Date()) {
171
+ throw new Error("Snooze date must be in the future");
172
+ }
173
+ return await this.alertRepository.create(processedAlertData);
174
+ }
175
+ /**
176
+ * Get alert by ID with business logic
177
+ */
178
+ async getAlertById(id, includeDeleted = false) {
179
+ if (!id) {
180
+ throw new Error("Alert ID is required");
181
+ }
182
+ const alert = await this.alertRepository.findById(id, includeDeleted);
183
+ // Business logic: Check if alert is snoozed and expired
184
+ if (alert?.snoozeUntil && alert.snoozeUntil <= new Date()) {
185
+ console.warn(`Alert ${id} snooze has expired`);
186
+ }
187
+ return alert;
188
+ }
189
+ /**
190
+ * Get all alerts with business logic filtering
191
+ */
192
+ async getAlerts(filters = {}) {
193
+ // Business logic: Validate filters
194
+ this.validateFilters(filters);
195
+ // Business logic: Apply business rules to filters
196
+ const enhancedFilters = this.applyBusinessRules(filters);
197
+ return await this.alertRepository.findAll(enhancedFilters);
198
+ }
199
+ /**
200
+ * Update an alert with business logic validation
201
+ */
202
+ async updateAlert(id, updateData) {
203
+ if (!id) {
204
+ throw new Error("Alert ID is required");
205
+ }
206
+ // Business logic: Validate update data
207
+ this.validateUpdateData(updateData);
208
+ // Business logic: Check if alert exists and is not deleted
209
+ const existingAlert = await this.alertRepository.findById(id);
210
+ if (!existingAlert) {
211
+ throw new Error("Alert not found");
212
+ }
213
+ // Business logic: Handle snooze validation
214
+ if (updateData.snoozeUntil) {
215
+ this.validateSnoozeDate(updateData.snoozeUntil);
216
+ }
217
+ return await this.alertRepository.update(id, updateData);
218
+ }
219
+ /**
220
+ * Soft delete an alert with business logic
221
+ */
222
+ async deleteAlert(id, deletedBy) {
223
+ if (!id || !deletedBy) {
224
+ throw new Error("Alert ID and deleted by user are required");
225
+ }
226
+ // Business logic: Check if alert can be deleted
227
+ const alert = await this.alertRepository.findById(id);
228
+ if (!alert) {
229
+ throw new Error("Alert not found");
230
+ }
231
+ // Business logic: Prevent deletion of critical alerts (optional rule)
232
+ if (alert.severity === alert_types_1.AlertSeverity.CRITICAL) {
233
+ throw new Error("Cannot delete critical alerts");
234
+ }
235
+ return await this.alertRepository.softDelete(id, deletedBy);
236
+ }
237
+ /**
238
+ * Mark alert as read with business logic
239
+ */
240
+ async markAsRead(id, updatedBy) {
241
+ if (!id || !updatedBy) {
242
+ throw new Error("Alert ID and updated by user are required");
243
+ }
244
+ const alertModel = await Alert_model_1.AlertModel.findById(id);
245
+ if (!alertModel)
246
+ return null;
247
+ alertModel.markAsRead(updatedBy);
248
+ return await alertModel.save();
249
+ }
250
+ /**
251
+ * Mark alert as unread with business logic
252
+ */
253
+ async markAsUnread(id, updatedBy) {
254
+ if (!id || !updatedBy) {
255
+ throw new Error("Alert ID and updated by user are required");
256
+ }
257
+ const alertModel = await Alert_model_1.AlertModel.findById(id);
258
+ if (!alertModel)
259
+ return null;
260
+ alertModel.markAsUnread(updatedBy);
261
+ return await alertModel.save();
262
+ }
263
+ /**
264
+ * Activate an alert with business logic
265
+ */
266
+ async activateAlert(id, updatedBy) {
267
+ if (!id || !updatedBy) {
268
+ throw new Error("Alert ID and updated by user are required");
269
+ }
270
+ const alertModel = await Alert_model_1.AlertModel.findById(id);
271
+ if (!alertModel)
272
+ return null;
273
+ alertModel.activate(updatedBy);
274
+ return await alertModel.save();
275
+ }
276
+ /**
277
+ * Deactivate an alert with business logic
278
+ */
279
+ async deactivateAlert(id, updatedBy) {
280
+ if (!id || !updatedBy) {
281
+ throw new Error("Alert ID and updated by user are required");
282
+ }
283
+ const alertModel = await Alert_model_1.AlertModel.findById(id);
284
+ if (!alertModel)
285
+ return null;
286
+ alertModel.deactivate(updatedBy);
287
+ return await alertModel.save();
288
+ }
289
+ /**
290
+ * Snooze an alert with business logic
291
+ */
292
+ async snoozeAlert(id, until, updatedBy) {
293
+ if (!id || !until || !updatedBy) {
294
+ throw new Error("Alert ID, snooze date, and updated by user are required");
295
+ }
296
+ // Business logic: Validate snooze date
297
+ this.validateSnoozeDate(until);
298
+ const alertModel = await Alert_model_1.AlertModel.findById(id);
299
+ if (!alertModel)
300
+ return null;
301
+ alertModel.snooze(until, updatedBy);
302
+ return await alertModel.save();
303
+ }
304
+ /**
305
+ * Unsnooze an alert with business logic
306
+ */
307
+ async unsnoozeAlert(id, updatedBy) {
308
+ if (!id || !updatedBy) {
309
+ throw new Error("Alert ID and updated by user are required");
310
+ }
311
+ const alertModel = await Alert_model_1.AlertModel.findById(id);
312
+ if (!alertModel)
313
+ return null;
314
+ alertModel.unsnooze(updatedBy);
315
+ return await alertModel.save();
316
+ }
317
+ /**
318
+ * Get alerts by property with business logic
319
+ */
320
+ async getAlertsByProperty(propertyId, includeDeleted = false) {
321
+ if (!propertyId) {
322
+ throw new Error("Property ID is required");
323
+ }
324
+ return await this.alertRepository.findByProperty(propertyId, includeDeleted);
325
+ }
326
+ /**
327
+ * Get alerts by entity with business logic
328
+ */
329
+ async getAlertsByEntity(entityId, entityType, includeDeleted = false) {
330
+ if (!entityId || !entityType) {
331
+ throw new Error("Entity ID and entity type are required");
332
+ }
333
+ return await this.alertRepository.findByEntity(entityId, entityType, includeDeleted);
334
+ }
335
+ /**
336
+ * Get alerts by category with business logic
337
+ */
338
+ async getAlertsByCategory(category, includeDeleted = false) {
339
+ if (!category) {
340
+ throw new Error("Alert category is required");
341
+ }
342
+ return await this.alertRepository.findByCategory(category, includeDeleted);
343
+ }
344
+ /**
345
+ * Get alerts by severity with business logic
346
+ */
347
+ async getAlertsBySeverity(severity, includeDeleted = false) {
348
+ if (!severity) {
349
+ throw new Error("Alert severity is required");
350
+ }
351
+ return await this.alertRepository.findBySeverity(severity, includeDeleted);
352
+ }
353
+ /**
354
+ * Get active alerts with business logic
355
+ */
356
+ async getActiveAlerts(includeDeleted = false) {
357
+ const activeAlerts = await this.alertRepository.findActive(includeDeleted);
358
+ // Business logic: Log active alerts for monitoring
359
+ if (activeAlerts.length > 0) {
360
+ console.log(`Found ${activeAlerts.length} active alerts`);
361
+ }
362
+ return activeAlerts;
363
+ }
364
+ /**
365
+ * Get unread alerts with business logic
366
+ */
367
+ async getUnreadAlerts(includeDeleted = false) {
368
+ const unreadAlerts = await this.alertRepository.findUnread(includeDeleted);
369
+ // Business logic: Log unread alerts for monitoring
370
+ if (unreadAlerts.length > 0) {
371
+ console.warn(`Found ${unreadAlerts.length} unread alerts`);
372
+ }
373
+ return unreadAlerts;
374
+ }
375
+ /**
376
+ * Get snoozed alerts with business logic
377
+ */
378
+ async getSnoozedAlerts(includeDeleted = false) {
379
+ return await this.alertRepository.findSnoozed(includeDeleted);
380
+ }
381
+ /**
382
+ * Get expired snooze alerts with business logic
383
+ */
384
+ async getExpiredSnoozeAlerts(includeDeleted = false) {
385
+ const expiredAlerts = await this.alertRepository.findExpiredSnooze(includeDeleted);
386
+ // Business logic: Log expired snooze alerts
387
+ if (expiredAlerts.length > 0) {
388
+ console.warn(`Found ${expiredAlerts.length} alerts with expired snooze`);
389
+ }
390
+ return expiredAlerts;
391
+ }
392
+ /**
393
+ * Get alert statistics with business logic
394
+ */
395
+ async getAlertStatistics(propertyId) {
396
+ const stats = await this.alertRepository.getStatistics(propertyId);
397
+ // Business logic: Add alerts for critical metrics
398
+ if (stats.unread > 0) {
399
+ console.warn(`Alert: ${stats.unread} unread alerts require attention`);
400
+ }
401
+ if (stats.bySeverity[alert_types_1.AlertSeverity.CRITICAL] > 0) {
402
+ console.error(`Alert: ${stats.bySeverity[alert_types_1.AlertSeverity.CRITICAL]} critical alerts require immediate attention`);
403
+ }
404
+ if (stats.bySeverity[alert_types_1.AlertSeverity.HIGH] > 0) {
405
+ console.warn(`Alert: ${stats.bySeverity[alert_types_1.AlertSeverity.HIGH]} high severity alerts require attention`);
406
+ }
407
+ return stats;
408
+ }
409
+ // Private business logic methods
410
+ validateAlertData(data) {
411
+ if (!data.title || data.title.trim().length < 3) {
412
+ throw new Error("Alert title must be at least 3 characters long");
413
+ }
414
+ if (!data.description || data.description.trim().length < 5) {
415
+ throw new Error("Alert description must be at least 5 characters long");
416
+ }
417
+ if (!data.propertyId) {
418
+ throw new Error("Property ID is required");
419
+ }
420
+ if (!data.entityType) {
421
+ throw new Error("Entity type is required");
422
+ }
423
+ }
424
+ validateFilters(filters) {
425
+ if (filters.limit && (filters.limit < 1 || filters.limit > 100)) {
426
+ throw new Error("Limit must be between 1 and 100");
427
+ }
428
+ if (filters.skip && filters.skip < 0) {
429
+ throw new Error("Skip must be non-negative");
430
+ }
431
+ }
432
+ validateUpdateData(data) {
433
+ if (data.title && data.title.trim().length < 3) {
434
+ throw new Error("Alert title must be at least 3 characters long");
435
+ }
436
+ if (data.description && data.description.trim().length < 5) {
437
+ throw new Error("Alert description must be at least 5 characters long");
438
+ }
439
+ }
440
+ validateSnoozeDate(snoozeUntil) {
441
+ if (snoozeUntil <= new Date()) {
442
+ throw new Error("Snooze date must be in the future");
443
+ }
444
+ }
445
+ determineDefaultSeverity(category) {
446
+ // Business logic: Determine default severity based on category
447
+ const categorySeverities = {
448
+ [alert_types_1.AlertCategory.READINESS]: alert_types_1.AlertSeverity.MEDIUM,
449
+ [alert_types_1.AlertCategory.OPERATIONS]: alert_types_1.AlertSeverity.HIGH,
450
+ [alert_types_1.AlertCategory.SECURITY]: alert_types_1.AlertSeverity.CRITICAL,
451
+ [alert_types_1.AlertCategory.ENERGY]: alert_types_1.AlertSeverity.LOW,
452
+ [alert_types_1.AlertCategory.OTHER]: alert_types_1.AlertSeverity.MEDIUM,
453
+ };
454
+ return categorySeverities[category] || alert_types_1.AlertSeverity.MEDIUM;
455
+ }
456
+ applyBusinessRules(filters) {
457
+ // Business logic: Apply additional filters based on business rules
458
+ const enhancedFilters = { ...filters };
459
+ // Example: Always exclude deleted alerts unless explicitly requested
460
+ if (!enhancedFilters.includeDeleted) {
461
+ enhancedFilters.includeDeleted = false;
462
+ }
463
+ return enhancedFilters;
464
+ }
465
+ };
466
+ __setFunctionName(_classThis, "AlertService");
467
+ (() => {
468
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
469
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
470
+ AlertService = _classThis = _classDescriptor.value;
471
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
472
+ __runInitializers(_classThis, _classExtraInitializers);
473
+ })();
474
+ return AlertService = _classThis;
475
+ })();
476
+ exports.AlertService = AlertService;