dt-common-device 2.0.7 → 3.0.1

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 (211) hide show
  1. package/README.md +352 -98
  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/interface.d.ts +101 -0
  35. package/dist/device/cloud/interface.js +3 -0
  36. package/dist/device/cloud/interfaces/ICloudDeviceService.d.ts +1 -1
  37. package/dist/device/cloud/interfaces/IDeviceConnectionService.d.ts +7 -0
  38. package/dist/device/cloud/interfaces/IDeviceConnectionService.js +3 -0
  39. package/dist/device/cloud/interfaces/IDevicesService.d.ts +9 -0
  40. package/dist/device/cloud/interfaces/IDevicesService.js +2 -0
  41. package/dist/device/cloud/interfaces/IRawDevice.d.ts +1 -1
  42. package/dist/device/cloud/services/Device.service.d.ts +39 -0
  43. package/dist/device/cloud/services/Device.service.js +9 -0
  44. package/dist/device/cloud/services/DeviceCloudService.d.ts +42 -0
  45. package/dist/device/cloud/services/DeviceCloudService.js +59 -0
  46. package/dist/device/cloud/services/DeviceHub.service.d.ts +3 -0
  47. package/dist/device/cloud/services/DeviceHub.service.js +6 -0
  48. package/dist/device/cloud/services/Hub.service.d.ts +25 -0
  49. package/dist/device/cloud/services/Hub.service.js +9 -0
  50. package/dist/device/cloud/services/SmartThingsDeviceService.d.ts +38 -0
  51. package/dist/device/cloud/services/SmartThingsDeviceService.js +52 -0
  52. package/dist/device/index.d.ts +4 -0
  53. package/dist/device/index.js +20 -0
  54. package/dist/device/local/events/EventHandler.js +6 -6
  55. package/dist/device/local/events/Events.d.ts +12 -33
  56. package/dist/device/local/events/Events.js +12 -33
  57. package/dist/device/local/interface.d.ts +0 -0
  58. package/dist/device/local/interface.js +1 -0
  59. package/dist/device/local/interfaces/index.d.ts +2 -3
  60. package/dist/device/local/interfaces/index.js +2 -3
  61. package/dist/device/local/repository/Device.repository.js +3 -3
  62. package/dist/device/local/repository/Hub.repository.js +4 -4
  63. package/dist/device/local/repository/Schedule.repository.js +2 -2
  64. package/dist/device/local/services/Device.service.d.ts +2 -2
  65. package/dist/device/local/services/Device.service.js +1 -1
  66. package/dist/device/local/services/DeviceHub.service.d.ts +11 -0
  67. package/dist/device/local/services/DeviceHub.service.js +40 -0
  68. package/dist/device/local/services/index.d.ts +0 -4
  69. package/dist/device/local/services/index.js +0 -4
  70. package/dist/events/BaseEventHandler.d.ts +2 -2
  71. package/dist/events/BaseEventHandler.js +2 -2
  72. package/dist/events/BaseEventTransformer.d.ts +1 -1
  73. package/dist/events/BaseEventTransformer.js +1 -1
  74. package/dist/events/DeviceEventHandler.d.ts +1 -1
  75. package/dist/events/DeviceEventHandler.js +2 -2
  76. package/dist/events/EventHandler.js +1 -1
  77. package/dist/events/EventHandlerOrchestrator.js +1 -1
  78. package/dist/events/EventProcessingService.js +1 -1
  79. package/dist/events/InternalEventSubscription.js +1 -1
  80. package/dist/index.d.ts +7 -5
  81. package/dist/index.js +16 -13
  82. package/dist/issues/Issue.model.d.ts +28 -0
  83. package/dist/issues/Issue.model.js +260 -0
  84. package/dist/issues/Issue.repository.d.ts +113 -0
  85. package/dist/issues/Issue.repository.js +401 -0
  86. package/dist/issues/Issue.service.d.ts +168 -0
  87. package/dist/issues/Issue.service.js +642 -0
  88. package/dist/issues/IssueBuilder.d.ts +109 -0
  89. package/dist/issues/IssueBuilder.example.d.ts +16 -0
  90. package/dist/issues/IssueBuilder.example.js +196 -0
  91. package/dist/issues/IssueBuilder.js +237 -0
  92. package/dist/issues/IssueService.example.d.ts +68 -0
  93. package/dist/issues/IssueService.example.js +177 -0
  94. package/dist/issues/index.d.ts +2 -0
  95. package/dist/issues/index.js +18 -0
  96. package/dist/issues/issue.types.d.ts +90 -0
  97. package/dist/issues/issue.types.js +40 -0
  98. package/dist/property/IProperty.d.ts +29 -0
  99. package/dist/property/IProperty.js +2 -0
  100. package/dist/property/Property.repository.d.ts +8 -0
  101. package/dist/property/Property.repository.js +95 -0
  102. package/dist/property/Property.service.d.ts +8 -0
  103. package/dist/property/Property.service.js +36 -0
  104. package/dist/property/index.d.ts +2 -0
  105. package/dist/property/index.js +18 -0
  106. package/dist/queue/entities/HybridHttpQueue.d.ts +24 -0
  107. package/dist/queue/entities/HybridHttpQueue.js +241 -0
  108. package/dist/queue/entities/index.d.ts +1 -0
  109. package/dist/queue/entities/index.js +17 -0
  110. package/dist/queue/index.d.ts +5 -0
  111. package/dist/queue/index.js +22 -0
  112. package/dist/queue/interfaces/IHttpRequestJob.d.ts +9 -0
  113. package/dist/queue/interfaces/IHttpRequestJob.js +2 -0
  114. package/dist/queue/interfaces/IHybridHttpQueue.d.ts +17 -0
  115. package/dist/queue/interfaces/IHybridHttpQueue.js +2 -0
  116. package/dist/queue/interfaces/IJobResult.d.ts +14 -0
  117. package/dist/queue/interfaces/IJobResult.js +2 -0
  118. package/dist/queue/interfaces/IRateLimitConfig.d.ts +5 -0
  119. package/dist/queue/interfaces/IRateLimitConfig.js +2 -0
  120. package/dist/queue/interfaces/index.d.ts +4 -0
  121. package/dist/queue/interfaces/index.js +20 -0
  122. package/dist/queue/services/QueueService.d.ts +19 -0
  123. package/dist/queue/services/QueueService.js +73 -0
  124. package/dist/queue/services/index.d.ts +1 -0
  125. package/dist/queue/services/index.js +17 -0
  126. package/dist/queue/types/http.types.d.ts +21 -0
  127. package/dist/queue/types/http.types.js +2 -0
  128. package/dist/queue/types/index.d.ts +2 -0
  129. package/dist/queue/types/index.js +18 -0
  130. package/dist/queue/types/queue.types.d.ts +31 -0
  131. package/dist/queue/types/queue.types.js +2 -0
  132. package/dist/queue/utils/index.d.ts +3 -0
  133. package/dist/queue/utils/index.js +19 -0
  134. package/dist/queue/utils/jobUtils.d.ts +10 -0
  135. package/dist/queue/utils/jobUtils.js +64 -0
  136. package/dist/queue/utils/queueUtils.d.ts +5 -0
  137. package/dist/queue/utils/queueUtils.js +60 -0
  138. package/dist/queue/utils/rateLimit.utils.d.ts +10 -0
  139. package/dist/queue/utils/rateLimit.utils.js +97 -0
  140. package/package.json +2 -1
  141. package/src/{device/local/models → alerts}/Alert.model.ts +1 -1
  142. package/src/{device/local/repository → alerts}/Alert.repository.ts +2 -2
  143. package/src/{device/local/services → alerts}/Alert.service.ts +14 -7
  144. package/src/{device/local/entities → alerts}/AlertBuilder.example.ts +2 -2
  145. package/src/{device/local/entities → alerts}/AlertBuilder.ts +14 -8
  146. package/src/{device/local/services → alerts}/AlertService.example.ts +6 -5
  147. package/src/{types → alerts}/alert.types.ts +2 -2
  148. package/src/alerts/index.ts +3 -0
  149. package/src/config/config.ts +7 -7
  150. package/src/{types → config}/config.types.ts +1 -1
  151. package/src/{device/local/repository → connection}/Connection.repository.ts +2 -2
  152. package/src/{device/local/services → connection}/Connection.service.ts +2 -2
  153. package/src/connection/index.ts +3 -0
  154. package/src/device/cloud/entities/CloudDevice.ts +2 -2
  155. package/src/device/cloud/entities/CloudDeviceService.ts +1 -1
  156. package/src/device/cloud/entities/DeviceFactory.ts +2 -2
  157. package/src/device/cloud/interfaces/ICloudDeviceService.ts +1 -1
  158. package/src/device/cloud/interfaces/IRawDevice.ts +1 -1
  159. package/src/device/local/interfaces/index.ts +2 -3
  160. package/src/device/local/repository/Device.repository.ts +3 -3
  161. package/src/device/local/repository/Hub.repository.ts +4 -4
  162. package/src/device/local/repository/Schedule.repository.ts +2 -2
  163. package/src/device/local/services/Device.service.ts +1 -1
  164. package/src/device/local/services/index.ts +0 -4
  165. package/{TROUBLESHOOTING.md → src/docs/TROUBLESHOOTING.md} +2 -2
  166. package/src/events/BaseEventHandler.ts +3 -3
  167. package/src/events/BaseEventTransformer.ts +2 -2
  168. package/src/events/DeviceEventHandler.ts +3 -3
  169. package/src/events/EventHandler.ts +1 -1
  170. package/src/events/EventHandlerOrchestrator.ts +2 -2
  171. package/src/events/EventProcessingService.ts +2 -2
  172. package/src/events/InternalEventSubscription.ts +2 -2
  173. package/src/index.ts +19 -13
  174. package/src/{device/local/models → issues}/Issue.model.ts +1 -1
  175. package/src/{device/local/repository → issues}/Issue.repository.ts +2 -2
  176. package/src/{device/local/services → issues}/Issue.service.ts +4 -4
  177. package/src/{device/local/entities → issues}/IssueBuilder.example.ts +1 -1
  178. package/src/{device/local/entities → issues}/IssueBuilder.ts +1 -1
  179. package/src/{device/local/services → issues}/IssueService.example.ts +6 -5
  180. package/src/issues/index.ts +2 -0
  181. package/src/{device/local/repository → property}/Property.repository.ts +2 -2
  182. package/src/{device/local/services → property}/Property.service.ts +1 -1
  183. package/src/property/index.ts +2 -0
  184. package/src/queue/entities/HybridHttpQueue.ts +272 -0
  185. package/src/queue/entities/index.ts +1 -0
  186. package/src/queue/index.ts +6 -0
  187. package/src/queue/interfaces/IHttpRequestJob.ts +10 -0
  188. package/src/queue/interfaces/IHybridHttpQueue.ts +24 -0
  189. package/src/queue/interfaces/IJobResult.ts +15 -0
  190. package/src/queue/interfaces/IRateLimitConfig.ts +5 -0
  191. package/src/queue/interfaces/index.ts +4 -0
  192. package/src/queue/services/QueueService.ts +39 -0
  193. package/src/queue/services/index.ts +1 -0
  194. package/src/queue/types/http.types.ts +22 -0
  195. package/src/queue/types/index.ts +2 -0
  196. package/src/queue/types/queue.types.ts +21 -0
  197. package/src/queue/utils/index.ts +3 -0
  198. package/src/queue/utils/jobUtils.ts +80 -0
  199. package/src/queue/utils/queueUtils.ts +91 -0
  200. package/src/queue/utils/rateLimit.utils.ts +131 -0
  201. package/tsconfig.json +4 -0
  202. package/src/device/local/entities/README.md +0 -173
  203. package/src/device/local/entities/index.ts +0 -2
  204. package/src/types/index.ts +0 -3
  205. /package/src/{device/local/interfaces → connection}/IConnection.ts +0 -0
  206. /package/src/{device/local/models → docs}/Alert.model.md +0 -0
  207. /package/src/{device/local/models/README.md → docs/Alerts&IssuesModel.md} +0 -0
  208. /package/src/{device/local/models → docs}/Issue.model.md +0 -0
  209. /package/{SECURITY.md → src/docs/SECURITY.md} +0 -0
  210. /package/src/{types → issues}/issue.types.ts +0 -0
  211. /package/src/{device/local/interfaces → property}/IProperty.ts +0 -0
@@ -0,0 +1,222 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.AlertSchema = exports.AlertModel = void 0;
37
+ const mongoose_1 = __importStar(require("mongoose"));
38
+ const alert_types_1 = require("./alert.types");
39
+ // Main Alert schema
40
+ const AlertSchema = new mongoose_1.Schema({
41
+ category: {
42
+ type: String,
43
+ enum: Object.values(alert_types_1.AlertCategory),
44
+ required: true,
45
+ },
46
+ propertyId: {
47
+ type: String,
48
+ required: true,
49
+ index: true,
50
+ },
51
+ title: {
52
+ type: String,
53
+ required: true,
54
+ trim: true,
55
+ },
56
+ description: {
57
+ type: String,
58
+ required: true,
59
+ trim: true,
60
+ },
61
+ entityId: {
62
+ type: String,
63
+ index: true,
64
+ },
65
+ entityType: {
66
+ type: String,
67
+ enum: Object.values(alert_types_1.EntityType),
68
+ required: true,
69
+ index: true,
70
+ },
71
+ severity: {
72
+ type: String,
73
+ enum: Object.values(alert_types_1.AlertSeverity),
74
+ default: alert_types_1.AlertSeverity.MEDIUM,
75
+ },
76
+ isRead: {
77
+ type: Boolean,
78
+ default: false,
79
+ index: true,
80
+ },
81
+ isActive: {
82
+ type: Boolean,
83
+ default: true,
84
+ index: true,
85
+ },
86
+ isDeleted: {
87
+ type: Boolean,
88
+ default: false,
89
+ },
90
+ snoozeUntil: {
91
+ type: Date,
92
+ },
93
+ createdBy: {
94
+ type: String,
95
+ },
96
+ updatedBy: {
97
+ type: String,
98
+ },
99
+ createdAt: {
100
+ type: Date,
101
+ default: Date.now,
102
+ },
103
+ updatedAt: {
104
+ type: Date,
105
+ default: Date.now,
106
+ },
107
+ }, {
108
+ timestamps: true,
109
+ collection: "dt_alerts",
110
+ });
111
+ exports.AlertSchema = AlertSchema;
112
+ // Compound indexes to match Prisma schema
113
+ AlertSchema.index({ propertyId: 1, isActive: 1, isRead: 1 });
114
+ AlertSchema.index({ entityId: 1, entityType: 1 });
115
+ AlertSchema.index({ createdAt: 1 });
116
+ // Pre-save middleware to update the updatedAt field
117
+ AlertSchema.pre("save", function (next) {
118
+ this.updatedAt = new Date();
119
+ next();
120
+ });
121
+ // Pre-update middleware to update the updatedAt field
122
+ AlertSchema.pre(["updateOne", "findOneAndUpdate", "updateMany"], function (next) {
123
+ this.set({ updatedAt: new Date() });
124
+ next();
125
+ });
126
+ // Instance methods
127
+ AlertSchema.methods.markAsRead = function (updatedBy) {
128
+ this.isRead = true;
129
+ this.updatedBy = updatedBy;
130
+ };
131
+ AlertSchema.methods.markAsUnread = function (updatedBy) {
132
+ this.isRead = false;
133
+ this.updatedBy = updatedBy;
134
+ };
135
+ AlertSchema.methods.activate = function (updatedBy) {
136
+ this.isActive = true;
137
+ this.updatedBy = updatedBy;
138
+ };
139
+ AlertSchema.methods.deactivate = function (updatedBy) {
140
+ this.isActive = false;
141
+ this.updatedBy = updatedBy;
142
+ };
143
+ AlertSchema.methods.snooze = function (until, updatedBy) {
144
+ this.snoozeUntil = until;
145
+ this.updatedBy = updatedBy;
146
+ };
147
+ AlertSchema.methods.unsnooze = function (updatedBy) {
148
+ this.snoozeUntil = undefined;
149
+ this.updatedBy = updatedBy;
150
+ };
151
+ // Static methods
152
+ AlertSchema.statics.findByProperty = function (propertyId, includeDeleted = false) {
153
+ const query = { propertyId };
154
+ if (!includeDeleted) {
155
+ query.isDeleted = false;
156
+ }
157
+ return this.find(query).sort({ createdAt: -1 });
158
+ };
159
+ AlertSchema.statics.findByEntity = function (entityId, entityType, includeDeleted = false) {
160
+ const query = { entityId, entityType };
161
+ if (!includeDeleted) {
162
+ query.isDeleted = false;
163
+ }
164
+ return this.find(query).sort({ createdAt: -1 });
165
+ };
166
+ AlertSchema.statics.findByCategory = function (category, includeDeleted = false) {
167
+ const query = { category };
168
+ if (!includeDeleted) {
169
+ query.isDeleted = false;
170
+ }
171
+ return this.find(query).sort({ createdAt: -1 });
172
+ };
173
+ AlertSchema.statics.findBySeverity = function (severity, includeDeleted = false) {
174
+ const query = { severity };
175
+ if (!includeDeleted) {
176
+ query.isDeleted = false;
177
+ }
178
+ return this.find(query).sort({ severity: -1, createdAt: -1 });
179
+ };
180
+ AlertSchema.statics.findActive = function (includeDeleted = false) {
181
+ const query = { isActive: true };
182
+ if (!includeDeleted) {
183
+ query.isDeleted = false;
184
+ }
185
+ return this.find(query).sort({ severity: -1, createdAt: -1 });
186
+ };
187
+ AlertSchema.statics.findUnread = function (includeDeleted = false) {
188
+ const query = { isRead: false };
189
+ if (!includeDeleted) {
190
+ query.isDeleted = false;
191
+ }
192
+ return this.find(query).sort({ severity: -1, createdAt: -1 });
193
+ };
194
+ AlertSchema.statics.findSnoozed = function (includeDeleted = false) {
195
+ const query = { snoozeUntil: { $exists: true, $ne: null } };
196
+ if (!includeDeleted) {
197
+ query.isDeleted = false;
198
+ }
199
+ return this.find(query).sort({ snoozeUntil: 1 });
200
+ };
201
+ AlertSchema.statics.findExpiredSnooze = function (includeDeleted = false) {
202
+ const query = {
203
+ snoozeUntil: { $lt: new Date() },
204
+ isActive: true,
205
+ };
206
+ if (!includeDeleted) {
207
+ query.isDeleted = false;
208
+ }
209
+ return this.find(query).sort({ snoozeUntil: 1 });
210
+ };
211
+ // Virtual for snooze status
212
+ AlertSchema.virtual("isSnoozed").get(function () {
213
+ return this.snoozeUntil && this.snoozeUntil > new Date();
214
+ });
215
+ AlertSchema.virtual("isSnoozeExpired").get(function () {
216
+ return this.snoozeUntil && this.snoozeUntil <= new Date();
217
+ });
218
+ // Ensure virtuals are serialized
219
+ AlertSchema.set("toJSON", { virtuals: true });
220
+ AlertSchema.set("toObject", { virtuals: true });
221
+ // Create and export the model
222
+ exports.AlertModel = mongoose_1.default.model("Alert", AlertSchema);
@@ -0,0 +1,106 @@
1
+ import { IAlertDocument } from "./Alert.model";
2
+ import { CreateAlertData, UpdateAlertData, AlertCategory, AlertSeverity, EntityType } from "./alert.types";
3
+ export declare class AlertRepository {
4
+ /**
5
+ * Create a new alert
6
+ */
7
+ create(alertData: CreateAlertData): Promise<IAlertDocument>;
8
+ /**
9
+ * Find alert by ID
10
+ */
11
+ findById(id: string, includeDeleted?: boolean): Promise<IAlertDocument | null>;
12
+ /**
13
+ * Find all alerts with filters
14
+ */
15
+ findAll(filters?: {
16
+ propertyId?: string;
17
+ category?: AlertCategory;
18
+ severity?: AlertSeverity;
19
+ entityType?: EntityType;
20
+ entityId?: string;
21
+ isActive?: boolean;
22
+ isRead?: boolean;
23
+ includeDeleted?: boolean;
24
+ limit?: number;
25
+ skip?: number;
26
+ sort?: {
27
+ [key: string]: 1 | -1;
28
+ };
29
+ }): Promise<IAlertDocument[]>;
30
+ /**
31
+ * Update an alert
32
+ */
33
+ update(id: string, updateData: UpdateAlertData): Promise<IAlertDocument | null>;
34
+ /**
35
+ * Soft delete an alert
36
+ */
37
+ softDelete(id: string, deletedBy: string): Promise<boolean>;
38
+ /**
39
+ * Permanently delete an alert
40
+ */
41
+ hardDelete(id: string): Promise<boolean>;
42
+ /**
43
+ * Count alerts with filters
44
+ */
45
+ count(filters?: {
46
+ propertyId?: string;
47
+ category?: AlertCategory;
48
+ severity?: AlertSeverity;
49
+ entityType?: EntityType;
50
+ entityId?: string;
51
+ isActive?: boolean;
52
+ isRead?: boolean;
53
+ includeDeleted?: boolean;
54
+ }): Promise<number>;
55
+ /**
56
+ * Find alerts by property
57
+ */
58
+ findByProperty(propertyId: string, includeDeleted?: boolean): Promise<IAlertDocument[]>;
59
+ /**
60
+ * Find alerts by entity
61
+ */
62
+ findByEntity(entityId: string, entityType: EntityType, includeDeleted?: boolean): Promise<IAlertDocument[]>;
63
+ /**
64
+ * Find alerts by category
65
+ */
66
+ findByCategory(category: AlertCategory, includeDeleted?: boolean): Promise<IAlertDocument[]>;
67
+ /**
68
+ * Find alerts by severity
69
+ */
70
+ findBySeverity(severity: AlertSeverity, includeDeleted?: boolean): Promise<IAlertDocument[]>;
71
+ /**
72
+ * Find active alerts
73
+ */
74
+ findActive(includeDeleted?: boolean): Promise<IAlertDocument[]>;
75
+ /**
76
+ * Find unread alerts
77
+ */
78
+ findUnread(includeDeleted?: boolean): Promise<IAlertDocument[]>;
79
+ /**
80
+ * Find snoozed alerts
81
+ */
82
+ findSnoozed(includeDeleted?: boolean): Promise<IAlertDocument[]>;
83
+ /**
84
+ * Find expired snooze alerts
85
+ */
86
+ findExpiredSnooze(includeDeleted?: boolean): Promise<IAlertDocument[]>;
87
+ /**
88
+ * Get alert statistics
89
+ */
90
+ getStatistics(propertyId?: string): Promise<{
91
+ total: number;
92
+ active: number;
93
+ unread: number;
94
+ snoozed: number;
95
+ bySeverity: Record<AlertSeverity, number>;
96
+ byCategory: Record<AlertCategory, number>;
97
+ }>;
98
+ /**
99
+ * Bulk update alerts
100
+ */
101
+ bulkUpdate(ids: string[], updateData: Partial<UpdateAlertData>): Promise<number>;
102
+ /**
103
+ * Bulk soft delete alerts
104
+ */
105
+ bulkSoftDelete(ids: string[], deletedBy: string): Promise<number>;
106
+ }
@@ -0,0 +1,374 @@
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.AlertRepository = 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
+ let AlertRepository = (() => {
46
+ let _classDecorators = [(0, typedi_1.Service)()];
47
+ let _classDescriptor;
48
+ let _classExtraInitializers = [];
49
+ let _classThis;
50
+ var AlertRepository = _classThis = class {
51
+ /**
52
+ * Create a new alert
53
+ */
54
+ async create(alertData) {
55
+ try {
56
+ const alert = new Alert_model_1.AlertModel({
57
+ ...alertData,
58
+ severity: alertData.severity || alert_types_1.AlertSeverity.MEDIUM,
59
+ isRead: false,
60
+ isActive: true,
61
+ isDeleted: false,
62
+ });
63
+ return await alert.save();
64
+ }
65
+ catch (error) {
66
+ throw new Error(`Failed to create alert: ${error instanceof Error ? error.message : "Unknown error"}`);
67
+ }
68
+ }
69
+ /**
70
+ * Find alert by ID
71
+ */
72
+ async findById(id, includeDeleted = false) {
73
+ try {
74
+ const query = { _id: id };
75
+ if (!includeDeleted) {
76
+ query.isDeleted = false;
77
+ }
78
+ return await Alert_model_1.AlertModel.findOne(query);
79
+ }
80
+ catch (error) {
81
+ throw new Error(`Failed to find alert by ID: ${error instanceof Error ? error.message : "Unknown error"}`);
82
+ }
83
+ }
84
+ /**
85
+ * Find all alerts with filters
86
+ */
87
+ async findAll(filters = {}) {
88
+ try {
89
+ const query = {};
90
+ if (filters.propertyId)
91
+ query.propertyId = filters.propertyId;
92
+ if (filters.category)
93
+ query.category = filters.category;
94
+ if (filters.severity)
95
+ query.severity = filters.severity;
96
+ if (filters.entityType)
97
+ query.entityType = filters.entityType;
98
+ if (filters.entityId)
99
+ query.entityId = filters.entityId;
100
+ if (filters.isActive !== undefined)
101
+ query.isActive = filters.isActive;
102
+ if (filters.isRead !== undefined)
103
+ query.isRead = filters.isRead;
104
+ if (!filters.includeDeleted)
105
+ query.isDeleted = false;
106
+ const queryBuilder = Alert_model_1.AlertModel.find(query);
107
+ if (filters.sort) {
108
+ queryBuilder.sort(filters.sort);
109
+ }
110
+ else {
111
+ queryBuilder.sort({ severity: -1, createdAt: -1 });
112
+ }
113
+ if (filters.skip)
114
+ queryBuilder.skip(filters.skip);
115
+ if (filters.limit)
116
+ queryBuilder.limit(filters.limit);
117
+ return await queryBuilder.exec();
118
+ }
119
+ catch (error) {
120
+ throw new Error(`Failed to find alerts: ${error instanceof Error ? error.message : "Unknown error"}`);
121
+ }
122
+ }
123
+ /**
124
+ * Update an alert
125
+ */
126
+ async update(id, updateData) {
127
+ try {
128
+ return await Alert_model_1.AlertModel.findByIdAndUpdate(id, { ...updateData, updatedAt: new Date() }, { new: true, runValidators: true });
129
+ }
130
+ catch (error) {
131
+ throw new Error(`Failed to update alert: ${error instanceof Error ? error.message : "Unknown error"}`);
132
+ }
133
+ }
134
+ /**
135
+ * Soft delete an alert
136
+ */
137
+ async softDelete(id, deletedBy) {
138
+ try {
139
+ const result = await Alert_model_1.AlertModel.findByIdAndUpdate(id, {
140
+ isDeleted: true,
141
+ updatedBy: deletedBy,
142
+ updatedAt: new Date(),
143
+ });
144
+ return !!result;
145
+ }
146
+ catch (error) {
147
+ throw new Error(`Failed to soft delete alert: ${error instanceof Error ? error.message : "Unknown error"}`);
148
+ }
149
+ }
150
+ /**
151
+ * Permanently delete an alert
152
+ */
153
+ async hardDelete(id) {
154
+ try {
155
+ const result = await Alert_model_1.AlertModel.findByIdAndDelete(id);
156
+ return !!result;
157
+ }
158
+ catch (error) {
159
+ throw new Error(`Failed to permanently delete alert: ${error instanceof Error ? error.message : "Unknown error"}`);
160
+ }
161
+ }
162
+ /**
163
+ * Count alerts with filters
164
+ */
165
+ async count(filters = {}) {
166
+ try {
167
+ const query = {};
168
+ if (filters.propertyId)
169
+ query.propertyId = filters.propertyId;
170
+ if (filters.category)
171
+ query.category = filters.category;
172
+ if (filters.severity)
173
+ query.severity = filters.severity;
174
+ if (filters.entityType)
175
+ query.entityType = filters.entityType;
176
+ if (filters.entityId)
177
+ query.entityId = filters.entityId;
178
+ if (filters.isActive !== undefined)
179
+ query.isActive = filters.isActive;
180
+ if (filters.isRead !== undefined)
181
+ query.isRead = filters.isRead;
182
+ if (!filters.includeDeleted)
183
+ query.isDeleted = false;
184
+ return await Alert_model_1.AlertModel.countDocuments(query);
185
+ }
186
+ catch (error) {
187
+ throw new Error(`Failed to count alerts: ${error instanceof Error ? error.message : "Unknown error"}`);
188
+ }
189
+ }
190
+ /**
191
+ * Find alerts by property
192
+ */
193
+ async findByProperty(propertyId, includeDeleted = false) {
194
+ try {
195
+ return await Alert_model_1.AlertModel.findByProperty(propertyId, includeDeleted);
196
+ }
197
+ catch (error) {
198
+ throw new Error(`Failed to find alerts by property: ${error instanceof Error ? error.message : "Unknown error"}`);
199
+ }
200
+ }
201
+ /**
202
+ * Find alerts by entity
203
+ */
204
+ async findByEntity(entityId, entityType, includeDeleted = false) {
205
+ try {
206
+ return await Alert_model_1.AlertModel.findByEntity(entityId, entityType, includeDeleted);
207
+ }
208
+ catch (error) {
209
+ throw new Error(`Failed to find alerts by entity: ${error instanceof Error ? error.message : "Unknown error"}`);
210
+ }
211
+ }
212
+ /**
213
+ * Find alerts by category
214
+ */
215
+ async findByCategory(category, includeDeleted = false) {
216
+ try {
217
+ return await Alert_model_1.AlertModel.findByCategory(category, includeDeleted);
218
+ }
219
+ catch (error) {
220
+ throw new Error(`Failed to find alerts by category: ${error instanceof Error ? error.message : "Unknown error"}`);
221
+ }
222
+ }
223
+ /**
224
+ * Find alerts by severity
225
+ */
226
+ async findBySeverity(severity, includeDeleted = false) {
227
+ try {
228
+ return await Alert_model_1.AlertModel.findBySeverity(severity, includeDeleted);
229
+ }
230
+ catch (error) {
231
+ throw new Error(`Failed to find alerts by severity: ${error instanceof Error ? error.message : "Unknown error"}`);
232
+ }
233
+ }
234
+ /**
235
+ * Find active alerts
236
+ */
237
+ async findActive(includeDeleted = false) {
238
+ try {
239
+ return await Alert_model_1.AlertModel.findActive(includeDeleted);
240
+ }
241
+ catch (error) {
242
+ throw new Error(`Failed to find active alerts: ${error instanceof Error ? error.message : "Unknown error"}`);
243
+ }
244
+ }
245
+ /**
246
+ * Find unread alerts
247
+ */
248
+ async findUnread(includeDeleted = false) {
249
+ try {
250
+ return await Alert_model_1.AlertModel.findUnread(includeDeleted);
251
+ }
252
+ catch (error) {
253
+ throw new Error(`Failed to find unread alerts: ${error instanceof Error ? error.message : "Unknown error"}`);
254
+ }
255
+ }
256
+ /**
257
+ * Find snoozed alerts
258
+ */
259
+ async findSnoozed(includeDeleted = false) {
260
+ try {
261
+ return await Alert_model_1.AlertModel.findSnoozed(includeDeleted);
262
+ }
263
+ catch (error) {
264
+ throw new Error(`Failed to find snoozed alerts: ${error instanceof Error ? error.message : "Unknown error"}`);
265
+ }
266
+ }
267
+ /**
268
+ * Find expired snooze alerts
269
+ */
270
+ async findExpiredSnooze(includeDeleted = false) {
271
+ try {
272
+ return await Alert_model_1.AlertModel.findExpiredSnooze(includeDeleted);
273
+ }
274
+ catch (error) {
275
+ throw new Error(`Failed to find expired snooze alerts: ${error instanceof Error ? error.message : "Unknown error"}`);
276
+ }
277
+ }
278
+ /**
279
+ * Get alert statistics
280
+ */
281
+ async getStatistics(propertyId) {
282
+ try {
283
+ const query = { isDeleted: false };
284
+ if (propertyId)
285
+ query.propertyId = propertyId;
286
+ const [total, active, unread, snoozed, severityStats, categoryStats] = await Promise.all([
287
+ Alert_model_1.AlertModel.countDocuments(query),
288
+ Alert_model_1.AlertModel.countDocuments({ ...query, isActive: true }),
289
+ Alert_model_1.AlertModel.countDocuments({ ...query, isRead: false }),
290
+ Alert_model_1.AlertModel.countDocuments({
291
+ ...query,
292
+ snoozeUntil: { $exists: true, $ne: null },
293
+ }),
294
+ Alert_model_1.AlertModel.aggregate([
295
+ { $match: query },
296
+ { $group: { _id: "$severity", count: { $sum: 1 } } },
297
+ ]),
298
+ Alert_model_1.AlertModel.aggregate([
299
+ { $match: query },
300
+ { $group: { _id: "$category", count: { $sum: 1 } } },
301
+ ]),
302
+ ]);
303
+ const bySeverity = Object.values(alert_types_1.AlertSeverity).reduce((acc, severity) => {
304
+ acc[severity] = 0;
305
+ return acc;
306
+ }, {});
307
+ const byCategory = Object.values(alert_types_1.AlertCategory).reduce((acc, category) => {
308
+ acc[category] = 0;
309
+ return acc;
310
+ }, {});
311
+ // Type-safe handling of aggregation results
312
+ severityStats.forEach((stat) => {
313
+ if (stat._id in bySeverity) {
314
+ bySeverity[stat._id] = stat.count;
315
+ }
316
+ });
317
+ categoryStats.forEach((stat) => {
318
+ if (stat._id in byCategory) {
319
+ byCategory[stat._id] = stat.count;
320
+ }
321
+ });
322
+ return {
323
+ total,
324
+ active,
325
+ unread,
326
+ snoozed,
327
+ bySeverity,
328
+ byCategory,
329
+ };
330
+ }
331
+ catch (error) {
332
+ throw new Error(`Failed to get alert statistics: ${error instanceof Error ? error.message : "Unknown error"}`);
333
+ }
334
+ }
335
+ /**
336
+ * Bulk update alerts
337
+ */
338
+ async bulkUpdate(ids, updateData) {
339
+ try {
340
+ const result = await Alert_model_1.AlertModel.updateMany({ _id: { $in: ids } }, { ...updateData, updatedAt: new Date() });
341
+ return result.modifiedCount;
342
+ }
343
+ catch (error) {
344
+ throw new Error(`Failed to bulk update alerts: ${error instanceof Error ? error.message : "Unknown error"}`);
345
+ }
346
+ }
347
+ /**
348
+ * Bulk soft delete alerts
349
+ */
350
+ async bulkSoftDelete(ids, deletedBy) {
351
+ try {
352
+ const result = await Alert_model_1.AlertModel.updateMany({ _id: { $in: ids } }, {
353
+ isDeleted: true,
354
+ updatedBy: deletedBy,
355
+ updatedAt: new Date(),
356
+ });
357
+ return result.modifiedCount;
358
+ }
359
+ catch (error) {
360
+ throw new Error(`Failed to bulk soft delete alerts: ${error instanceof Error ? error.message : "Unknown error"}`);
361
+ }
362
+ }
363
+ };
364
+ __setFunctionName(_classThis, "AlertRepository");
365
+ (() => {
366
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
367
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
368
+ AlertRepository = _classThis = _classDescriptor.value;
369
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
370
+ __runInitializers(_classThis, _classExtraInitializers);
371
+ })();
372
+ return AlertRepository = _classThis;
373
+ })();
374
+ exports.AlertRepository = AlertRepository;