shuttlepro-shared 1.2.33 → 1.2.35

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,7 +1,7 @@
1
1
  const { UserRole } = require("../../models/UserRole");
2
2
  const {
3
3
  getDataFromRedisSet,
4
- addDataToRedisSet,
4
+ addArrayToRedisSet,
5
5
  } = require("../../config/redis");
6
6
 
7
7
  const defaultSelect =
@@ -13,7 +13,7 @@ const CACHE_KEY_USER_WORKSPACE_PREFIX = "userrole_workspace_";
13
13
 
14
14
  // Clear user workspace cache
15
15
  const clearUserWorkspaceCache = async (userId) => {
16
- await addDataToRedisSet(`${CACHE_KEY_USER_WORKSPACE_PREFIX}${userId}`, null);
16
+ await addArrayToRedisSet(`${CACHE_KEY_USER_WORKSPACE_PREFIX}${userId}`, null);
17
17
  };
18
18
 
19
19
  // Get cached members
@@ -41,7 +41,7 @@ const getCachedMembers = async (filter = {}) => {
41
41
  .lean()
42
42
  .exec();
43
43
 
44
- await addDataToRedisSet(cacheKey, members);
44
+ await addArrayToRedisSet(cacheKey, members);
45
45
  }
46
46
 
47
47
  return members;
@@ -69,7 +69,7 @@ const updateCachedMembers = async (filter = {}) => {
69
69
  .lean()
70
70
  .exec();
71
71
 
72
- await addDataToRedisSet(cacheKey, members);
72
+ await addArrayToRedisSet(cacheKey, members);
73
73
  return members;
74
74
  };
75
75
 
@@ -123,12 +123,8 @@ const deleteUserRole = async (id) => {
123
123
 
124
124
  const findUserWorkspaces = async (userId) => {
125
125
  const cacheKey = `${CACHE_KEY_USER_WORKSPACE_PREFIX}${userId}`;
126
- console.log(cacheKey, "cacheKey");
127
126
  let userRoleWithWorkspaces = await getDataFromRedisSet(cacheKey);
128
-
129
- console.log(userRoleWithWorkspaces, "userRoleWithWorkspaces");
130
-
131
- if (!userRoleWithWorkspaces) {
127
+ if (!userRoleWithWorkspaces.length) {
132
128
  userRoleWithWorkspaces = await UserRole.find({
133
129
  userId: userId,
134
130
  })
@@ -145,12 +141,11 @@ const findUserWorkspaces = async (userId) => {
145
141
  })
146
142
  .lean()
147
143
  .exec();
148
- console.log(userRoleWithWorkspaces, "userRoleWithWorkspaces");
144
+ console.log(userRoleWithWorkspaces, "through db");
149
145
  if (userRoleWithWorkspaces) {
150
- await addDataToRedisSet(cacheKey, userRoleWithWorkspaces);
146
+ await addArrayToRedisSet(cacheKey, userRoleWithWorkspaces);
151
147
  }
152
148
  }
153
-
154
149
  return userRoleWithWorkspaces;
155
150
  };
156
151
 
package/config/redis.js CHANGED
@@ -103,13 +103,30 @@ const addDataToRedisSet = async (key, value) => {
103
103
  return false;
104
104
  }
105
105
  };
106
+
107
+ const addArrayToRedisSet = async (key, value) => {
108
+ try {
109
+ await connectRedis();
110
+ console.log(key, "key");
111
+ const arr = Array.isArray(value) ? value : [value];
112
+ console.log(arr, "arr");
113
+ await client.sAdd(key, ...arr); // spread values
114
+ return true;
115
+ } catch (err) {
116
+ console.error("Add Error:", err);
117
+ return false;
118
+ }
119
+ };
120
+
106
121
  const getDataFromRedisSet = async (key) => {
107
122
  try {
108
123
  await connectRedis();
124
+ console.log(key, "key");
109
125
  const values = await client.sMembers(key);
110
- return values; // returns an array of values
126
+ console.log(values, "values");
127
+ return values;
111
128
  } catch (err) {
112
- console.error("Redis Error:", err);
129
+ console.error("Get Error:", err);
113
130
  return [];
114
131
  }
115
132
  };
@@ -199,4 +216,5 @@ module.exports = {
199
216
  removeDataFromRedisSet,
200
217
  isMemberOfRedisSet,
201
218
  getDataFromRedisSet,
219
+ addArrayToRedisSet,
202
220
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shuttlepro-shared",
3
- "version": "1.2.33",
3
+ "version": "1.2.35",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {