shuttlepro-shared 1.2.27 → 1.2.28
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,12 +1,56 @@
|
|
|
1
|
-
const
|
|
1
|
+
const UserRole = require("../../models/UserRole");
|
|
2
|
+
const { getRedisData, addDataToRedisSet } = require("../../config/redis");
|
|
2
3
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
const defaultSelect =
|
|
5
|
+
"iconUrl thumbUrl name productsCount ordersCount contact email address websiteUrl facebookUrl twitterUrl instagramUrl members defaultModule";
|
|
6
|
+
|
|
7
|
+
// Cache keys
|
|
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 addDataToRedisSet(`${CACHE_KEY_USER_WORKSPACE_PREFIX}${userId}`, null);
|
|
7
14
|
};
|
|
8
15
|
|
|
9
|
-
|
|
16
|
+
// Get cached members
|
|
17
|
+
const getCachedMembers = async (filter = {}) => {
|
|
18
|
+
const cacheKey =
|
|
19
|
+
Object.keys(filter).length > 0
|
|
20
|
+
? `${CACHE_KEY_ALL_MEMBERS}_${JSON.stringify(filter)}`
|
|
21
|
+
: CACHE_KEY_ALL_MEMBERS;
|
|
22
|
+
|
|
23
|
+
let members = await getRedisData(cacheKey);
|
|
24
|
+
|
|
25
|
+
if (!members) {
|
|
26
|
+
members = await UserRole.find(filter)
|
|
27
|
+
.select("userId role userShift roleId")
|
|
28
|
+
.populate({
|
|
29
|
+
path: "roleId",
|
|
30
|
+
select: "id name userId userShift permissionId modulePermissions",
|
|
31
|
+
populate: [{ path: "permissionId", select: "id name" }],
|
|
32
|
+
})
|
|
33
|
+
.populate({
|
|
34
|
+
path: "userId",
|
|
35
|
+
match: { isDeleted: false, suspended: false },
|
|
36
|
+
select: "firstName lastName role email picture userShift",
|
|
37
|
+
})
|
|
38
|
+
.lean()
|
|
39
|
+
.exec();
|
|
40
|
+
|
|
41
|
+
await addDataToRedisSet(cacheKey, members);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return members;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// Update the cache for all members
|
|
48
|
+
const updateCachedMembers = async (filter = {}) => {
|
|
49
|
+
const cacheKey =
|
|
50
|
+
Object.keys(filter).length > 0
|
|
51
|
+
? `${CACHE_KEY_ALL_MEMBERS}_${JSON.stringify(filter)}`
|
|
52
|
+
: CACHE_KEY_ALL_MEMBERS;
|
|
53
|
+
|
|
10
54
|
const members = await UserRole.find(filter)
|
|
11
55
|
.select("userId role userShift roleId")
|
|
12
56
|
.populate({
|
|
@@ -18,28 +62,90 @@ const findUserRole = async (filter = {}) => {
|
|
|
18
62
|
path: "userId",
|
|
19
63
|
match: { isDeleted: false, suspended: false },
|
|
20
64
|
select: "firstName lastName role email picture userShift",
|
|
21
|
-
})
|
|
65
|
+
})
|
|
66
|
+
.lean()
|
|
67
|
+
.exec();
|
|
22
68
|
|
|
69
|
+
await addDataToRedisSet(cacheKey, members);
|
|
23
70
|
return members;
|
|
24
71
|
};
|
|
25
72
|
|
|
73
|
+
const createUserRole = async (data) => {
|
|
74
|
+
const newRole = new UserRole(data);
|
|
75
|
+
const savedRole = await newRole.save();
|
|
76
|
+
|
|
77
|
+
// Update cache
|
|
78
|
+
await updateCachedMembers();
|
|
79
|
+
if (data.userId) {
|
|
80
|
+
await clearUserWorkspaceCache(data.userId);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return savedRole;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const findUserRole = async (filter = {}) => {
|
|
87
|
+
return await getCachedMembers(filter);
|
|
88
|
+
};
|
|
89
|
+
|
|
26
90
|
const updateUserRole = async (id, data) => {
|
|
27
91
|
const updatedRole = await UserRole.findByIdAndUpdate(id, data, {
|
|
28
92
|
new: true,
|
|
29
93
|
}).exec();
|
|
30
94
|
|
|
95
|
+
if (updatedRole) {
|
|
96
|
+
await updateCachedMembers();
|
|
97
|
+
if (updatedRole.userId) {
|
|
98
|
+
await clearUserWorkspaceCache(updatedRole.userId);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
31
102
|
return updatedRole;
|
|
32
103
|
};
|
|
33
104
|
|
|
34
105
|
const deleteUserRole = async (id) => {
|
|
106
|
+
const role = await UserRole.findById(id).exec();
|
|
107
|
+
const userId = role?.userId;
|
|
108
|
+
|
|
35
109
|
const deletedRole = await UserRole.findByIdAndDelete(id).exec();
|
|
36
110
|
|
|
111
|
+
if (deletedRole) {
|
|
112
|
+
await updateCachedMembers();
|
|
113
|
+
if (userId) {
|
|
114
|
+
await clearUserWorkspaceCache(userId);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
37
118
|
return deletedRole;
|
|
38
119
|
};
|
|
39
120
|
|
|
121
|
+
const findUserRoleWithWorkspaces = async (userId) => {
|
|
122
|
+
const cacheKey = `${CACHE_KEY_USER_WORKSPACE_PREFIX}${userId}`;
|
|
123
|
+
let userRoleWithWorkspaces = await getRedisData(cacheKey);
|
|
124
|
+
|
|
125
|
+
if (!userRoleWithWorkspaces) {
|
|
126
|
+
userRoleWithWorkspaces = await UserRole.findOne({
|
|
127
|
+
userId: userId,
|
|
128
|
+
})
|
|
129
|
+
.populate({
|
|
130
|
+
path: "workspaceId",
|
|
131
|
+
select: defaultSelect,
|
|
132
|
+
match: { isDeleted: false },
|
|
133
|
+
})
|
|
134
|
+
.lean()
|
|
135
|
+
.exec();
|
|
136
|
+
|
|
137
|
+
if (userRoleWithWorkspaces) {
|
|
138
|
+
await addDataToRedisSet(cacheKey, userRoleWithWorkspaces);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return userRoleWithWorkspaces;
|
|
143
|
+
};
|
|
144
|
+
|
|
40
145
|
module.exports = {
|
|
41
146
|
createUserRole,
|
|
42
147
|
findUserRole,
|
|
43
148
|
updateUserRole,
|
|
44
149
|
deleteUserRole,
|
|
150
|
+
findUserRoleWithWorkspaces,
|
|
45
151
|
};
|