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.
Files changed (132) hide show
  1. package/TROUBLESHOOTING.md +184 -0
  2. package/dist/config/config.d.ts +9 -2
  3. package/dist/config/config.js +97 -14
  4. package/dist/constants/Event.d.ts +75 -0
  5. package/dist/constants/Event.js +78 -0
  6. package/dist/db/db.d.ts +1 -0
  7. package/dist/db/db.js +18 -2
  8. package/dist/device/local/entities/AlertBuilder.d.ts +87 -0
  9. package/dist/device/local/entities/AlertBuilder.example.d.ts +11 -0
  10. package/dist/device/local/entities/AlertBuilder.example.js +117 -0
  11. package/dist/device/local/entities/AlertBuilder.js +179 -0
  12. package/dist/device/local/entities/IssueBuilder.d.ts +109 -0
  13. package/dist/device/local/entities/IssueBuilder.example.d.ts +16 -0
  14. package/dist/device/local/entities/IssueBuilder.example.js +196 -0
  15. package/dist/device/local/entities/IssueBuilder.js +237 -0
  16. package/dist/device/local/entities/index.d.ts +2 -0
  17. package/dist/device/local/entities/index.js +7 -0
  18. package/dist/device/local/interfaces/IDevice.d.ts +10 -9
  19. package/dist/device/local/interfaces/IDevice.js +7 -0
  20. package/dist/device/local/models/Alert.model.d.ts +28 -0
  21. package/dist/device/local/models/Alert.model.js +222 -0
  22. package/dist/device/local/models/Issue.model.d.ts +28 -0
  23. package/dist/device/local/models/Issue.model.js +260 -0
  24. package/dist/device/local/repository/Alert.repository.d.ts +106 -0
  25. package/dist/device/local/repository/Alert.repository.js +374 -0
  26. package/dist/device/local/repository/Device.repository.d.ts +10 -2
  27. package/dist/device/local/repository/Device.repository.js +153 -30
  28. package/dist/device/local/repository/Hub.repository.d.ts +1 -1
  29. package/dist/device/local/repository/Hub.repository.js +60 -18
  30. package/dist/device/local/repository/Issue.repository.d.ts +113 -0
  31. package/dist/device/local/repository/Issue.repository.js +401 -0
  32. package/dist/device/local/repository/Schedule.repository.d.ts +1 -1
  33. package/dist/device/local/repository/Schedule.repository.js +14 -18
  34. package/dist/device/local/services/Alert.service.d.ts +135 -5
  35. package/dist/device/local/services/Alert.service.js +471 -7
  36. package/dist/device/local/services/AlertService.example.d.ts +55 -0
  37. package/dist/device/local/services/AlertService.example.js +148 -0
  38. package/dist/device/local/services/Device.service.d.ts +8 -5
  39. package/dist/device/local/services/Device.service.js +58 -40
  40. package/dist/device/local/services/Issue.service.d.ts +168 -0
  41. package/dist/device/local/services/Issue.service.js +642 -0
  42. package/dist/device/local/services/IssueService.example.d.ts +68 -0
  43. package/dist/device/local/services/IssueService.example.js +177 -0
  44. package/dist/device/local/services/index.d.ts +7 -5
  45. package/dist/device/local/services/index.js +21 -11
  46. package/dist/events/BaseEventHandler.d.ts +43 -0
  47. package/dist/events/BaseEventHandler.js +111 -0
  48. package/dist/events/BaseEventTransformer.d.ts +26 -0
  49. package/dist/events/BaseEventTransformer.js +72 -0
  50. package/dist/events/DeviceEventHandler.d.ts +15 -0
  51. package/dist/events/DeviceEventHandler.js +152 -0
  52. package/dist/events/DeviceEventTransformerFactory.d.ts +27 -0
  53. package/dist/events/DeviceEventTransformerFactory.js +116 -0
  54. package/dist/events/EventHandler.d.ts +11 -0
  55. package/dist/events/EventHandler.js +106 -0
  56. package/dist/events/EventHandlerOrchestrator.d.ts +35 -0
  57. package/dist/events/EventHandlerOrchestrator.js +141 -0
  58. package/dist/events/EventProcessingService.d.ts +43 -0
  59. package/dist/events/EventProcessingService.js +243 -0
  60. package/dist/events/InternalEventSubscription.d.ts +44 -0
  61. package/dist/events/InternalEventSubscription.js +152 -0
  62. package/dist/events/index.d.ts +9 -0
  63. package/dist/events/index.js +21 -0
  64. package/dist/events/interfaces/DeviceEvent.d.ts +48 -0
  65. package/dist/events/interfaces/DeviceEvent.js +2 -0
  66. package/dist/events/interfaces/IEventHandler.d.ts +23 -0
  67. package/dist/events/interfaces/IEventHandler.js +2 -0
  68. package/dist/events/interfaces/IEventTransformer.d.ts +7 -0
  69. package/dist/events/interfaces/IEventTransformer.js +2 -0
  70. package/dist/events/interfaces/IInternalEvent.d.ts +42 -0
  71. package/dist/events/interfaces/IInternalEvent.js +2 -0
  72. package/dist/events/interfaces/index.d.ts +4 -0
  73. package/dist/events/interfaces/index.js +20 -0
  74. package/dist/index.d.ts +6 -2
  75. package/dist/index.js +9 -2
  76. package/dist/types/alert.types.d.ts +57 -0
  77. package/dist/types/alert.types.js +22 -0
  78. package/dist/types/config.types.d.ts +15 -4
  79. package/dist/types/index.d.ts +2 -0
  80. package/dist/types/index.js +2 -0
  81. package/dist/types/issue.types.d.ts +90 -0
  82. package/dist/types/issue.types.js +40 -0
  83. package/dist/utils/http-utils.d.ts +13 -0
  84. package/dist/utils/http-utils.js +117 -0
  85. package/package.json +2 -1
  86. package/src/config/config.ts +117 -14
  87. package/src/{device/local/events/Events.ts → constants/Event.ts} +34 -13
  88. package/src/db/db.ts +14 -5
  89. package/src/device/local/entities/AlertBuilder.example.ts +126 -0
  90. package/src/device/local/entities/AlertBuilder.ts +202 -0
  91. package/src/device/local/entities/IssueBuilder.example.ts +210 -0
  92. package/src/device/local/entities/IssueBuilder.ts +263 -0
  93. package/src/device/local/entities/README.md +173 -0
  94. package/src/device/local/entities/index.ts +2 -0
  95. package/src/device/local/interfaces/IDevice.ts +11 -9
  96. package/src/device/local/models/Alert.model.md +319 -0
  97. package/src/device/local/models/Alert.model.ts +283 -0
  98. package/src/device/local/models/Issue.model.md +386 -0
  99. package/src/device/local/models/Issue.model.ts +350 -0
  100. package/src/device/local/models/README.md +312 -0
  101. package/src/device/local/repository/Alert.repository.ts +465 -0
  102. package/src/device/local/repository/Device.repository.ts +241 -32
  103. package/src/device/local/repository/Hub.repository.ts +74 -18
  104. package/src/device/local/repository/Issue.repository.ts +517 -0
  105. package/src/device/local/repository/Schedule.repository.ts +28 -22
  106. package/src/device/local/services/Alert.service.ts +617 -5
  107. package/src/device/local/services/AlertService.example.ts +229 -0
  108. package/src/device/local/services/Device.service.ts +70 -50
  109. package/src/device/local/services/Issue.service.ts +872 -0
  110. package/src/device/local/services/IssueService.example.ts +307 -0
  111. package/src/device/local/services/index.ts +7 -5
  112. package/src/events/BaseEventHandler.ts +145 -0
  113. package/src/events/BaseEventTransformer.ts +97 -0
  114. package/src/events/DeviceEventHandler.ts +211 -0
  115. package/src/events/DeviceEventTransformerFactory.ts +77 -0
  116. package/src/{device/local/events → events}/EventHandler.ts +19 -15
  117. package/src/events/EventHandlerOrchestrator.ts +119 -0
  118. package/src/events/EventProcessingService.ts +248 -0
  119. package/src/events/InternalEventSubscription.ts +219 -0
  120. package/src/events/index.ts +9 -0
  121. package/src/events/interfaces/DeviceEvent.ts +56 -0
  122. package/src/events/interfaces/IEventHandler.ts +28 -0
  123. package/src/events/interfaces/IEventTransformer.ts +8 -0
  124. package/src/events/interfaces/IInternalEvent.ts +47 -0
  125. package/src/events/interfaces/index.ts +4 -0
  126. package/src/index.ts +9 -2
  127. package/src/types/alert.types.ts +64 -0
  128. package/src/types/config.types.ts +17 -4
  129. package/src/types/index.ts +2 -0
  130. package/src/types/issue.types.ts +98 -0
  131. package/src/utils/http-utils.ts +143 -0
  132. package/src/device/local/events/index.ts +0 -2
@@ -0,0 +1,237 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IssueBuilder = void 0;
4
+ const issue_types_1 = require("../../../types/issue.types");
5
+ /**
6
+ * IssueBuilder - A builder pattern implementation for constructing CreateIssueData objects
7
+ *
8
+ * This builder provides a fluent interface for creating issue data with proper validation
9
+ * and default values. It follows the Builder pattern which is a standard design pattern
10
+ * in TypeScript for constructing complex objects.
11
+ *
12
+ * Usage example:
13
+ * const issueData = new IssueBuilder()
14
+ * .setCategory(IssuesCategory.OPERATIONS)
15
+ * .setPropertyId("prop123")
16
+ * .setTitle("Device Maintenance Required")
17
+ * .setDescription("Device requires scheduled maintenance")
18
+ * .setEntityId("device456")
19
+ * .setEntityType(EntityType.DEVICE)
20
+ * .setPriority(IssuePriority.HIGH)
21
+ * .setAssignedTo("tech789")
22
+ * .setCreatedBy("user123")
23
+ * .setDueDate(new Date("2024-01-15"))
24
+ * .build();
25
+ */
26
+ class IssueBuilder {
27
+ constructor() {
28
+ this.data = {};
29
+ }
30
+ /**
31
+ * Sets the issue category
32
+ */
33
+ setCategory(category) {
34
+ this.data.category = category;
35
+ return this;
36
+ }
37
+ /**
38
+ * Sets the property ID
39
+ */
40
+ setPropertyId(propertyId) {
41
+ if (!propertyId || propertyId.trim() === "") {
42
+ throw new Error("Property ID is required and cannot be empty");
43
+ }
44
+ this.data.propertyId = propertyId;
45
+ return this;
46
+ }
47
+ /**
48
+ * Sets the issue title
49
+ */
50
+ setTitle(title) {
51
+ if (!title || title.trim() === "") {
52
+ throw new Error("Title is required and cannot be empty");
53
+ }
54
+ this.data.title = title.trim();
55
+ return this;
56
+ }
57
+ /**
58
+ * Sets the issue description
59
+ */
60
+ setDescription(description) {
61
+ if (!description || description.trim() === "") {
62
+ throw new Error("Description is required and cannot be empty");
63
+ }
64
+ this.data.description = description.trim();
65
+ return this;
66
+ }
67
+ /**
68
+ * Sets the entity ID (optional)
69
+ */
70
+ setEntityId(entityId) {
71
+ if (entityId !== undefined) {
72
+ this.data.entityId = entityId.trim() || undefined;
73
+ }
74
+ return this;
75
+ }
76
+ /**
77
+ * Sets the entity type
78
+ */
79
+ setEntityType(entityType) {
80
+ this.data.entityType = entityType;
81
+ return this;
82
+ }
83
+ /**
84
+ * Sets the issue priority (optional, defaults to MEDIUM)
85
+ */
86
+ setPriority(priority) {
87
+ if (priority !== undefined) {
88
+ this.data.priority = priority;
89
+ }
90
+ return this;
91
+ }
92
+ /**
93
+ * Sets the user assigned to the issue (optional)
94
+ */
95
+ setAssignedTo(assignedTo) {
96
+ if (assignedTo !== undefined) {
97
+ this.data.assignedTo = assignedTo.trim() || undefined;
98
+ }
99
+ return this;
100
+ }
101
+ /**
102
+ * Sets the user who created the issue (required)
103
+ */
104
+ setCreatedBy(createdBy) {
105
+ if (!createdBy || createdBy.trim() === "") {
106
+ throw new Error("Created by user is required and cannot be empty");
107
+ }
108
+ this.data.createdBy = createdBy.trim();
109
+ return this;
110
+ }
111
+ /**
112
+ * Sets the due date (optional)
113
+ */
114
+ setDueDate(dueDate) {
115
+ if (dueDate !== undefined) {
116
+ this.data.dueDate = dueDate;
117
+ }
118
+ return this;
119
+ }
120
+ /**
121
+ * Validates that all required fields are present
122
+ */
123
+ validate() {
124
+ const requiredFields = ["category", "propertyId", "title", "description", "entityType", "createdBy"];
125
+ const missingFields = requiredFields.filter(field => !this.data[field]);
126
+ if (missingFields.length > 0) {
127
+ throw new Error(`Missing required fields: ${missingFields.join(", ")}`);
128
+ }
129
+ }
130
+ /**
131
+ * Builds and returns the CreateIssueData object
132
+ * @throws Error if required fields are missing
133
+ */
134
+ build() {
135
+ this.validate();
136
+ // Set default priority if not provided
137
+ if (!this.data.priority) {
138
+ this.data.priority = issue_types_1.IssuePriority.MEDIUM;
139
+ }
140
+ return this.data;
141
+ }
142
+ /**
143
+ * Resets the builder to its initial state
144
+ */
145
+ reset() {
146
+ this.data = {};
147
+ return this;
148
+ }
149
+ /**
150
+ * Creates a new builder instance with predefined values for common issue types
151
+ */
152
+ static createReadinessIssue() {
153
+ return new IssueBuilder()
154
+ .setCategory(issue_types_1.IssuesCategory.READINESS)
155
+ .setPriority(issue_types_1.IssuePriority.MEDIUM);
156
+ }
157
+ static createOperationsIssue() {
158
+ return new IssueBuilder()
159
+ .setCategory(issue_types_1.IssuesCategory.OPERATIONS)
160
+ .setPriority(issue_types_1.IssuePriority.HIGH);
161
+ }
162
+ static createSecurityIssue() {
163
+ return new IssueBuilder()
164
+ .setCategory(issue_types_1.IssuesCategory.SECURITY)
165
+ .setPriority(issue_types_1.IssuePriority.CRITICAL);
166
+ }
167
+ static createEnergyIssue() {
168
+ return new IssueBuilder()
169
+ .setCategory(issue_types_1.IssuesCategory.ENERGY)
170
+ .setPriority(issue_types_1.IssuePriority.LOW);
171
+ }
172
+ /**
173
+ * Creates a device-specific issue builder
174
+ */
175
+ static createDeviceIssue(deviceId, propertyId) {
176
+ return new IssueBuilder()
177
+ .setEntityType(issue_types_1.EntityType.DEVICE)
178
+ .setEntityId(deviceId)
179
+ .setPropertyId(propertyId);
180
+ }
181
+ /**
182
+ * Creates a hub-specific issue builder
183
+ */
184
+ static createHubIssue(hubId, propertyId) {
185
+ return new IssueBuilder()
186
+ .setEntityType(issue_types_1.EntityType.HUB)
187
+ .setEntityId(hubId)
188
+ .setPropertyId(propertyId);
189
+ }
190
+ /**
191
+ * Creates a user-specific issue builder
192
+ */
193
+ static createUserIssue(userId, propertyId) {
194
+ return new IssueBuilder()
195
+ .setEntityType(issue_types_1.EntityType.USER)
196
+ .setEntityId(userId)
197
+ .setPropertyId(propertyId);
198
+ }
199
+ /**
200
+ * Creates a property-specific issue builder
201
+ */
202
+ static createPropertyIssue(propertyId) {
203
+ return new IssueBuilder()
204
+ .setEntityType(issue_types_1.EntityType.PROPERTY)
205
+ .setEntityId(propertyId)
206
+ .setPropertyId(propertyId);
207
+ }
208
+ /**
209
+ * Creates a maintenance issue builder
210
+ */
211
+ static createMaintenanceIssue(propertyId, entityId, entityType) {
212
+ const builder = new IssueBuilder()
213
+ .setCategory(issue_types_1.IssuesCategory.READINESS)
214
+ .setPropertyId(propertyId)
215
+ .setPriority(issue_types_1.IssuePriority.MEDIUM);
216
+ if (entityId)
217
+ builder.setEntityId(entityId);
218
+ if (entityType)
219
+ builder.setEntityType(entityType);
220
+ return builder;
221
+ }
222
+ /**
223
+ * Creates an urgent issue builder
224
+ */
225
+ static createUrgentIssue(propertyId, entityId, entityType) {
226
+ const builder = new IssueBuilder()
227
+ .setCategory(issue_types_1.IssuesCategory.OPERATIONS)
228
+ .setPropertyId(propertyId)
229
+ .setPriority(issue_types_1.IssuePriority.URGENT);
230
+ if (entityId)
231
+ builder.setEntityId(entityId);
232
+ if (entityType)
233
+ builder.setEntityType(entityType);
234
+ return builder;
235
+ }
236
+ }
237
+ exports.IssueBuilder = IssueBuilder;
@@ -0,0 +1,2 @@
1
+ export { AlertBuilder } from "./AlertBuilder";
2
+ export { IssueBuilder } from "./IssueBuilder";
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IssueBuilder = exports.AlertBuilder = void 0;
4
+ var AlertBuilder_1 = require("./AlertBuilder");
5
+ Object.defineProperty(exports, "AlertBuilder", { enumerable: true, get: function () { return AlertBuilder_1.AlertBuilder; } });
6
+ var IssueBuilder_1 = require("./IssueBuilder");
7
+ Object.defineProperty(exports, "IssueBuilder", { enumerable: true, get: function () { return IssueBuilder_1.IssueBuilder; } });
@@ -35,18 +35,19 @@ export interface IDevice {
35
35
  id: string;
36
36
  type: string;
37
37
  };
38
- status: {
39
- online: boolean;
40
- error?: {
41
- type?: string;
42
- message?: string;
43
- default?: object;
44
- };
45
- lastUpdated?: string;
46
- };
38
+ status: INewStatus;
47
39
  state?: object;
48
40
  metaData?: object;
49
41
  createdAt?: Date;
50
42
  updatedAt?: Date;
51
43
  hubDeviceDetails?: IDevice[];
52
44
  }
45
+ export declare class INewStatus {
46
+ online: boolean;
47
+ error?: {
48
+ type?: string;
49
+ message?: string;
50
+ default?: object;
51
+ };
52
+ lastUpdated?: string;
53
+ }
@@ -1,3 +1,10 @@
1
1
  "use strict";
2
2
  // Interfaces for DeviceService methods
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.INewStatus = void 0;
5
+ class INewStatus {
6
+ constructor() {
7
+ this.online = false;
8
+ }
9
+ }
10
+ exports.INewStatus = INewStatus;
@@ -0,0 +1,28 @@
1
+ import mongoose, { Model } from "mongoose";
2
+ import { AlertCategory, AlertSeverity, EntityType, AlertDocument as IAlertDocument, CreateAlertData, UpdateAlertData } from "../../../types/alert.types";
3
+ interface IAlertMethods {
4
+ markAsRead(updatedBy: string): void;
5
+ markAsUnread(updatedBy: string): void;
6
+ activate(updatedBy: string): void;
7
+ deactivate(updatedBy: string): void;
8
+ snooze(until: Date, updatedBy: string): void;
9
+ unsnooze(updatedBy: string): void;
10
+ }
11
+ interface IAlertModel extends Model<IAlertDocument, {}, IAlertMethods> {
12
+ findByProperty(propertyId: string, includeDeleted?: boolean): Promise<IAlertDocument[]>;
13
+ findByEntity(entityId: string, entityType: EntityType, includeDeleted?: boolean): Promise<IAlertDocument[]>;
14
+ findByCategory(category: AlertCategory, includeDeleted?: boolean): Promise<IAlertDocument[]>;
15
+ findBySeverity(severity: AlertSeverity, includeDeleted?: boolean): Promise<IAlertDocument[]>;
16
+ findActive(includeDeleted?: boolean): Promise<IAlertDocument[]>;
17
+ findUnread(includeDeleted?: boolean): Promise<IAlertDocument[]>;
18
+ findSnoozed(includeDeleted?: boolean): Promise<IAlertDocument[]>;
19
+ findExpiredSnooze(includeDeleted?: boolean): Promise<IAlertDocument[]>;
20
+ }
21
+ declare const AlertSchema: mongoose.Schema<IAlertDocument, IAlertModel, IAlertMethods, {}, {}, {}, mongoose.DefaultSchemaOptions, IAlertDocument, mongoose.Document<unknown, {}, mongoose.FlatRecord<IAlertDocument>, {}> & Omit<mongoose.FlatRecord<IAlertDocument> & Required<{
22
+ _id: string;
23
+ }> & {
24
+ __v: number;
25
+ }, keyof IAlertMethods> & IAlertMethods>;
26
+ export declare const AlertModel: IAlertModel;
27
+ export { AlertSchema };
28
+ export type { IAlertDocument, CreateAlertData, UpdateAlertData, IAlertMethods, IAlertModel, };
@@ -0,0 +1,222 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.AlertSchema = exports.AlertModel = void 0;
37
+ const mongoose_1 = __importStar(require("mongoose"));
38
+ const alert_types_1 = require("../../../types/alert.types");
39
+ // Main Alert schema
40
+ const AlertSchema = new mongoose_1.Schema({
41
+ category: {
42
+ type: String,
43
+ enum: Object.values(alert_types_1.AlertCategory),
44
+ required: true,
45
+ },
46
+ propertyId: {
47
+ type: String,
48
+ required: true,
49
+ index: true,
50
+ },
51
+ title: {
52
+ type: String,
53
+ required: true,
54
+ trim: true,
55
+ },
56
+ description: {
57
+ type: String,
58
+ required: true,
59
+ trim: true,
60
+ },
61
+ entityId: {
62
+ type: String,
63
+ index: true,
64
+ },
65
+ entityType: {
66
+ type: String,
67
+ enum: Object.values(alert_types_1.EntityType),
68
+ required: true,
69
+ index: true,
70
+ },
71
+ severity: {
72
+ type: String,
73
+ enum: Object.values(alert_types_1.AlertSeverity),
74
+ default: alert_types_1.AlertSeverity.MEDIUM,
75
+ },
76
+ isRead: {
77
+ type: Boolean,
78
+ default: false,
79
+ index: true,
80
+ },
81
+ isActive: {
82
+ type: Boolean,
83
+ default: true,
84
+ index: true,
85
+ },
86
+ isDeleted: {
87
+ type: Boolean,
88
+ default: false,
89
+ },
90
+ snoozeUntil: {
91
+ type: Date,
92
+ },
93
+ createdBy: {
94
+ type: String,
95
+ },
96
+ updatedBy: {
97
+ type: String,
98
+ },
99
+ createdAt: {
100
+ type: Date,
101
+ default: Date.now,
102
+ },
103
+ updatedAt: {
104
+ type: Date,
105
+ default: Date.now,
106
+ },
107
+ }, {
108
+ timestamps: true,
109
+ collection: "dt_alerts",
110
+ });
111
+ exports.AlertSchema = AlertSchema;
112
+ // Compound indexes to match Prisma schema
113
+ AlertSchema.index({ propertyId: 1, isActive: 1, isRead: 1 });
114
+ AlertSchema.index({ entityId: 1, entityType: 1 });
115
+ AlertSchema.index({ createdAt: 1 });
116
+ // Pre-save middleware to update the updatedAt field
117
+ AlertSchema.pre("save", function (next) {
118
+ this.updatedAt = new Date();
119
+ next();
120
+ });
121
+ // Pre-update middleware to update the updatedAt field
122
+ AlertSchema.pre(["updateOne", "findOneAndUpdate", "updateMany"], function (next) {
123
+ this.set({ updatedAt: new Date() });
124
+ next();
125
+ });
126
+ // Instance methods
127
+ AlertSchema.methods.markAsRead = function (updatedBy) {
128
+ this.isRead = true;
129
+ this.updatedBy = updatedBy;
130
+ };
131
+ AlertSchema.methods.markAsUnread = function (updatedBy) {
132
+ this.isRead = false;
133
+ this.updatedBy = updatedBy;
134
+ };
135
+ AlertSchema.methods.activate = function (updatedBy) {
136
+ this.isActive = true;
137
+ this.updatedBy = updatedBy;
138
+ };
139
+ AlertSchema.methods.deactivate = function (updatedBy) {
140
+ this.isActive = false;
141
+ this.updatedBy = updatedBy;
142
+ };
143
+ AlertSchema.methods.snooze = function (until, updatedBy) {
144
+ this.snoozeUntil = until;
145
+ this.updatedBy = updatedBy;
146
+ };
147
+ AlertSchema.methods.unsnooze = function (updatedBy) {
148
+ this.snoozeUntil = undefined;
149
+ this.updatedBy = updatedBy;
150
+ };
151
+ // Static methods
152
+ AlertSchema.statics.findByProperty = function (propertyId, includeDeleted = false) {
153
+ const query = { propertyId };
154
+ if (!includeDeleted) {
155
+ query.isDeleted = false;
156
+ }
157
+ return this.find(query).sort({ createdAt: -1 });
158
+ };
159
+ AlertSchema.statics.findByEntity = function (entityId, entityType, includeDeleted = false) {
160
+ const query = { entityId, entityType };
161
+ if (!includeDeleted) {
162
+ query.isDeleted = false;
163
+ }
164
+ return this.find(query).sort({ createdAt: -1 });
165
+ };
166
+ AlertSchema.statics.findByCategory = function (category, includeDeleted = false) {
167
+ const query = { category };
168
+ if (!includeDeleted) {
169
+ query.isDeleted = false;
170
+ }
171
+ return this.find(query).sort({ createdAt: -1 });
172
+ };
173
+ AlertSchema.statics.findBySeverity = function (severity, includeDeleted = false) {
174
+ const query = { severity };
175
+ if (!includeDeleted) {
176
+ query.isDeleted = false;
177
+ }
178
+ return this.find(query).sort({ severity: -1, createdAt: -1 });
179
+ };
180
+ AlertSchema.statics.findActive = function (includeDeleted = false) {
181
+ const query = { isActive: true };
182
+ if (!includeDeleted) {
183
+ query.isDeleted = false;
184
+ }
185
+ return this.find(query).sort({ severity: -1, createdAt: -1 });
186
+ };
187
+ AlertSchema.statics.findUnread = function (includeDeleted = false) {
188
+ const query = { isRead: false };
189
+ if (!includeDeleted) {
190
+ query.isDeleted = false;
191
+ }
192
+ return this.find(query).sort({ severity: -1, createdAt: -1 });
193
+ };
194
+ AlertSchema.statics.findSnoozed = function (includeDeleted = false) {
195
+ const query = { snoozeUntil: { $exists: true, $ne: null } };
196
+ if (!includeDeleted) {
197
+ query.isDeleted = false;
198
+ }
199
+ return this.find(query).sort({ snoozeUntil: 1 });
200
+ };
201
+ AlertSchema.statics.findExpiredSnooze = function (includeDeleted = false) {
202
+ const query = {
203
+ snoozeUntil: { $lt: new Date() },
204
+ isActive: true,
205
+ };
206
+ if (!includeDeleted) {
207
+ query.isDeleted = false;
208
+ }
209
+ return this.find(query).sort({ snoozeUntil: 1 });
210
+ };
211
+ // Virtual for snooze status
212
+ AlertSchema.virtual("isSnoozed").get(function () {
213
+ return this.snoozeUntil && this.snoozeUntil > new Date();
214
+ });
215
+ AlertSchema.virtual("isSnoozeExpired").get(function () {
216
+ return this.snoozeUntil && this.snoozeUntil <= new Date();
217
+ });
218
+ // Ensure virtuals are serialized
219
+ AlertSchema.set("toJSON", { virtuals: true });
220
+ AlertSchema.set("toObject", { virtuals: true });
221
+ // Create and export the model
222
+ exports.AlertModel = mongoose_1.default.model("Alert", AlertSchema);
@@ -0,0 +1,28 @@
1
+ import mongoose, { Model } from "mongoose";
2
+ import { EntityType, IssueStatus, IssuePriority, IssueDocument as IIssueDocument, CreateIssueData, UpdateIssueData, AddCommentData } from "../../../types/issue.types";
3
+ interface IIssueMethods {
4
+ addComment(commentData: AddCommentData): void;
5
+ updateComment(commentId: string, content: string, userId: string): boolean;
6
+ removeComment(commentId: string): boolean;
7
+ resolve(resolvedBy: string): void;
8
+ reopen(updatedBy: string): void;
9
+ assign(userId: string, assignedBy: string): void;
10
+ unassign(unassignedBy: string): void;
11
+ }
12
+ interface IIssueModel extends Model<IIssueDocument, {}, IIssueMethods> {
13
+ findByProperty(propertyId: string, includeDeleted?: boolean): Promise<IIssueDocument[]>;
14
+ findByAssignee(assignedTo: string, includeDeleted?: boolean): Promise<IIssueDocument[]>;
15
+ findByEntity(entityId: string, entityType: EntityType, includeDeleted?: boolean): Promise<IIssueDocument[]>;
16
+ findByStatus(status: IssueStatus, includeDeleted?: boolean): Promise<IIssueDocument[]>;
17
+ findByPriority(priority: IssuePriority, includeDeleted?: boolean): Promise<IIssueDocument[]>;
18
+ findOverdue(includeDeleted?: boolean): Promise<IIssueDocument[]>;
19
+ findUpcoming(days?: number, includeDeleted?: boolean): Promise<IIssueDocument[]>;
20
+ }
21
+ declare const IssueSchema: mongoose.Schema<IIssueDocument, IIssueModel, IIssueMethods, {}, {}, {}, mongoose.DefaultSchemaOptions, IIssueDocument, mongoose.Document<unknown, {}, mongoose.FlatRecord<IIssueDocument>, {}> & Omit<mongoose.FlatRecord<IIssueDocument> & Required<{
22
+ _id: string;
23
+ }> & {
24
+ __v: number;
25
+ }, keyof IIssueMethods> & IIssueMethods>;
26
+ export declare const IssueModel: IIssueModel;
27
+ export { IssueSchema };
28
+ export type { IIssueDocument, CreateIssueData, UpdateIssueData, AddCommentData, IIssueMethods, IIssueModel, };