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
package/src/docs/Alert.model.md
DELETED
|
@@ -1,319 +0,0 @@
|
|
|
1
|
-
# Alert Model Documentation
|
|
2
|
-
|
|
3
|
-
This document describes the Mongoose implementation of the Alert model, which was converted from a Prisma schema to provide comprehensive alert management functionality.
|
|
4
|
-
|
|
5
|
-
## Overview
|
|
6
|
-
|
|
7
|
-
The Alert model provides a complete alert management system with the following features:
|
|
8
|
-
|
|
9
|
-
- **Alert Management**: Create, read, update, and delete alerts
|
|
10
|
-
- **Severity Levels**: Assign severity levels (INFO, LOW, MEDIUM, HIGH, CRITICAL)
|
|
11
|
-
- **Categories**: Categorize alerts (DEVICE_OFFLINE, DEVICE_BATTERY_LOW, etc.)
|
|
12
|
-
- **Entity References**: Link alerts to various entities (DEVICE, HUB, etc.)
|
|
13
|
-
- **Read Status**: Track read/unread status
|
|
14
|
-
- **Active Status**: Activate/deactivate alerts
|
|
15
|
-
- **Snooze Functionality**: Temporarily suppress alerts until a specified date
|
|
16
|
-
- **Soft Deletion**: Support for soft deletion with recovery capability
|
|
17
|
-
- **Statistics**: Comprehensive alert statistics and reporting
|
|
18
|
-
|
|
19
|
-
## Model Structure
|
|
20
|
-
|
|
21
|
-
### Core Fields
|
|
22
|
-
|
|
23
|
-
| Field | Type | Required | Description |
|
|
24
|
-
| ------------- | ------------- | -------- | ------------------------------------- |
|
|
25
|
-
| `_id` | String | Yes | MongoDB ObjectId (auto-generated) |
|
|
26
|
-
| `category` | AlertCategory | Yes | Alert category (DEVICE_OFFLINE, etc.) |
|
|
27
|
-
| `propertyId` | String | Yes | Property UUID reference |
|
|
28
|
-
| `title` | String | Yes | Alert title |
|
|
29
|
-
| `description` | String | Yes | Alert description |
|
|
30
|
-
| `entityId` | String | No | Generic entity reference UUID |
|
|
31
|
-
| `entityType` | EntityType | Yes | Entity type (DEVICE, HUB, etc.) |
|
|
32
|
-
| `severity` | AlertSeverity | Yes | Alert severity (default: MEDIUM) |
|
|
33
|
-
| `isRead` | Boolean | Yes | Read status flag (default: false) |
|
|
34
|
-
| `isActive` | Boolean | Yes | Active status flag (default: true) |
|
|
35
|
-
| `isDeleted` | Boolean | Yes | Soft deletion flag (default: false) |
|
|
36
|
-
| `snoozeUntil` | Date | No | Snooze expiration date |
|
|
37
|
-
| `createdBy` | String | No | User UUID who created the alert |
|
|
38
|
-
| `updatedBy` | String | No | User UUID who last updated |
|
|
39
|
-
| `createdAt` | Date | Yes | Creation timestamp |
|
|
40
|
-
| `updatedAt` | Date | Yes | Last update timestamp |
|
|
41
|
-
|
|
42
|
-
### Enums
|
|
43
|
-
|
|
44
|
-
#### AlertCategory
|
|
45
|
-
|
|
46
|
-
```typescript
|
|
47
|
-
enum AlertCategory {
|
|
48
|
-
READINESS = "READINESS",
|
|
49
|
-
OPERATIONS = "OPERATIONS",
|
|
50
|
-
SECURITY = "SECURITY",
|
|
51
|
-
ENERGY = "ENERGY",
|
|
52
|
-
OTHER = "OTHER",
|
|
53
|
-
}
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
#### AlertSeverity
|
|
57
|
-
|
|
58
|
-
```typescript
|
|
59
|
-
enum AlertSeverity {
|
|
60
|
-
INFO = "INFO",
|
|
61
|
-
LOW = "LOW",
|
|
62
|
-
MEDIUM = "MEDIUM",
|
|
63
|
-
HIGH = "HIGH",
|
|
64
|
-
CRITICAL = "CRITICAL",
|
|
65
|
-
}
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
#### EntityType
|
|
69
|
-
|
|
70
|
-
```typescript
|
|
71
|
-
enum EntityType {
|
|
72
|
-
DEVICE = "DEVICE",
|
|
73
|
-
COLLECTION = "COLLECTION",
|
|
74
|
-
USER = "USER",
|
|
75
|
-
INTEGRATION = "INTEGRATION",
|
|
76
|
-
PROPERTY = "PROPERTY",
|
|
77
|
-
HUB = "HUB",
|
|
78
|
-
SCHEDULE = "SCHEDULE",
|
|
79
|
-
ALERT = "ALERT",
|
|
80
|
-
OTHER = "OTHER",
|
|
81
|
-
}
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
## Usage Examples
|
|
85
|
-
|
|
86
|
-
### Basic CRUD Operations
|
|
87
|
-
|
|
88
|
-
#### Creating an Alert
|
|
89
|
-
|
|
90
|
-
```typescript
|
|
91
|
-
import { AlertService } from "../services/Alert.service";
|
|
92
|
-
import {
|
|
93
|
-
AlertCategory,
|
|
94
|
-
EntityType,
|
|
95
|
-
AlertSeverity,
|
|
96
|
-
} from "../../../types/alert.types";
|
|
97
|
-
|
|
98
|
-
const alertService = new AlertService();
|
|
99
|
-
|
|
100
|
-
const newAlert = await alertService.createAlert({
|
|
101
|
-
category: AlertCategory.READINESS,
|
|
102
|
-
propertyId: "property-uuid",
|
|
103
|
-
title: "Device Offline",
|
|
104
|
-
description: "Device has been offline for more than 5 minutes",
|
|
105
|
-
entityId: "device-uuid",
|
|
106
|
-
entityType: EntityType.DEVICE,
|
|
107
|
-
severity: AlertSeverity.HIGH,
|
|
108
|
-
createdBy: "user-uuid",
|
|
109
|
-
});
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
#### Getting Alerts
|
|
113
|
-
|
|
114
|
-
```typescript
|
|
115
|
-
// Get all alerts for a property
|
|
116
|
-
const propertyAlerts = await alertService.getAlertsByProperty("property-uuid");
|
|
117
|
-
|
|
118
|
-
// Get alerts by severity
|
|
119
|
-
const criticalAlerts = await alertService.getAlertsBySeverity(
|
|
120
|
-
AlertSeverity.CRITICAL
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
// Get unread alerts
|
|
124
|
-
const unreadAlerts = await alertService.getUnreadAlerts();
|
|
125
|
-
|
|
126
|
-
// Get alerts with filters
|
|
127
|
-
const filteredAlerts = await alertService.getAlerts({
|
|
128
|
-
propertyId: "property-uuid",
|
|
129
|
-
severity: AlertSeverity.HIGH,
|
|
130
|
-
isActive: true,
|
|
131
|
-
limit: 10,
|
|
132
|
-
skip: 0,
|
|
133
|
-
});
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
#### Updating an Alert
|
|
137
|
-
|
|
138
|
-
```typescript
|
|
139
|
-
// Mark alert as read
|
|
140
|
-
await alertService.markAsRead(alertId, "user-uuid");
|
|
141
|
-
|
|
142
|
-
// Snooze alert for 24 hours
|
|
143
|
-
const snoozeUntil = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
|
144
|
-
await alertService.snoozeAlert(alertId, snoozeUntil, "user-uuid");
|
|
145
|
-
|
|
146
|
-
// Deactivate alert
|
|
147
|
-
await alertService.deactivateAlert(alertId, "user-uuid");
|
|
148
|
-
|
|
149
|
-
// Update alert details
|
|
150
|
-
await alertService.updateAlert(alertId, {
|
|
151
|
-
title: "Updated Alert Title",
|
|
152
|
-
description: "Updated description",
|
|
153
|
-
updatedBy: "user-uuid",
|
|
154
|
-
});
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
### Advanced Operations
|
|
158
|
-
|
|
159
|
-
#### Snooze Management
|
|
160
|
-
|
|
161
|
-
```typescript
|
|
162
|
-
// Snooze an alert until tomorrow
|
|
163
|
-
const tomorrow = new Date();
|
|
164
|
-
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
165
|
-
await alertService.snoozeAlert(alertId, tomorrow, "user-uuid");
|
|
166
|
-
|
|
167
|
-
// Remove snooze
|
|
168
|
-
await alertService.unsnoozeAlert(alertId, "user-uuid");
|
|
169
|
-
|
|
170
|
-
// Get expired snooze alerts
|
|
171
|
-
const expiredAlerts = await alertService.getExpiredSnoozeAlerts();
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
#### Alert Statistics
|
|
175
|
-
|
|
176
|
-
```typescript
|
|
177
|
-
// Get overall statistics
|
|
178
|
-
const stats = await alertService.getAlertStatistics();
|
|
179
|
-
|
|
180
|
-
// Get property-specific statistics
|
|
181
|
-
const propertyStats = await alertService.getAlertStatistics("property-uuid");
|
|
182
|
-
|
|
183
|
-
console.log(`Total alerts: ${stats.total}`);
|
|
184
|
-
console.log(`Active alerts: ${stats.active}`);
|
|
185
|
-
console.log(`Unread alerts: ${stats.unread}`);
|
|
186
|
-
console.log(`Snoozed alerts: ${stats.snoozed}`);
|
|
187
|
-
console.log(`Critical alerts: ${stats.bySeverity.CRITICAL}`);
|
|
188
|
-
console.log(`Readiness alerts: ${stats.byCategory.READINESS}`);
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
#### Repository Pattern Usage
|
|
192
|
-
|
|
193
|
-
For more advanced data access patterns, use the repository:
|
|
194
|
-
|
|
195
|
-
```typescript
|
|
196
|
-
import { AlertRepository } from "../repository/Alert.repository";
|
|
197
|
-
|
|
198
|
-
const alertRepo = new AlertRepository();
|
|
199
|
-
|
|
200
|
-
// Bulk operations
|
|
201
|
-
await alertRepo.bulkUpdate(["alert1", "alert2"], {
|
|
202
|
-
isRead: true,
|
|
203
|
-
updatedBy: "user-uuid",
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
// Count alerts
|
|
207
|
-
const count = await alertRepo.count({
|
|
208
|
-
propertyId: "property-uuid",
|
|
209
|
-
severity: AlertSeverity.HIGH,
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
// Advanced filtering with sorting
|
|
213
|
-
const alerts = await alertRepo.findAll({
|
|
214
|
-
propertyId: "property-uuid",
|
|
215
|
-
sort: { severity: -1, createdAt: -1 },
|
|
216
|
-
limit: 50,
|
|
217
|
-
});
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
## Business Rules
|
|
221
|
-
|
|
222
|
-
### Severity Assignment
|
|
223
|
-
|
|
224
|
-
Default severity is automatically determined based on the alert category:
|
|
225
|
-
|
|
226
|
-
| Category | Default Severity |
|
|
227
|
-
| ---------- | ---------------- |
|
|
228
|
-
| READINESS | HIGH |
|
|
229
|
-
| OPERATIONS | MEDIUM |
|
|
230
|
-
| SECURITY | CRITICAL |
|
|
231
|
-
| ENERGY | MEDIUM |
|
|
232
|
-
| OTHER | LOW |
|
|
233
|
-
|
|
234
|
-
### Snooze Validation
|
|
235
|
-
|
|
236
|
-
- Snooze dates must be in the future
|
|
237
|
-
- Attempting to set a past snooze date will throw an error
|
|
238
|
-
- Expired snooze alerts are automatically detected
|
|
239
|
-
|
|
240
|
-
### Critical Alert Protection
|
|
241
|
-
|
|
242
|
-
- Critical alerts cannot be deleted (soft or hard delete)
|
|
243
|
-
- Attempting to delete a critical alert will throw an error
|
|
244
|
-
|
|
245
|
-
### Monitoring and Logging
|
|
246
|
-
|
|
247
|
-
The service automatically logs important events:
|
|
248
|
-
|
|
249
|
-
- Unread alerts count
|
|
250
|
-
- Critical alerts count
|
|
251
|
-
- High severity alerts count
|
|
252
|
-
- Expired snooze alerts
|
|
253
|
-
|
|
254
|
-
## Indexes
|
|
255
|
-
|
|
256
|
-
The model includes the following indexes for optimal performance:
|
|
257
|
-
|
|
258
|
-
- `propertyId` (single field)
|
|
259
|
-
- `entityId` (single field)
|
|
260
|
-
- `entityType` (single field)
|
|
261
|
-
- `severity` (single field)
|
|
262
|
-
- `isRead` (single field)
|
|
263
|
-
- `isActive` (single field)
|
|
264
|
-
- `createdAt` (single field)
|
|
265
|
-
- `{ propertyId: 1, isActive: 1, isRead: 1 }` (compound)
|
|
266
|
-
- `{ entityId: 1, entityType: 1 }` (compound)
|
|
267
|
-
|
|
268
|
-
## Middleware
|
|
269
|
-
|
|
270
|
-
The model includes pre-save and pre-update middleware that automatically:
|
|
271
|
-
|
|
272
|
-
- Updates the `updatedAt` timestamp on save
|
|
273
|
-
- Updates the `updatedAt` timestamp on update operations
|
|
274
|
-
|
|
275
|
-
## Virtual Fields
|
|
276
|
-
|
|
277
|
-
- `isSnoozed`: Returns `true` if the alert is currently snoozed
|
|
278
|
-
- `isSnoozeExpired`: Returns `true` if the alert snooze has expired
|
|
279
|
-
|
|
280
|
-
## Error Handling
|
|
281
|
-
|
|
282
|
-
All operations include comprehensive error handling with descriptive error messages. The service and repository layers wrap database operations in try-catch blocks and provide meaningful error messages.
|
|
283
|
-
|
|
284
|
-
Common error scenarios:
|
|
285
|
-
|
|
286
|
-
- **Invalid snooze date**: "Snooze date must be in the future"
|
|
287
|
-
- **Critical alert deletion**: "Cannot delete critical alerts"
|
|
288
|
-
- **Missing required fields**: "Alert title must be at least 3 characters long"
|
|
289
|
-
- **Invalid filters**: "Limit must be between 1 and 100"
|
|
290
|
-
|
|
291
|
-
## Best Practices
|
|
292
|
-
|
|
293
|
-
1. **Use Appropriate Severity**: Choose severity levels based on business impact
|
|
294
|
-
2. **Set Meaningful Titles**: Use descriptive titles for better alert identification
|
|
295
|
-
3. **Use Snooze Wisely**: Only snooze alerts when appropriate, not as a permanent solution
|
|
296
|
-
4. **Monitor Statistics**: Regularly check alert statistics for insights
|
|
297
|
-
5. **Handle Expired Snoozes**: Implement processes to handle expired snooze alerts
|
|
298
|
-
6. **Use Categories**: Properly categorize alerts for better organization
|
|
299
|
-
7. **Set Entity References**: Link alerts to specific entities when possible
|
|
300
|
-
8. **Use Soft Deletion**: Always use soft deletion for audit trails
|
|
301
|
-
9. **Validate Input**: Always validate alert data before creation
|
|
302
|
-
10. **Monitor Critical Alerts**: Implement immediate notification for critical alerts
|
|
303
|
-
|
|
304
|
-
## Migration from Prisma
|
|
305
|
-
|
|
306
|
-
This Mongoose model maintains full compatibility with the original Prisma schema:
|
|
307
|
-
|
|
308
|
-
- All fields and types are preserved
|
|
309
|
-
- Indexes match the Prisma schema
|
|
310
|
-
- Relationships are maintained through foreign key references
|
|
311
|
-
- Collection name matches Prisma (`dt_alerts`)
|
|
312
|
-
|
|
313
|
-
The model provides additional features beyond the Prisma schema:
|
|
314
|
-
|
|
315
|
-
- Instance methods for common operations
|
|
316
|
-
- Static methods for complex queries
|
|
317
|
-
- Virtual fields for computed properties
|
|
318
|
-
- Comprehensive business logic validation
|
|
319
|
-
- Built-in monitoring and logging
|
|
@@ -1,312 +0,0 @@
|
|
|
1
|
-
# Local Device Models
|
|
2
|
-
|
|
3
|
-
This directory contains Mongoose models for local device management, implementing Prisma schema compatibility.
|
|
4
|
-
|
|
5
|
-
## Models
|
|
6
|
-
|
|
7
|
-
### Alert Model (`Alert.model.ts`)
|
|
8
|
-
|
|
9
|
-
A comprehensive alert management system with snooze functionality, severity levels, and read status tracking.
|
|
10
|
-
|
|
11
|
-
**📖 [Detailed Documentation](./Alert.model.md)**
|
|
12
|
-
|
|
13
|
-
#### Key Features
|
|
14
|
-
|
|
15
|
-
- **Severity Management**: INFO, LOW, MEDIUM, HIGH, CRITICAL levels
|
|
16
|
-
- **Snooze Functionality**: Temporarily suppress alerts until specified date
|
|
17
|
-
- **Read Status**: Track read/unread status with automatic logging
|
|
18
|
-
- **Category System**: READINESS, OPERATIONS, SECURITY, ENERGY, OTHER
|
|
19
|
-
- **Entity References**: Link alerts to devices, hubs, and other entities
|
|
20
|
-
- **Statistics**: Comprehensive alert statistics and monitoring
|
|
21
|
-
|
|
22
|
-
#### Quick Example
|
|
23
|
-
|
|
24
|
-
```typescript
|
|
25
|
-
import { AlertService } from "../services/Alert.service";
|
|
26
|
-
import {
|
|
27
|
-
AlertCategory,
|
|
28
|
-
AlertSeverity,
|
|
29
|
-
EntityType,
|
|
30
|
-
} from "../../../types/alert.types";
|
|
31
|
-
|
|
32
|
-
const alertService = new AlertService();
|
|
33
|
-
|
|
34
|
-
// Create a critical alert
|
|
35
|
-
const alert = await alertService.createAlert({
|
|
36
|
-
category: AlertCategory.READINESS,
|
|
37
|
-
propertyId: "property-123",
|
|
38
|
-
title: "Device Offline",
|
|
39
|
-
description: "Device has been offline for more than 5 minutes",
|
|
40
|
-
entityId: "device-456",
|
|
41
|
-
entityType: EntityType.DEVICE,
|
|
42
|
-
severity: AlertSeverity.HIGH,
|
|
43
|
-
createdBy: "user-789",
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
// Snooze for 24 hours
|
|
47
|
-
await alertService.snoozeAlert(
|
|
48
|
-
alert._id,
|
|
49
|
-
new Date(Date.now() + 24 * 60 * 60 * 1000),
|
|
50
|
-
"user-789"
|
|
51
|
-
);
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
### Issue Model (`Issue.model.ts`)
|
|
55
|
-
|
|
56
|
-
A comprehensive issue tracking system with assignment, comments, and resolution workflow.
|
|
57
|
-
|
|
58
|
-
**📖 [Detailed Documentation](./Issue.model.md)**
|
|
59
|
-
|
|
60
|
-
#### Key Features
|
|
61
|
-
|
|
62
|
-
- **Status Workflow**: OPEN → IN_PROGRESS → RESOLVED → CLOSED
|
|
63
|
-
- **Assignment System**: Assign issues to users with validation
|
|
64
|
-
- **Comment System**: Add, update, and remove comments on issues
|
|
65
|
-
- **Priority Management**: LOW, MEDIUM, HIGH, CRITICAL with overdue calculation
|
|
66
|
-
- **Due Date Tracking**: Set and monitor due dates with automatic overdue detection
|
|
67
|
-
- **Search & Statistics**: Full-text search and comprehensive reporting
|
|
68
|
-
|
|
69
|
-
#### Quick Example
|
|
70
|
-
|
|
71
|
-
```typescript
|
|
72
|
-
import { IssueService } from "../services/Issue.service";
|
|
73
|
-
import {
|
|
74
|
-
IssueCategory,
|
|
75
|
-
IssuePriority,
|
|
76
|
-
EntityType,
|
|
77
|
-
} from "../../../types/issue.types";
|
|
78
|
-
|
|
79
|
-
const issueService = new IssueService();
|
|
80
|
-
|
|
81
|
-
// Create an issue
|
|
82
|
-
const issue = await issueService.createIssue({
|
|
83
|
-
category: IssueCategory.READINESS,
|
|
84
|
-
propertyId: "property-123",
|
|
85
|
-
title: "Device Connection Issue",
|
|
86
|
-
description: "Device is not responding to commands",
|
|
87
|
-
entityId: "device-456",
|
|
88
|
-
entityType: EntityType.DEVICE,
|
|
89
|
-
priority: IssuePriority.HIGH,
|
|
90
|
-
createdBy: "user-789",
|
|
91
|
-
dueDate: new Date("2024-01-15"),
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
// Assign and start progress
|
|
95
|
-
await issueService.assignIssue(issue._id, "tech-user-123", "user-789");
|
|
96
|
-
await issueService.updateIssue(issue._id, {
|
|
97
|
-
status: IssueStatus.IN_PROGRESS,
|
|
98
|
-
updatedBy: "tech-user-123",
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
// Add comment
|
|
102
|
-
await issueService.addComment(issue._id, {
|
|
103
|
-
userId: "tech-user-123",
|
|
104
|
-
content: "Investigating the connection issue",
|
|
105
|
-
});
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
## Architecture
|
|
109
|
-
|
|
110
|
-
### Clean Architecture Pattern
|
|
111
|
-
|
|
112
|
-
Both models follow a clean architecture pattern with clear separation of concerns:
|
|
113
|
-
|
|
114
|
-
```
|
|
115
|
-
Service Layer (Business Logic)
|
|
116
|
-
↓
|
|
117
|
-
Repository Layer (Data Access)
|
|
118
|
-
↓
|
|
119
|
-
Model Layer (Data Structure)
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
#### Service Layer
|
|
123
|
-
|
|
124
|
-
- **Business Logic**: Validation, business rules, workflow management
|
|
125
|
-
- **Error Handling**: Comprehensive error messages and validation
|
|
126
|
-
- **Monitoring**: Built-in logging for important events
|
|
127
|
-
- **Repository Usage**: All data access through repository layer
|
|
128
|
-
|
|
129
|
-
#### Repository Layer
|
|
130
|
-
|
|
131
|
-
- **Data Access**: CRUD operations with error handling
|
|
132
|
-
- **Query Optimization**: Efficient filtering and sorting
|
|
133
|
-
- **Bulk Operations**: Support for bulk updates and deletes
|
|
134
|
-
- **Statistics**: Aggregation and reporting functions
|
|
135
|
-
|
|
136
|
-
#### Model Layer
|
|
137
|
-
|
|
138
|
-
- **Schema Definition**: Mongoose schemas with validation
|
|
139
|
-
- **Instance Methods**: Common operations on individual documents
|
|
140
|
-
- **Static Methods**: Complex queries and filtering
|
|
141
|
-
- **Virtual Fields**: Computed properties
|
|
142
|
-
- **Middleware**: Pre/post save hooks for automation
|
|
143
|
-
|
|
144
|
-
## Common Patterns
|
|
145
|
-
|
|
146
|
-
### Business Rules Implementation
|
|
147
|
-
|
|
148
|
-
Both models implement business rules in the service layer:
|
|
149
|
-
|
|
150
|
-
```typescript
|
|
151
|
-
// Alert Service - Severity Assignment
|
|
152
|
-
private determineDefaultSeverity(category: AlertCategory): AlertSeverity {
|
|
153
|
-
const categorySeverities: Record<AlertCategory, AlertSeverity> = {
|
|
154
|
-
[AlertCategory.READINESS]: AlertSeverity.HIGH,
|
|
155
|
-
[AlertCategory.SECURITY]: AlertSeverity.CRITICAL,
|
|
156
|
-
// ... more mappings
|
|
157
|
-
};
|
|
158
|
-
return categorySeverities[category] || AlertSeverity.MEDIUM;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Issue Service - Status Transitions
|
|
162
|
-
private validateStatusTransition(currentStatus: IssueStatus, newStatus: IssueStatus): void {
|
|
163
|
-
const validTransitions = {
|
|
164
|
-
[IssueStatus.OPEN]: [IssueStatus.IN_PROGRESS],
|
|
165
|
-
[IssueStatus.IN_PROGRESS]: [IssueStatus.RESOLVED],
|
|
166
|
-
[IssueStatus.RESOLVED]: [IssueStatus.CLOSED, IssueStatus.OPEN],
|
|
167
|
-
// ... more transitions
|
|
168
|
-
};
|
|
169
|
-
// Validation logic
|
|
170
|
-
}
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
### Error Handling
|
|
174
|
-
|
|
175
|
-
Consistent error handling across all layers:
|
|
176
|
-
|
|
177
|
-
```typescript
|
|
178
|
-
// Repository Layer
|
|
179
|
-
try {
|
|
180
|
-
return await AlertModel.findById(id);
|
|
181
|
-
} catch (error) {
|
|
182
|
-
throw new Error(
|
|
183
|
-
`Failed to find alert by ID: ${
|
|
184
|
-
error instanceof Error ? error.message : "Unknown error"
|
|
185
|
-
}`
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Service Layer
|
|
190
|
-
if (!id) {
|
|
191
|
-
throw new Error("Alert ID is required");
|
|
192
|
-
}
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
### Monitoring and Logging
|
|
196
|
-
|
|
197
|
-
Built-in monitoring for important events:
|
|
198
|
-
|
|
199
|
-
```typescript
|
|
200
|
-
// Alert monitoring
|
|
201
|
-
if (stats.bySeverity[AlertSeverity.CRITICAL] > 0) {
|
|
202
|
-
console.error(
|
|
203
|
-
`Alert: ${
|
|
204
|
-
stats.bySeverity[AlertSeverity.CRITICAL]
|
|
205
|
-
} critical alerts require immediate attention`
|
|
206
|
-
);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// Issue monitoring
|
|
210
|
-
if (stats.overdue > 0) {
|
|
211
|
-
console.warn(`Alert: ${stats.overdue} overdue issues require attention`);
|
|
212
|
-
}
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
## Best Practices
|
|
216
|
-
|
|
217
|
-
### 1. **Use Service Layer for Business Logic**
|
|
218
|
-
|
|
219
|
-
Always use the service layer for operations that involve business rules, validation, or workflow management.
|
|
220
|
-
|
|
221
|
-
### 2. **Use Repository Layer for Data Access**
|
|
222
|
-
|
|
223
|
-
Use the repository layer for direct data access operations, bulk operations, and complex queries.
|
|
224
|
-
|
|
225
|
-
### 3. **Validate Input Data**
|
|
226
|
-
|
|
227
|
-
Always validate input data before processing to ensure data integrity.
|
|
228
|
-
|
|
229
|
-
### 4. **Handle Errors Gracefully**
|
|
230
|
-
|
|
231
|
-
Use try-catch blocks and provide meaningful error messages for better debugging.
|
|
232
|
-
|
|
233
|
-
### 5. **Use Soft Deletion**
|
|
234
|
-
|
|
235
|
-
Always use soft deletion unless explicitly required to permanently remove data.
|
|
236
|
-
|
|
237
|
-
### 6. **Monitor Important Events**
|
|
238
|
-
|
|
239
|
-
Implement logging for critical events like overdue issues, critical alerts, and status changes.
|
|
240
|
-
|
|
241
|
-
### 7. **Use TypeScript Interfaces**
|
|
242
|
-
|
|
243
|
-
Leverage TypeScript interfaces for type safety and better development experience.
|
|
244
|
-
|
|
245
|
-
### 8. **Follow Naming Conventions**
|
|
246
|
-
|
|
247
|
-
Use consistent naming conventions across all layers for better maintainability.
|
|
248
|
-
|
|
249
|
-
### 9. **Document Business Rules**
|
|
250
|
-
|
|
251
|
-
Clearly document business rules and validation logic for future reference.
|
|
252
|
-
|
|
253
|
-
### 10. **Test Business Logic**
|
|
254
|
-
|
|
255
|
-
Write tests for business logic to ensure rules are correctly implemented.
|
|
256
|
-
|
|
257
|
-
## Migration from Prisma
|
|
258
|
-
|
|
259
|
-
Both models maintain full compatibility with their original Prisma schemas:
|
|
260
|
-
|
|
261
|
-
- ✅ All fields and types preserved
|
|
262
|
-
- ✅ Indexes match Prisma schema
|
|
263
|
-
- ✅ Relationships maintained through foreign key references
|
|
264
|
-
- ✅ Collection names match Prisma (`dt_alerts`, `dt_issues`)
|
|
265
|
-
|
|
266
|
-
Additional features beyond Prisma:
|
|
267
|
-
|
|
268
|
-
- ✅ Instance methods for common operations
|
|
269
|
-
- ✅ Static methods for complex queries
|
|
270
|
-
- ✅ Virtual fields for computed properties
|
|
271
|
-
- ✅ Comprehensive business logic validation
|
|
272
|
-
- ✅ Built-in monitoring and logging
|
|
273
|
-
- ✅ Comment management system (Issues)
|
|
274
|
-
- ✅ Snooze functionality (Alerts)
|
|
275
|
-
- ✅ Overdue calculation logic (Issues)
|
|
276
|
-
|
|
277
|
-
## Quick Start
|
|
278
|
-
|
|
279
|
-
1. **Import Services**:
|
|
280
|
-
|
|
281
|
-
```typescript
|
|
282
|
-
import { AlertService, IssueService } from "../services";
|
|
283
|
-
```
|
|
284
|
-
|
|
285
|
-
2. **Create Instances**:
|
|
286
|
-
|
|
287
|
-
```typescript
|
|
288
|
-
const alertService = new AlertService();
|
|
289
|
-
const issueService = new IssueService();
|
|
290
|
-
```
|
|
291
|
-
|
|
292
|
-
3. **Use Business Logic**:
|
|
293
|
-
|
|
294
|
-
```typescript
|
|
295
|
-
// Create alert with automatic severity assignment
|
|
296
|
-
const alert = await alertService.createAlert(alertData);
|
|
297
|
-
|
|
298
|
-
// Create issue with workflow validation
|
|
299
|
-
const issue = await issueService.createIssue(issueData);
|
|
300
|
-
```
|
|
301
|
-
|
|
302
|
-
4. **Monitor and Report**:
|
|
303
|
-
```typescript
|
|
304
|
-
// Get statistics
|
|
305
|
-
const alertStats = await alertService.getAlertStatistics();
|
|
306
|
-
const issueStats = await issueService.getIssueStatistics();
|
|
307
|
-
```
|
|
308
|
-
|
|
309
|
-
For detailed usage examples and advanced features, refer to the individual model documentation:
|
|
310
|
-
|
|
311
|
-
- [Alert Model Documentation](./Alert.model.md)
|
|
312
|
-
- [Issue Model Documentation](./Issue.model.md)
|