shuttlepro-shared 1.2.47 → 1.2.49
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
|
};
|
|
@@ -10,7 +10,7 @@ const socialMediaSettingRepository = require("./socialMediaSetting.repository");
|
|
|
10
10
|
const notificationSettingRepository = require("./notificationSetting.repository");
|
|
11
11
|
const chatMemberRepository = require("./chatMember.repository");
|
|
12
12
|
const roleRepository = require("./role.repository");
|
|
13
|
-
|
|
13
|
+
const userPermissionRepository = require("./userPermission.repository");
|
|
14
14
|
exports.module = {
|
|
15
15
|
workspaceRepository,
|
|
16
16
|
integrationRepository,
|
|
@@ -24,4 +24,5 @@ exports.module = {
|
|
|
24
24
|
notificationSettingRepository,
|
|
25
25
|
chatMemberRepository,
|
|
26
26
|
roleRepository,
|
|
27
|
+
userPermissionRepository,
|
|
27
28
|
};
|
|
@@ -1,31 +1,218 @@
|
|
|
1
|
+
const { getRedisData, setRedisData } = require("../../config/redis");
|
|
1
2
|
const UserPermission = require("../../models/UserPermission");
|
|
2
3
|
|
|
4
|
+
const CACHE_KEY_ALL = "user_permissions_all";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Get cached user permissions for all users.
|
|
8
|
+
*/
|
|
9
|
+
const getCachedAllUserPermissions = async () => {
|
|
10
|
+
let permissions = await getRedisData(CACHE_KEY_ALL);
|
|
11
|
+
if (!permissions) {
|
|
12
|
+
permissions = await UserPermission.find({}).lean().exec();
|
|
13
|
+
await setRedisData(CACHE_KEY_ALL, permissions);
|
|
14
|
+
}
|
|
15
|
+
return permissions;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Update cached user permissions for all users.
|
|
20
|
+
*/
|
|
21
|
+
const updateCachedAllUserPermissions = async () => {
|
|
22
|
+
const permissions = await UserPermission.find({}).lean().exec();
|
|
23
|
+
await setRedisData(CACHE_KEY_ALL, permissions);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Get cached user permissions for a specific user.
|
|
28
|
+
* Uses the all permissions cache and filters by userId.
|
|
29
|
+
*/
|
|
30
|
+
const getCachedUserPermissions = async (userId) => {
|
|
31
|
+
const allPermissions = await getCachedAllUserPermissions();
|
|
32
|
+
return allPermissions.filter((perm) => perm.userId === userId);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Create a new user permission and update cache.
|
|
37
|
+
*/
|
|
3
38
|
const createUserPermission = async (data) => {
|
|
4
39
|
const newPermission = new UserPermission(data);
|
|
5
40
|
const savedPermission = await newPermission.save();
|
|
41
|
+
|
|
42
|
+
// Update the main cache
|
|
43
|
+
await updateCachedAllUserPermissions();
|
|
44
|
+
|
|
6
45
|
return savedPermission;
|
|
7
46
|
};
|
|
8
47
|
|
|
9
|
-
|
|
10
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Find user permissions by user ID.
|
|
50
|
+
*/
|
|
51
|
+
const findUserPermissionsByUserId = async (userId) => {
|
|
52
|
+
return await getCachedUserPermissions(userId);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Find user permissions by user ID and workspace ID.
|
|
57
|
+
*/
|
|
58
|
+
const fetchUserPermissionsByWorkspaceId = async (userId, workspaceId) => {
|
|
59
|
+
const allPermissions = await getCachedAllUserPermissions();
|
|
60
|
+
return allPermissions.filter(
|
|
61
|
+
(perm) => perm.userId === userId && perm.workspaceId === workspaceId
|
|
62
|
+
);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Find a single user permission by filter.
|
|
67
|
+
*/
|
|
68
|
+
const findUserPermission = async (filter, workspaceId = null) => {
|
|
69
|
+
const allPermissions = await getCachedAllUserPermissions();
|
|
70
|
+
|
|
71
|
+
let filteredPermissions = allPermissions;
|
|
72
|
+
if (workspaceId) {
|
|
73
|
+
filteredPermissions = allPermissions.filter(
|
|
74
|
+
(perm) => perm.workspaceId === workspaceId
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
filteredPermissions.find((perm) =>
|
|
80
|
+
Object.entries(filter).every(([key, value]) => perm[key] === value)
|
|
81
|
+
) || null
|
|
82
|
+
);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Find all user permissions that match a filter.
|
|
87
|
+
*/
|
|
88
|
+
const findAllUserPermissionsByFilter = async (
|
|
89
|
+
filter = {},
|
|
90
|
+
workspaceId = null
|
|
91
|
+
) => {
|
|
92
|
+
const allPermissions = await getCachedAllUserPermissions();
|
|
93
|
+
|
|
94
|
+
let filteredPermissions = allPermissions;
|
|
95
|
+
if (workspaceId) {
|
|
96
|
+
filteredPermissions = allPermissions.filter(
|
|
97
|
+
(perm) => perm.workspaceId === workspaceId
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return (
|
|
102
|
+
filteredPermissions.filter((perm) =>
|
|
103
|
+
Object.entries(filter).every(([key, value]) => perm[key] === value)
|
|
104
|
+
) || []
|
|
105
|
+
);
|
|
11
106
|
};
|
|
12
107
|
|
|
108
|
+
/**
|
|
109
|
+
* Find all user permissions (without filtering).
|
|
110
|
+
*/
|
|
111
|
+
const findAllUserPermissions = async () => {
|
|
112
|
+
return await getCachedAllUserPermissions();
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Update a user permission by ID.
|
|
117
|
+
*/
|
|
13
118
|
const updateUserPermission = async (id, data) => {
|
|
14
119
|
const updatedPermission = await UserPermission.findByIdAndUpdate(id, data, {
|
|
15
120
|
new: true,
|
|
16
121
|
}).exec();
|
|
17
122
|
|
|
123
|
+
if (updatedPermission) {
|
|
124
|
+
// Update the main cache
|
|
125
|
+
await updateCachedAllUserPermissions();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return updatedPermission;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Update a user permission by filter.
|
|
133
|
+
*/
|
|
134
|
+
const updateUserPermissionByFilter = async (
|
|
135
|
+
filter,
|
|
136
|
+
data,
|
|
137
|
+
workspaceId = null
|
|
138
|
+
) => {
|
|
139
|
+
// Add workspace filter if provided
|
|
140
|
+
const queryFilter = workspaceId ? { ...filter, workspaceId } : filter;
|
|
141
|
+
|
|
142
|
+
const updatedPermission = await UserPermission.findOneAndUpdate(
|
|
143
|
+
queryFilter,
|
|
144
|
+
data,
|
|
145
|
+
{
|
|
146
|
+
new: true,
|
|
147
|
+
}
|
|
148
|
+
).exec();
|
|
149
|
+
|
|
150
|
+
if (updatedPermission) {
|
|
151
|
+
// Update the main cache
|
|
152
|
+
await updateCachedAllUserPermissions();
|
|
153
|
+
}
|
|
154
|
+
|
|
18
155
|
return updatedPermission;
|
|
19
156
|
};
|
|
20
157
|
|
|
158
|
+
/**
|
|
159
|
+
* Delete a user permission by ID.
|
|
160
|
+
*/
|
|
21
161
|
const deleteUserPermission = async (id) => {
|
|
22
162
|
const deletedPermission = await UserPermission.findByIdAndDelete(id).exec();
|
|
163
|
+
|
|
164
|
+
if (deletedPermission) {
|
|
165
|
+
// Update the main cache
|
|
166
|
+
await updateCachedAllUserPermissions();
|
|
167
|
+
}
|
|
168
|
+
|
|
23
169
|
return deletedPermission;
|
|
24
170
|
};
|
|
25
171
|
|
|
172
|
+
/**
|
|
173
|
+
* Delete user permissions by filter.
|
|
174
|
+
*/
|
|
175
|
+
const deleteUserPermissionsByFilter = async (filter) => {
|
|
176
|
+
const result = await UserPermission.deleteMany(filter).exec();
|
|
177
|
+
|
|
178
|
+
if (result.deletedCount > 0) {
|
|
179
|
+
// Update the main cache
|
|
180
|
+
await updateCachedAllUserPermissions();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return result;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Delete all user permissions for a specific workspace.
|
|
188
|
+
*/
|
|
189
|
+
const deleteAllUserPermissionsByWorkspace = async (workspaceId) => {
|
|
190
|
+
await UserPermission.deleteMany({ workspaceId }).exec();
|
|
191
|
+
|
|
192
|
+
// Update the main cache
|
|
193
|
+
await updateCachedAllUserPermissions();
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Bulk create or update user permissions.
|
|
198
|
+
*/
|
|
199
|
+
const bulkCreateAndUpdateUserPermissions = async (operations) => {
|
|
200
|
+
await UserPermission.bulkWrite(operations);
|
|
201
|
+
await updateCachedAllUserPermissions();
|
|
202
|
+
return operations;
|
|
203
|
+
};
|
|
204
|
+
|
|
26
205
|
module.exports = {
|
|
27
206
|
createUserPermission,
|
|
28
207
|
findUserPermission,
|
|
208
|
+
findUserPermissionsByUserId,
|
|
209
|
+
fetchUserPermissionsByWorkspaceId,
|
|
210
|
+
findAllUserPermissionsByFilter,
|
|
211
|
+
findAllUserPermissions,
|
|
29
212
|
updateUserPermission,
|
|
213
|
+
updateUserPermissionByFilter,
|
|
30
214
|
deleteUserPermission,
|
|
215
|
+
deleteUserPermissionsByFilter,
|
|
216
|
+
deleteAllUserPermissionsByWorkspace,
|
|
217
|
+
bulkCreateAndUpdateUserPermissions,
|
|
31
218
|
};
|