shuttlepro-shared 1.2.63 → 1.2.65
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.
|
@@ -1,3 +1,407 @@
|
|
|
1
|
+
// const { UserRole } = require("../../models/UserRole");
|
|
2
|
+
// const { getRedisData, setRedisData } = require("../../config/redis");
|
|
3
|
+
|
|
4
|
+
// const defaultSelect =
|
|
5
|
+
// "iconUrl thumbUrl name productsCount ordersCount contact email address websiteUrl facebookUrl twitterUrl instagramUrl members defaultModule";
|
|
6
|
+
|
|
7
|
+
// // Cache keys
|
|
8
|
+
// const CACHE_KEY_ALL_MEMBERS = "userroles_all_members";
|
|
9
|
+
// const CACHE_KEY_USER_WORKSPACE_PREFIX = "userrole_workspace_";
|
|
10
|
+
// const CACHE_KEY_USER_PERMISSIONS_PREFIX = "user_permissions_";
|
|
11
|
+
|
|
12
|
+
// // Comprehensive cache update method
|
|
13
|
+
// const updateAllRelatedCaches = async (userId, workspaceId) => {
|
|
14
|
+
// try {
|
|
15
|
+
// // Update all members cache
|
|
16
|
+
// await updateCachedMembers();
|
|
17
|
+
|
|
18
|
+
// // Clear user workspace cache
|
|
19
|
+
// if (userId) {
|
|
20
|
+
// await clearUserWorkspaceCache(userId);
|
|
21
|
+
// }
|
|
22
|
+
|
|
23
|
+
// // Clear user permissions cache
|
|
24
|
+
// if (workspaceId && userId) {
|
|
25
|
+
// await clearUserPermissionsCache(workspaceId, userId);
|
|
26
|
+
// }
|
|
27
|
+
// } catch (error) {
|
|
28
|
+
// console.error("Error updating related caches:", error);
|
|
29
|
+
// }
|
|
30
|
+
// };
|
|
31
|
+
|
|
32
|
+
// // Clear user workspace cache
|
|
33
|
+
// const clearUserWorkspaceCache = async (userId) => {
|
|
34
|
+
// await setRedisData(
|
|
35
|
+
// `${CACHE_KEY_USER_WORKSPACE_PREFIX}${userId}`,
|
|
36
|
+
// null,
|
|
37
|
+
// null,
|
|
38
|
+
// 0
|
|
39
|
+
// );
|
|
40
|
+
// };
|
|
41
|
+
|
|
42
|
+
// // Clear user permissions cache
|
|
43
|
+
// const clearUserPermissionsCache = async (workspaceId, userId) => {
|
|
44
|
+
// const cacheKey = `${CACHE_KEY_USER_PERMISSIONS_PREFIX}${workspaceId}_${userId}`;
|
|
45
|
+
// await setRedisData(cacheKey, null, null, 0);
|
|
46
|
+
// };
|
|
47
|
+
|
|
48
|
+
// // Get cached members
|
|
49
|
+
// const getCachedMembers = async (filter = {}) => {
|
|
50
|
+
// const cacheKey =
|
|
51
|
+
// Object.keys(filter).length > 0
|
|
52
|
+
// ? `${CACHE_KEY_ALL_MEMBERS}_${JSON.stringify(filter)}`
|
|
53
|
+
// : CACHE_KEY_ALL_MEMBERS;
|
|
54
|
+
|
|
55
|
+
// let members = await getRedisData(cacheKey);
|
|
56
|
+
|
|
57
|
+
// if (!members) {
|
|
58
|
+
// members = await UserRole.find(filter)
|
|
59
|
+
// .select("userId role userShift roleId")
|
|
60
|
+
// .populate({
|
|
61
|
+
// path: "roleId",
|
|
62
|
+
// select: "id name userId userShift permissionId modulePermissions",
|
|
63
|
+
// populate: [{ path: "permissionId", select: "id name" }],
|
|
64
|
+
// })
|
|
65
|
+
// .populate({
|
|
66
|
+
// path: "userId",
|
|
67
|
+
// match: { isDeleted: false, suspended: false },
|
|
68
|
+
// select: "firstName lastName role email picture userShift",
|
|
69
|
+
// })
|
|
70
|
+
// .lean()
|
|
71
|
+
// .exec();
|
|
72
|
+
|
|
73
|
+
// await setRedisData(
|
|
74
|
+
// cacheKey,
|
|
75
|
+
// members?.filter((member) => member?.userId && member?.roleId),
|
|
76
|
+
// null,
|
|
77
|
+
// 0
|
|
78
|
+
// );
|
|
79
|
+
// }
|
|
80
|
+
|
|
81
|
+
// return members;
|
|
82
|
+
// };
|
|
83
|
+
|
|
84
|
+
// // Update the cache for all members
|
|
85
|
+
// const updateCachedMembers = async (filter = {}) => {
|
|
86
|
+
// const cacheKey =
|
|
87
|
+
// Object.keys(filter).length > 0
|
|
88
|
+
// ? `${CACHE_KEY_ALL_MEMBERS}_${JSON.stringify(filter)}`
|
|
89
|
+
// : CACHE_KEY_ALL_MEMBERS;
|
|
90
|
+
|
|
91
|
+
// const members = await UserRole.find(filter)
|
|
92
|
+
// .select("userId role userShift roleId")
|
|
93
|
+
// .populate({
|
|
94
|
+
// path: "roleId",
|
|
95
|
+
// select: "id name userId userShift permissionId modulePermissions",
|
|
96
|
+
// populate: [{ path: "permissionId", select: "id name" }],
|
|
97
|
+
// })
|
|
98
|
+
// .populate({
|
|
99
|
+
// path: "userId",
|
|
100
|
+
// match: { isDeleted: false, suspended: false },
|
|
101
|
+
// select: "firstName lastName role email picture userShift",
|
|
102
|
+
// })
|
|
103
|
+
// .lean()
|
|
104
|
+
// .exec();
|
|
105
|
+
|
|
106
|
+
// await setRedisData(
|
|
107
|
+
// cacheKey,
|
|
108
|
+
// members?.filter((member) => member?.userId && member?.roleId),
|
|
109
|
+
// null,
|
|
110
|
+
// 0
|
|
111
|
+
// );
|
|
112
|
+
// return members;
|
|
113
|
+
// };
|
|
114
|
+
|
|
115
|
+
// const createUserRole = async (data) => {
|
|
116
|
+
// const savedRole = await UserRole.create(data);
|
|
117
|
+
|
|
118
|
+
// // Update all related caches
|
|
119
|
+
// await updateAllRelatedCaches(
|
|
120
|
+
// savedRole?.userId?.toString(),
|
|
121
|
+
// savedRole?.workspaceId?.toString()
|
|
122
|
+
// );
|
|
123
|
+
|
|
124
|
+
// return savedRole;
|
|
125
|
+
// };
|
|
126
|
+
|
|
127
|
+
// const findUserRole = async (filter = {}) => {
|
|
128
|
+
// return await getCachedMembers(filter);
|
|
129
|
+
// };
|
|
130
|
+
|
|
131
|
+
// const updateUserRole = async (id, data) => {
|
|
132
|
+
// const updatedRole = await UserRole.findByIdAndUpdate(id, data, {
|
|
133
|
+
// new: true,
|
|
134
|
+
// }).exec();
|
|
135
|
+
|
|
136
|
+
// if (updatedRole) {
|
|
137
|
+
// // Update all related caches
|
|
138
|
+
// await updateAllRelatedCaches(updatedRole.userId, updatedRole.workspaceId);
|
|
139
|
+
// }
|
|
140
|
+
|
|
141
|
+
// return updatedRole;
|
|
142
|
+
// };
|
|
143
|
+
|
|
144
|
+
// const deleteUserRoleByFilter = async (filter) => {
|
|
145
|
+
// // Find the role before deleting to get userId and workspaceId
|
|
146
|
+
// const roleToDelete = await UserRole.findOne(filter).exec();
|
|
147
|
+
|
|
148
|
+
// const deletedRole = await UserRole.findOneAndDelete(filter).exec();
|
|
149
|
+
|
|
150
|
+
// if (deletedRole) {
|
|
151
|
+
// // Update all related caches
|
|
152
|
+
// await updateAllRelatedCaches(
|
|
153
|
+
// roleToDelete?.userId,
|
|
154
|
+
// roleToDelete?.workspaceId
|
|
155
|
+
// );
|
|
156
|
+
// }
|
|
157
|
+
|
|
158
|
+
// return deletedRole;
|
|
159
|
+
// };
|
|
160
|
+
|
|
161
|
+
// const deleteUserRole = async (id) => {
|
|
162
|
+
// const role = await UserRole.findById(id).exec();
|
|
163
|
+
// const userId = role?.userId;
|
|
164
|
+
// const workspaceId = role?.workspaceId;
|
|
165
|
+
|
|
166
|
+
// const deletedRole = await UserRole.findByIdAndDelete(id).exec();
|
|
167
|
+
|
|
168
|
+
// if (deletedRole) {
|
|
169
|
+
// // Update all related caches
|
|
170
|
+
// await updateAllRelatedCaches(userId, workspaceId);
|
|
171
|
+
// }
|
|
172
|
+
|
|
173
|
+
// return deletedRole;
|
|
174
|
+
// };
|
|
175
|
+
|
|
176
|
+
// const findUserWorkspaces = async (userId) => {
|
|
177
|
+
// const cacheKey = `${CACHE_KEY_USER_WORKSPACE_PREFIX}${userId}`;
|
|
178
|
+
// let userRoleWithWorkspaces = await getRedisData(cacheKey);
|
|
179
|
+
// if (!userRoleWithWorkspaces) {
|
|
180
|
+
// userRoleWithWorkspaces = await UserRole.find({
|
|
181
|
+
// userId: userId,
|
|
182
|
+
// })
|
|
183
|
+
// .select("userId roleId workspaceId")
|
|
184
|
+
// .populate({
|
|
185
|
+
// path: "roleId",
|
|
186
|
+
// select: "id name userId userShift permissionId modulePermissions",
|
|
187
|
+
// populate: [{ path: "permissionId", select: "id name" }],
|
|
188
|
+
// })
|
|
189
|
+
// .populate({
|
|
190
|
+
// path: "workspaceId",
|
|
191
|
+
// select: defaultSelect,
|
|
192
|
+
// match: { isDeleted: false },
|
|
193
|
+
// })
|
|
194
|
+
// .lean()
|
|
195
|
+
// .exec();
|
|
196
|
+
// userRoleWithWorkspaces = userRoleWithWorkspaces
|
|
197
|
+
// ?.filter((userRole) => userRole?.workspaceId)
|
|
198
|
+
// ?.map((ur) => {
|
|
199
|
+
// return {
|
|
200
|
+
// ...ur?.workspaceId,
|
|
201
|
+
// id: ur?.workspaceId?._id?.toString(),
|
|
202
|
+
// _id: ur?.workspaceId?._id?.toString(),
|
|
203
|
+
// role: ur?.roleId?.name || "",
|
|
204
|
+
// permissions: ur?.roleId?.modulePermissions?.modules || [],
|
|
205
|
+
// };
|
|
206
|
+
// });
|
|
207
|
+
// if (userRoleWithWorkspaces) {
|
|
208
|
+
// await setRedisData(cacheKey, userRoleWithWorkspaces, null, 0);
|
|
209
|
+
// }
|
|
210
|
+
// }
|
|
211
|
+
// return userRoleWithWorkspaces;
|
|
212
|
+
// };
|
|
213
|
+
|
|
214
|
+
// const convertPermissionsArrayToObject = (originalArray) => {
|
|
215
|
+
// return originalArray?.reduce((acc, item) => {
|
|
216
|
+
// const { id, actions, integrationPermissions, ...extras } = item;
|
|
217
|
+
// const formattedExtras = {};
|
|
218
|
+
// if (extras.deletable !== undefined) {
|
|
219
|
+
// formattedExtras.deletable = extras.deletable;
|
|
220
|
+
// }
|
|
221
|
+
// if (extras.createUpdateable !== undefined) {
|
|
222
|
+
// formattedExtras.createUpdateable = extras.createUpdateable;
|
|
223
|
+
// }
|
|
224
|
+
// acc[id] = {
|
|
225
|
+
// actions: Object.keys(actions).filter((action) => actions[action]),
|
|
226
|
+
// ...formattedExtras,
|
|
227
|
+
// ...(integrationPermissions && {
|
|
228
|
+
// integrationPermissions: Object.entries(integrationPermissions)
|
|
229
|
+
// .filter(([_, allowed]) => allowed)
|
|
230
|
+
// .map(([key]) => key),
|
|
231
|
+
// }),
|
|
232
|
+
// };
|
|
233
|
+
// return acc;
|
|
234
|
+
// }, {});
|
|
235
|
+
// };
|
|
236
|
+
|
|
237
|
+
// const getUserRoleWithPermissions = async (workspaceId, userId) => {
|
|
238
|
+
// const cacheKey = `${CACHE_KEY_USER_PERMISSIONS_PREFIX}${workspaceId}_${userId}`;
|
|
239
|
+
// let cachedData = await getRedisData(cacheKey);
|
|
240
|
+
|
|
241
|
+
// if (cachedData && cachedData.roleWithPermissions) {
|
|
242
|
+
// return cachedData.roleWithPermissions;
|
|
243
|
+
// }
|
|
244
|
+
|
|
245
|
+
// try {
|
|
246
|
+
// let data = await UserRole.findOne({
|
|
247
|
+
// workspaceId: workspaceId,
|
|
248
|
+
// userId: userId,
|
|
249
|
+
// })
|
|
250
|
+
// .populate({
|
|
251
|
+
// path: "roleId",
|
|
252
|
+
// select:
|
|
253
|
+
// "id name userId userShift permissionId modulePermissions defaultModule",
|
|
254
|
+
// populate: [{ path: "permissionId", select: "id name" }],
|
|
255
|
+
// })
|
|
256
|
+
// .lean()
|
|
257
|
+
// .exec();
|
|
258
|
+
|
|
259
|
+
// if (!data) {
|
|
260
|
+
// return null;
|
|
261
|
+
// }
|
|
262
|
+
|
|
263
|
+
// const roleWithPermissions = {
|
|
264
|
+
// ...data,
|
|
265
|
+
// roleId: data?.roleId?._id?.toString() || "",
|
|
266
|
+
// role: data?.roleId?.name || "",
|
|
267
|
+
// permissions:
|
|
268
|
+
// convertPermissionsArrayToObject(
|
|
269
|
+
// data?.roleId?.modulePermissions?.modules
|
|
270
|
+
// ) || [],
|
|
271
|
+
// parentRole: data?.roleId?.permissionId?.name || "",
|
|
272
|
+
// parentRoleId: data?.roleId?.permissionId?._id?.toString() || "",
|
|
273
|
+
// defaultModule: data?.roleId?.defaultModule || "",
|
|
274
|
+
// };
|
|
275
|
+
|
|
276
|
+
// // Store in cache with other permission data
|
|
277
|
+
// const permissionsData = cachedData || {};
|
|
278
|
+
// permissionsData.roleWithPermissions = roleWithPermissions;
|
|
279
|
+
// await setRedisData(cacheKey, permissionsData, null, 0);
|
|
280
|
+
|
|
281
|
+
// return roleWithPermissions;
|
|
282
|
+
// } catch (err) {
|
|
283
|
+
// console.error("Error in getUserRoleWithPermissions:", err);
|
|
284
|
+
// return null;
|
|
285
|
+
// }
|
|
286
|
+
// };
|
|
287
|
+
|
|
288
|
+
// const getUserPermissions = async (workspaceId, userId) => {
|
|
289
|
+
// const cacheKey = `${CACHE_KEY_USER_PERMISSIONS_PREFIX}${workspaceId}_${userId}`;
|
|
290
|
+
// let cachedData = await getRedisData(cacheKey);
|
|
291
|
+
|
|
292
|
+
// if (cachedData && cachedData.permissions) {
|
|
293
|
+
// return cachedData.permissions;
|
|
294
|
+
// }
|
|
295
|
+
|
|
296
|
+
// try {
|
|
297
|
+
// let data = await UserRole.findOne({
|
|
298
|
+
// workspaceId: workspaceId,
|
|
299
|
+
// userId: userId,
|
|
300
|
+
// })
|
|
301
|
+
// .select("roleId userId")
|
|
302
|
+
// .populate({
|
|
303
|
+
// path: "roleId",
|
|
304
|
+
// select: "modulePermissions",
|
|
305
|
+
// })
|
|
306
|
+
// .lean()
|
|
307
|
+
// .exec();
|
|
308
|
+
|
|
309
|
+
// if (!data) {
|
|
310
|
+
// return null;
|
|
311
|
+
// }
|
|
312
|
+
|
|
313
|
+
// const permissions =
|
|
314
|
+
// convertPermissionsArrayToObject(
|
|
315
|
+
// data?.roleId?.modulePermissions?.modules
|
|
316
|
+
// ) || null;
|
|
317
|
+
|
|
318
|
+
// // Store in cache with other permission data
|
|
319
|
+
// const permissionsData = cachedData || {};
|
|
320
|
+
// permissionsData.permissions = permissions;
|
|
321
|
+
// await setRedisData(cacheKey, permissionsData, null, 0);
|
|
322
|
+
|
|
323
|
+
// return permissions;
|
|
324
|
+
// } catch (err) {
|
|
325
|
+
// console.error("Error in getUserPermissions:", err);
|
|
326
|
+
// return null;
|
|
327
|
+
// }
|
|
328
|
+
// };
|
|
329
|
+
|
|
330
|
+
// const getChannelsPermissions = async (workspaceId, userId) => {
|
|
331
|
+
// const cacheKey = `${CACHE_KEY_USER_PERMISSIONS_PREFIX}${workspaceId}_${userId}`;
|
|
332
|
+
// let cachedData = await getRedisData(cacheKey);
|
|
333
|
+
|
|
334
|
+
// if (cachedData && cachedData.channelsPermissions) {
|
|
335
|
+
// return cachedData.channelsPermissions;
|
|
336
|
+
// }
|
|
337
|
+
|
|
338
|
+
// try {
|
|
339
|
+
// let data = await UserRole.findOne({
|
|
340
|
+
// workspaceId: workspaceId,
|
|
341
|
+
// userId: userId,
|
|
342
|
+
// })
|
|
343
|
+
// .select("roleId userId")
|
|
344
|
+
// .populate({
|
|
345
|
+
// path: "roleId",
|
|
346
|
+
// select: "modulePermissions",
|
|
347
|
+
// })
|
|
348
|
+
// .lean()
|
|
349
|
+
// .exec();
|
|
350
|
+
|
|
351
|
+
// if (!data) {
|
|
352
|
+
// return [];
|
|
353
|
+
// }
|
|
354
|
+
|
|
355
|
+
// const channelsPermissions =
|
|
356
|
+
// convertPermissionsArrayToObject(
|
|
357
|
+
// data?.roleId?.modulePermissions?.modules
|
|
358
|
+
// )?.["conversations"]?.["integrationPermissions"] || [];
|
|
359
|
+
|
|
360
|
+
// // Store in cache with other permission data
|
|
361
|
+
// const permissionsData = cachedData || {};
|
|
362
|
+
// permissionsData.channelsPermissions = channelsPermissions;
|
|
363
|
+
// await setRedisData(cacheKey, permissionsData, null, 0);
|
|
364
|
+
|
|
365
|
+
// return channelsPermissions;
|
|
366
|
+
// } catch (err) {
|
|
367
|
+
// console.error("Error in getChannelsPermissions:", err);
|
|
368
|
+
// return [];
|
|
369
|
+
// }
|
|
370
|
+
// };
|
|
371
|
+
|
|
372
|
+
// const findOneAndUpdateUserRole = async ({
|
|
373
|
+
// workspaceId,
|
|
374
|
+
// userId,
|
|
375
|
+
// userShift,
|
|
376
|
+
// role,
|
|
377
|
+
// }) => {
|
|
378
|
+
// const updatedRole = await UserRole.findOneAndUpdate(
|
|
379
|
+
// { workspaceId, userId },
|
|
380
|
+
// { $set: { userShift, role } },
|
|
381
|
+
// { new: true, upsert: false }
|
|
382
|
+
// ).exec();
|
|
383
|
+
|
|
384
|
+
// if (updatedRole) {
|
|
385
|
+
// // Update all related caches
|
|
386
|
+
// await updateAllRelatedCaches(userId, workspaceId);
|
|
387
|
+
// }
|
|
388
|
+
|
|
389
|
+
// return updatedRole;
|
|
390
|
+
// };
|
|
391
|
+
|
|
392
|
+
// module.exports = {
|
|
393
|
+
// createUserRole,
|
|
394
|
+
// findUserRole,
|
|
395
|
+
// updateUserRole,
|
|
396
|
+
// deleteUserRoleByFilter,
|
|
397
|
+
// deleteUserRole,
|
|
398
|
+
// findUserWorkspaces,
|
|
399
|
+
// getUserRoleWithPermissions,
|
|
400
|
+
// getUserPermissions,
|
|
401
|
+
// getChannelsPermissions,
|
|
402
|
+
// findOneAndUpdateUserRole,
|
|
403
|
+
// };
|
|
404
|
+
|
|
1
405
|
const { UserRole } = require("../../models/UserRole");
|
|
2
406
|
const { getRedisData, setRedisData } = require("../../config/redis");
|
|
3
407
|
|
|
@@ -6,17 +410,21 @@ const defaultSelect =
|
|
|
6
410
|
|
|
7
411
|
// Cache keys
|
|
8
412
|
const CACHE_KEY_ALL_MEMBERS = "userroles_all_members";
|
|
9
|
-
const CACHE_KEY_USER_WORKSPACE_PREFIX = "userrole_workspace_";
|
|
10
413
|
const CACHE_KEY_USER_PERMISSIONS_PREFIX = "user_permissions_";
|
|
11
414
|
|
|
12
|
-
//
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
415
|
+
// Comprehensive cache update method
|
|
416
|
+
const updateAllRelatedCaches = async (userId, workspaceId) => {
|
|
417
|
+
try {
|
|
418
|
+
// Update all members cache
|
|
419
|
+
await updateCachedMembers();
|
|
420
|
+
|
|
421
|
+
// Clear user permissions cache
|
|
422
|
+
if (workspaceId && userId) {
|
|
423
|
+
await clearUserPermissionsCache(workspaceId, userId);
|
|
424
|
+
}
|
|
425
|
+
} catch (error) {
|
|
426
|
+
console.error("Error updating related caches:", error);
|
|
427
|
+
}
|
|
20
428
|
};
|
|
21
429
|
|
|
22
430
|
// Clear user permissions cache
|
|
@@ -25,15 +433,43 @@ const clearUserPermissionsCache = async (workspaceId, userId) => {
|
|
|
25
433
|
await setRedisData(cacheKey, null, null, 0);
|
|
26
434
|
};
|
|
27
435
|
|
|
436
|
+
// Find single user role by filter
|
|
437
|
+
const findSingleUserRoleByFilter = async (
|
|
438
|
+
filter = {},
|
|
439
|
+
populateOptions = {}
|
|
440
|
+
) => {
|
|
441
|
+
const defaultPopulateOptions = {
|
|
442
|
+
rolePopulate: {
|
|
443
|
+
path: "roleId",
|
|
444
|
+
select: "id name userId userShift permissionId modulePermissions",
|
|
445
|
+
populate: [{ path: "permissionId", select: "id name" }],
|
|
446
|
+
},
|
|
447
|
+
userPopulate: {
|
|
448
|
+
path: "userId",
|
|
449
|
+
match: { isDeleted: false, suspended: false },
|
|
450
|
+
select: "firstName lastName role email picture userShift",
|
|
451
|
+
},
|
|
452
|
+
};
|
|
453
|
+
const mergedPopulateOptions = {
|
|
454
|
+
rolePopulate: {
|
|
455
|
+
...defaultPopulateOptions.rolePopulate,
|
|
456
|
+
...populateOptions.rolePopulate,
|
|
457
|
+
},
|
|
458
|
+
userPopulate: {
|
|
459
|
+
...defaultPopulateOptions.userPopulate,
|
|
460
|
+
...populateOptions.userPopulate,
|
|
461
|
+
},
|
|
462
|
+
};
|
|
463
|
+
return await UserRole.findOne(filter)
|
|
464
|
+
.populate(mergedPopulateOptions.rolePopulate)
|
|
465
|
+
.populate(mergedPopulateOptions.userPopulate)
|
|
466
|
+
.lean()
|
|
467
|
+
.exec();
|
|
468
|
+
};
|
|
469
|
+
|
|
28
470
|
// Get cached members
|
|
29
471
|
const getCachedMembers = async (filter = {}) => {
|
|
30
|
-
|
|
31
|
-
Object.keys(filter).length > 0
|
|
32
|
-
? `${CACHE_KEY_ALL_MEMBERS}_${JSON.stringify(filter)}`
|
|
33
|
-
: CACHE_KEY_ALL_MEMBERS;
|
|
34
|
-
|
|
35
|
-
let members = await getRedisData(cacheKey);
|
|
36
|
-
|
|
472
|
+
let members = await getRedisData(CACHE_KEY_ALL_MEMBERS);
|
|
37
473
|
if (!members) {
|
|
38
474
|
members = await UserRole.find(filter)
|
|
39
475
|
.select("userId role userShift roleId")
|
|
@@ -50,19 +486,24 @@ const getCachedMembers = async (filter = {}) => {
|
|
|
50
486
|
.lean()
|
|
51
487
|
.exec();
|
|
52
488
|
|
|
53
|
-
await setRedisData(
|
|
489
|
+
await setRedisData(
|
|
490
|
+
CACHE_KEY_ALL_MEMBERS,
|
|
491
|
+
members?.filter((member) => member?.userId && member?.roleId),
|
|
492
|
+
null,
|
|
493
|
+
3600
|
|
494
|
+
);
|
|
54
495
|
}
|
|
55
496
|
|
|
56
|
-
|
|
497
|
+
// Apply filter in memory if needed
|
|
498
|
+
return filter && Object.keys(filter).length > 0
|
|
499
|
+
? members.filter((member) =>
|
|
500
|
+
Object.entries(filter).every(([key, value]) => member[key] === value)
|
|
501
|
+
)
|
|
502
|
+
: members;
|
|
57
503
|
};
|
|
58
504
|
|
|
59
505
|
// Update the cache for all members
|
|
60
506
|
const updateCachedMembers = async (filter = {}) => {
|
|
61
|
-
const cacheKey =
|
|
62
|
-
Object.keys(filter).length > 0
|
|
63
|
-
? `${CACHE_KEY_ALL_MEMBERS}_${JSON.stringify(filter)}`
|
|
64
|
-
: CACHE_KEY_ALL_MEMBERS;
|
|
65
|
-
|
|
66
507
|
const members = await UserRole.find(filter)
|
|
67
508
|
.select("userId role userShift roleId")
|
|
68
509
|
.populate({
|
|
@@ -77,28 +518,44 @@ const updateCachedMembers = async (filter = {}) => {
|
|
|
77
518
|
})
|
|
78
519
|
.lean()
|
|
79
520
|
.exec();
|
|
80
|
-
|
|
81
|
-
|
|
521
|
+
await setRedisData(
|
|
522
|
+
CACHE_KEY_ALL_MEMBERS,
|
|
523
|
+
members?.filter((member) => member?.userId && member?.roleId),
|
|
524
|
+
null,
|
|
525
|
+
3600
|
|
526
|
+
);
|
|
82
527
|
return members;
|
|
83
528
|
};
|
|
84
529
|
|
|
85
530
|
const createUserRole = async (data) => {
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
// Clear permissions cache if workspaceId exists
|
|
94
|
-
if (data.workspaceId) {
|
|
95
|
-
await clearUserPermissionsCache(data.workspaceId, data.userId);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
531
|
+
const savedRole = await UserRole.create(data);
|
|
532
|
+
|
|
533
|
+
// Update all related caches
|
|
534
|
+
await updateAllRelatedCaches(
|
|
535
|
+
savedRole?.userId?.toString(),
|
|
536
|
+
savedRole?.workspaceId?.toString()
|
|
537
|
+
);
|
|
98
538
|
|
|
99
539
|
return savedRole;
|
|
100
540
|
};
|
|
101
541
|
|
|
542
|
+
const findOneAndUpdateUserRole = async ({
|
|
543
|
+
workspaceId,
|
|
544
|
+
userId,
|
|
545
|
+
userShift,
|
|
546
|
+
role,
|
|
547
|
+
}) => {
|
|
548
|
+
const updatedRole = await UserRole.findOneAndUpdate(
|
|
549
|
+
{ workspaceId, userId },
|
|
550
|
+
{ $set: { userShift, role } },
|
|
551
|
+
{ new: true, upsert: false }
|
|
552
|
+
).exec();
|
|
553
|
+
if (updatedRole) {
|
|
554
|
+
await updateAllRelatedCaches(userId, workspaceId);
|
|
555
|
+
}
|
|
556
|
+
return updatedRole;
|
|
557
|
+
};
|
|
558
|
+
|
|
102
559
|
const findUserRole = async (filter = {}) => {
|
|
103
560
|
return await getCachedMembers(filter);
|
|
104
561
|
};
|
|
@@ -109,24 +566,27 @@ const updateUserRole = async (id, data) => {
|
|
|
109
566
|
}).exec();
|
|
110
567
|
|
|
111
568
|
if (updatedRole) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
await clearUserWorkspaceCache(updatedRole.userId);
|
|
115
|
-
// Clear permissions cache if workspaceId exists
|
|
116
|
-
if (updatedRole.workspaceId) {
|
|
117
|
-
await clearUserPermissionsCache(
|
|
118
|
-
updatedRole.workspaceId,
|
|
119
|
-
updatedRole.userId
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
569
|
+
// Update all related caches
|
|
570
|
+
await updateAllRelatedCaches(updatedRole.userId, updatedRole.workspaceId);
|
|
123
571
|
}
|
|
124
572
|
|
|
125
573
|
return updatedRole;
|
|
126
574
|
};
|
|
127
575
|
|
|
128
576
|
const deleteUserRoleByFilter = async (filter) => {
|
|
577
|
+
// Find the role before deleting to get userId and workspaceId
|
|
578
|
+
const roleToDelete = await UserRole.findOne(filter).exec();
|
|
579
|
+
|
|
129
580
|
const deletedRole = await UserRole.findOneAndDelete(filter).exec();
|
|
581
|
+
|
|
582
|
+
if (deletedRole) {
|
|
583
|
+
// Update all related caches
|
|
584
|
+
await updateAllRelatedCaches(
|
|
585
|
+
roleToDelete?.userId,
|
|
586
|
+
roleToDelete?.workspaceId
|
|
587
|
+
);
|
|
588
|
+
}
|
|
589
|
+
|
|
130
590
|
return deletedRole;
|
|
131
591
|
};
|
|
132
592
|
|
|
@@ -138,26 +598,18 @@ const deleteUserRole = async (id) => {
|
|
|
138
598
|
const deletedRole = await UserRole.findByIdAndDelete(id).exec();
|
|
139
599
|
|
|
140
600
|
if (deletedRole) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
await clearUserWorkspaceCache(userId);
|
|
144
|
-
// Clear permissions cache if workspaceId exists
|
|
145
|
-
if (workspaceId) {
|
|
146
|
-
await clearUserPermissionsCache(workspaceId, userId);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
601
|
+
// Update all related caches
|
|
602
|
+
await updateAllRelatedCaches(userId, workspaceId);
|
|
149
603
|
}
|
|
150
604
|
|
|
151
605
|
return deletedRole;
|
|
152
606
|
};
|
|
153
607
|
|
|
154
608
|
const findUserWorkspaces = async (userId) => {
|
|
155
|
-
|
|
156
|
-
|
|
609
|
+
let userRoleWithWorkspaces = await getRedisData(CACHE_KEY_ALL_MEMBERS);
|
|
610
|
+
|
|
157
611
|
if (!userRoleWithWorkspaces) {
|
|
158
|
-
userRoleWithWorkspaces = await UserRole.find(
|
|
159
|
-
userId: userId,
|
|
160
|
-
})
|
|
612
|
+
userRoleWithWorkspaces = await UserRole.find()
|
|
161
613
|
.select("userId roleId workspaceId")
|
|
162
614
|
.populate({
|
|
163
615
|
path: "roleId",
|
|
@@ -171,6 +623,7 @@ const findUserWorkspaces = async (userId) => {
|
|
|
171
623
|
})
|
|
172
624
|
.lean()
|
|
173
625
|
.exec();
|
|
626
|
+
|
|
174
627
|
userRoleWithWorkspaces = userRoleWithWorkspaces
|
|
175
628
|
?.filter((userRole) => userRole?.workspaceId)
|
|
176
629
|
?.map((ur) => {
|
|
@@ -182,11 +635,21 @@ const findUserWorkspaces = async (userId) => {
|
|
|
182
635
|
permissions: ur?.roleId?.modulePermissions?.modules || [],
|
|
183
636
|
};
|
|
184
637
|
});
|
|
638
|
+
|
|
185
639
|
if (userRoleWithWorkspaces) {
|
|
186
|
-
await setRedisData(
|
|
640
|
+
await setRedisData(
|
|
641
|
+
CACHE_KEY_ALL_MEMBERS,
|
|
642
|
+
userRoleWithWorkspaces,
|
|
643
|
+
null,
|
|
644
|
+
3600
|
|
645
|
+
);
|
|
187
646
|
}
|
|
188
647
|
}
|
|
189
|
-
|
|
648
|
+
|
|
649
|
+
// Filter workspaces for specific user
|
|
650
|
+
return userRoleWithWorkspaces?.filter(
|
|
651
|
+
(workspace) => workspace.userId?.toString() === userId.toString()
|
|
652
|
+
);
|
|
190
653
|
};
|
|
191
654
|
|
|
192
655
|
const convertPermissionsArrayToObject = (originalArray) => {
|
|
@@ -338,7 +801,7 @@ const getChannelsPermissions = async (workspaceId, userId) => {
|
|
|
338
801
|
// Store in cache with other permission data
|
|
339
802
|
const permissionsData = cachedData || {};
|
|
340
803
|
permissionsData.channelsPermissions = channelsPermissions;
|
|
341
|
-
await setRedisData(cacheKey, permissionsData,
|
|
804
|
+
await setRedisData(cacheKey, permissionsData, 3600);
|
|
342
805
|
|
|
343
806
|
return channelsPermissions;
|
|
344
807
|
} catch (err) {
|
|
@@ -347,29 +810,6 @@ const getChannelsPermissions = async (workspaceId, userId) => {
|
|
|
347
810
|
}
|
|
348
811
|
};
|
|
349
812
|
|
|
350
|
-
const findOneAndUpdateUserRole = async ({
|
|
351
|
-
workspaceId,
|
|
352
|
-
userId,
|
|
353
|
-
userShift,
|
|
354
|
-
role,
|
|
355
|
-
}) => {
|
|
356
|
-
const updatedRole = await UserRole.findOneAndUpdate(
|
|
357
|
-
{ workspaceId, userId },
|
|
358
|
-
{ $set: { userShift, role } },
|
|
359
|
-
{ new: true, upsert: false }
|
|
360
|
-
).exec();
|
|
361
|
-
|
|
362
|
-
if (updatedRole) {
|
|
363
|
-
await updateCachedMembers();
|
|
364
|
-
await clearUserWorkspaceCache(userId);
|
|
365
|
-
if (workspaceId) {
|
|
366
|
-
await clearUserPermissionsCache(workspaceId, userId);
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
return updatedRole;
|
|
371
|
-
};
|
|
372
|
-
|
|
373
813
|
module.exports = {
|
|
374
814
|
createUserRole,
|
|
375
815
|
findUserRole,
|
|
@@ -381,4 +821,5 @@ module.exports = {
|
|
|
381
821
|
getUserPermissions,
|
|
382
822
|
getChannelsPermissions,
|
|
383
823
|
findOneAndUpdateUserRole,
|
|
824
|
+
findSingleUserRoleByFilter,
|
|
384
825
|
};
|