dt-common-device 7.8.15 → 7.9.1
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/audit/AuditUtils.js +171 -102
- package/package.json +1 -1
package/dist/audit/AuditUtils.js
CHANGED
|
@@ -78,6 +78,7 @@ const services_1 = require("../entities/device/local/services");
|
|
|
78
78
|
const typedi_1 = __importStar(require("typedi"));
|
|
79
79
|
const pms_1 = require("../entities/pms");
|
|
80
80
|
const admin_1 = require("../entities/admin");
|
|
81
|
+
const config_1 = require("../config/config");
|
|
81
82
|
let AuditUtils = (() => {
|
|
82
83
|
let _classDecorators = [(0, typedi_1.Service)()];
|
|
83
84
|
let _classDescriptor;
|
|
@@ -132,60 +133,74 @@ let AuditUtils = (() => {
|
|
|
132
133
|
return audit;
|
|
133
134
|
}
|
|
134
135
|
async populateAuditFields(audit) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
if (propertyId
|
|
138
|
-
|
|
136
|
+
try {
|
|
137
|
+
const { propertyId, propertyName, userId, userName, guestId, guestName, deviceId, deviceName, zoneId, zoneName, accessGroupId, accessGroupName, scheduleId, } = audit;
|
|
138
|
+
if (propertyId && !propertyName) {
|
|
139
|
+
(0, config_1.getLogger)().info(`Property ID: ${propertyId}`);
|
|
140
|
+
if (propertyId === "triggered_externally") {
|
|
141
|
+
audit.propertyName = "Triggered Externally";
|
|
142
|
+
}
|
|
143
|
+
else if (propertyId === "property_not_found") {
|
|
144
|
+
(0, config_1.getLogger)().info(`Property ID: ${propertyId}`);
|
|
145
|
+
audit.propertyName = "Property Not Found";
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
(0, config_1.getLogger)().info(`Property ID: ${propertyId}`);
|
|
149
|
+
audit.propertyName = await this.getPropertyName(propertyId);
|
|
150
|
+
}
|
|
139
151
|
}
|
|
140
|
-
|
|
141
|
-
audit.
|
|
152
|
+
if (!propertyId && deviceId) {
|
|
153
|
+
audit.propertyId = await this.getFieldFromDevice(deviceId, "propertyId");
|
|
154
|
+
audit.propertyName = await this.getPropertyName(audit.propertyId);
|
|
142
155
|
}
|
|
143
|
-
|
|
144
|
-
audit.
|
|
156
|
+
if (zoneId && !zoneName)
|
|
157
|
+
audit.zoneName = await this.getZoneName(zoneId);
|
|
158
|
+
if (!zoneId && deviceId) {
|
|
159
|
+
audit.zoneId = await this.getFieldFromDevice(deviceId, "zoneId");
|
|
160
|
+
audit.zoneName = await this.getZoneName(audit.zoneId);
|
|
161
|
+
}
|
|
162
|
+
if (userId && !userName)
|
|
163
|
+
audit.userName = await this.getUserName(userId);
|
|
164
|
+
if (guestId && !guestName)
|
|
165
|
+
audit.guestName = await this.getGuestName(guestId);
|
|
166
|
+
if (deviceId && !deviceName) {
|
|
167
|
+
audit.deviceName = await this.getDeviceName(deviceId);
|
|
168
|
+
}
|
|
169
|
+
if (accessGroupId && !accessGroupName)
|
|
170
|
+
audit.accessGroupName = await this.getAccessGroupName(accessGroupId);
|
|
171
|
+
if (scheduleId) {
|
|
172
|
+
const { scheduleStartDate, scheduleEndDate, scheduleDuration, scheduleSource, scheduleStatus, } = await this.getScheduleDetails(scheduleId);
|
|
173
|
+
audit.scheduleStartDate = scheduleStartDate;
|
|
174
|
+
audit.scheduleEndDate = scheduleEndDate;
|
|
175
|
+
audit.scheduleDuration = scheduleDuration;
|
|
176
|
+
audit.scheduleSource = scheduleSource;
|
|
177
|
+
audit.scheduleStatus = scheduleStatus;
|
|
145
178
|
}
|
|
146
179
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
audit.propertyName = await this.getPropertyName(audit.propertyId);
|
|
150
|
-
}
|
|
151
|
-
if (zoneId && !zoneName)
|
|
152
|
-
audit.zoneName = await this.getZoneName(zoneId);
|
|
153
|
-
if (!zoneId && deviceId) {
|
|
154
|
-
audit.zoneId = await this.getFieldFromDevice(deviceId, "zoneId");
|
|
155
|
-
audit.zoneName = await this.getZoneName(audit.zoneId);
|
|
156
|
-
}
|
|
157
|
-
if (userId && !userName)
|
|
158
|
-
audit.userName = await this.getUserName(userId);
|
|
159
|
-
if (guestId && !guestName)
|
|
160
|
-
audit.guestName = await this.getGuestName(guestId);
|
|
161
|
-
if (deviceId && !deviceName) {
|
|
162
|
-
audit.deviceName = await this.getDeviceName(deviceId);
|
|
163
|
-
}
|
|
164
|
-
if (accessGroupId && !accessGroupName)
|
|
165
|
-
audit.accessGroupName = await this.getAccessGroupName(accessGroupId);
|
|
166
|
-
if (scheduleId) {
|
|
167
|
-
const { scheduleStartDate, scheduleEndDate, scheduleDuration, scheduleSource, scheduleStatus, } = await this.getScheduleDetails(scheduleId);
|
|
168
|
-
audit.scheduleStartDate = scheduleStartDate;
|
|
169
|
-
audit.scheduleEndDate = scheduleEndDate;
|
|
170
|
-
audit.scheduleDuration = scheduleDuration;
|
|
171
|
-
audit.scheduleSource = scheduleSource;
|
|
172
|
-
audit.scheduleStatus = scheduleStatus;
|
|
180
|
+
catch (error) {
|
|
181
|
+
(0, config_1.getLogger)().error(`Error in populateAuditFields: ${error instanceof Error ? error.message : error}`);
|
|
173
182
|
}
|
|
174
183
|
}
|
|
175
184
|
/**
|
|
176
185
|
* Generic utility to get cached entity data from Redis using individual keys
|
|
177
186
|
*/
|
|
178
187
|
async getCachedEntityData(entityType, entityId, fetchFunction) {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
188
|
+
try {
|
|
189
|
+
const cacheKey = `${this.CACHE_PREFIX}:${entityType}:${entityId}`;
|
|
190
|
+
// Try to get from cache first
|
|
191
|
+
const cachedData = await this.redisUtils.get(cacheKey);
|
|
192
|
+
if (cachedData) {
|
|
193
|
+
return JSON.parse(cachedData);
|
|
194
|
+
}
|
|
195
|
+
// If not in cache, fetch from database and cache it
|
|
196
|
+
const entityData = await fetchFunction();
|
|
197
|
+
await this.redisUtils.set(cacheKey, JSON.stringify(entityData), this.CACHE_TTL);
|
|
198
|
+
return entityData;
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
(0, config_1.getLogger)().error(`Error in getCachedEntityData: ${error instanceof Error ? error.message : error}`);
|
|
202
|
+
throw error;
|
|
184
203
|
}
|
|
185
|
-
// If not in cache, fetch from database and cache it
|
|
186
|
-
const entityData = await fetchFunction();
|
|
187
|
-
await this.redisUtils.set(cacheKey, JSON.stringify(entityData), this.CACHE_TTL);
|
|
188
|
-
return entityData;
|
|
189
204
|
}
|
|
190
205
|
/**
|
|
191
206
|
* Utility to calculate schedule duration in days
|
|
@@ -195,85 +210,139 @@ let AuditUtils = (() => {
|
|
|
195
210
|
return Math.ceil(duration / (1000 * 60 * 60 * 24));
|
|
196
211
|
}
|
|
197
212
|
async getPropertyName(propertyId) {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
213
|
+
try {
|
|
214
|
+
return await this.getCachedEntityData("property", propertyId, async () => {
|
|
215
|
+
const property = await typedi_1.default.get(property_1.LocalPropertyService).getProperty(propertyId);
|
|
216
|
+
return property?.name || "property_not_found";
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
catch (error) {
|
|
220
|
+
(0, config_1.getLogger)().error(`Error in getPropertyName: ${error instanceof Error ? error.message : error}`);
|
|
221
|
+
return "";
|
|
222
|
+
}
|
|
202
223
|
}
|
|
203
224
|
async getFieldFromDevice(deviceId, field) {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
225
|
+
try {
|
|
226
|
+
const redisKey = `device:${deviceId}`;
|
|
227
|
+
const redisDevice = await this.redisUtils.hget(redisKey, "device");
|
|
228
|
+
if (redisDevice) {
|
|
229
|
+
return redisDevice[field];
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
const device = await typedi_1.default.get(services_1.LocalDeviceService).getDevice(deviceId);
|
|
233
|
+
await this.redisUtils.hset(redisKey, "device", JSON.stringify(device));
|
|
234
|
+
await this.redisUtils.expire(redisKey, this.CACHE_TTL);
|
|
235
|
+
return device[field];
|
|
236
|
+
}
|
|
208
237
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
await this.redisUtils.expire(redisKey, this.CACHE_TTL);
|
|
213
|
-
return device[field];
|
|
238
|
+
catch (error) {
|
|
239
|
+
(0, config_1.getLogger)().error(`Error in getFieldFromDevice: ${error instanceof Error ? error.message : error}`);
|
|
240
|
+
return "";
|
|
214
241
|
}
|
|
215
242
|
}
|
|
216
243
|
async getUserName(userId) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
244
|
+
try {
|
|
245
|
+
return await this.getCachedEntityData("user", userId, async () => {
|
|
246
|
+
const user = await typedi_1.default.get(admin_1.AdminService).getUser(userId);
|
|
247
|
+
if (!user)
|
|
248
|
+
return "user_not_found";
|
|
249
|
+
return `${user?.firstName || ""} ${user?.lastName || ""}`.trim();
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
catch (error) {
|
|
253
|
+
(0, config_1.getLogger)().error(`Error in getUserName: ${error instanceof Error ? error.message : error}`);
|
|
254
|
+
return "";
|
|
255
|
+
}
|
|
223
256
|
}
|
|
224
257
|
async getGuestName(guestId) {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
258
|
+
try {
|
|
259
|
+
return await this.getCachedEntityData("guest", guestId, async () => {
|
|
260
|
+
const guest = await typedi_1.default.get(pms_1.PmsService).getGuest(guestId);
|
|
261
|
+
if (!guest)
|
|
262
|
+
return "";
|
|
263
|
+
return `${guest?.firstName || ""} ${guest?.lastName || ""}`.trim();
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
(0, config_1.getLogger)().error(`Error in getGuestName: ${error instanceof Error ? error.message : error}`);
|
|
268
|
+
return "";
|
|
269
|
+
}
|
|
231
270
|
}
|
|
232
271
|
async getDeviceName(deviceId) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
272
|
+
try {
|
|
273
|
+
return await this.getCachedEntityData("device", deviceId, async () => {
|
|
274
|
+
const device = await typedi_1.default.get(services_1.LocalDeviceService).querySelect({ deviceId }, ["name"]);
|
|
275
|
+
if (!device)
|
|
276
|
+
return "";
|
|
277
|
+
return device[0]?.name || "";
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
catch (error) {
|
|
281
|
+
(0, config_1.getLogger)().error(`Error in getDeviceName: ${error instanceof Error ? error.message : error}`);
|
|
282
|
+
return "";
|
|
283
|
+
}
|
|
239
284
|
}
|
|
240
285
|
async getZoneName(zoneId) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
286
|
+
try {
|
|
287
|
+
return await this.getCachedEntityData("zone", zoneId, async () => {
|
|
288
|
+
const zone = await typedi_1.default.get(admin_1.AdminService).getZone(zoneId);
|
|
289
|
+
if (!zone)
|
|
290
|
+
return "";
|
|
291
|
+
return zone?.name || "";
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
catch (error) {
|
|
295
|
+
(0, config_1.getLogger)().error(`Error in getZoneName: ${error instanceof Error ? error.message : error}`);
|
|
296
|
+
return "";
|
|
297
|
+
}
|
|
247
298
|
}
|
|
248
299
|
async getAccessGroupName(accessGroupId) {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
300
|
+
try {
|
|
301
|
+
return await this.getCachedEntityData("accessGroup", accessGroupId, async () => {
|
|
302
|
+
const accessGroup = await typedi_1.default.get(admin_1.AdminService).getAccessGroup(accessGroupId);
|
|
303
|
+
if (!accessGroup)
|
|
304
|
+
return "";
|
|
305
|
+
return accessGroup?.name || "";
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
catch (error) {
|
|
309
|
+
(0, config_1.getLogger)().error(`Error in getAccessGroupName: ${error instanceof Error ? error.message : error}`);
|
|
310
|
+
return "";
|
|
311
|
+
}
|
|
255
312
|
}
|
|
256
313
|
async getScheduleDetails(scheduleId) {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
314
|
+
try {
|
|
315
|
+
return await this.getCachedEntityData("schedule", scheduleId, async () => {
|
|
316
|
+
const schedule = await typedi_1.default.get(pms_1.PmsService).getSchedule(scheduleId);
|
|
317
|
+
if (!schedule) {
|
|
318
|
+
return {
|
|
319
|
+
scheduleStartDate: "",
|
|
320
|
+
scheduleEndDate: "",
|
|
321
|
+
scheduleDuration: 0,
|
|
322
|
+
scheduleSource: "",
|
|
323
|
+
scheduleStatus: "",
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
const scheduleDuration = this.calculateScheduleDuration(schedule.startTime, schedule.endTime);
|
|
260
327
|
return {
|
|
261
|
-
scheduleStartDate:
|
|
262
|
-
scheduleEndDate:
|
|
263
|
-
scheduleDuration
|
|
264
|
-
scheduleSource:
|
|
265
|
-
scheduleStatus:
|
|
328
|
+
scheduleStartDate: schedule.startTime,
|
|
329
|
+
scheduleEndDate: schedule.endTime,
|
|
330
|
+
scheduleDuration,
|
|
331
|
+
scheduleSource: schedule.source,
|
|
332
|
+
scheduleStatus: schedule.status,
|
|
266
333
|
};
|
|
267
|
-
}
|
|
268
|
-
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
catch (error) {
|
|
337
|
+
(0, config_1.getLogger)().error(`Error in getScheduleDetails: ${error instanceof Error ? error.message : error}`);
|
|
269
338
|
return {
|
|
270
|
-
scheduleStartDate:
|
|
271
|
-
scheduleEndDate:
|
|
272
|
-
scheduleDuration,
|
|
273
|
-
scheduleSource:
|
|
274
|
-
scheduleStatus:
|
|
339
|
+
scheduleStartDate: "",
|
|
340
|
+
scheduleEndDate: "",
|
|
341
|
+
scheduleDuration: 0,
|
|
342
|
+
scheduleSource: "",
|
|
343
|
+
scheduleStatus: "",
|
|
275
344
|
};
|
|
276
|
-
}
|
|
345
|
+
}
|
|
277
346
|
}
|
|
278
347
|
};
|
|
279
348
|
__setFunctionName(_classThis, "AuditUtils");
|