dt-common-device 11.1.9 → 11.2.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.
|
@@ -3,10 +3,11 @@ export declare class AdminRepository {
|
|
|
3
3
|
private readonly deviceRepository;
|
|
4
4
|
private readonly postgres;
|
|
5
5
|
private readonly localDeviceService;
|
|
6
|
+
private readonly redisUtils;
|
|
6
7
|
constructor();
|
|
7
|
-
getZonesByAccessGroupIds(accessGroupIds: string[]): Promise<any[]>;
|
|
8
|
+
getZonesByAccessGroupIds(accessGroupIds: string[], propertyId: string): Promise<any[]>;
|
|
8
9
|
getZonesByAccessGroups(accessGroupIds: string[]): Promise<any[]>;
|
|
9
|
-
getAccessGroup(accessGroupId: string): Promise<IAccessGroup | null>;
|
|
10
|
+
getAccessGroup(accessGroupId: string, propertyId?: string): Promise<IAccessGroup | null>;
|
|
10
11
|
getZoneAccessGroupByZoneId(zoneId: string): Promise<IZoneAccessGroup[] | null>;
|
|
11
12
|
getZone(zoneId: string, propertyId?: string): Promise<IZone | null>;
|
|
12
13
|
getUser(userId: string): Promise<IUser | null>;
|
|
@@ -78,6 +78,7 @@ const Device_repository_1 = require("../device/local/repository/Device.repositor
|
|
|
78
78
|
const db_1 = require("../../db/db");
|
|
79
79
|
const interfaces_1 = require("../device/cloud/interfaces");
|
|
80
80
|
const services_1 = require("../device/local/services");
|
|
81
|
+
const config_1 = require("../../config/config");
|
|
81
82
|
let AdminRepository = (() => {
|
|
82
83
|
let _classDecorators = [(0, typedi_1.Service)()];
|
|
83
84
|
let _classDescriptor;
|
|
@@ -88,25 +89,9 @@ let AdminRepository = (() => {
|
|
|
88
89
|
this.deviceRepository = typedi_1.default.get(Device_repository_1.DeviceRepository);
|
|
89
90
|
this.postgres = (0, db_1.getPostgresClient)();
|
|
90
91
|
this.localDeviceService = typedi_1.default.get(services_1.LocalDeviceService);
|
|
92
|
+
this.redisUtils = typedi_1.default.get(utils_1.RedisUtils);
|
|
91
93
|
}
|
|
92
|
-
async getZonesByAccessGroupIds(accessGroupIds) {
|
|
93
|
-
// // Get propertyId from any of the accessGroupIds
|
|
94
|
-
// const accessGroupIdsResult = await this.postgres.query(
|
|
95
|
-
// `SELECT "propertyId" FROM dt_collections WHERE "id" = ANY($1) LIMIT 1`,
|
|
96
|
-
// [accessGroupIds]
|
|
97
|
-
// );
|
|
98
|
-
// const propertyId = accessGroupIdsResult.rows[0].propertyId;
|
|
99
|
-
// const sortedAccessGroupIds = [...accessGroupIds].sort((a, b) =>
|
|
100
|
-
// a.localeCompare(b)
|
|
101
|
-
// );
|
|
102
|
-
// // Check if the result is already cached
|
|
103
|
-
// const redisKey = `${propertyId}:zonesAndDevicesByAccessGroupIds:${sortedAccessGroupIds.join(
|
|
104
|
-
// ","
|
|
105
|
-
// )}`;
|
|
106
|
-
// const cachedResult = await this.redisUtils.get(redisKey);
|
|
107
|
-
// if (cachedResult) {
|
|
108
|
-
// return JSON.parse(cachedResult);
|
|
109
|
-
// }
|
|
94
|
+
async getZonesByAccessGroupIds(accessGroupIds, propertyId) {
|
|
110
95
|
// If not cached, get the result from the database
|
|
111
96
|
const result = await this.postgres.query(`SELECT
|
|
112
97
|
"zc"."id" AS "zoneCollectionMapId",
|
|
@@ -140,7 +125,18 @@ let AdminRepository = (() => {
|
|
|
140
125
|
const collectionZone = [];
|
|
141
126
|
for (let zone of response) {
|
|
142
127
|
let zoneIds = [];
|
|
143
|
-
|
|
128
|
+
let zones = null;
|
|
129
|
+
const redisKey = `${propertyId}:childZones:${zone.zoneId}`;
|
|
130
|
+
const zonesCache = await this.redisUtils.get(redisKey);
|
|
131
|
+
if (zonesCache !== null && zonesCache !== undefined) {
|
|
132
|
+
(0, config_1.getLogger)().info(`Got child zones from redis`);
|
|
133
|
+
zones = JSON.parse(zonesCache);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
const response = await (0, utils_1.getAdminServiceAxiosInstance)().get(`/zones/child?zoneId=${zone.zoneId}`);
|
|
137
|
+
zones = response?.data?.data;
|
|
138
|
+
await this.redisUtils.set(redisKey, JSON.stringify(zones), 86400);
|
|
139
|
+
}
|
|
144
140
|
zoneIds.push(zone.zoneId);
|
|
145
141
|
if (zones.childZones?.length > 0) {
|
|
146
142
|
const nestedZoneIds = new Set(_zones(zones.childZones));
|
|
@@ -245,11 +241,20 @@ let AdminRepository = (() => {
|
|
|
245
241
|
]);
|
|
246
242
|
return zonesResult.rows.map((e) => e.id);
|
|
247
243
|
}
|
|
248
|
-
async getAccessGroup(accessGroupId) {
|
|
249
|
-
|
|
244
|
+
async getAccessGroup(accessGroupId, propertyId) {
|
|
245
|
+
let query;
|
|
246
|
+
if (propertyId) {
|
|
247
|
+
query = `
|
|
248
|
+
SELECT * FROM dt_collections
|
|
249
|
+
WHERE "id" = $1 AND "propertyId" = $2
|
|
250
|
+
`;
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
query = `
|
|
250
254
|
SELECT * FROM dt_collections
|
|
251
255
|
WHERE "id" = $1
|
|
252
|
-
|
|
256
|
+
`;
|
|
257
|
+
}
|
|
253
258
|
const result = await this.postgres.query(query, [accessGroupId]);
|
|
254
259
|
if (result.rows.length > 0) {
|
|
255
260
|
return result.rows[0];
|
|
@@ -3,9 +3,9 @@ export declare class AdminService {
|
|
|
3
3
|
private readonly adminRepository;
|
|
4
4
|
private readonly redisUtils;
|
|
5
5
|
constructor();
|
|
6
|
-
getZonesByAccessGroupIds(accessGroupIds: string[]): Promise<any[] | undefined>;
|
|
6
|
+
getZonesByAccessGroupIds(accessGroupIds: string[], propertyId: string): Promise<any[] | undefined>;
|
|
7
7
|
getZonesByAccessGroups(accessGroupIds: string[]): Promise<any[] | undefined>;
|
|
8
|
-
getAccessGroup(accessGroupId: string): Promise<IAccessGroup | null>;
|
|
8
|
+
getAccessGroup(accessGroupId: string, propertyId?: string): Promise<IAccessGroup | null>;
|
|
9
9
|
getAccessGroupByZoneId(zoneId: string): Promise<IAccessGroup[] | []>;
|
|
10
10
|
getZone(zoneId: string, propertyId?: string): Promise<IZone | null>;
|
|
11
11
|
getUser(userId: string): Promise<IUser | null>;
|
|
@@ -85,9 +85,9 @@ let AdminService = (() => {
|
|
|
85
85
|
this.adminRepository = typedi_1.default.get(Admin_repository_1.AdminRepository);
|
|
86
86
|
this.redisUtils = typedi_1.default.get(redis_utils_1.RedisUtils);
|
|
87
87
|
}
|
|
88
|
-
async getZonesByAccessGroupIds(accessGroupIds) {
|
|
88
|
+
async getZonesByAccessGroupIds(accessGroupIds, propertyId) {
|
|
89
89
|
try {
|
|
90
|
-
return await this.adminRepository.getZonesByAccessGroupIds(accessGroupIds);
|
|
90
|
+
return await this.adminRepository.getZonesByAccessGroupIds(accessGroupIds, propertyId);
|
|
91
91
|
}
|
|
92
92
|
catch (error) {
|
|
93
93
|
console.log(error);
|
|
@@ -101,13 +101,20 @@ let AdminService = (() => {
|
|
|
101
101
|
console.log(error);
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
|
-
async getAccessGroup(accessGroupId) {
|
|
104
|
+
async getAccessGroup(accessGroupId, propertyId) {
|
|
105
105
|
if (!accessGroupId) {
|
|
106
106
|
throw new Error("Access Group ID is required");
|
|
107
107
|
}
|
|
108
|
-
|
|
108
|
+
let accessGroup;
|
|
109
|
+
const redisKey = `collection:${accessGroupId}`;
|
|
110
|
+
accessGroup = await this.redisUtils.hget(redisKey, accessGroupId);
|
|
111
|
+
if (accessGroup)
|
|
112
|
+
return accessGroup;
|
|
113
|
+
accessGroup = await this.adminRepository.getAccessGroup(accessGroupId, propertyId);
|
|
109
114
|
if (!accessGroup)
|
|
110
115
|
return null;
|
|
116
|
+
await this.redisUtils.hsetWithTTL(redisKey, accessGroup?.id, JSON.stringify(accessGroup), 86400);
|
|
117
|
+
await this.redisUtils.sadd(`property_collections:${accessGroup?.propertyId}`, accessGroup?.id);
|
|
111
118
|
return accessGroup;
|
|
112
119
|
}
|
|
113
120
|
async getAccessGroupByZoneId(zoneId) {
|