dt-common-device 3.0.10 → 3.0.11
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.
- package/dist/config/config.d.ts +1 -2
- package/dist/config/config.js +4 -5
- package/dist/device/local/repository/Schedule.repository.d.ts +0 -1
- package/dist/device/local/repository/Schedule.repository.js +6 -6
- package/dist/queue/entities/HybridHttpQueue.d.ts +4 -14
- package/dist/queue/entities/HybridHttpQueue.js +31 -119
- package/dist/queue/interfaces/IHybridHttpQueue.d.ts +2 -12
- package/dist/queue/interfaces/IJobResult.d.ts +1 -8
- package/dist/queue/interfaces/index.d.ts +0 -1
- package/dist/queue/interfaces/index.js +0 -1
- package/dist/queue/services/QueueService.d.ts +2 -12
- package/dist/queue/types/queue.types.d.ts +10 -29
- package/dist/queue/utils/jobUtils.d.ts +0 -3
- package/dist/queue/utils/jobUtils.js +0 -48
- package/dist/queue/utils/queueUtils.d.ts +7 -0
- package/dist/queue/utils/queueUtils.js +113 -4
- package/package.json +6 -1
- package/.eslintrc.js +0 -44
- package/src/alerts/Alert.model.ts +0 -289
- package/src/alerts/Alert.repository.ts +0 -487
- package/src/alerts/Alert.service.ts +0 -711
- package/src/alerts/AlertBuilder.example.ts +0 -126
- package/src/alerts/AlertBuilder.ts +0 -208
- package/src/alerts/AlertService.example.ts +0 -232
- package/src/alerts/alert.types.ts +0 -64
- package/src/alerts/index.ts +0 -3
- package/src/config/config.ts +0 -202
- package/src/config/config.types.ts +0 -21
- package/src/connection/Connection.repository.ts +0 -52
- package/src/connection/Connection.service.ts +0 -39
- package/src/connection/IConnection.ts +0 -27
- package/src/connection/index.ts +0 -3
- package/src/constants/ConnectionProviders.ts +0 -11
- package/src/constants/Event.ts +0 -89
- package/src/constants/Service.ts +0 -17
- package/src/constants/index.ts +0 -3
- package/src/db/db.ts +0 -24
- package/src/db/index.ts +0 -2
- package/src/db/redis.ts +0 -20
- package/src/device/cloud/entities/CloudDevice.ts +0 -40
- package/src/device/cloud/entities/CloudDeviceService.ts +0 -8
- package/src/device/cloud/entities/DeviceFactory.ts +0 -27
- package/src/device/cloud/entities/index.ts +0 -3
- package/src/device/cloud/interfaces/ICloudDevice.ts +0 -14
- package/src/device/cloud/interfaces/ICloudDeviceService.ts +0 -6
- package/src/device/cloud/interfaces/IDeviceFactory.ts +0 -5
- package/src/device/cloud/interfaces/IRawDataTransformer.ts +0 -5
- package/src/device/cloud/interfaces/IRawDevice.ts +0 -19
- package/src/device/cloud/interfaces/index.ts +0 -5
- package/src/device/local/interfaces/IDevice.ts +0 -62
- package/src/device/local/interfaces/IDtDevice.ts +0 -16
- package/src/device/local/interfaces/ISchedule.ts +0 -40
- package/src/device/local/interfaces/index.ts +0 -3
- package/src/device/local/repository/Device.repository.ts +0 -368
- package/src/device/local/repository/Hub.repository.ts +0 -107
- package/src/device/local/repository/Schedule.repository.ts +0 -72
- package/src/device/local/services/Device.service.ts +0 -436
- package/src/device/local/services/Hub.service.ts +0 -57
- package/src/device/local/services/Schedule.service.ts +0 -26
- package/src/device/local/services/index.ts +0 -3
- package/src/docs/Alert.model.md +0 -319
- package/src/docs/Alerts&IssuesModel.md +0 -312
- package/src/docs/Issue.model.md +0 -386
- package/src/docs/SECURITY.md +0 -67
- package/src/docs/TROUBLESHOOTING.md +0 -184
- package/src/events/BaseEventHandler.ts +0 -145
- package/src/events/BaseEventTransformer.ts +0 -97
- package/src/events/DeviceEventHandler.ts +0 -213
- package/src/events/DeviceEventTransformerFactory.ts +0 -77
- package/src/events/EventHandler.ts +0 -124
- package/src/events/EventHandlerOrchestrator.ts +0 -119
- package/src/events/EventProcessingService.ts +0 -248
- package/src/events/InternalEventSubscription.ts +0 -194
- package/src/events/index.ts +0 -9
- package/src/events/interfaces/DeviceEvent.ts +0 -56
- package/src/events/interfaces/IEventHandler.ts +0 -28
- package/src/events/interfaces/IEventTransformer.ts +0 -8
- package/src/events/interfaces/IInternalEvent.ts +0 -33
- package/src/events/interfaces/index.ts +0 -4
- package/src/index.ts +0 -43
- package/src/issues/Issue.model.ts +0 -350
- package/src/issues/Issue.repository.ts +0 -517
- package/src/issues/Issue.service.ts +0 -932
- package/src/issues/IssueBuilder.example.ts +0 -210
- package/src/issues/IssueBuilder.ts +0 -263
- package/src/issues/IssueService.example.ts +0 -310
- package/src/issues/index.ts +0 -2
- package/src/issues/issue.types.ts +0 -98
- package/src/property/IProperty.ts +0 -30
- package/src/property/Property.repository.ts +0 -53
- package/src/property/Property.service.ts +0 -38
- package/src/property/index.ts +0 -2
- package/src/queue/entities/HybridHttpQueue.ts +0 -274
- package/src/queue/entities/index.ts +0 -1
- package/src/queue/index.ts +0 -6
- package/src/queue/interfaces/IHttpRequestJob.ts +0 -10
- package/src/queue/interfaces/IHybridHttpQueue.ts +0 -25
- package/src/queue/interfaces/IJobResult.ts +0 -15
- package/src/queue/interfaces/IRateLimitConfig.ts +0 -5
- package/src/queue/interfaces/index.ts +0 -4
- package/src/queue/services/QueueService.ts +0 -40
- package/src/queue/services/index.ts +0 -1
- package/src/queue/types/http.types.ts +0 -23
- package/src/queue/types/index.ts +0 -2
- package/src/queue/types/queue.types.ts +0 -21
- package/src/queue/utils/index.ts +0 -3
- package/src/queue/utils/jobUtils.ts +0 -79
- package/src/queue/utils/queueUtils.ts +0 -84
- package/src/queue/utils/rateLimit.utils.ts +0 -131
- package/src/utils/http.utils.ts +0 -143
- package/src/utils/index.ts +0 -2
- package/src/utils/redis.utils.ts +0 -74
- package/tsconfig.json +0 -20
|
@@ -1,487 +0,0 @@
|
|
|
1
|
-
import { Service } from "typedi";
|
|
2
|
-
import { AlertModel, IAlertDocument } from "./Alert.model";
|
|
3
|
-
import {
|
|
4
|
-
CreateAlertData,
|
|
5
|
-
UpdateAlertData,
|
|
6
|
-
AlertCategory,
|
|
7
|
-
AlertSeverity,
|
|
8
|
-
EntityType,
|
|
9
|
-
} from "./alert.types";
|
|
10
|
-
|
|
11
|
-
@Service()
|
|
12
|
-
export class AlertRepository {
|
|
13
|
-
/**
|
|
14
|
-
* Create a new alert
|
|
15
|
-
*/
|
|
16
|
-
async create(alertData: CreateAlertData): Promise<IAlertDocument> {
|
|
17
|
-
try {
|
|
18
|
-
const alert = new AlertModel({
|
|
19
|
-
...alertData,
|
|
20
|
-
severity: alertData.severity || AlertSeverity.MEDIUM,
|
|
21
|
-
isRead: false,
|
|
22
|
-
isActive: true,
|
|
23
|
-
isDeleted: false,
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
return await alert.save();
|
|
27
|
-
} catch (error) {
|
|
28
|
-
throw new Error(
|
|
29
|
-
`Failed to create alert: ${
|
|
30
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
31
|
-
}`
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Find alert by ID
|
|
38
|
-
*/
|
|
39
|
-
async findById(
|
|
40
|
-
id: string,
|
|
41
|
-
includeDeleted = false
|
|
42
|
-
): Promise<IAlertDocument | null> {
|
|
43
|
-
try {
|
|
44
|
-
const query: any = { _id: id };
|
|
45
|
-
if (!includeDeleted) {
|
|
46
|
-
query.isDeleted = false;
|
|
47
|
-
}
|
|
48
|
-
return await AlertModel.findOne(query);
|
|
49
|
-
} catch (error) {
|
|
50
|
-
throw new Error(
|
|
51
|
-
`Failed to find alert by ID: ${
|
|
52
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
53
|
-
}`
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Find all alerts with filters
|
|
60
|
-
*/
|
|
61
|
-
async findAll(
|
|
62
|
-
filters: {
|
|
63
|
-
propertyId?: string;
|
|
64
|
-
category?: AlertCategory | AlertCategory[];
|
|
65
|
-
severity?: AlertSeverity;
|
|
66
|
-
entityType?: EntityType;
|
|
67
|
-
entityId?: string;
|
|
68
|
-
isActive?: boolean;
|
|
69
|
-
isRead?: boolean;
|
|
70
|
-
includeDeleted?: boolean;
|
|
71
|
-
limit?: number;
|
|
72
|
-
skip?: number;
|
|
73
|
-
sort?: { [key: string]: 1 | -1 };
|
|
74
|
-
} = {}
|
|
75
|
-
): Promise<IAlertDocument[]> {
|
|
76
|
-
try {
|
|
77
|
-
const query: any = {};
|
|
78
|
-
|
|
79
|
-
if (filters.propertyId) query.propertyId = filters.propertyId;
|
|
80
|
-
if (filters.category) {
|
|
81
|
-
if (Array.isArray(filters.category)) {
|
|
82
|
-
query.category = { $in: filters.category };
|
|
83
|
-
} else {
|
|
84
|
-
query.category = { $in: [filters.category] };
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
if (filters.severity) query.severity = filters.severity;
|
|
88
|
-
if (filters.entityType) query.entityType = filters.entityType;
|
|
89
|
-
if (filters.entityId) query.entityId = filters.entityId;
|
|
90
|
-
if (filters.isActive !== undefined) query.isActive = filters.isActive;
|
|
91
|
-
if (filters.isRead !== undefined) query.isRead = filters.isRead;
|
|
92
|
-
if (!filters.includeDeleted) query.isDeleted = false;
|
|
93
|
-
|
|
94
|
-
const queryBuilder = AlertModel.find(query);
|
|
95
|
-
|
|
96
|
-
if (filters.sort) {
|
|
97
|
-
queryBuilder.sort(filters.sort);
|
|
98
|
-
} else {
|
|
99
|
-
queryBuilder.sort({ severity: -1, createdAt: -1 });
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (filters.skip) queryBuilder.skip(filters.skip);
|
|
103
|
-
if (filters.limit) queryBuilder.limit(filters.limit);
|
|
104
|
-
|
|
105
|
-
return await queryBuilder.exec();
|
|
106
|
-
} catch (error) {
|
|
107
|
-
throw new Error(
|
|
108
|
-
`Failed to find alerts: ${
|
|
109
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
110
|
-
}`
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Update an alert
|
|
117
|
-
*/
|
|
118
|
-
async update(
|
|
119
|
-
id: string,
|
|
120
|
-
updateData: UpdateAlertData
|
|
121
|
-
): Promise<IAlertDocument | null> {
|
|
122
|
-
try {
|
|
123
|
-
return await AlertModel.findByIdAndUpdate(
|
|
124
|
-
id,
|
|
125
|
-
{ ...updateData, updatedAt: new Date() },
|
|
126
|
-
{ new: true, runValidators: true }
|
|
127
|
-
);
|
|
128
|
-
} catch (error) {
|
|
129
|
-
throw new Error(
|
|
130
|
-
`Failed to update alert: ${
|
|
131
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
132
|
-
}`
|
|
133
|
-
);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Soft delete an alert
|
|
139
|
-
*/
|
|
140
|
-
async softDelete(id: string, deletedBy: string): Promise<boolean> {
|
|
141
|
-
try {
|
|
142
|
-
const result = await AlertModel.findByIdAndUpdate(id, {
|
|
143
|
-
isDeleted: true,
|
|
144
|
-
updatedBy: deletedBy,
|
|
145
|
-
updatedAt: new Date(),
|
|
146
|
-
});
|
|
147
|
-
return !!result;
|
|
148
|
-
} catch (error) {
|
|
149
|
-
throw new Error(
|
|
150
|
-
`Failed to soft delete alert: ${
|
|
151
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
152
|
-
}`
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Permanently delete an alert
|
|
159
|
-
*/
|
|
160
|
-
async hardDelete(id: string): Promise<boolean> {
|
|
161
|
-
try {
|
|
162
|
-
const result = await AlertModel.findByIdAndDelete(id);
|
|
163
|
-
return !!result;
|
|
164
|
-
} catch (error) {
|
|
165
|
-
throw new Error(
|
|
166
|
-
`Failed to permanently delete alert: ${
|
|
167
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
168
|
-
}`
|
|
169
|
-
);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Count alerts with filters
|
|
175
|
-
*/
|
|
176
|
-
async count(
|
|
177
|
-
filters: {
|
|
178
|
-
propertyId?: string;
|
|
179
|
-
category?: AlertCategory | AlertCategory[];
|
|
180
|
-
severity?: AlertSeverity;
|
|
181
|
-
entityType?: EntityType;
|
|
182
|
-
entityId?: string;
|
|
183
|
-
isActive?: boolean;
|
|
184
|
-
isRead?: boolean;
|
|
185
|
-
includeDeleted?: boolean;
|
|
186
|
-
} = {}
|
|
187
|
-
): Promise<number> {
|
|
188
|
-
try {
|
|
189
|
-
const query: any = {};
|
|
190
|
-
|
|
191
|
-
if (filters.propertyId) query.propertyId = filters.propertyId;
|
|
192
|
-
if (filters.category) {
|
|
193
|
-
if (Array.isArray(filters.category)) {
|
|
194
|
-
query.category = { $in: filters.category };
|
|
195
|
-
} else {
|
|
196
|
-
query.category = { $in: [filters.category] };
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
if (filters.severity) query.severity = filters.severity;
|
|
200
|
-
if (filters.entityType) query.entityType = filters.entityType;
|
|
201
|
-
if (filters.entityId) query.entityId = filters.entityId;
|
|
202
|
-
if (filters.isActive !== undefined) query.isActive = filters.isActive;
|
|
203
|
-
if (filters.isRead !== undefined) query.isRead = filters.isRead;
|
|
204
|
-
if (!filters.includeDeleted) query.isDeleted = false;
|
|
205
|
-
|
|
206
|
-
return await AlertModel.countDocuments(query);
|
|
207
|
-
} catch (error) {
|
|
208
|
-
throw new Error(
|
|
209
|
-
`Failed to count alerts: ${
|
|
210
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
211
|
-
}`
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Find alerts by property
|
|
218
|
-
*/
|
|
219
|
-
async findByProperty(
|
|
220
|
-
propertyId: string,
|
|
221
|
-
includeDeleted = false
|
|
222
|
-
): Promise<IAlertDocument[]> {
|
|
223
|
-
try {
|
|
224
|
-
return await AlertModel.findByProperty(propertyId, includeDeleted);
|
|
225
|
-
} catch (error) {
|
|
226
|
-
throw new Error(
|
|
227
|
-
`Failed to find alerts by property: ${
|
|
228
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
229
|
-
}`
|
|
230
|
-
);
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Find alerts by entity
|
|
236
|
-
*/
|
|
237
|
-
async findByEntity(
|
|
238
|
-
entityId: string,
|
|
239
|
-
entityType: EntityType,
|
|
240
|
-
includeDeleted = false
|
|
241
|
-
): Promise<IAlertDocument[]> {
|
|
242
|
-
try {
|
|
243
|
-
return await AlertModel.findByEntity(
|
|
244
|
-
entityId,
|
|
245
|
-
entityType,
|
|
246
|
-
includeDeleted
|
|
247
|
-
);
|
|
248
|
-
} catch (error) {
|
|
249
|
-
throw new Error(
|
|
250
|
-
`Failed to find alerts by entity: ${
|
|
251
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
252
|
-
}`
|
|
253
|
-
);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Find alerts by category
|
|
259
|
-
*/
|
|
260
|
-
async findByCategory(
|
|
261
|
-
category: AlertCategory | AlertCategory[],
|
|
262
|
-
includeDeleted = false
|
|
263
|
-
): Promise<IAlertDocument[]> {
|
|
264
|
-
try {
|
|
265
|
-
if (Array.isArray(category)) {
|
|
266
|
-
// Use $in operator for array of categories
|
|
267
|
-
const query: any = { category: { $in: category } };
|
|
268
|
-
if (!includeDeleted) {
|
|
269
|
-
query.isDeleted = false;
|
|
270
|
-
}
|
|
271
|
-
return await AlertModel.find(query).sort({ createdAt: -1 });
|
|
272
|
-
} else {
|
|
273
|
-
return await AlertModel.findByCategory(category, includeDeleted);
|
|
274
|
-
}
|
|
275
|
-
} catch (error) {
|
|
276
|
-
throw new Error(
|
|
277
|
-
`Failed to find alerts by category: ${
|
|
278
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
279
|
-
}`
|
|
280
|
-
);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* Find alerts by severity
|
|
286
|
-
*/
|
|
287
|
-
async findBySeverity(
|
|
288
|
-
severity: AlertSeverity,
|
|
289
|
-
includeDeleted = false
|
|
290
|
-
): Promise<IAlertDocument[]> {
|
|
291
|
-
try {
|
|
292
|
-
return await AlertModel.findBySeverity(severity, includeDeleted);
|
|
293
|
-
} catch (error) {
|
|
294
|
-
throw new Error(
|
|
295
|
-
`Failed to find alerts by severity: ${
|
|
296
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
297
|
-
}`
|
|
298
|
-
);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* Find active alerts
|
|
304
|
-
*/
|
|
305
|
-
async findActive(includeDeleted = false): Promise<IAlertDocument[]> {
|
|
306
|
-
try {
|
|
307
|
-
return await AlertModel.findActive(includeDeleted);
|
|
308
|
-
} catch (error) {
|
|
309
|
-
throw new Error(
|
|
310
|
-
`Failed to find active alerts: ${
|
|
311
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
312
|
-
}`
|
|
313
|
-
);
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* Find unread alerts
|
|
319
|
-
*/
|
|
320
|
-
async findUnread(includeDeleted = false): Promise<IAlertDocument[]> {
|
|
321
|
-
try {
|
|
322
|
-
return await AlertModel.findUnread(includeDeleted);
|
|
323
|
-
} catch (error) {
|
|
324
|
-
throw new Error(
|
|
325
|
-
`Failed to find unread alerts: ${
|
|
326
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
327
|
-
}`
|
|
328
|
-
);
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* Find snoozed alerts
|
|
334
|
-
*/
|
|
335
|
-
async findSnoozed(includeDeleted = false): Promise<IAlertDocument[]> {
|
|
336
|
-
try {
|
|
337
|
-
return await AlertModel.findSnoozed(includeDeleted);
|
|
338
|
-
} catch (error) {
|
|
339
|
-
throw new Error(
|
|
340
|
-
`Failed to find snoozed alerts: ${
|
|
341
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
342
|
-
}`
|
|
343
|
-
);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
/**
|
|
348
|
-
* Find expired snooze alerts
|
|
349
|
-
*/
|
|
350
|
-
async findExpiredSnooze(includeDeleted = false): Promise<IAlertDocument[]> {
|
|
351
|
-
try {
|
|
352
|
-
return await AlertModel.findExpiredSnooze(includeDeleted);
|
|
353
|
-
} catch (error) {
|
|
354
|
-
throw new Error(
|
|
355
|
-
`Failed to find expired snooze alerts: ${
|
|
356
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
357
|
-
}`
|
|
358
|
-
);
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* Get alert statistics
|
|
364
|
-
*/
|
|
365
|
-
async getStatistics(propertyId?: string): Promise<{
|
|
366
|
-
total: number;
|
|
367
|
-
active: number;
|
|
368
|
-
unread: number;
|
|
369
|
-
snoozed: number;
|
|
370
|
-
bySeverity: Record<AlertSeverity, number>;
|
|
371
|
-
byCategory: Record<AlertCategory, number>;
|
|
372
|
-
}> {
|
|
373
|
-
try {
|
|
374
|
-
const query: any = { isDeleted: false };
|
|
375
|
-
if (propertyId) query.propertyId = propertyId;
|
|
376
|
-
|
|
377
|
-
const [total, active, unread, snoozed, severityStats, categoryStats] =
|
|
378
|
-
await Promise.all([
|
|
379
|
-
AlertModel.countDocuments(query),
|
|
380
|
-
AlertModel.countDocuments({ ...query, isActive: true }),
|
|
381
|
-
AlertModel.countDocuments({ ...query, isRead: false }),
|
|
382
|
-
AlertModel.countDocuments({
|
|
383
|
-
...query,
|
|
384
|
-
snoozeUntil: { $exists: true, $ne: null },
|
|
385
|
-
}),
|
|
386
|
-
AlertModel.aggregate([
|
|
387
|
-
{ $match: query },
|
|
388
|
-
{ $group: { _id: "$severity", count: { $sum: 1 } } },
|
|
389
|
-
]),
|
|
390
|
-
AlertModel.aggregate([
|
|
391
|
-
{ $match: query },
|
|
392
|
-
{ $unwind: "$category" },
|
|
393
|
-
{ $group: { _id: "$category", count: { $sum: 1 } } },
|
|
394
|
-
]),
|
|
395
|
-
]);
|
|
396
|
-
|
|
397
|
-
const bySeverity = Object.values(AlertSeverity).reduce(
|
|
398
|
-
(acc, severity) => {
|
|
399
|
-
acc[severity] = 0;
|
|
400
|
-
return acc;
|
|
401
|
-
},
|
|
402
|
-
{} as Record<AlertSeverity, number>
|
|
403
|
-
);
|
|
404
|
-
|
|
405
|
-
const byCategory = Object.values(AlertCategory).reduce(
|
|
406
|
-
(acc, category) => {
|
|
407
|
-
acc[category] = 0;
|
|
408
|
-
return acc;
|
|
409
|
-
},
|
|
410
|
-
{} as Record<AlertCategory, number>
|
|
411
|
-
);
|
|
412
|
-
|
|
413
|
-
// Type-safe handling of aggregation results
|
|
414
|
-
severityStats.forEach((stat: { _id: string; count: number }) => {
|
|
415
|
-
if (stat._id in bySeverity) {
|
|
416
|
-
bySeverity[stat._id as AlertSeverity] = stat.count;
|
|
417
|
-
}
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
categoryStats.forEach((stat: { _id: string; count: number }) => {
|
|
421
|
-
if (stat._id in byCategory) {
|
|
422
|
-
byCategory[stat._id as AlertCategory] = stat.count;
|
|
423
|
-
}
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
return {
|
|
427
|
-
total,
|
|
428
|
-
active,
|
|
429
|
-
unread,
|
|
430
|
-
snoozed,
|
|
431
|
-
bySeverity,
|
|
432
|
-
byCategory,
|
|
433
|
-
};
|
|
434
|
-
} catch (error) {
|
|
435
|
-
throw new Error(
|
|
436
|
-
`Failed to get alert statistics: ${
|
|
437
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
438
|
-
}`
|
|
439
|
-
);
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
/**
|
|
444
|
-
* Bulk update alerts
|
|
445
|
-
*/
|
|
446
|
-
async bulkUpdate(
|
|
447
|
-
ids: string[],
|
|
448
|
-
updateData: Partial<UpdateAlertData>
|
|
449
|
-
): Promise<number> {
|
|
450
|
-
try {
|
|
451
|
-
const result = await AlertModel.updateMany(
|
|
452
|
-
{ _id: { $in: ids } },
|
|
453
|
-
{ ...updateData, updatedAt: new Date() }
|
|
454
|
-
);
|
|
455
|
-
return result.modifiedCount;
|
|
456
|
-
} catch (error) {
|
|
457
|
-
throw new Error(
|
|
458
|
-
`Failed to bulk update alerts: ${
|
|
459
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
460
|
-
}`
|
|
461
|
-
);
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
/**
|
|
466
|
-
* Bulk soft delete alerts
|
|
467
|
-
*/
|
|
468
|
-
async bulkSoftDelete(ids: string[], deletedBy: string): Promise<number> {
|
|
469
|
-
try {
|
|
470
|
-
const result = await AlertModel.updateMany(
|
|
471
|
-
{ _id: { $in: ids } },
|
|
472
|
-
{
|
|
473
|
-
isDeleted: true,
|
|
474
|
-
updatedBy: deletedBy,
|
|
475
|
-
updatedAt: new Date(),
|
|
476
|
-
}
|
|
477
|
-
);
|
|
478
|
-
return result.modifiedCount;
|
|
479
|
-
} catch (error) {
|
|
480
|
-
throw new Error(
|
|
481
|
-
`Failed to bulk soft delete alerts: ${
|
|
482
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
483
|
-
}`
|
|
484
|
-
);
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
}
|