shuttlepro-shared 1.2.48 → 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.
@@ -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
- const findUserPermission = async (filter) => {
10
- return await UserPermission.findOne(filter).exec();
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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shuttlepro-shared",
3
- "version": "1.2.48",
3
+ "version": "1.2.49",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {