shuttlepro-shared 1.2.40 → 1.2.43
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.
|
@@ -79,6 +79,30 @@ const findTemplateByFilter = async (
|
|
|
79
79
|
) || null
|
|
80
80
|
);
|
|
81
81
|
};
|
|
82
|
+
const findAllTemplateByFilter = async (
|
|
83
|
+
filter,
|
|
84
|
+
bodyFilter = {},
|
|
85
|
+
workspaceId = null
|
|
86
|
+
) => {
|
|
87
|
+
const allTemplates = await getCachedAllDescriptionTemplates();
|
|
88
|
+
|
|
89
|
+
// Filter by workspaceId if provided
|
|
90
|
+
let filteredTemplates = workspaceId
|
|
91
|
+
? allTemplates.filter((desc) => desc.workspaceId === workspaceId)
|
|
92
|
+
: allTemplates;
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
filteredTemplates.filter(
|
|
96
|
+
(item) =>
|
|
97
|
+
// Check direct properties of item
|
|
98
|
+
Object.entries(filter).every(([key, value]) => item[key] === value) &&
|
|
99
|
+
// If bodyFilter is provided, check inside item.body
|
|
100
|
+
Object.entries(bodyFilter).every(
|
|
101
|
+
([key, value]) => item.body?.[key] === value
|
|
102
|
+
)
|
|
103
|
+
) || null
|
|
104
|
+
);
|
|
105
|
+
};
|
|
82
106
|
const createOrUpdateTemplate = async (filter, data) => {
|
|
83
107
|
const template = await DescriptionTemplate.findOneAndUpdate(filter, data, {
|
|
84
108
|
upsert: true,
|
|
@@ -201,4 +225,5 @@ module.exports = {
|
|
|
201
225
|
findTemplateById,
|
|
202
226
|
deleteTemplatesByFilter,
|
|
203
227
|
insertManyTemplates,
|
|
228
|
+
findAllTemplateByFilter,
|
|
204
229
|
};
|
|
@@ -1,61 +1,12 @@
|
|
|
1
1
|
const { UserRole } = require("../../models/UserRole");
|
|
2
|
-
const { getRedisData, setRedisData } = require("../../config/redis");
|
|
3
2
|
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const CACHE_KEY_ALL_MEMBERS = "userroles_all_members";
|
|
9
|
-
const CACHE_KEY_USER_WORKSPACE_PREFIX = "userrole_workspace_";
|
|
10
|
-
|
|
11
|
-
// Clear user workspace cache
|
|
12
|
-
const clearUserWorkspaceCache = async (userId) => {
|
|
13
|
-
await setRedisData(
|
|
14
|
-
`${CACHE_KEY_USER_WORKSPACE_PREFIX}${userId}`,
|
|
15
|
-
null,
|
|
16
|
-
null,
|
|
17
|
-
0
|
|
18
|
-
);
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
// Get cached members
|
|
22
|
-
const getCachedMembers = async (filter = {}) => {
|
|
23
|
-
const cacheKey =
|
|
24
|
-
Object.keys(filter).length > 0
|
|
25
|
-
? `${CACHE_KEY_ALL_MEMBERS}_${JSON.stringify(filter)}`
|
|
26
|
-
: CACHE_KEY_ALL_MEMBERS;
|
|
27
|
-
|
|
28
|
-
let members = await getRedisData(cacheKey);
|
|
29
|
-
|
|
30
|
-
if (!members) {
|
|
31
|
-
members = await UserRole.find(filter)
|
|
32
|
-
.select("userId role userShift roleId")
|
|
33
|
-
.populate({
|
|
34
|
-
path: "roleId",
|
|
35
|
-
select: "id name userId userShift permissionId modulePermissions",
|
|
36
|
-
populate: [{ path: "permissionId", select: "id name" }],
|
|
37
|
-
})
|
|
38
|
-
.populate({
|
|
39
|
-
path: "userId",
|
|
40
|
-
match: { isDeleted: false, suspended: false },
|
|
41
|
-
select: "firstName lastName role email picture userShift",
|
|
42
|
-
})
|
|
43
|
-
.lean()
|
|
44
|
-
.exec();
|
|
45
|
-
|
|
46
|
-
await setRedisData(cacheKey, members, null, 0);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return members;
|
|
3
|
+
const createUserRole = async (data) => {
|
|
4
|
+
const newRole = new UserRole(data);
|
|
5
|
+
const savedRole = await newRole.save();
|
|
6
|
+
return savedRole;
|
|
50
7
|
};
|
|
51
8
|
|
|
52
|
-
|
|
53
|
-
const updateCachedMembers = async (filter = {}) => {
|
|
54
|
-
const cacheKey =
|
|
55
|
-
Object.keys(filter).length > 0
|
|
56
|
-
? `${CACHE_KEY_ALL_MEMBERS}_${JSON.stringify(filter)}`
|
|
57
|
-
: CACHE_KEY_ALL_MEMBERS;
|
|
58
|
-
|
|
9
|
+
const findUserRole = async (filter = {}) => {
|
|
59
10
|
const members = await UserRole.find(filter)
|
|
60
11
|
.select("userId role userShift roleId")
|
|
61
12
|
.populate({
|
|
@@ -67,102 +18,28 @@ const updateCachedMembers = async (filter = {}) => {
|
|
|
67
18
|
path: "userId",
|
|
68
19
|
match: { isDeleted: false, suspended: false },
|
|
69
20
|
select: "firstName lastName role email picture userShift",
|
|
70
|
-
})
|
|
71
|
-
.lean()
|
|
72
|
-
.exec();
|
|
21
|
+
});
|
|
73
22
|
|
|
74
|
-
await setRedisData(cacheKey, members, null, 0);
|
|
75
23
|
return members;
|
|
76
24
|
};
|
|
77
25
|
|
|
78
|
-
const createUserRole = async (data) => {
|
|
79
|
-
const newRole = new UserRole(data);
|
|
80
|
-
const savedRole = await newRole.save();
|
|
81
|
-
|
|
82
|
-
// Update cache
|
|
83
|
-
await updateCachedMembers();
|
|
84
|
-
if (data.userId) {
|
|
85
|
-
await clearUserWorkspaceCache(data.userId);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return savedRole;
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const findUserRole = async (filter = {}) => {
|
|
92
|
-
return await getCachedMembers(filter);
|
|
93
|
-
};
|
|
94
|
-
|
|
95
26
|
const updateUserRole = async (id, data) => {
|
|
96
27
|
const updatedRole = await UserRole.findByIdAndUpdate(id, data, {
|
|
97
28
|
new: true,
|
|
98
29
|
}).exec();
|
|
99
30
|
|
|
100
|
-
if (updatedRole) {
|
|
101
|
-
await updateCachedMembers();
|
|
102
|
-
if (updatedRole.userId) {
|
|
103
|
-
await clearUserWorkspaceCache(updatedRole.userId);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
31
|
return updatedRole;
|
|
108
32
|
};
|
|
109
33
|
|
|
110
34
|
const deleteUserRole = async (id) => {
|
|
111
|
-
const role = await UserRole.findById(id).exec();
|
|
112
|
-
const userId = role?.userId;
|
|
113
|
-
|
|
114
35
|
const deletedRole = await UserRole.findByIdAndDelete(id).exec();
|
|
115
36
|
|
|
116
|
-
if (deletedRole) {
|
|
117
|
-
await updateCachedMembers();
|
|
118
|
-
if (userId) {
|
|
119
|
-
await clearUserWorkspaceCache(userId);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
37
|
return deletedRole;
|
|
124
38
|
};
|
|
125
39
|
|
|
126
|
-
const findUserWorkspaces = async (userId) => {
|
|
127
|
-
const cacheKey = `${CACHE_KEY_USER_WORKSPACE_PREFIX}${userId}`;
|
|
128
|
-
let userRoleWithWorkspaces = await getRedisData(cacheKey);
|
|
129
|
-
if (!userRoleWithWorkspaces.length) {
|
|
130
|
-
userRoleWithWorkspaces = await UserRole.find({
|
|
131
|
-
userId: userId,
|
|
132
|
-
})
|
|
133
|
-
.select("userId roleId workspaceId")
|
|
134
|
-
.populate({
|
|
135
|
-
path: "roleId",
|
|
136
|
-
select: "id name userId userShift permissionId modulePermissions",
|
|
137
|
-
populate: [{ path: "permissionId", select: "id name" }],
|
|
138
|
-
})
|
|
139
|
-
.populate({
|
|
140
|
-
path: "workspaceId",
|
|
141
|
-
select: defaultSelect,
|
|
142
|
-
match: { isDeleted: false },
|
|
143
|
-
})
|
|
144
|
-
.lean()
|
|
145
|
-
.exec();
|
|
146
|
-
userRoleWithWorkspaces = userRoleWithWorkspaces.map((ur) => {
|
|
147
|
-
return {
|
|
148
|
-
...ur?.workspaceId,
|
|
149
|
-
id: ur?.workspaceId?._id?.toString(),
|
|
150
|
-
_id: ur?.workspaceId?._id?.toString(),
|
|
151
|
-
role: ur?.roleId?.name || "",
|
|
152
|
-
permissions: ur?.roleId?.modulePermissions?.modules || [],
|
|
153
|
-
};
|
|
154
|
-
});
|
|
155
|
-
if (userRoleWithWorkspaces.length) {
|
|
156
|
-
await setRedisData(cacheKey, userRoleWithWorkspaces, null, 0);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
return userRoleWithWorkspaces;
|
|
160
|
-
};
|
|
161
|
-
|
|
162
40
|
module.exports = {
|
|
163
41
|
createUserRole,
|
|
164
42
|
findUserRole,
|
|
165
43
|
updateUserRole,
|
|
166
44
|
deleteUserRole,
|
|
167
|
-
findUserWorkspaces,
|
|
168
45
|
};
|
package/config/redis.js
CHANGED