shuttlepro-shared 1.2.64 → 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,7 +410,6 @@ 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
415
|
// Comprehensive cache update method
|
|
@@ -15,11 +418,6 @@ const updateAllRelatedCaches = async (userId, workspaceId) => {
|
|
|
15
418
|
// Update all members cache
|
|
16
419
|
await updateCachedMembers();
|
|
17
420
|
|
|
18
|
-
// Clear user workspace cache
|
|
19
|
-
if (userId) {
|
|
20
|
-
await clearUserWorkspaceCache(userId);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
421
|
// Clear user permissions cache
|
|
24
422
|
if (workspaceId && userId) {
|
|
25
423
|
await clearUserPermissionsCache(workspaceId, userId);
|
|
@@ -29,31 +427,49 @@ const updateAllRelatedCaches = async (userId, workspaceId) => {
|
|
|
29
427
|
}
|
|
30
428
|
};
|
|
31
429
|
|
|
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
430
|
// Clear user permissions cache
|
|
43
431
|
const clearUserPermissionsCache = async (workspaceId, userId) => {
|
|
44
432
|
const cacheKey = `${CACHE_KEY_USER_PERMISSIONS_PREFIX}${workspaceId}_${userId}`;
|
|
45
433
|
await setRedisData(cacheKey, null, null, 0);
|
|
46
434
|
};
|
|
47
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
|
+
|
|
48
470
|
// Get cached members
|
|
49
471
|
const getCachedMembers = async (filter = {}) => {
|
|
50
|
-
|
|
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
|
-
|
|
472
|
+
let members = await getRedisData(CACHE_KEY_ALL_MEMBERS);
|
|
57
473
|
if (!members) {
|
|
58
474
|
members = await UserRole.find(filter)
|
|
59
475
|
.select("userId role userShift roleId")
|
|
@@ -71,23 +487,23 @@ const getCachedMembers = async (filter = {}) => {
|
|
|
71
487
|
.exec();
|
|
72
488
|
|
|
73
489
|
await setRedisData(
|
|
74
|
-
|
|
490
|
+
CACHE_KEY_ALL_MEMBERS,
|
|
75
491
|
members?.filter((member) => member?.userId && member?.roleId),
|
|
76
492
|
null,
|
|
77
|
-
|
|
493
|
+
3600
|
|
78
494
|
);
|
|
79
495
|
}
|
|
80
496
|
|
|
81
|
-
|
|
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;
|
|
82
503
|
};
|
|
83
504
|
|
|
84
505
|
// Update the cache for all members
|
|
85
506
|
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
507
|
const members = await UserRole.find(filter)
|
|
92
508
|
.select("userId role userShift roleId")
|
|
93
509
|
.populate({
|
|
@@ -102,12 +518,11 @@ const updateCachedMembers = async (filter = {}) => {
|
|
|
102
518
|
})
|
|
103
519
|
.lean()
|
|
104
520
|
.exec();
|
|
105
|
-
|
|
106
521
|
await setRedisData(
|
|
107
|
-
|
|
522
|
+
CACHE_KEY_ALL_MEMBERS,
|
|
108
523
|
members?.filter((member) => member?.userId && member?.roleId),
|
|
109
524
|
null,
|
|
110
|
-
|
|
525
|
+
3600
|
|
111
526
|
);
|
|
112
527
|
return members;
|
|
113
528
|
};
|
|
@@ -124,6 +539,23 @@ const createUserRole = async (data) => {
|
|
|
124
539
|
return savedRole;
|
|
125
540
|
};
|
|
126
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
|
+
|
|
127
559
|
const findUserRole = async (filter = {}) => {
|
|
128
560
|
return await getCachedMembers(filter);
|
|
129
561
|
};
|
|
@@ -174,12 +606,10 @@ const deleteUserRole = async (id) => {
|
|
|
174
606
|
};
|
|
175
607
|
|
|
176
608
|
const findUserWorkspaces = async (userId) => {
|
|
177
|
-
|
|
178
|
-
|
|
609
|
+
let userRoleWithWorkspaces = await getRedisData(CACHE_KEY_ALL_MEMBERS);
|
|
610
|
+
|
|
179
611
|
if (!userRoleWithWorkspaces) {
|
|
180
|
-
userRoleWithWorkspaces = await UserRole.find(
|
|
181
|
-
userId: userId,
|
|
182
|
-
})
|
|
612
|
+
userRoleWithWorkspaces = await UserRole.find()
|
|
183
613
|
.select("userId roleId workspaceId")
|
|
184
614
|
.populate({
|
|
185
615
|
path: "roleId",
|
|
@@ -193,6 +623,7 @@ const findUserWorkspaces = async (userId) => {
|
|
|
193
623
|
})
|
|
194
624
|
.lean()
|
|
195
625
|
.exec();
|
|
626
|
+
|
|
196
627
|
userRoleWithWorkspaces = userRoleWithWorkspaces
|
|
197
628
|
?.filter((userRole) => userRole?.workspaceId)
|
|
198
629
|
?.map((ur) => {
|
|
@@ -204,11 +635,21 @@ const findUserWorkspaces = async (userId) => {
|
|
|
204
635
|
permissions: ur?.roleId?.modulePermissions?.modules || [],
|
|
205
636
|
};
|
|
206
637
|
});
|
|
638
|
+
|
|
207
639
|
if (userRoleWithWorkspaces) {
|
|
208
|
-
await setRedisData(
|
|
640
|
+
await setRedisData(
|
|
641
|
+
CACHE_KEY_ALL_MEMBERS,
|
|
642
|
+
userRoleWithWorkspaces,
|
|
643
|
+
null,
|
|
644
|
+
3600
|
|
645
|
+
);
|
|
209
646
|
}
|
|
210
647
|
}
|
|
211
|
-
|
|
648
|
+
|
|
649
|
+
// Filter workspaces for specific user
|
|
650
|
+
return userRoleWithWorkspaces?.filter(
|
|
651
|
+
(workspace) => workspace.userId?.toString() === userId.toString()
|
|
652
|
+
);
|
|
212
653
|
};
|
|
213
654
|
|
|
214
655
|
const convertPermissionsArrayToObject = (originalArray) => {
|
|
@@ -360,7 +801,7 @@ const getChannelsPermissions = async (workspaceId, userId) => {
|
|
|
360
801
|
// Store in cache with other permission data
|
|
361
802
|
const permissionsData = cachedData || {};
|
|
362
803
|
permissionsData.channelsPermissions = channelsPermissions;
|
|
363
|
-
await setRedisData(cacheKey, permissionsData,
|
|
804
|
+
await setRedisData(cacheKey, permissionsData, 3600);
|
|
364
805
|
|
|
365
806
|
return channelsPermissions;
|
|
366
807
|
} catch (err) {
|
|
@@ -369,26 +810,6 @@ const getChannelsPermissions = async (workspaceId, userId) => {
|
|
|
369
810
|
}
|
|
370
811
|
};
|
|
371
812
|
|
|
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
813
|
module.exports = {
|
|
393
814
|
createUserRole,
|
|
394
815
|
findUserRole,
|
|
@@ -400,4 +821,5 @@ module.exports = {
|
|
|
400
821
|
getUserPermissions,
|
|
401
822
|
getChannelsPermissions,
|
|
402
823
|
findOneAndUpdateUserRole,
|
|
824
|
+
findSingleUserRoleByFilter,
|
|
403
825
|
};
|