shuttlepro-shared 1.2.85 → 1.2.87
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/common/repositories/role.repository.js +129 -107
- package/config/redis.js +5 -0
- package/package.json +1 -1
|
@@ -1,61 +1,83 @@
|
|
|
1
1
|
const Role = require("../../models/Role");
|
|
2
2
|
const { getRedisData, setRedisData } = require("../../config/redis");
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
4
|
+
// Cache keys
|
|
5
|
+
const ROLES = "roles";
|
|
6
|
+
const DEFAULT_ROLES = "default_roles";
|
|
7
|
+
const CACHE_TIME = 3600 * 2;
|
|
8
|
+
|
|
9
|
+
// Update the cache for workspace roles
|
|
10
|
+
const updateCachedRoles = async (workspaceId) => {
|
|
11
|
+
try {
|
|
12
|
+
const roles = await Role.find({ workspaceId })
|
|
13
|
+
.populate("permissionId")
|
|
14
|
+
.lean()
|
|
15
|
+
.exec();
|
|
16
|
+
await setRedisData(workspaceId, roles, ROLES, CACHE_TIME);
|
|
17
|
+
return roles;
|
|
18
|
+
} catch (error) {
|
|
19
|
+
console.error("Error updating cached roles:", error);
|
|
20
|
+
return [];
|
|
14
21
|
}
|
|
15
|
-
return roles;
|
|
16
22
|
};
|
|
17
23
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
await
|
|
24
|
+
// Get roles from cache or database for a specific workspace
|
|
25
|
+
const getRoles = async (workspaceId) => {
|
|
26
|
+
let roles = await getRedisData(workspaceId, ROLES);
|
|
27
|
+
if (!roles || roles?.length === 0) {
|
|
28
|
+
roles = await Role.find({ workspaceId })
|
|
29
|
+
.populate("permissionId")
|
|
30
|
+
.lean()
|
|
31
|
+
.exec();
|
|
32
|
+
await setRedisData(workspaceId, roles, ROLES, CACHE_TIME);
|
|
33
|
+
}
|
|
34
|
+
return roles;
|
|
21
35
|
};
|
|
22
36
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
37
|
+
// Update default roles cache (system-wide roles with no workspace)
|
|
38
|
+
const updateCachedDefaultRoles = async () => {
|
|
39
|
+
try {
|
|
26
40
|
const data = await Role.find({ name: { $ne: "super" }, workspaceId: null })
|
|
27
41
|
.select("name")
|
|
28
42
|
.sort({ _id: -1 })
|
|
29
43
|
.lean()
|
|
30
44
|
.exec();
|
|
31
45
|
|
|
32
|
-
defaultRoles = data.map((d) => {
|
|
46
|
+
const defaultRoles = data.map((d) => {
|
|
33
47
|
return {
|
|
34
48
|
id: d?._id,
|
|
35
49
|
name: d?.name,
|
|
36
50
|
};
|
|
37
51
|
});
|
|
38
52
|
|
|
39
|
-
await setRedisData(
|
|
53
|
+
await setRedisData("system", defaultRoles, DEFAULT_ROLES, CACHE_TIME);
|
|
54
|
+
return defaultRoles;
|
|
55
|
+
} catch (error) {
|
|
56
|
+
console.error("Error updating cached default roles:", error);
|
|
57
|
+
return [];
|
|
40
58
|
}
|
|
41
|
-
return defaultRoles;
|
|
42
59
|
};
|
|
43
60
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
.
|
|
49
|
-
|
|
61
|
+
// Get default roles from cache or database
|
|
62
|
+
const getDefaultRoles = async () => {
|
|
63
|
+
let defaultRoles = await getRedisData("system", DEFAULT_ROLES);
|
|
64
|
+
if (!defaultRoles || defaultRoles?.length === 0) {
|
|
65
|
+
const data = await Role.find({ name: { $ne: "super" }, workspaceId: null })
|
|
66
|
+
.select("name")
|
|
67
|
+
.sort({ _id: -1 })
|
|
68
|
+
.lean()
|
|
69
|
+
.exec();
|
|
50
70
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
71
|
+
defaultRoles = data.map((d) => {
|
|
72
|
+
return {
|
|
73
|
+
id: d?._id,
|
|
74
|
+
name: d?.name,
|
|
75
|
+
};
|
|
76
|
+
});
|
|
57
77
|
|
|
58
|
-
|
|
78
|
+
await setRedisData("system", defaultRoles, DEFAULT_ROLES, CACHE_TIME);
|
|
79
|
+
}
|
|
80
|
+
return defaultRoles;
|
|
59
81
|
};
|
|
60
82
|
|
|
61
83
|
const createRole = async (roleData) => {
|
|
@@ -64,31 +86,56 @@ const createRole = async (roleData) => {
|
|
|
64
86
|
.populate("permissionId")
|
|
65
87
|
.lean()
|
|
66
88
|
.exec();
|
|
67
|
-
|
|
89
|
+
|
|
90
|
+
if (createdRole.workspaceId) {
|
|
91
|
+
await updateCachedRoles(createdRole.workspaceId);
|
|
92
|
+
} else {
|
|
93
|
+
// If it's a default/system role
|
|
94
|
+
await updateCachedDefaultRoles();
|
|
95
|
+
}
|
|
96
|
+
|
|
68
97
|
return createdRole;
|
|
69
98
|
};
|
|
70
99
|
|
|
71
|
-
const findRoleById = async (id) => {
|
|
72
|
-
const roles = await
|
|
73
|
-
const role = roles.find((r) => r._id.toString() === id)
|
|
100
|
+
const findRoleById = async (id, workspaceId) => {
|
|
101
|
+
const roles = await getRoles(workspaceId);
|
|
102
|
+
const role = roles.find((r) => r._id.toString() === id);
|
|
74
103
|
|
|
75
104
|
if (role) return role;
|
|
76
105
|
|
|
77
106
|
// If not found in cache, query directly
|
|
78
|
-
|
|
107
|
+
const freshRole = await Role.findById(id)
|
|
108
|
+
.populate("permissionId")
|
|
109
|
+
.lean()
|
|
110
|
+
.exec();
|
|
111
|
+
if (freshRole && freshRole.workspaceId?.toString() === workspaceId) {
|
|
112
|
+
// Update the cache with the new data
|
|
113
|
+
await updateCachedRoles(workspaceId);
|
|
114
|
+
}
|
|
115
|
+
return freshRole;
|
|
79
116
|
};
|
|
80
117
|
|
|
81
118
|
const findSingleRole = async (filter) => {
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
roles
|
|
119
|
+
const workspaceId = filter.workspaceId;
|
|
120
|
+
if (workspaceId) {
|
|
121
|
+
const roles = await getRoles(workspaceId);
|
|
122
|
+
const role = roles.find((r) =>
|
|
85
123
|
Object.entries(filter).every(([key, value]) => r[key] === value)
|
|
86
|
-
)
|
|
124
|
+
);
|
|
87
125
|
|
|
88
|
-
|
|
126
|
+
if (role) return role;
|
|
127
|
+
}
|
|
89
128
|
|
|
90
|
-
// If not found in cache, query directly
|
|
91
|
-
|
|
129
|
+
// If not found in cache or no workspaceId provided, query directly
|
|
130
|
+
const freshRole = await Role.findOne(filter)
|
|
131
|
+
.populate("permissionId")
|
|
132
|
+
.lean()
|
|
133
|
+
.exec();
|
|
134
|
+
if (freshRole && freshRole.workspaceId) {
|
|
135
|
+
// Update the cache with the new data
|
|
136
|
+
await updateCachedRoles(freshRole.workspaceId);
|
|
137
|
+
}
|
|
138
|
+
return freshRole;
|
|
92
139
|
};
|
|
93
140
|
|
|
94
141
|
const findRolesByPagination = async (body) => {
|
|
@@ -126,12 +173,11 @@ const findRolesByPagination = async (body) => {
|
|
|
126
173
|
};
|
|
127
174
|
|
|
128
175
|
const findRoles = async (workspaceId) => {
|
|
129
|
-
|
|
130
|
-
return roles.filter((r) => r.workspaceId === workspaceId);
|
|
176
|
+
return await getRoles(workspaceId);
|
|
131
177
|
};
|
|
132
178
|
|
|
133
179
|
const findDefaultRoles = async () => {
|
|
134
|
-
return await
|
|
180
|
+
return await getDefaultRoles();
|
|
135
181
|
};
|
|
136
182
|
|
|
137
183
|
const updateRoleById = async (id, updateData) => {
|
|
@@ -151,7 +197,9 @@ const updateRoleById = async (id, updateData) => {
|
|
|
151
197
|
.exec();
|
|
152
198
|
|
|
153
199
|
if (updatedRole) {
|
|
154
|
-
|
|
200
|
+
if (updatedRole.workspaceId) {
|
|
201
|
+
await updateCachedRoles(updatedRole.workspaceId);
|
|
202
|
+
}
|
|
155
203
|
// Update default roles cache if needed
|
|
156
204
|
if (!updatedRole.workspaceId) {
|
|
157
205
|
await updateCachedDefaultRoles();
|
|
@@ -170,7 +218,9 @@ const updateRoleByFilter = async (filter, updateData) => {
|
|
|
170
218
|
.exec();
|
|
171
219
|
|
|
172
220
|
if (updatedRole) {
|
|
173
|
-
|
|
221
|
+
if (updatedRole.workspaceId) {
|
|
222
|
+
await updateCachedRoles(updatedRole.workspaceId);
|
|
223
|
+
}
|
|
174
224
|
// Update default roles cache if needed
|
|
175
225
|
if (!updatedRole.workspaceId) {
|
|
176
226
|
await updateCachedDefaultRoles();
|
|
@@ -182,20 +232,21 @@ const updateRoleByFilter = async (filter, updateData) => {
|
|
|
182
232
|
|
|
183
233
|
const updateRolesByFilter = async (filter, updateData) => {
|
|
184
234
|
const result = await Role.updateMany(filter, updateData, { new: true });
|
|
185
|
-
|
|
186
235
|
if (result.matchedCount > 0) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
236
|
+
if (filter.workspaceId) {
|
|
237
|
+
await updateCachedRoles(filter.workspaceId);
|
|
238
|
+
} else {
|
|
239
|
+
if (!filter.workspaceId || filter.workspaceId === null) {
|
|
240
|
+
await updateCachedDefaultRoles();
|
|
241
|
+
}
|
|
192
242
|
}
|
|
193
243
|
}
|
|
194
|
-
|
|
195
244
|
return result;
|
|
196
245
|
};
|
|
197
246
|
|
|
198
247
|
const addLogToRole = async (id, logData) => {
|
|
248
|
+
const role = await Role.findById(id).lean().exec();
|
|
249
|
+
const workspaceId = role?.workspaceId;
|
|
199
250
|
const updatedRole = await Role.findByIdAndUpdate(
|
|
200
251
|
id,
|
|
201
252
|
{ $push: { logs: logData } },
|
|
@@ -205,14 +256,15 @@ const addLogToRole = async (id, logData) => {
|
|
|
205
256
|
.lean()
|
|
206
257
|
.exec();
|
|
207
258
|
|
|
208
|
-
if (updatedRole) {
|
|
209
|
-
await updateCachedRoles();
|
|
259
|
+
if (updatedRole && workspaceId) {
|
|
260
|
+
await updateCachedRoles(workspaceId);
|
|
210
261
|
}
|
|
211
|
-
|
|
212
262
|
return updatedRole;
|
|
213
263
|
};
|
|
214
264
|
|
|
215
265
|
const clearRoleLogs = async (id) => {
|
|
266
|
+
const role = await Role.findById(id).lean().exec();
|
|
267
|
+
const workspaceId = role?.workspaceId;
|
|
216
268
|
const updatedRole = await Role.findByIdAndUpdate(
|
|
217
269
|
id,
|
|
218
270
|
{ $set: { logs: [] } },
|
|
@@ -221,15 +273,15 @@ const clearRoleLogs = async (id) => {
|
|
|
221
273
|
.populate("permissionId")
|
|
222
274
|
.lean()
|
|
223
275
|
.exec();
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
await updateCachedRoles();
|
|
276
|
+
if (updatedRole && workspaceId) {
|
|
277
|
+
await updateCachedRoles(workspaceId);
|
|
227
278
|
}
|
|
228
|
-
|
|
229
279
|
return updatedRole;
|
|
230
280
|
};
|
|
231
281
|
|
|
232
282
|
const addPermissionToRole = async (id, permission) => {
|
|
283
|
+
const role = await Role.findById(id).lean().exec();
|
|
284
|
+
const workspaceId = role?.workspaceId;
|
|
233
285
|
const updatedRole = await Role.findByIdAndUpdate(
|
|
234
286
|
id,
|
|
235
287
|
{ $push: { permissions: permission } },
|
|
@@ -238,15 +290,16 @@ const addPermissionToRole = async (id, permission) => {
|
|
|
238
290
|
.populate("permissionId")
|
|
239
291
|
.lean()
|
|
240
292
|
.exec();
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
await updateCachedRoles();
|
|
293
|
+
if (updatedRole && workspaceId) {
|
|
294
|
+
await updateCachedRoles(workspaceId);
|
|
244
295
|
}
|
|
245
|
-
|
|
246
296
|
return updatedRole;
|
|
247
297
|
};
|
|
248
298
|
|
|
249
299
|
const removePermissionFromRole = async (id, permission) => {
|
|
300
|
+
const role = await Role.findById(id).lean().exec();
|
|
301
|
+
const workspaceId = role?.workspaceId;
|
|
302
|
+
|
|
250
303
|
const updatedRole = await Role.findByIdAndUpdate(
|
|
251
304
|
id,
|
|
252
305
|
{ $pull: { permissions: permission } },
|
|
@@ -256,56 +309,23 @@ const removePermissionFromRole = async (id, permission) => {
|
|
|
256
309
|
.lean()
|
|
257
310
|
.exec();
|
|
258
311
|
|
|
259
|
-
if (updatedRole) {
|
|
260
|
-
await updateCachedRoles();
|
|
312
|
+
if (updatedRole && workspaceId) {
|
|
313
|
+
await updateCachedRoles(workspaceId);
|
|
261
314
|
}
|
|
262
315
|
|
|
263
316
|
return updatedRole;
|
|
264
317
|
};
|
|
265
318
|
|
|
266
|
-
const deleteRoleById = async (id) => {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
if (deletedRole) {
|
|
270
|
-
await updateCachedRoles();
|
|
271
|
-
// Update default roles cache if needed
|
|
272
|
-
if (!deletedRole.workspaceId) {
|
|
273
|
-
await updateCachedDefaultRoles();
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
return deletedRole;
|
|
319
|
+
const deleteRoleById = async (id, workspaceId) => {
|
|
320
|
+
return await Role.deleteOne({ _id: id }).exec();
|
|
278
321
|
};
|
|
279
322
|
|
|
280
323
|
const deleteSingleRole = async (filter) => {
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
if (deletedRole) {
|
|
284
|
-
await updateCachedRoles();
|
|
285
|
-
// Update default roles cache if needed
|
|
286
|
-
if (!deletedRole.workspaceId) {
|
|
287
|
-
await updateCachedDefaultRoles();
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
return deletedRole;
|
|
324
|
+
return await Role.deleteOne(filter).exec();
|
|
292
325
|
};
|
|
293
326
|
|
|
294
327
|
const deleteManyRoles = async (filter) => {
|
|
295
|
-
|
|
296
|
-
const includesDefaultRoles =
|
|
297
|
-
!filter.workspaceId || filter.workspaceId === null;
|
|
298
|
-
|
|
299
|
-
const result = await Role.deleteMany(filter);
|
|
300
|
-
|
|
301
|
-
if (result.deletedCount > 0) {
|
|
302
|
-
await updateCachedRoles();
|
|
303
|
-
if (includesDefaultRoles) {
|
|
304
|
-
await updateCachedDefaultRoles();
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
return result;
|
|
328
|
+
return await Role.deleteMany(filter);
|
|
309
329
|
};
|
|
310
330
|
|
|
311
331
|
module.exports = {
|
|
@@ -325,4 +345,6 @@ module.exports = {
|
|
|
325
345
|
deleteManyRoles,
|
|
326
346
|
findRolesByPagination,
|
|
327
347
|
findDefaultRoles,
|
|
348
|
+
updateCachedRoles,
|
|
349
|
+
updateCachedDefaultRoles,
|
|
328
350
|
};
|
package/config/redis.js
CHANGED
|
@@ -81,8 +81,12 @@ const setRedisData = async (
|
|
|
81
81
|
await connectRedis();
|
|
82
82
|
const stringifiedValue = JSON.stringify(value);
|
|
83
83
|
let result;
|
|
84
|
+
|
|
84
85
|
if (field) {
|
|
85
86
|
result = await client.hSet(key, field, stringifiedValue);
|
|
87
|
+
if (expiryInSeconds) {
|
|
88
|
+
await client.expire(key, expiryInSeconds);
|
|
89
|
+
}
|
|
86
90
|
} else {
|
|
87
91
|
result = expiryInSeconds
|
|
88
92
|
? await client.set(key, stringifiedValue, { EX: expiryInSeconds })
|
|
@@ -94,6 +98,7 @@ const setRedisData = async (
|
|
|
94
98
|
return null;
|
|
95
99
|
}
|
|
96
100
|
};
|
|
101
|
+
|
|
97
102
|
const addDataToRedisSet = async (key, value) => {
|
|
98
103
|
try {
|
|
99
104
|
await connectRedis();
|