dt-common-device 13.0.9 → 13.0.10
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.
|
@@ -9,12 +9,14 @@ export declare class AdminRepository {
|
|
|
9
9
|
getZonesByAccessGroups(accessGroupIds: string[], type?: string[]): Promise<any[]>;
|
|
10
10
|
getAccessGroup(accessGroupId: string, propertyId?: string): Promise<IAccessGroup | null>;
|
|
11
11
|
getZoneAccessGroupByZoneId(zoneId: string): Promise<IZoneAccessGroup[] | null>;
|
|
12
|
+
getZoneIdsByAccessGroupIds(accessGroupIds: string[]): Promise<string[]>;
|
|
12
13
|
getAllParentZones(zoneId: string): Promise<string[]>;
|
|
13
14
|
getDirectChildZones(zoneId: string, propertyId?: string): Promise<string[]>;
|
|
14
15
|
getAllChildZones(zoneId: string, propertyId?: string): Promise<string[]>;
|
|
15
16
|
getAccessGroupsByZoneId(zoneId: string): Promise<IAccessGroup[]>;
|
|
16
17
|
getAccessGroupsByZoneIds(zoneIds: string[]): Promise<IAccessGroup[]>;
|
|
17
18
|
getZone(zoneId: string, propertyId?: string): Promise<IZone | null>;
|
|
19
|
+
getZonesByIds(zoneIds: string[]): Promise<IZone[]>;
|
|
18
20
|
getUser(userId: string): Promise<IUser | null>;
|
|
19
21
|
getZoneByDeviceId(deviceId: string): Promise<IZone | null>;
|
|
20
22
|
getAccessGroups(propertyId: string, accessibleBy?: string): Promise<IAccessGroup[]>;
|
|
@@ -278,6 +278,18 @@ let AdminRepository = (() => {
|
|
|
278
278
|
}
|
|
279
279
|
return null;
|
|
280
280
|
}
|
|
281
|
+
async getZoneIdsByAccessGroupIds(accessGroupIds) {
|
|
282
|
+
if (accessGroupIds.length === 0) {
|
|
283
|
+
return [];
|
|
284
|
+
}
|
|
285
|
+
const query = `
|
|
286
|
+
SELECT DISTINCT "zoneId"
|
|
287
|
+
FROM dt_zones_collection_map
|
|
288
|
+
WHERE "collectionId" = ANY($1)
|
|
289
|
+
`;
|
|
290
|
+
const result = await this.postgres.query(query, [accessGroupIds]);
|
|
291
|
+
return result.rows.map((row) => row.zoneId);
|
|
292
|
+
}
|
|
281
293
|
async getAllParentZones(zoneId) {
|
|
282
294
|
const allParentZoneIds = [];
|
|
283
295
|
let currentZoneId = zoneId;
|
|
@@ -416,6 +428,40 @@ let AdminRepository = (() => {
|
|
|
416
428
|
throw new Error("Failed to get zone");
|
|
417
429
|
}
|
|
418
430
|
}
|
|
431
|
+
async getZonesByIds(zoneIds) {
|
|
432
|
+
if (zoneIds.length === 0) {
|
|
433
|
+
return [];
|
|
434
|
+
}
|
|
435
|
+
try {
|
|
436
|
+
// Batch query to get all zones at once
|
|
437
|
+
const query = `SELECT * FROM dt_zones WHERE "id" = ANY($1)`;
|
|
438
|
+
const result = await this.postgres.query(query, [zoneIds]);
|
|
439
|
+
// Get all unique zone type IDs
|
|
440
|
+
const zoneTypeIds = [
|
|
441
|
+
...new Set(result.rows.map((row) => row.zoneTypeId)),
|
|
442
|
+
];
|
|
443
|
+
// Batch query for zone types
|
|
444
|
+
let zoneTypesMap = new Map();
|
|
445
|
+
if (zoneTypeIds.length > 0) {
|
|
446
|
+
const zoneTypesQuery = `SELECT * FROM "dt_zoneTypes" WHERE "id" = ANY($1)`;
|
|
447
|
+
const zoneTypesResult = await this.postgres.query(zoneTypesQuery, [
|
|
448
|
+
zoneTypeIds,
|
|
449
|
+
]);
|
|
450
|
+
zoneTypesResult.rows.forEach((zt) => {
|
|
451
|
+
zoneTypesMap.set(zt.id, { id: zt.id, name: zt.name });
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
// Map zone types to zones
|
|
455
|
+
return result.rows.map((zoneData) => {
|
|
456
|
+
zoneData.zoneType = zoneTypesMap.get(zoneData.zoneTypeId) || {};
|
|
457
|
+
return zoneData;
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
catch (error) {
|
|
461
|
+
console.error("Error in getZonesByIds:", error);
|
|
462
|
+
return [];
|
|
463
|
+
}
|
|
464
|
+
}
|
|
419
465
|
async getUser(userId) {
|
|
420
466
|
const user = await this.postgres.query(`SELECT * FROM dt_users WHERE "id" = $1`, [userId]);
|
|
421
467
|
if (user.rows.length > 0) {
|
|
@@ -8,6 +8,7 @@ export declare class AdminService {
|
|
|
8
8
|
getAccessGroup(accessGroupId: string, propertyId?: string): Promise<IAccessGroup | null>;
|
|
9
9
|
getAccessGroupByZoneId(zoneId: string): Promise<IAccessGroup[] | []>;
|
|
10
10
|
getAccessgroupBySubParentZoneId(zoneId: string): Promise<IAccessGroup[]>;
|
|
11
|
+
getAllParentSubZonesByAccessGroupIds(accessGroupIds: string[]): Promise<string[]>;
|
|
11
12
|
getZone(zoneId: string, propertyId?: string): Promise<IZone | null>;
|
|
12
13
|
getUser(userId: string): Promise<IUser | null>;
|
|
13
14
|
getZoneByDeviceId(deviceId: string): Promise<IZone | null>;
|
|
@@ -133,6 +133,7 @@ let AdminService = (() => {
|
|
|
133
133
|
}
|
|
134
134
|
return accessGroups;
|
|
135
135
|
}
|
|
136
|
+
//--------------------------USED FOR QUERY RESERVATION-----------------------
|
|
136
137
|
async getAccessgroupBySubParentZoneId(zoneId) {
|
|
137
138
|
if (!zoneId) {
|
|
138
139
|
throw new Error("Zone ID is required");
|
|
@@ -211,6 +212,51 @@ let AdminService = (() => {
|
|
|
211
212
|
return [];
|
|
212
213
|
}
|
|
213
214
|
}
|
|
215
|
+
// used for query reservation to find all the zones mapped with the access groups
|
|
216
|
+
async getAllParentSubZonesByAccessGroupIds(accessGroupIds) {
|
|
217
|
+
if (!accessGroupIds || accessGroupIds.length === 0) {
|
|
218
|
+
throw new Error("Access Group IDs are required");
|
|
219
|
+
}
|
|
220
|
+
try {
|
|
221
|
+
// Step 1: Find all zones mapped with these access groups
|
|
222
|
+
const mappedZoneIds = await this.adminRepository.getZoneIdsByAccessGroupIds(accessGroupIds);
|
|
223
|
+
if (mappedZoneIds.length === 0) {
|
|
224
|
+
return [];
|
|
225
|
+
}
|
|
226
|
+
// OPTIMIZATION 1: Batch get all zones at once (reduces DB calls from O(n) to O(1))
|
|
227
|
+
const zones = await this.adminRepository.getZonesByIds(mappedZoneIds);
|
|
228
|
+
const zoneMap = new Map();
|
|
229
|
+
zones.forEach((zone) => zoneMap.set(zone.id, zone));
|
|
230
|
+
// Use Set to collect unique zone IDs from the start
|
|
231
|
+
const allZoneIds = new Set(mappedZoneIds);
|
|
232
|
+
// OPTIMIZATION 2: Process all zones in parallel (reduces time from O(n) sequential to O(1) parallel)
|
|
233
|
+
const zoneProcessingPromises = mappedZoneIds.map(async (zoneId) => {
|
|
234
|
+
const zone = zoneMap.get(zoneId);
|
|
235
|
+
if (!zone) {
|
|
236
|
+
return { parentIds: [], childIds: [] };
|
|
237
|
+
}
|
|
238
|
+
// OPTIMIZATION 3: Get parents and children in parallel for each zone
|
|
239
|
+
const [parentZoneIds, childZoneIds] = await Promise.all([
|
|
240
|
+
this.adminRepository.getAllParentZones(zoneId),
|
|
241
|
+
this.adminRepository.getAllChildZones(zoneId, zone.propertyId),
|
|
242
|
+
]);
|
|
243
|
+
return { parentIds: parentZoneIds, childIds: childZoneIds };
|
|
244
|
+
});
|
|
245
|
+
// Wait for all zone processing to complete
|
|
246
|
+
const results = await Promise.all(zoneProcessingPromises);
|
|
247
|
+
// OPTIMIZATION 4: Batch add all IDs to Set (O(n) operation)
|
|
248
|
+
results.forEach(({ parentIds, childIds }) => {
|
|
249
|
+
parentIds.forEach((id) => allZoneIds.add(id));
|
|
250
|
+
childIds.forEach((id) => allZoneIds.add(id));
|
|
251
|
+
});
|
|
252
|
+
return Array.from(allZoneIds);
|
|
253
|
+
}
|
|
254
|
+
catch (error) {
|
|
255
|
+
console.error("Error in getAllParentSubZonesByAccessGroupIds:", error);
|
|
256
|
+
return [];
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
//----------------------------------------------------------------------------------
|
|
214
260
|
async getZone(zoneId, propertyId) {
|
|
215
261
|
if (!zoneId) {
|
|
216
262
|
throw new Error("Zone ID is required");
|