shuttlepro-shared 1.2.66 → 1.2.68

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,6 +1,5 @@
1
1
  const { UserRole } = require("../../models/UserRole");
2
2
  const { getRedisData, setRedisData } = require("../../config/redis");
3
- const mongoose = require("mongoose");
4
3
 
5
4
  const defaultSelect =
6
5
  "iconUrl thumbUrl name productsCount ordersCount contact email address websiteUrl facebookUrl twitterUrl instagramUrl members defaultModule";
@@ -231,7 +230,7 @@ const deleteUserRole = async (id) => {
231
230
  };
232
231
 
233
232
  const findUserWorkspaces = async (userId) => {
234
- return await UserRole.find({ userId: userId })
233
+ let userRoleWithWorkspaces = await UserRole.find({ userId: userId })
235
234
  .select("userId roleId workspaceId")
236
235
  .populate({
237
236
  path: "roleId",
@@ -257,97 +256,50 @@ const findUserWorkspaces = async (userId) => {
257
256
  permissions: ur?.roleId?.modulePermissions?.modules || [],
258
257
  };
259
258
  });
259
+ return userRoleWithWorkspaces || [];
260
260
  };
261
261
 
262
- const getUserRoleWithPermissions = async (workspaceId, userId) => {
263
- const cacheKey = `${CACHE_KEY_USER_PERMISSIONS_PREFIX}${workspaceId}_${userId}`;
264
- let cachedData = await getRedisData(cacheKey);
265
-
266
- if (cachedData && cachedData.roleWithPermissions) {
267
- return cachedData.roleWithPermissions;
268
- }
269
-
262
+ const getUserPermissions = async (workspaceId, userId) => {
270
263
  try {
271
- let data = await UserRole.findOne({
272
- workspaceId: workspaceId,
273
- userId: userId,
274
- })
275
- .populate({
276
- path: "roleId",
277
- select:
278
- "id name userId userShift permissionId modulePermissions defaultModule",
279
- populate: [{ path: "permissionId", select: "id name" }],
280
- })
281
- .lean()
282
- .exec();
283
-
284
- if (!data) {
285
- return null;
286
- }
287
-
288
- const roleWithPermissions = {
289
- ...data,
290
- roleId: data?.roleId?._id?.toString() || "",
291
- role: data?.roleId?.name || "",
292
- permissions:
264
+ let members = await getCachedMembers({ workspaceId, userId });
265
+ console.log(members, "members");
266
+ if (members?.length) {
267
+ return (
293
268
  convertPermissionsArrayToObject(
294
- data?.roleId?.modulePermissions?.modules
295
- ) || [],
296
- parentRole: data?.roleId?.permissionId?.name || "",
297
- parentRoleId: data?.roleId?.permissionId?._id?.toString() || "",
298
- defaultModule: data?.roleId?.defaultModule || "",
299
- };
300
-
301
- // Store in cache with other permission data
302
- const permissionsData = cachedData || {};
303
- permissionsData.roleWithPermissions = roleWithPermissions;
304
- await setRedisData(cacheKey, permissionsData);
305
-
306
- return roleWithPermissions;
269
+ members?.[0]?.roleId?.modulePermissions?.modules
270
+ ) || null
271
+ );
272
+ }
273
+ return null;
307
274
  } catch (err) {
308
- console.error("Error in getUserRoleWithPermissions:", err);
275
+ console.error("Error in getUserPermissions:", err);
309
276
  return null;
310
277
  }
311
278
  };
312
279
 
313
- const getUserPermissions = async (workspaceId, userId) => {
314
- const cacheKey = `${CACHE_KEY_USER_PERMISSIONS_PREFIX}${workspaceId}_${userId}`;
315
- let cachedData = await getRedisData(cacheKey);
316
-
317
- if (cachedData && cachedData.permissions) {
318
- return cachedData.permissions;
319
- }
320
-
280
+ const getUserRoleWithPermissions = async (workspaceId, userId) => {
321
281
  try {
322
- let data = await UserRole.findOne({
323
- workspaceId: workspaceId,
324
- userId: userId,
325
- })
326
- .select("roleId userId")
327
- .populate({
328
- path: "roleId",
329
- select: "modulePermissions",
330
- })
331
- .lean()
332
- .exec();
333
-
334
- if (!data) {
335
- return null;
282
+ let members = await getCachedMembers({ workspaceId, userId });
283
+ console.log(members, "members");
284
+ if (members?.length) {
285
+ let data = members?.[0];
286
+ const roleWithPermissions = {
287
+ ...data,
288
+ roleId: data?.roleId?._id?.toString() || "",
289
+ role: data?.roleId?.name || "",
290
+ permissions:
291
+ convertPermissionsArrayToObject(
292
+ data?.roleId?.modulePermissions?.modules
293
+ ) || [],
294
+ parentRole: data?.roleId?.permissionId?.name || "",
295
+ parentRoleId: data?.roleId?.permissionId?._id?.toString() || "",
296
+ defaultModule: data?.roleId?.defaultModule || "",
297
+ };
298
+ return roleWithPermissions || null;
336
299
  }
337
-
338
- const permissions =
339
- convertPermissionsArrayToObject(
340
- data?.roleId?.modulePermissions?.modules
341
- ) || null;
342
-
343
- // Store in cache with other permission data
344
- const permissionsData = cachedData || {};
345
- permissionsData.permissions = permissions;
346
- await setRedisData(cacheKey, permissionsData);
347
-
348
- return permissions;
300
+ return null;
349
301
  } catch (err) {
350
- console.error("Error in getUserPermissions:", err);
302
+ console.error("Error in getUserRoleWithPermissions:", err);
351
303
  return null;
352
304
  }
353
305
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shuttlepro-shared",
3
- "version": "1.2.66",
3
+ "version": "1.2.68",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {