shuttlepro-shared 1.1.64 → 1.1.65

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