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,64 @@
|
|
|
1
|
+
import { EntityType } from "./issue.types";
|
|
2
|
+
|
|
3
|
+
export enum AlertCategory {
|
|
4
|
+
READINESS = "READINESS",
|
|
5
|
+
OPERATIONS = "OPERATIONS",
|
|
6
|
+
SECURITY = "SECURITY",
|
|
7
|
+
ENERGY = "ENERGY",
|
|
8
|
+
OTHER = "OTHER",
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export enum AlertSeverity {
|
|
12
|
+
INFO = "INFO",
|
|
13
|
+
LOW = "LOW",
|
|
14
|
+
MEDIUM = "MEDIUM",
|
|
15
|
+
HIGH = "HIGH",
|
|
16
|
+
CRITICAL = "CRITICAL",
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface AlertDocument {
|
|
20
|
+
_id: string;
|
|
21
|
+
category: AlertCategory;
|
|
22
|
+
propertyId: string;
|
|
23
|
+
title: string;
|
|
24
|
+
description: string;
|
|
25
|
+
entityId?: string;
|
|
26
|
+
entityType: EntityType;
|
|
27
|
+
severity: AlertSeverity;
|
|
28
|
+
isRead: boolean;
|
|
29
|
+
isActive: boolean;
|
|
30
|
+
isDeleted: boolean;
|
|
31
|
+
snoozeUntil?: Date;
|
|
32
|
+
createdBy?: string;
|
|
33
|
+
updatedBy?: string;
|
|
34
|
+
createdAt: Date;
|
|
35
|
+
updatedAt: Date;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface CreateAlertData {
|
|
39
|
+
category: AlertCategory;
|
|
40
|
+
propertyId: string;
|
|
41
|
+
title: string;
|
|
42
|
+
description: string;
|
|
43
|
+
entityId?: string;
|
|
44
|
+
entityType: EntityType;
|
|
45
|
+
severity?: AlertSeverity;
|
|
46
|
+
createdBy?: string;
|
|
47
|
+
snoozeUntil?: Date;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface UpdateAlertData {
|
|
51
|
+
category?: AlertCategory;
|
|
52
|
+
title?: string;
|
|
53
|
+
description?: string;
|
|
54
|
+
entityId?: string;
|
|
55
|
+
entityType?: EntityType;
|
|
56
|
+
severity?: AlertSeverity;
|
|
57
|
+
isRead?: boolean;
|
|
58
|
+
isActive?: boolean;
|
|
59
|
+
snoozeUntil?: Date;
|
|
60
|
+
updatedBy?: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Re-export EntityType from issue.types.ts to avoid duplication
|
|
64
|
+
export { EntityType } from "./issue.types";
|
|
@@ -1,8 +1,21 @@
|
|
|
1
|
+
import { IInternalEvent } from "../events/interfaces/IInternalEvent";
|
|
2
|
+
|
|
3
|
+
export interface ILogger {
|
|
4
|
+
info(message: string, ...args: any[]): void;
|
|
5
|
+
warn(message: string, ...args: any[]): void;
|
|
6
|
+
error(message: string, ...args: any[]): void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
type AllowedSource = "ACCESS_SERVICE" | "ADMIN_SERVICE" | "ENERGY_SERVICE";
|
|
10
|
+
|
|
1
11
|
export type DeviceConfig = {
|
|
2
|
-
|
|
3
|
-
|
|
12
|
+
SOURCE: AllowedSource;
|
|
13
|
+
SQS_QUEUE_URL: string;
|
|
4
14
|
DEVICE_SERVICE?: string;
|
|
5
|
-
|
|
15
|
+
ADMIN_SERVICE?: string;
|
|
16
|
+
ACCESS_SERVICE?: string;
|
|
6
17
|
ENERGY_SERVICE?: string;
|
|
7
|
-
|
|
18
|
+
INTERNAL_EVENT_HANDLER: IInternalEvent;
|
|
19
|
+
LOGGER: ILogger;
|
|
20
|
+
[key: string]: any;
|
|
8
21
|
};
|
package/src/types/index.ts
CHANGED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
export enum IssuesCategory {
|
|
2
|
+
READINESS = "READINESS",
|
|
3
|
+
OPERATIONS = "OPERATIONS",
|
|
4
|
+
SECURITY = "SECURITY",
|
|
5
|
+
ENERGY = "ENERGY",
|
|
6
|
+
OTHER = "OTHER",
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export enum EntityType {
|
|
10
|
+
DEVICE = "DEVICE",
|
|
11
|
+
COLLECTION = "COLLECTION",
|
|
12
|
+
USER = "USER",
|
|
13
|
+
INTEGRATION = "INTEGRATION",
|
|
14
|
+
PROPERTY = "PROPERTY",
|
|
15
|
+
HUB = "HUB",
|
|
16
|
+
SCHEDULE = "SCHEDULE",
|
|
17
|
+
ALERT = "ALERT",
|
|
18
|
+
OTHER = "OTHER",
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export enum IssueStatus {
|
|
22
|
+
PENDING = "PENDING",
|
|
23
|
+
IN_PROGRESS = "IN_PROGRESS",
|
|
24
|
+
RESOLVED = "RESOLVED",
|
|
25
|
+
CLOSED = "CLOSED",
|
|
26
|
+
CANCELLED = "CANCELLED",
|
|
27
|
+
ON_HOLD = "ON_HOLD",
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export enum IssuePriority {
|
|
31
|
+
LOW = "LOW",
|
|
32
|
+
MEDIUM = "MEDIUM",
|
|
33
|
+
HIGH = "HIGH",
|
|
34
|
+
CRITICAL = "CRITICAL",
|
|
35
|
+
URGENT = "URGENT",
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface IssueComment {
|
|
39
|
+
id: string;
|
|
40
|
+
userId: string;
|
|
41
|
+
content: string;
|
|
42
|
+
createdAt: Date;
|
|
43
|
+
updatedAt?: Date;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface IssueDocument {
|
|
47
|
+
_id: string;
|
|
48
|
+
category: IssuesCategory;
|
|
49
|
+
propertyId: string;
|
|
50
|
+
title: string;
|
|
51
|
+
description: string;
|
|
52
|
+
entityId?: string;
|
|
53
|
+
entityType: EntityType;
|
|
54
|
+
status: IssueStatus;
|
|
55
|
+
priority: IssuePriority;
|
|
56
|
+
assignedTo?: string;
|
|
57
|
+
createdBy: string;
|
|
58
|
+
updatedBy?: string;
|
|
59
|
+
isDeleted: boolean;
|
|
60
|
+
createdAt: Date;
|
|
61
|
+
updatedAt: Date;
|
|
62
|
+
resolvedAt?: Date;
|
|
63
|
+
dueDate?: Date;
|
|
64
|
+
comments?: IssueComment[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface CreateIssueData {
|
|
68
|
+
category: IssuesCategory;
|
|
69
|
+
propertyId: string;
|
|
70
|
+
title: string;
|
|
71
|
+
description: string;
|
|
72
|
+
entityId?: string;
|
|
73
|
+
entityType: EntityType;
|
|
74
|
+
priority?: IssuePriority;
|
|
75
|
+
assignedTo?: string;
|
|
76
|
+
createdBy: string;
|
|
77
|
+
dueDate?: Date;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface UpdateIssueData {
|
|
81
|
+
category?: IssuesCategory;
|
|
82
|
+
title?: string;
|
|
83
|
+
description?: string;
|
|
84
|
+
entityId?: string;
|
|
85
|
+
entityType?: EntityType;
|
|
86
|
+
status?: IssueStatus;
|
|
87
|
+
priority?: IssuePriority;
|
|
88
|
+
assignedTo?: string;
|
|
89
|
+
updatedBy: string;
|
|
90
|
+
resolvedAt?: Date;
|
|
91
|
+
dueDate?: Date;
|
|
92
|
+
isDeleted?: boolean;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface AddCommentData {
|
|
96
|
+
userId: string;
|
|
97
|
+
content: string;
|
|
98
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { getConfig } from "../config/config";
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Validates if a URL is properly formatted and accessible
|
|
6
|
+
*/
|
|
7
|
+
export function validateServiceUrl(url: string): boolean {
|
|
8
|
+
try {
|
|
9
|
+
const parsedUrl = new URL(url);
|
|
10
|
+
return parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:";
|
|
11
|
+
} catch (error) {
|
|
12
|
+
getConfig().LOGGER.error(`Invalid service URL: ${url}`, error);
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Creates a properly configured axios instance with error handling
|
|
19
|
+
*/
|
|
20
|
+
export function createAxiosInstance(baseURL?: string) {
|
|
21
|
+
const instance = axios.create({
|
|
22
|
+
baseURL,
|
|
23
|
+
timeout: 30000, // 30 seconds timeout
|
|
24
|
+
maxRedirects: 5,
|
|
25
|
+
validateStatus: (status: number) => status < 500, // Don't throw on 4xx errors
|
|
26
|
+
headers: {
|
|
27
|
+
"Content-Type": "application/json",
|
|
28
|
+
"User-Agent": "dt-common-device/1.3.0",
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
// Add request interceptor for logging
|
|
33
|
+
instance.interceptors.request.use(
|
|
34
|
+
(config: any) => {
|
|
35
|
+
const logger = getConfig().LOGGER;
|
|
36
|
+
logger.info(
|
|
37
|
+
`Making request to: ${config.method?.toUpperCase()} ${config.url}`,
|
|
38
|
+
{
|
|
39
|
+
baseURL: config.baseURL,
|
|
40
|
+
timeout: config.timeout,
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
return config;
|
|
44
|
+
},
|
|
45
|
+
(error: any) => {
|
|
46
|
+
getConfig().LOGGER.error("Request interceptor error:", error);
|
|
47
|
+
return Promise.reject(error);
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
// Add response interceptor for error handling
|
|
52
|
+
instance.interceptors.response.use(
|
|
53
|
+
(response: any) => {
|
|
54
|
+
return response;
|
|
55
|
+
},
|
|
56
|
+
(error: any) => {
|
|
57
|
+
const logger = getConfig().LOGGER;
|
|
58
|
+
const errorInfo = {
|
|
59
|
+
url: error.config?.url,
|
|
60
|
+
method: error.config?.method,
|
|
61
|
+
status: error.response?.status,
|
|
62
|
+
statusText: error.response?.statusText,
|
|
63
|
+
message: error.message,
|
|
64
|
+
code: error.code,
|
|
65
|
+
baseURL: error.config?.baseURL,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
logger.error("HTTP request failed:", errorInfo);
|
|
69
|
+
|
|
70
|
+
// Log additional details for network errors
|
|
71
|
+
if (error.code === "ECONNREFUSED" || error.code === "ENOTFOUND") {
|
|
72
|
+
logger.error("Network connectivity issue detected. Please check:", {
|
|
73
|
+
serviceUrl: error.config?.baseURL,
|
|
74
|
+
errorCode: error.code,
|
|
75
|
+
errorMessage: error.message,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return Promise.reject(error);
|
|
80
|
+
}
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
return instance;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Centralized axios instance for device service
|
|
88
|
+
*/
|
|
89
|
+
let deviceServiceAxiosInstance: any = null;
|
|
90
|
+
|
|
91
|
+
export function getDeviceServiceAxiosInstance(): any {
|
|
92
|
+
if (!deviceServiceAxiosInstance) {
|
|
93
|
+
const { DEVICE_SERVICE } = getConfig();
|
|
94
|
+
if (!DEVICE_SERVICE) {
|
|
95
|
+
throw new Error(
|
|
96
|
+
"DEVICE_SERVICE is not configured. Call initialize() first with DEVICE_SERVICE."
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
deviceServiceAxiosInstance = createAxiosInstance(DEVICE_SERVICE);
|
|
100
|
+
}
|
|
101
|
+
return deviceServiceAxiosInstance;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Retry function for failed HTTP requests
|
|
106
|
+
*/
|
|
107
|
+
export async function retryRequest<T>(
|
|
108
|
+
requestFn: () => Promise<T>,
|
|
109
|
+
maxRetries: number = 3,
|
|
110
|
+
delay: number = 1000
|
|
111
|
+
): Promise<T> {
|
|
112
|
+
let lastError: any;
|
|
113
|
+
|
|
114
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
115
|
+
try {
|
|
116
|
+
return await requestFn();
|
|
117
|
+
} catch (error: any) {
|
|
118
|
+
lastError = error;
|
|
119
|
+
|
|
120
|
+
if (attempt === maxRetries) {
|
|
121
|
+
getConfig().LOGGER.error(
|
|
122
|
+
`Request failed after ${maxRetries} attempts:`,
|
|
123
|
+
error
|
|
124
|
+
);
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
getConfig().LOGGER.warn(
|
|
129
|
+
`Request attempt ${attempt} failed, retrying in ${delay}ms:`,
|
|
130
|
+
{
|
|
131
|
+
error: error.message,
|
|
132
|
+
attempt,
|
|
133
|
+
maxRetries,
|
|
134
|
+
}
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
138
|
+
delay *= 2; // Exponential backoff
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
throw lastError;
|
|
143
|
+
}
|