shuttlepro-shared 1.1.95 → 1.1.96
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.
- package/index.js +0 -2
- package/models/UserRole.js +106 -0
- package/package.json +1 -1
- package/utils/index.js +0 -23
- package/common/services/index.js +0 -5
- package/common/services/userRoleAndPermissions.js +0 -88
package/index.js
CHANGED
package/models/UserRole.js
CHANGED
|
@@ -52,6 +52,112 @@ const userRoleSchema = new Schema(
|
|
|
52
52
|
|
|
53
53
|
const UserRole = mongoose.model("UserRole", userRoleSchema);
|
|
54
54
|
|
|
55
|
+
const convertPermissionsArrayToObject = (originalArray) => {
|
|
56
|
+
return originalArray?.reduce((acc, item) => {
|
|
57
|
+
const { id, actions, integrationPermissions, ...extras } = item;
|
|
58
|
+
const formattedExtras = {};
|
|
59
|
+
if (extras.deletable !== undefined) {
|
|
60
|
+
formattedExtras.deletable = extras.deletable;
|
|
61
|
+
}
|
|
62
|
+
if (extras.createUpdateable !== undefined) {
|
|
63
|
+
formattedExtras.createUpdateable = extras.createUpdateable;
|
|
64
|
+
}
|
|
65
|
+
acc[id] = {
|
|
66
|
+
actions: Object.keys(actions).filter((action) => actions[action]),
|
|
67
|
+
...formattedExtras,
|
|
68
|
+
...(integrationPermissions && {
|
|
69
|
+
integrationPermissions: Object.entries(integrationPermissions)
|
|
70
|
+
.filter(([_, allowed]) => allowed)
|
|
71
|
+
.map(([key]) => key),
|
|
72
|
+
}),
|
|
73
|
+
};
|
|
74
|
+
return acc;
|
|
75
|
+
}, {});
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const getUserRoleWithPermissions = async (workspaceId, userId) => {
|
|
79
|
+
try {
|
|
80
|
+
let data = await UserRole.findOne({
|
|
81
|
+
workspaceId: workspaceId,
|
|
82
|
+
userId: userId,
|
|
83
|
+
})
|
|
84
|
+
.populate({
|
|
85
|
+
path: "roleId",
|
|
86
|
+
select:
|
|
87
|
+
"id name userId userShift permissionId modulePermissions defaultModule",
|
|
88
|
+
populate: [{ path: "permissionId", select: "id name" }],
|
|
89
|
+
})
|
|
90
|
+
.lean()
|
|
91
|
+
.exec();
|
|
92
|
+
return {
|
|
93
|
+
...data,
|
|
94
|
+
roleId: data?.roleId?._id?.toString() || "",
|
|
95
|
+
role: data?.roleId?.name || "",
|
|
96
|
+
permissions:
|
|
97
|
+
convertPermissionsArrayToObject(
|
|
98
|
+
data?.roleId?.modulePermissions?.modules
|
|
99
|
+
) || [],
|
|
100
|
+
parentRole: data?.roleId?.permissionId?.name || "",
|
|
101
|
+
parentRoleId: data?.roleId?.permissionId?._id?.toString() || "",
|
|
102
|
+
defaultModule: data?.roleId?.defaultModule || "",
|
|
103
|
+
};
|
|
104
|
+
} catch (err) {
|
|
105
|
+
console.error("Error in getUserRole:", err);
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const getUserPermissions = async (workspaceId, userId) => {
|
|
111
|
+
try {
|
|
112
|
+
let data = await UserRole.findOne({
|
|
113
|
+
workspaceId: workspaceId,
|
|
114
|
+
userId: userId,
|
|
115
|
+
})
|
|
116
|
+
.select("roleId userId")
|
|
117
|
+
.populate({
|
|
118
|
+
path: "roleId",
|
|
119
|
+
select: "modulePermissions",
|
|
120
|
+
})
|
|
121
|
+
.lean()
|
|
122
|
+
.exec();
|
|
123
|
+
return (
|
|
124
|
+
convertPermissionsArrayToObject(
|
|
125
|
+
data?.roleId?.modulePermissions?.modules
|
|
126
|
+
) || null
|
|
127
|
+
);
|
|
128
|
+
} catch (err) {
|
|
129
|
+
console.error("Error in getUserRole:", err);
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const getChannelsPermissions = async (workspaceId, userId) => {
|
|
135
|
+
try {
|
|
136
|
+
let data = await UserRole.findOne({
|
|
137
|
+
workspaceId: workspaceId,
|
|
138
|
+
userId: userId,
|
|
139
|
+
})
|
|
140
|
+
.select("roleId userId")
|
|
141
|
+
.populate({
|
|
142
|
+
path: "roleId",
|
|
143
|
+
select: "modulePermissions",
|
|
144
|
+
})
|
|
145
|
+
.lean()
|
|
146
|
+
.exec();
|
|
147
|
+
return (
|
|
148
|
+
convertPermissionsArrayToObject(
|
|
149
|
+
data?.roleId?.modulePermissions?.modules
|
|
150
|
+
)?.["coversations"]?.["integrationPermissions"] || []
|
|
151
|
+
);
|
|
152
|
+
} catch (err) {
|
|
153
|
+
console.error("Error in getUserRole:", err);
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
55
158
|
module.exports = {
|
|
56
159
|
UserRole,
|
|
160
|
+
getChannelsPermissions,
|
|
161
|
+
getUserPermissions,
|
|
162
|
+
getUserRoleWithPermissions,
|
|
57
163
|
};
|
package/package.json
CHANGED
package/utils/index.js
CHANGED
|
@@ -22,26 +22,3 @@ 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
|
-
};
|
package/common/services/index.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
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
|
-
};
|