shuttlepro-shared 1.1.65 → 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,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
|
|
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
|
|
67
|
-
|
|
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
|
|
78
|
-
|
|
79
|
-
|
|
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,12 +69,19 @@ 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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
+
}
|
|
90
82
|
|
|
91
83
|
return (
|
|
92
|
-
|
|
84
|
+
filteredIntegrations.find((intg) =>
|
|
93
85
|
Object.entries(filter).every(([key, value]) => intg[key] === value)
|
|
94
86
|
) || null
|
|
95
87
|
);
|
|
@@ -111,10 +103,7 @@ const updateIntegration = async (id, data) => {
|
|
|
111
103
|
}).exec();
|
|
112
104
|
|
|
113
105
|
if (updatedIntegration) {
|
|
114
|
-
|
|
115
|
-
if (workspaceId) {
|
|
116
|
-
await updateCachedIntegrations(workspaceId);
|
|
117
|
-
}
|
|
106
|
+
// Update only the main cache
|
|
118
107
|
await updateCachedAllIntegrations();
|
|
119
108
|
}
|
|
120
109
|
|
|
@@ -125,14 +114,19 @@ const updateIntegration = async (id, data) => {
|
|
|
125
114
|
* Update an integration by filter.
|
|
126
115
|
*/
|
|
127
116
|
const updateIntegrationByFilter = async (filter, data, workspaceId = null) => {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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();
|
|
131
127
|
|
|
132
128
|
if (updatedIntegration) {
|
|
133
|
-
|
|
134
|
-
await updateCachedIntegrations(workspaceId);
|
|
135
|
-
}
|
|
129
|
+
// Update only the main cache
|
|
136
130
|
await updateCachedAllIntegrations();
|
|
137
131
|
}
|
|
138
132
|
|
|
@@ -146,10 +140,7 @@ const deleteIntegration = async (id) => {
|
|
|
146
140
|
const deletedIntegration = await Integration.findByIdAndDelete(id).exec();
|
|
147
141
|
|
|
148
142
|
if (deletedIntegration) {
|
|
149
|
-
|
|
150
|
-
if (workspaceId) {
|
|
151
|
-
await updateCachedIntegrations(workspaceId);
|
|
152
|
-
}
|
|
143
|
+
// Update only the main cache
|
|
153
144
|
await updateCachedAllIntegrations();
|
|
154
145
|
}
|
|
155
146
|
|
|
@@ -161,13 +152,14 @@ const deleteIntegration = async (id) => {
|
|
|
161
152
|
*/
|
|
162
153
|
const deleteAllIntegrationsByWorkspace = async (workspaceId) => {
|
|
163
154
|
await Integration.deleteMany({ workspaceId }).exec();
|
|
164
|
-
|
|
155
|
+
|
|
156
|
+
// Update only the main cache
|
|
165
157
|
await updateCachedAllIntegrations();
|
|
166
158
|
};
|
|
167
159
|
|
|
168
160
|
module.exports = {
|
|
169
161
|
createIntegration,
|
|
170
|
-
|
|
162
|
+
findIntegrationsByWorkspaceId,
|
|
171
163
|
findIntegrationsByWorkspace,
|
|
172
164
|
findIntegrationByFilter,
|
|
173
165
|
findAllIntegrations,
|