shuttlepro-shared 1.1.64 → 1.1.66

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