shuttlepro-shared 1.1.93 → 1.1.95

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.
@@ -0,0 +1,5 @@
1
+ const userRoleAndPermissionService = require("./userRoleAndPermissions");
2
+
3
+ exports.module = {
4
+ userRoleAndPermissionService,
5
+ };
@@ -0,0 +1,88 @@
1
+ const UserRole = require("../../models/UserRole");
2
+ const { convertPermissionsArrayToObject } = require("../../utils");
3
+
4
+ const getUserRoleWithPermissions = async (workspaceId, userId) => {
5
+ try {
6
+ let data = await UserRole.findOne({
7
+ workspaceId: workspaceId,
8
+ userId: userId,
9
+ })
10
+ .populate({
11
+ path: "roleId",
12
+ select:
13
+ "id name userId userShift permissionId modulePermissions defaultModule",
14
+ populate: [{ path: "permissionId", select: "id name" }],
15
+ })
16
+ .lean()
17
+ .exec();
18
+ return {
19
+ ...data,
20
+ roleId: data?.roleId?._id?.toString() || "",
21
+ role: data?.roleId?.name || "",
22
+ permissions:
23
+ convertPermissionsArrayToObject(
24
+ data?.roleId?.modulePermissions?.modules
25
+ ) || [],
26
+ parentRole: data?.roleId?.permissionId?.name || "",
27
+ parentRoleId: data?.roleId?.permissionId?._id?.toString() || "",
28
+ defaultModule: data?.roleId?.defaultModule || "",
29
+ };
30
+ } catch (err) {
31
+ console.error("Error in getUserRole:", err);
32
+ return null;
33
+ }
34
+ };
35
+
36
+ const getUserPermissions = async (workspaceId, userId) => {
37
+ try {
38
+ let data = await UserRole.findOne({
39
+ workspaceId: workspaceId,
40
+ userId: userId,
41
+ })
42
+ .select("roleId userId")
43
+ .populate({
44
+ path: "roleId",
45
+ select: "modulePermissions",
46
+ })
47
+ .lean()
48
+ .exec();
49
+ return (
50
+ convertPermissionsArrayToObject(
51
+ data?.roleId?.modulePermissions?.modules
52
+ ) || null
53
+ );
54
+ } catch (err) {
55
+ console.error("Error in getUserRole:", err);
56
+ return null;
57
+ }
58
+ };
59
+
60
+ const getChannelsPermissions = async (workspaceId, userId) => {
61
+ try {
62
+ let data = await UserRole.findOne({
63
+ workspaceId: workspaceId,
64
+ userId: userId,
65
+ })
66
+ .select("roleId userId")
67
+ .populate({
68
+ path: "roleId",
69
+ select: "modulePermissions",
70
+ })
71
+ .lean()
72
+ .exec();
73
+ return (
74
+ convertPermissionsArrayToObject(
75
+ data?.roleId?.modulePermissions?.modules
76
+ )?.["coversations"]?.["integrationPermissions"] || []
77
+ );
78
+ } catch (err) {
79
+ console.error("Error in getUserRole:", err);
80
+ return null;
81
+ }
82
+ };
83
+
84
+ exports.module = {
85
+ getUserRoleWithPermissions,
86
+ getUserPermissions,
87
+ getChannelsPermissions,
88
+ };
package/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  const sharedModels = require("./models");
2
2
  const sharedFunctions = require("./functions");
3
+ const services = require("./common/services");
3
4
 
4
5
  module.exports = {
5
6
  sharedModels,
6
7
  sharedFunctions,
8
+ services,
7
9
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shuttlepro-shared",
3
- "version": "1.1.93",
3
+ "version": "1.1.95",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/utils/index.js CHANGED
@@ -22,3 +22,26 @@ exports.getModule = (obj, module) => {
22
22
  null
23
23
  );
24
24
  };
25
+
26
+ exports.convertPermissionsArrayToObject = (originalArray) => {
27
+ return originalArray?.reduce((acc, item) => {
28
+ const { id, actions, integrationPermissions, ...extras } = item;
29
+ const formattedExtras = {};
30
+ if (extras.deletable !== undefined) {
31
+ formattedExtras.deletable = extras.deletable;
32
+ }
33
+ if (extras.createUpdateable !== undefined) {
34
+ formattedExtras.createUpdateable = extras.createUpdateable;
35
+ }
36
+ acc[id] = {
37
+ actions: Object.keys(actions).filter((action) => actions[action]),
38
+ ...formattedExtras,
39
+ ...(integrationPermissions && {
40
+ integrationPermissions: Object.entries(integrationPermissions)
41
+ .filter(([_, allowed]) => allowed)
42
+ .map(([key]) => key),
43
+ }),
44
+ };
45
+ return acc;
46
+ }, {});
47
+ };