shuttlepro-shared 1.4.90 → 1.4.92

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,47 +1,11 @@
1
- const { getRedisData, setRedisData } = require("../../config/redis");
2
1
  const UserPermission = require("../../models/UserPermission");
3
2
 
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
3
  /**
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.
4
+ * Create a new user permission.
37
5
  */
38
6
  const createUserPermission = async (data) => {
39
7
  const newPermission = new UserPermission(data);
40
8
  const savedPermission = await newPermission.save();
41
-
42
- // Update the main cache
43
- await updateCachedAllUserPermissions();
44
-
45
9
  return savedPermission;
46
10
  };
47
11
 
@@ -49,37 +13,27 @@ const createUserPermission = async (data) => {
49
13
  * Find user permissions by user ID.
50
14
  */
51
15
  const findUserPermissionsByUserId = async (userId) => {
52
- return await getCachedUserPermissions(userId);
16
+ return await UserPermission.find({ userId }).lean().exec();
53
17
  };
54
18
 
55
19
  /**
56
20
  * Find user permissions by user ID and workspace ID.
57
21
  */
58
22
  const fetchUserPermissionsByWorkspaceId = async (userId, workspaceId) => {
59
- const allPermissions = await getCachedAllUserPermissions();
60
- return allPermissions.filter(
61
- (perm) => perm.userId === userId && perm.workspaceId === workspaceId
62
- );
23
+ return await UserPermission.find({
24
+ userId,
25
+ workspaceId,
26
+ })
27
+ .lean()
28
+ .exec();
63
29
  };
64
30
 
65
31
  /**
66
32
  * Find a single user permission by filter.
67
33
  */
68
34
  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
- );
35
+ const queryFilter = workspaceId ? { ...filter, workspaceId } : filter;
36
+ return await UserPermission.findOne(queryFilter).lean().exec();
83
37
  };
84
38
 
85
39
  /**
@@ -89,27 +43,15 @@ const findAllUserPermissionsByFilter = async (
89
43
  filter = {},
90
44
  workspaceId = null
91
45
  ) => {
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
- );
46
+ const queryFilter = workspaceId ? { ...filter, workspaceId } : filter;
47
+ return await UserPermission.find(queryFilter).lean().exec();
106
48
  };
107
49
 
108
50
  /**
109
51
  * Find all user permissions (without filtering).
110
52
  */
111
53
  const findAllUserPermissions = async () => {
112
- return await getCachedAllUserPermissions();
54
+ return await UserPermission.find({}).lean().exec();
113
55
  };
114
56
 
115
57
  /**
@@ -119,12 +61,6 @@ const updateUserPermission = async (id, data) => {
119
61
  const updatedPermission = await UserPermission.findByIdAndUpdate(id, data, {
120
62
  new: true,
121
63
  }).exec();
122
-
123
- if (updatedPermission) {
124
- // Update the main cache
125
- await updateCachedAllUserPermissions();
126
- }
127
-
128
64
  return updatedPermission;
129
65
  };
130
66
 
@@ -147,11 +83,6 @@ const updateUserPermissionByFilter = async (
147
83
  }
148
84
  ).exec();
149
85
 
150
- if (updatedPermission) {
151
- // Update the main cache
152
- await updateCachedAllUserPermissions();
153
- }
154
-
155
86
  return updatedPermission;
156
87
  };
157
88
 
@@ -160,12 +91,6 @@ const updateUserPermissionByFilter = async (
160
91
  */
161
92
  const deleteUserPermission = async (id) => {
162
93
  const deletedPermission = await UserPermission.findByIdAndDelete(id).exec();
163
-
164
- if (deletedPermission) {
165
- // Update the main cache
166
- await updateCachedAllUserPermissions();
167
- }
168
-
169
94
  return deletedPermission;
170
95
  };
171
96
 
@@ -174,12 +99,6 @@ const deleteUserPermission = async (id) => {
174
99
  */
175
100
  const deleteUserPermissionsByFilter = async (filter) => {
176
101
  const result = await UserPermission.deleteMany(filter).exec();
177
-
178
- if (result.deletedCount > 0) {
179
- // Update the main cache
180
- await updateCachedAllUserPermissions();
181
- }
182
-
183
102
  return result;
184
103
  };
185
104
 
@@ -187,27 +106,23 @@ const deleteUserPermissionsByFilter = async (filter) => {
187
106
  * Delete all user permissions for a specific workspace.
188
107
  */
189
108
  const deleteAllUserPermissionsByWorkspace = async (workspaceId) => {
190
- await UserPermission.deleteMany({ workspaceId }).exec();
191
-
192
- // Update the main cache
193
- await updateCachedAllUserPermissions();
109
+ const result = await UserPermission.deleteMany({ workspaceId }).exec();
110
+ return result;
194
111
  };
195
112
 
196
113
  /**
197
114
  * Bulk create or update user permissions.
198
115
  */
199
116
  const bulkCreateAndUpdateUserPermissions = async (operations) => {
200
- await UserPermission.bulkWrite(operations);
201
- await updateCachedAllUserPermissions();
202
- return operations;
117
+ const result = await UserPermission.bulkWrite(operations);
118
+ return result;
203
119
  };
204
120
 
205
121
  /**
206
- * Insert multiple user permissions and update cache.
122
+ * Insert multiple user permissions.
207
123
  */
208
124
  const insertManyUserPermissions = async (permissionsArray) => {
209
125
  const inserted = await UserPermission.insertMany(permissionsArray);
210
- await updateCachedAllUserPermissions();
211
126
  return inserted;
212
127
  };
213
128
 
package/models/Profile.js CHANGED
@@ -36,13 +36,21 @@ const addOrUpdateProfilesInRedis = async (profile) => {
36
36
 
37
37
  const fetchProfiles = async (workspaceId) => {
38
38
  try {
39
- let profiles = await getRedisData(`${workspaceId}-profiles`);
40
- if (!profiles || profiles.length === 0) {
41
- profiles = await Profile.find({ workspaceId }).lean().exec();
42
- setRedisData(`${workspaceId}-profiles`, profiles);
39
+ if (!workspaceId) {
40
+ return [];
43
41
  }
44
- return profiles;
42
+
43
+ // Direct database query with proper indexing on workspaceId
44
+ // Using lean() for faster plain object retrieval
45
+ // Sorting by createdAt for consistency
46
+ const profiles = await Profile.find({ workspaceId })
47
+ .sort({ createdAt: -1 })
48
+ .lean()
49
+ .exec();
50
+
51
+ return profiles || [];
45
52
  } catch (err) {
53
+ console.error("Error in fetchProfiles:", err);
46
54
  return [];
47
55
  }
48
56
  };
@@ -129,3 +137,5 @@ module.exports = {
129
137
  Profile,
130
138
  createUpdateProfileModified,
131
139
  };
140
+
141
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shuttlepro-shared",
3
- "version": "1.4.90",
3
+ "version": "1.4.92",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {