shuttlepro-shared 1.1.65 → 1.1.67

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,21 +1,8 @@
1
1
  const { getRedisData, setRedisData } = require("../../config/redis");
2
2
  const Integration = require("../../models/Integration");
3
3
 
4
- const CACHE_KEY_PREFIX = "integrations";
5
4
  const CACHE_KEY_ALL = "integrations_all";
6
5
 
7
- /**
8
- * Get cached integrations for a specific workspace.
9
- */
10
- const getCachedIntegrations = async (workspaceId) => {
11
- let integrations = await getRedisData(workspaceId, CACHE_KEY_PREFIX);
12
- if (!integrations) {
13
- integrations = await Integration.find({ workspaceId }).exec();
14
- await setRedisData(workspaceId, integrations, CACHE_KEY_PREFIX);
15
- }
16
- return integrations;
17
- };
18
-
19
6
  /**
20
7
  * Get cached integrations for all workspaces.
21
8
  */
@@ -28,14 +15,6 @@ const getCachedAllIntegrations = async () => {
28
15
  return integrations;
29
16
  };
30
17
 
31
- /**
32
- * Update cached integrations for a specific workspace.
33
- */
34
- const updateCachedIntegrations = async (workspaceId) => {
35
- const integrations = await Integration.find({ workspaceId }).exec();
36
- await setRedisData(workspaceId, integrations, CACHE_KEY_PREFIX);
37
- };
38
-
39
18
  /**
40
19
  * Update cached integrations for all workspaces.
41
20
  */
@@ -44,6 +23,15 @@ const updateCachedAllIntegrations = async () => {
44
23
  await setRedisData(CACHE_KEY_ALL, integrations);
45
24
  };
46
25
 
26
+ /**
27
+ * Get cached integrations for a specific workspace.
28
+ * Now uses the all integrations cache and filters by workspaceId.
29
+ */
30
+ const getCachedIntegrations = async (workspaceId) => {
31
+ const allIntegrations = await getCachedAllIntegrations();
32
+ return allIntegrations.filter((intg) => intg.workspaceId === workspaceId);
33
+ };
34
+
47
35
  /**
48
36
  * Create a new integration and update cache.
49
37
  */
@@ -51,10 +39,7 @@ const createIntegration = async (data) => {
51
39
  const newIntegration = new Integration(data);
52
40
  const savedIntegration = await newIntegration.save();
53
41
 
54
- // Update relevant caches
55
- if (savedIntegration.workspaceId) {
56
- await updateCachedIntegrations(savedIntegration.workspaceId);
57
- }
42
+ // Update only the main cache
58
43
  await updateCachedAllIntegrations();
59
44
 
60
45
  return savedIntegration;
@@ -63,20 +48,20 @@ const createIntegration = async (data) => {
63
48
  /**
64
49
  * Find an integration by ID.
65
50
  */
66
- const findIntegrationById = async (id, workspaceId) => {
67
- const integrations = workspaceId
68
- ? await getCachedIntegrations(workspaceId)
69
- : await getCachedAllIntegrations();
70
- return integrations.find((intg) => intg._id.toString() === id) || null;
51
+ const findIntegrationsByWorkspaceId = async (workspaceId) => {
52
+ return await getCachedIntegrations(workspaceId);
71
53
  };
72
54
 
73
55
  /**
74
56
  * Find integrations by workspace.
75
57
  */
76
58
  const findIntegrationsByWorkspace = async (workspaceId, filter = {}) => {
77
- const integrations = await getCachedIntegrations(workspaceId);
78
- return integrations.filter((intg) =>
79
- Object.entries(filter).every(([key, value]) => intg[key] === value)
59
+ const allIntegrations = await getCachedIntegrations(workspaceId);
60
+
61
+ return allIntegrations.filter(
62
+ (intg) =>
63
+ intg.workspaceId === workspaceId &&
64
+ Object.entries(filter).every(([key, value]) => intg[key] === value)
80
65
  );
81
66
  };
82
67
 
@@ -84,13 +69,29 @@ const findIntegrationsByWorkspace = async (workspaceId, filter = {}) => {
84
69
  * Find integration by filter (supports both workspace & non-workspace).
85
70
  */
86
71
  const findIntegrationByFilter = async (filter, workspaceId = null) => {
87
- const integrations = workspaceId
88
- ? await getCachedIntegrations(workspaceId)
89
- : await getCachedAllIntegrations();
72
+ const allIntegrations = await getCachedAllIntegrations();
73
+
74
+ let filteredIntegrations = allIntegrations;
75
+ if (workspaceId) {
76
+ filteredIntegrations = allIntegrations.filter(
77
+ (intg) => intg.workspaceId === workspaceId
78
+ );
79
+ }
80
+
81
+ console.log(filteredIntegrations, "filteredIntegrations");
82
+ console.log(filter, "filter");
83
+
84
+ console.log(
85
+ filteredIntegrations.find((item) =>
86
+ Object.entries(filter).every(
87
+ ([key, value]) => item?.body?.[key] === value
88
+ )
89
+ )
90
+ );
90
91
 
91
92
  return (
92
- integrations.find((intg) =>
93
- Object.entries(filter).every(([key, value]) => intg[key] === value)
93
+ filteredIntegrations.find((item) =>
94
+ Object.entries(filter).every(([key, value]) => item.body?.[key] === value)
94
95
  ) || null
95
96
  );
96
97
  };
@@ -111,10 +112,7 @@ const updateIntegration = async (id, data) => {
111
112
  }).exec();
112
113
 
113
114
  if (updatedIntegration) {
114
- const { workspaceId } = updatedIntegration;
115
- if (workspaceId) {
116
- await updateCachedIntegrations(workspaceId);
117
- }
115
+ // Update only the main cache
118
116
  await updateCachedAllIntegrations();
119
117
  }
120
118
 
@@ -125,14 +123,19 @@ const updateIntegration = async (id, data) => {
125
123
  * Update an integration by filter.
126
124
  */
127
125
  const updateIntegrationByFilter = async (filter, data, workspaceId = null) => {
128
- const updatedIntegration = await Integration.findOneAndUpdate(filter, data, {
129
- new: true,
130
- }).exec();
126
+ // Add workspace filter if provided
127
+ const queryFilter = workspaceId ? { ...filter, workspaceId } : filter;
128
+
129
+ const updatedIntegration = await Integration.findOneAndUpdate(
130
+ queryFilter,
131
+ data,
132
+ {
133
+ new: true,
134
+ }
135
+ ).exec();
131
136
 
132
137
  if (updatedIntegration) {
133
- if (workspaceId) {
134
- await updateCachedIntegrations(workspaceId);
135
- }
138
+ // Update only the main cache
136
139
  await updateCachedAllIntegrations();
137
140
  }
138
141
 
@@ -146,10 +149,7 @@ const deleteIntegration = async (id) => {
146
149
  const deletedIntegration = await Integration.findByIdAndDelete(id).exec();
147
150
 
148
151
  if (deletedIntegration) {
149
- const { workspaceId } = deletedIntegration;
150
- if (workspaceId) {
151
- await updateCachedIntegrations(workspaceId);
152
- }
152
+ // Update only the main cache
153
153
  await updateCachedAllIntegrations();
154
154
  }
155
155
 
@@ -161,13 +161,14 @@ const deleteIntegration = async (id) => {
161
161
  */
162
162
  const deleteAllIntegrationsByWorkspace = async (workspaceId) => {
163
163
  await Integration.deleteMany({ workspaceId }).exec();
164
- await updateCachedIntegrations(workspaceId);
164
+
165
+ // Update only the main cache
165
166
  await updateCachedAllIntegrations();
166
167
  };
167
168
 
168
169
  module.exports = {
169
170
  createIntegration,
170
- findIntegrationById,
171
+ findIntegrationsByWorkspaceId,
171
172
  findIntegrationsByWorkspace,
172
173
  findIntegrationByFilter,
173
174
  findAllIntegrations,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shuttlepro-shared",
3
- "version": "1.1.65",
3
+ "version": "1.1.67",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {