dt-common-device 1.3.0 → 2.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.
- package/TROUBLESHOOTING.md +184 -0
- package/dist/config/config.d.ts +9 -2
- package/dist/config/config.js +97 -14
- package/dist/constants/Event.d.ts +75 -0
- package/dist/constants/Event.js +78 -0
- package/dist/db/db.d.ts +1 -0
- package/dist/db/db.js +18 -2
- package/dist/device/local/entities/AlertBuilder.d.ts +87 -0
- package/dist/device/local/entities/AlertBuilder.example.d.ts +11 -0
- package/dist/device/local/entities/AlertBuilder.example.js +117 -0
- package/dist/device/local/entities/AlertBuilder.js +179 -0
- package/dist/device/local/entities/IssueBuilder.d.ts +109 -0
- package/dist/device/local/entities/IssueBuilder.example.d.ts +16 -0
- package/dist/device/local/entities/IssueBuilder.example.js +196 -0
- package/dist/device/local/entities/IssueBuilder.js +237 -0
- package/dist/device/local/entities/index.d.ts +2 -0
- package/dist/device/local/entities/index.js +7 -0
- package/dist/device/local/interfaces/IDevice.d.ts +10 -9
- package/dist/device/local/interfaces/IDevice.js +7 -0
- package/dist/device/local/models/Alert.model.d.ts +28 -0
- package/dist/device/local/models/Alert.model.js +222 -0
- package/dist/device/local/models/Issue.model.d.ts +28 -0
- package/dist/device/local/models/Issue.model.js +260 -0
- package/dist/device/local/repository/Alert.repository.d.ts +106 -0
- package/dist/device/local/repository/Alert.repository.js +374 -0
- package/dist/device/local/repository/Device.repository.d.ts +10 -2
- package/dist/device/local/repository/Device.repository.js +153 -30
- package/dist/device/local/repository/Hub.repository.d.ts +1 -1
- package/dist/device/local/repository/Hub.repository.js +60 -18
- package/dist/device/local/repository/Issue.repository.d.ts +113 -0
- package/dist/device/local/repository/Issue.repository.js +401 -0
- package/dist/device/local/repository/Schedule.repository.d.ts +1 -1
- package/dist/device/local/repository/Schedule.repository.js +14 -18
- package/dist/device/local/services/Alert.service.d.ts +135 -5
- package/dist/device/local/services/Alert.service.js +471 -7
- package/dist/device/local/services/AlertService.example.d.ts +55 -0
- package/dist/device/local/services/AlertService.example.js +148 -0
- package/dist/device/local/services/Device.service.d.ts +8 -5
- package/dist/device/local/services/Device.service.js +58 -40
- package/dist/device/local/services/Issue.service.d.ts +168 -0
- package/dist/device/local/services/Issue.service.js +642 -0
- package/dist/device/local/services/IssueService.example.d.ts +68 -0
- package/dist/device/local/services/IssueService.example.js +177 -0
- package/dist/device/local/services/index.d.ts +7 -5
- package/dist/device/local/services/index.js +21 -11
- package/dist/events/BaseEventHandler.d.ts +43 -0
- package/dist/events/BaseEventHandler.js +111 -0
- package/dist/events/BaseEventTransformer.d.ts +26 -0
- package/dist/events/BaseEventTransformer.js +72 -0
- package/dist/events/DeviceEventHandler.d.ts +15 -0
- package/dist/events/DeviceEventHandler.js +152 -0
- package/dist/events/DeviceEventTransformerFactory.d.ts +27 -0
- package/dist/events/DeviceEventTransformerFactory.js +116 -0
- package/dist/events/EventHandler.d.ts +11 -0
- package/dist/events/EventHandler.js +106 -0
- package/dist/events/EventHandlerOrchestrator.d.ts +35 -0
- package/dist/events/EventHandlerOrchestrator.js +141 -0
- package/dist/events/EventProcessingService.d.ts +43 -0
- package/dist/events/EventProcessingService.js +243 -0
- package/dist/events/InternalEventSubscription.d.ts +44 -0
- package/dist/events/InternalEventSubscription.js +152 -0
- package/dist/events/index.d.ts +9 -0
- package/dist/events/index.js +21 -0
- package/dist/events/interfaces/DeviceEvent.d.ts +48 -0
- package/dist/events/interfaces/DeviceEvent.js +2 -0
- package/dist/events/interfaces/IEventHandler.d.ts +23 -0
- package/dist/events/interfaces/IEventHandler.js +2 -0
- package/dist/events/interfaces/IEventTransformer.d.ts +7 -0
- package/dist/events/interfaces/IEventTransformer.js +2 -0
- package/dist/events/interfaces/IInternalEvent.d.ts +42 -0
- package/dist/events/interfaces/IInternalEvent.js +2 -0
- package/dist/events/interfaces/index.d.ts +4 -0
- package/dist/events/interfaces/index.js +20 -0
- package/dist/index.d.ts +6 -2
- package/dist/index.js +9 -2
- package/dist/types/alert.types.d.ts +57 -0
- package/dist/types/alert.types.js +22 -0
- package/dist/types/config.types.d.ts +15 -4
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +2 -0
- package/dist/types/issue.types.d.ts +90 -0
- package/dist/types/issue.types.js +40 -0
- package/dist/utils/http-utils.d.ts +13 -0
- package/dist/utils/http-utils.js +117 -0
- package/package.json +2 -1
- package/src/config/config.ts +117 -14
- package/src/{device/local/events/Events.ts → constants/Event.ts} +34 -13
- package/src/db/db.ts +14 -5
- package/src/device/local/entities/AlertBuilder.example.ts +126 -0
- package/src/device/local/entities/AlertBuilder.ts +202 -0
- package/src/device/local/entities/IssueBuilder.example.ts +210 -0
- package/src/device/local/entities/IssueBuilder.ts +263 -0
- package/src/device/local/entities/README.md +173 -0
- package/src/device/local/entities/index.ts +2 -0
- package/src/device/local/interfaces/IDevice.ts +11 -9
- package/src/device/local/models/Alert.model.md +319 -0
- package/src/device/local/models/Alert.model.ts +283 -0
- package/src/device/local/models/Issue.model.md +386 -0
- package/src/device/local/models/Issue.model.ts +350 -0
- package/src/device/local/models/README.md +312 -0
- package/src/device/local/repository/Alert.repository.ts +465 -0
- package/src/device/local/repository/Device.repository.ts +241 -32
- package/src/device/local/repository/Hub.repository.ts +74 -18
- package/src/device/local/repository/Issue.repository.ts +517 -0
- package/src/device/local/repository/Schedule.repository.ts +28 -22
- package/src/device/local/services/Alert.service.ts +617 -5
- package/src/device/local/services/AlertService.example.ts +229 -0
- package/src/device/local/services/Device.service.ts +70 -50
- package/src/device/local/services/Issue.service.ts +872 -0
- package/src/device/local/services/IssueService.example.ts +307 -0
- package/src/device/local/services/index.ts +7 -5
- package/src/events/BaseEventHandler.ts +145 -0
- package/src/events/BaseEventTransformer.ts +97 -0
- package/src/events/DeviceEventHandler.ts +211 -0
- package/src/events/DeviceEventTransformerFactory.ts +77 -0
- package/src/{device/local/events → events}/EventHandler.ts +19 -15
- package/src/events/EventHandlerOrchestrator.ts +119 -0
- package/src/events/EventProcessingService.ts +248 -0
- package/src/events/InternalEventSubscription.ts +219 -0
- package/src/events/index.ts +9 -0
- package/src/events/interfaces/DeviceEvent.ts +56 -0
- package/src/events/interfaces/IEventHandler.ts +28 -0
- package/src/events/interfaces/IEventTransformer.ts +8 -0
- package/src/events/interfaces/IInternalEvent.ts +47 -0
- package/src/events/interfaces/index.ts +4 -0
- package/src/index.ts +9 -2
- package/src/types/alert.types.ts +64 -0
- package/src/types/config.types.ts +17 -4
- package/src/types/index.ts +2 -0
- package/src/types/issue.types.ts +98 -0
- package/src/utils/http-utils.ts +143 -0
- package/src/device/local/events/index.ts +0 -2
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CreateIssueData,
|
|
3
|
+
IssuesCategory,
|
|
4
|
+
IssuePriority,
|
|
5
|
+
EntityType,
|
|
6
|
+
} from "../../../types/issue.types";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* IssueBuilder - A builder pattern implementation for constructing CreateIssueData objects
|
|
10
|
+
*
|
|
11
|
+
* This builder provides a fluent interface for creating issue data with proper validation
|
|
12
|
+
* and default values. It follows the Builder pattern which is a standard design pattern
|
|
13
|
+
* in TypeScript for constructing complex objects.
|
|
14
|
+
*
|
|
15
|
+
* Usage example:
|
|
16
|
+
* const issueData = new IssueBuilder()
|
|
17
|
+
* .setCategory(IssuesCategory.OPERATIONS)
|
|
18
|
+
* .setPropertyId("prop123")
|
|
19
|
+
* .setTitle("Device Maintenance Required")
|
|
20
|
+
* .setDescription("Device requires scheduled maintenance")
|
|
21
|
+
* .setEntityId("device456")
|
|
22
|
+
* .setEntityType(EntityType.DEVICE)
|
|
23
|
+
* .setPriority(IssuePriority.HIGH)
|
|
24
|
+
* .setAssignedTo("tech789")
|
|
25
|
+
* .setCreatedBy("user123")
|
|
26
|
+
* .setDueDate(new Date("2024-01-15"))
|
|
27
|
+
* .build();
|
|
28
|
+
*/
|
|
29
|
+
export class IssueBuilder {
|
|
30
|
+
private data: Partial<CreateIssueData> = {};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Sets the issue category
|
|
34
|
+
*/
|
|
35
|
+
setCategory(category: IssuesCategory): IssueBuilder {
|
|
36
|
+
this.data.category = category;
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Sets the property ID
|
|
42
|
+
*/
|
|
43
|
+
setPropertyId(propertyId: string): IssueBuilder {
|
|
44
|
+
if (!propertyId || propertyId.trim() === "") {
|
|
45
|
+
throw new Error("Property ID is required and cannot be empty");
|
|
46
|
+
}
|
|
47
|
+
this.data.propertyId = propertyId;
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Sets the issue title
|
|
53
|
+
*/
|
|
54
|
+
setTitle(title: string): IssueBuilder {
|
|
55
|
+
if (!title || title.trim() === "") {
|
|
56
|
+
throw new Error("Title is required and cannot be empty");
|
|
57
|
+
}
|
|
58
|
+
this.data.title = title.trim();
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Sets the issue description
|
|
64
|
+
*/
|
|
65
|
+
setDescription(description: string): IssueBuilder {
|
|
66
|
+
if (!description || description.trim() === "") {
|
|
67
|
+
throw new Error("Description is required and cannot be empty");
|
|
68
|
+
}
|
|
69
|
+
this.data.description = description.trim();
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Sets the entity ID (optional)
|
|
75
|
+
*/
|
|
76
|
+
setEntityId(entityId?: string): IssueBuilder {
|
|
77
|
+
if (entityId !== undefined) {
|
|
78
|
+
this.data.entityId = entityId.trim() || undefined;
|
|
79
|
+
}
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Sets the entity type
|
|
85
|
+
*/
|
|
86
|
+
setEntityType(entityType: EntityType): IssueBuilder {
|
|
87
|
+
this.data.entityType = entityType;
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Sets the issue priority (optional, defaults to MEDIUM)
|
|
93
|
+
*/
|
|
94
|
+
setPriority(priority?: IssuePriority): IssueBuilder {
|
|
95
|
+
if (priority !== undefined) {
|
|
96
|
+
this.data.priority = priority;
|
|
97
|
+
}
|
|
98
|
+
return this;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Sets the user assigned to the issue (optional)
|
|
103
|
+
*/
|
|
104
|
+
setAssignedTo(assignedTo?: string): IssueBuilder {
|
|
105
|
+
if (assignedTo !== undefined) {
|
|
106
|
+
this.data.assignedTo = assignedTo.trim() || undefined;
|
|
107
|
+
}
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Sets the user who created the issue (required)
|
|
113
|
+
*/
|
|
114
|
+
setCreatedBy(createdBy: string): IssueBuilder {
|
|
115
|
+
if (!createdBy || createdBy.trim() === "") {
|
|
116
|
+
throw new Error("Created by user is required and cannot be empty");
|
|
117
|
+
}
|
|
118
|
+
this.data.createdBy = createdBy.trim();
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Sets the due date (optional)
|
|
124
|
+
*/
|
|
125
|
+
setDueDate(dueDate?: Date): IssueBuilder {
|
|
126
|
+
if (dueDate !== undefined) {
|
|
127
|
+
this.data.dueDate = dueDate;
|
|
128
|
+
}
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Validates that all required fields are present
|
|
134
|
+
*/
|
|
135
|
+
private validate(): void {
|
|
136
|
+
const requiredFields = ["category", "propertyId", "title", "description", "entityType", "createdBy"];
|
|
137
|
+
const missingFields = requiredFields.filter(field => !this.data[field as keyof CreateIssueData]);
|
|
138
|
+
|
|
139
|
+
if (missingFields.length > 0) {
|
|
140
|
+
throw new Error(`Missing required fields: ${missingFields.join(", ")}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Builds and returns the CreateIssueData object
|
|
146
|
+
* @throws Error if required fields are missing
|
|
147
|
+
*/
|
|
148
|
+
build(): CreateIssueData {
|
|
149
|
+
this.validate();
|
|
150
|
+
|
|
151
|
+
// Set default priority if not provided
|
|
152
|
+
if (!this.data.priority) {
|
|
153
|
+
this.data.priority = IssuePriority.MEDIUM;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return this.data as CreateIssueData;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Resets the builder to its initial state
|
|
161
|
+
*/
|
|
162
|
+
reset(): IssueBuilder {
|
|
163
|
+
this.data = {};
|
|
164
|
+
return this;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Creates a new builder instance with predefined values for common issue types
|
|
169
|
+
*/
|
|
170
|
+
static createReadinessIssue(): IssueBuilder {
|
|
171
|
+
return new IssueBuilder()
|
|
172
|
+
.setCategory(IssuesCategory.READINESS)
|
|
173
|
+
.setPriority(IssuePriority.MEDIUM);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
static createOperationsIssue(): IssueBuilder {
|
|
177
|
+
return new IssueBuilder()
|
|
178
|
+
.setCategory(IssuesCategory.OPERATIONS)
|
|
179
|
+
.setPriority(IssuePriority.HIGH);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
static createSecurityIssue(): IssueBuilder {
|
|
183
|
+
return new IssueBuilder()
|
|
184
|
+
.setCategory(IssuesCategory.SECURITY)
|
|
185
|
+
.setPriority(IssuePriority.CRITICAL);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
static createEnergyIssue(): IssueBuilder {
|
|
189
|
+
return new IssueBuilder()
|
|
190
|
+
.setCategory(IssuesCategory.ENERGY)
|
|
191
|
+
.setPriority(IssuePriority.LOW);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Creates a device-specific issue builder
|
|
196
|
+
*/
|
|
197
|
+
static createDeviceIssue(deviceId: string, propertyId: string): IssueBuilder {
|
|
198
|
+
return new IssueBuilder()
|
|
199
|
+
.setEntityType(EntityType.DEVICE)
|
|
200
|
+
.setEntityId(deviceId)
|
|
201
|
+
.setPropertyId(propertyId);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Creates a hub-specific issue builder
|
|
206
|
+
*/
|
|
207
|
+
static createHubIssue(hubId: string, propertyId: string): IssueBuilder {
|
|
208
|
+
return new IssueBuilder()
|
|
209
|
+
.setEntityType(EntityType.HUB)
|
|
210
|
+
.setEntityId(hubId)
|
|
211
|
+
.setPropertyId(propertyId);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Creates a user-specific issue builder
|
|
216
|
+
*/
|
|
217
|
+
static createUserIssue(userId: string, propertyId: string): IssueBuilder {
|
|
218
|
+
return new IssueBuilder()
|
|
219
|
+
.setEntityType(EntityType.USER)
|
|
220
|
+
.setEntityId(userId)
|
|
221
|
+
.setPropertyId(propertyId);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Creates a property-specific issue builder
|
|
226
|
+
*/
|
|
227
|
+
static createPropertyIssue(propertyId: string): IssueBuilder {
|
|
228
|
+
return new IssueBuilder()
|
|
229
|
+
.setEntityType(EntityType.PROPERTY)
|
|
230
|
+
.setEntityId(propertyId)
|
|
231
|
+
.setPropertyId(propertyId);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Creates a maintenance issue builder
|
|
236
|
+
*/
|
|
237
|
+
static createMaintenanceIssue(propertyId: string, entityId?: string, entityType?: EntityType): IssueBuilder {
|
|
238
|
+
const builder = new IssueBuilder()
|
|
239
|
+
.setCategory(IssuesCategory.READINESS)
|
|
240
|
+
.setPropertyId(propertyId)
|
|
241
|
+
.setPriority(IssuePriority.MEDIUM);
|
|
242
|
+
|
|
243
|
+
if (entityId) builder.setEntityId(entityId);
|
|
244
|
+
if (entityType) builder.setEntityType(entityType);
|
|
245
|
+
|
|
246
|
+
return builder;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Creates an urgent issue builder
|
|
251
|
+
*/
|
|
252
|
+
static createUrgentIssue(propertyId: string, entityId?: string, entityType?: EntityType): IssueBuilder {
|
|
253
|
+
const builder = new IssueBuilder()
|
|
254
|
+
.setCategory(IssuesCategory.OPERATIONS)
|
|
255
|
+
.setPropertyId(propertyId)
|
|
256
|
+
.setPriority(IssuePriority.URGENT);
|
|
257
|
+
|
|
258
|
+
if (entityId) builder.setEntityId(entityId);
|
|
259
|
+
if (entityType) builder.setEntityType(entityType);
|
|
260
|
+
|
|
261
|
+
return builder;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# AlertBuilder - Builder Pattern Implementation
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
The `AlertBuilder` is a TypeScript implementation of the Builder design pattern, specifically designed to help construct `CreateAlertData` objects with a fluent interface. This is a standard and widely-used design pattern in TypeScript for creating complex objects with optional parameters.
|
|
6
|
+
|
|
7
|
+
## Why Use the Builder Pattern?
|
|
8
|
+
|
|
9
|
+
The Builder pattern is particularly useful for:
|
|
10
|
+
|
|
11
|
+
1. **Complex Object Construction**: When creating objects with many optional parameters
|
|
12
|
+
2. **Fluent Interface**: Provides a readable, chainable API
|
|
13
|
+
3. **Validation**: Ensures required fields are provided before object creation
|
|
14
|
+
4. **Default Values**: Automatically sets sensible defaults
|
|
15
|
+
5. **Type Safety**: Provides compile-time type checking
|
|
16
|
+
|
|
17
|
+
## Usage Examples
|
|
18
|
+
|
|
19
|
+
### Basic Usage
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { AlertBuilder } from "./AlertBuilder";
|
|
23
|
+
import { AlertCategory, AlertSeverity, EntityType } from "../../../types/alert.types";
|
|
24
|
+
|
|
25
|
+
const alertData = new AlertBuilder()
|
|
26
|
+
.setCategory(AlertCategory.OPERATIONS)
|
|
27
|
+
.setPropertyId("prop123")
|
|
28
|
+
.setTitle("Device Offline")
|
|
29
|
+
.setDescription("Device has been offline for more than 5 minutes")
|
|
30
|
+
.setEntityId("device456")
|
|
31
|
+
.setEntityType(EntityType.DEVICE)
|
|
32
|
+
.setSeverity(AlertSeverity.HIGH)
|
|
33
|
+
.setCreatedBy("user789")
|
|
34
|
+
.build();
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Using Static Factory Methods
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
// Create a device-specific alert
|
|
41
|
+
const deviceAlert = AlertBuilder.createDeviceAlert("device123", "prop456")
|
|
42
|
+
.setCategory(AlertCategory.READINESS)
|
|
43
|
+
.setTitle("Device Maintenance Required")
|
|
44
|
+
.setDescription("Device firmware update is available")
|
|
45
|
+
.setSeverity(AlertSeverity.MEDIUM)
|
|
46
|
+
.setCreatedBy("system")
|
|
47
|
+
.build();
|
|
48
|
+
|
|
49
|
+
// Create a hub-specific alert
|
|
50
|
+
const hubAlert = AlertBuilder.createHubAlert("hub789", "prop202")
|
|
51
|
+
.setCategory(AlertCategory.OPERATIONS)
|
|
52
|
+
.setTitle("Hub Connection Lost")
|
|
53
|
+
.setDescription("Hub has lost connection to the network")
|
|
54
|
+
.setSeverity(AlertSeverity.CRITICAL)
|
|
55
|
+
.setCreatedBy("network-monitor")
|
|
56
|
+
.build();
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Using Predefined Alert Types
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
// Security alert with CRITICAL severity
|
|
63
|
+
const securityAlert = AlertBuilder.createSecurityAlert()
|
|
64
|
+
.setPropertyId("prop789")
|
|
65
|
+
.setTitle("Unauthorized Access Attempt")
|
|
66
|
+
.setDescription("Multiple failed login attempts detected")
|
|
67
|
+
.setEntityId("user123")
|
|
68
|
+
.setEntityType(EntityType.USER)
|
|
69
|
+
.setCreatedBy("security-system")
|
|
70
|
+
.build();
|
|
71
|
+
|
|
72
|
+
// Energy alert with LOW severity
|
|
73
|
+
const energyAlert = AlertBuilder.createEnergyAlert()
|
|
74
|
+
.setPropertyId("prop101")
|
|
75
|
+
.setTitle("High Energy Consumption")
|
|
76
|
+
.setDescription("Energy usage is 20% above normal levels")
|
|
77
|
+
.setEntityId("zone456")
|
|
78
|
+
.setEntityType(EntityType.COLLECTION)
|
|
79
|
+
.setCreatedBy("energy-monitor")
|
|
80
|
+
.build();
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Reusing Builder Instances
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
const builder = new AlertBuilder()
|
|
87
|
+
.setPropertyId("prop404")
|
|
88
|
+
.setCreatedBy("system");
|
|
89
|
+
|
|
90
|
+
const alert1 = builder
|
|
91
|
+
.setCategory(AlertCategory.OPERATIONS)
|
|
92
|
+
.setTitle("Device Temperature High")
|
|
93
|
+
.setDescription("Device temperature exceeds normal operating range")
|
|
94
|
+
.setEntityId("device789")
|
|
95
|
+
.setEntityType(EntityType.DEVICE)
|
|
96
|
+
.setSeverity(AlertSeverity.HIGH)
|
|
97
|
+
.build();
|
|
98
|
+
|
|
99
|
+
const alert2 = builder
|
|
100
|
+
.reset()
|
|
101
|
+
.setPropertyId("prop404")
|
|
102
|
+
.setCategory(AlertCategory.ENERGY)
|
|
103
|
+
.setTitle("Low Battery Warning")
|
|
104
|
+
.setDescription("Device battery level is below 20%")
|
|
105
|
+
.setEntityId("device789")
|
|
106
|
+
.setEntityType(EntityType.DEVICE)
|
|
107
|
+
.setSeverity(AlertSeverity.MEDIUM)
|
|
108
|
+
.setCreatedBy("system")
|
|
109
|
+
.build();
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Available Methods
|
|
113
|
+
|
|
114
|
+
### Instance Methods
|
|
115
|
+
|
|
116
|
+
- `setCategory(category: AlertCategory)`: Sets the alert category
|
|
117
|
+
- `setPropertyId(propertyId: string)`: Sets the property ID (required)
|
|
118
|
+
- `setTitle(title: string)`: Sets the alert title (required)
|
|
119
|
+
- `setDescription(description: string)`: Sets the alert description (required)
|
|
120
|
+
- `setEntityId(entityId?: string)`: Sets the entity ID (optional)
|
|
121
|
+
- `setEntityType(entityType: EntityType)`: Sets the entity type (required)
|
|
122
|
+
- `setSeverity(severity?: AlertSeverity)`: Sets the alert severity (optional, defaults to MEDIUM)
|
|
123
|
+
- `setCreatedBy(createdBy?: string)`: Sets the user who created the alert (optional)
|
|
124
|
+
- `setSnoozeUntil(snoozeUntil?: Date)`: Sets the snooze until date (optional)
|
|
125
|
+
- `build()`: Builds and returns the CreateAlertData object
|
|
126
|
+
- `reset()`: Resets the builder to its initial state
|
|
127
|
+
|
|
128
|
+
### Static Factory Methods
|
|
129
|
+
|
|
130
|
+
- `createReadinessAlert()`: Creates a builder with READINESS category and MEDIUM severity
|
|
131
|
+
- `createOperationsAlert()`: Creates a builder with OPERATIONS category and HIGH severity
|
|
132
|
+
- `createSecurityAlert()`: Creates a builder with SECURITY category and CRITICAL severity
|
|
133
|
+
- `createEnergyAlert()`: Creates a builder with ENERGY category and LOW severity
|
|
134
|
+
- `createDeviceAlert(deviceId: string, propertyId: string)`: Creates a device-specific alert builder
|
|
135
|
+
- `createHubAlert(hubId: string, propertyId: string)`: Creates a hub-specific alert builder
|
|
136
|
+
|
|
137
|
+
## Validation
|
|
138
|
+
|
|
139
|
+
The builder automatically validates that all required fields are present when `build()` is called. Required fields are:
|
|
140
|
+
|
|
141
|
+
- `category`
|
|
142
|
+
- `propertyId`
|
|
143
|
+
- `title`
|
|
144
|
+
- `description`
|
|
145
|
+
- `entityType`
|
|
146
|
+
|
|
147
|
+
If any required field is missing, an error will be thrown with details about which fields are missing.
|
|
148
|
+
|
|
149
|
+
## Default Values
|
|
150
|
+
|
|
151
|
+
- `severity`: Defaults to `AlertSeverity.MEDIUM` if not specified
|
|
152
|
+
|
|
153
|
+
## Error Handling
|
|
154
|
+
|
|
155
|
+
The builder includes comprehensive error handling:
|
|
156
|
+
|
|
157
|
+
- Validates required fields before building
|
|
158
|
+
- Trims whitespace from string inputs
|
|
159
|
+
- Validates that propertyId and title are not empty strings
|
|
160
|
+
- Provides clear error messages for missing or invalid data
|
|
161
|
+
|
|
162
|
+
## Design Pattern Benefits
|
|
163
|
+
|
|
164
|
+
This implementation follows the Builder pattern which provides:
|
|
165
|
+
|
|
166
|
+
1. **Separation of Concerns**: Object construction is separated from object representation
|
|
167
|
+
2. **Fluent Interface**: Method chaining for better readability
|
|
168
|
+
3. **Immutability**: Each method returns a new builder instance
|
|
169
|
+
4. **Validation**: Built-in validation before object creation
|
|
170
|
+
5. **Flexibility**: Easy to add new fields or modify existing ones
|
|
171
|
+
6. **Type Safety**: Full TypeScript support with compile-time checking
|
|
172
|
+
|
|
173
|
+
This is indeed a standard design pattern in TypeScript and is widely used in enterprise applications for constructing complex objects with many optional parameters.
|
|
@@ -37,18 +37,20 @@ export interface IDevice {
|
|
|
37
37
|
id: string;
|
|
38
38
|
type: string;
|
|
39
39
|
};
|
|
40
|
-
status:
|
|
41
|
-
online: boolean;
|
|
42
|
-
error?: {
|
|
43
|
-
type?: string;
|
|
44
|
-
message?: string;
|
|
45
|
-
default?: object;
|
|
46
|
-
};
|
|
47
|
-
lastUpdated?: string;
|
|
48
|
-
};
|
|
40
|
+
status: INewStatus;
|
|
49
41
|
state?: object;
|
|
50
42
|
metaData?: object;
|
|
51
43
|
createdAt?: Date;
|
|
52
44
|
updatedAt?: Date;
|
|
53
45
|
hubDeviceDetails?: IDevice[];
|
|
54
46
|
}
|
|
47
|
+
|
|
48
|
+
export class INewStatus {
|
|
49
|
+
online: boolean = false;
|
|
50
|
+
error?: {
|
|
51
|
+
type?: string;
|
|
52
|
+
message?: string;
|
|
53
|
+
default?: object;
|
|
54
|
+
};
|
|
55
|
+
lastUpdated?: string;
|
|
56
|
+
}
|