shuttlepro-shared 1.3.19 → 1.3.21

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.
Files changed (65) hide show
  1. package/common/repositories/chatMember.repository.js +9 -0
  2. package/common/repositories/customerProfile.repository.js +147 -0
  3. package/common/repositories/customerTimeline.repository.js +78 -0
  4. package/common/repositories/descriptionTemplates.repository.js +229 -0
  5. package/common/repositories/index.js +34 -0
  6. package/common/repositories/integration.repository.js +210 -0
  7. package/common/repositories/label.repository.js +95 -0
  8. package/common/repositories/notificationSettings.repository.js +95 -0
  9. package/common/repositories/role.repository.js +333 -0
  10. package/common/repositories/settings.repository.js +32 -0
  11. package/common/repositories/shipper.repository.js +77 -0
  12. package/common/repositories/socialMediaSetting.repository.js +33 -0
  13. package/common/repositories/user.repository.js +150 -0
  14. package/common/repositories/userPermission.repository.js +228 -0
  15. package/common/repositories/userRepository.js +31 -0
  16. package/common/repositories/userRole.repository.js +236 -0
  17. package/common/repositories/userRolePermission.repository.js +59 -0
  18. package/common/repositories/workspace.repository.js +147 -0
  19. package/config/bull.js +78 -0
  20. package/config/config.js +14 -0
  21. package/config/database.js +4 -0
  22. package/config/index.js +13 -0
  23. package/config/redis.js +196 -0
  24. package/config/socket.js +172 -0
  25. package/constants/index.js +15 -0
  26. package/index.js +8 -0
  27. package/models/AgentActivity.js +192 -0
  28. package/models/Assignment.js +23 -0
  29. package/models/BusinessDistribution.js +23 -0
  30. package/models/Card.js +144 -0
  31. package/models/CardComments.js +33 -0
  32. package/models/ChatMember.js +17 -0
  33. package/models/Chatbot.js +20 -0
  34. package/models/Checkpoint.js +50 -0
  35. package/models/City.js +17 -0
  36. package/models/Column.js +28 -0
  37. package/models/Conversation.js +87 -0
  38. package/models/Customer.js +36 -0
  39. package/models/CustomerProfile.js +30 -0
  40. package/models/CustomerTimeline.js +28 -0
  41. package/models/DefaultRolePermission.js +34 -0
  42. package/models/DescriptionTemplate.js +22 -0
  43. package/models/Integration.js +51 -0
  44. package/models/Label.js +42 -0
  45. package/models/Message.js +47 -0
  46. package/models/NewProduct.js +71 -0
  47. package/models/NotificationSettings.js +130 -0
  48. package/models/Order.js +254 -0
  49. package/models/OrderProduct.js +37 -0
  50. package/models/Profile.js +127 -0
  51. package/models/Report.js +27 -0
  52. package/models/Setting.js +18 -0
  53. package/models/Shipper.js +62 -0
  54. package/models/SocialMediaSetting.js +28 -0
  55. package/models/Status.js +58 -0
  56. package/models/StatusType.js +10 -0
  57. package/models/Step.js +50 -0
  58. package/models/Type.js +25 -0
  59. package/models/UserRole.js +1 -119
  60. package/models/UserWorkflow.js +46 -0
  61. package/models/Workspace.js +309 -0
  62. package/models.js +62 -1
  63. package/package.json +13 -2
  64. package/utils/decorator-factory.js +264 -0
  65. package/utils/logger.js +41 -0
@@ -0,0 +1,9 @@
1
+ const ChatMember = require("../../models/ChatMember");
2
+
3
+ const findChatMemberById = async (id) => {
4
+ return await ChatMember.findById(id).select("userName phoneNo email");
5
+ };
6
+
7
+ module.exports = {
8
+ findChatMemberById,
9
+ };
@@ -0,0 +1,147 @@
1
+ const { getRedisData, setRedisData } = require("../../config/redis");
2
+ const CustomerProfile = require("../../models/CustomerProfile");
3
+
4
+ const CACHE_KEY_ALL = "customer_profiles_all";
5
+
6
+ const getCachedAllCustomerProfiles = async () => {
7
+ let profiles = await getRedisData(CACHE_KEY_ALL);
8
+ if (!profiles) {
9
+ profiles = await CustomerProfile.find({}).lean().exec();
10
+ await setRedisData(CACHE_KEY_ALL, profiles);
11
+ }
12
+ return profiles;
13
+ };
14
+
15
+ const updateCachedAllCustomerProfiles = async () => {
16
+ const profiles = await CustomerProfile.find({}).lean().exec();
17
+ await setRedisData(CACHE_KEY_ALL, profiles);
18
+ };
19
+
20
+ const getCachedCustomerProfiles = async (workspaceId) => {
21
+ const allProfiles = await getCachedAllCustomerProfiles();
22
+ return allProfiles.filter((profile) => profile.workspaceId === workspaceId);
23
+ };
24
+
25
+ const createCustomerProfile = async (data) => {
26
+ const newProfile = new CustomerProfile(data);
27
+ const savedProfile = await newProfile.save();
28
+
29
+ updateCachedAllCustomerProfiles();
30
+ return savedProfile;
31
+ };
32
+
33
+ const findCustomerProfilesByWorkspaceId = async (workspaceId) => {
34
+ return await getCachedCustomerProfiles(workspaceId);
35
+ };
36
+
37
+ const findCustomerProfilesByWorkspace = async (workspaceId, filter = {}) => {
38
+ const profiles = await getCachedCustomerProfiles(workspaceId);
39
+ return profiles.filter((profile) =>
40
+ Object.entries(filter).every(([key, value]) => profile[key] === value)
41
+ );
42
+ };
43
+
44
+ const findCustomerProfileByFilter = async (
45
+ filter = {},
46
+ bodyFilter = {},
47
+ workspaceId = null
48
+ ) => {
49
+ const allProfiles = await getCachedAllCustomerProfiles();
50
+
51
+ const filtered = workspaceId
52
+ ? allProfiles.filter((p) => p.workspaceId === workspaceId)
53
+ : allProfiles;
54
+
55
+ return (
56
+ filtered.find(
57
+ (profile) =>
58
+ Object.entries(filter).every(
59
+ ([key, value]) => profile[key] === value
60
+ ) &&
61
+ Object.entries(bodyFilter).every(
62
+ ([key, value]) => profile.body?.[key] === value
63
+ )
64
+ ) || null
65
+ );
66
+ };
67
+
68
+ const findAllCustomerProfilesByFilter = async (
69
+ filter = {},
70
+ bodyFilter = {},
71
+ workspaceId = null
72
+ ) => {
73
+ const allProfiles = await getCachedAllCustomerProfiles();
74
+
75
+ const filtered = workspaceId
76
+ ? allProfiles.filter((p) => p.workspaceId === workspaceId)
77
+ : allProfiles;
78
+
79
+ return (
80
+ filtered.filter(
81
+ (profile) =>
82
+ Object.entries(filter).every(
83
+ ([key, value]) => profile[key] === value
84
+ ) &&
85
+ Object.entries(bodyFilter).every(
86
+ ([key, value]) => profile.body?.[key] === value
87
+ )
88
+ ) || []
89
+ );
90
+ };
91
+
92
+ const updateCustomerProfile = async (id, data) => {
93
+ const updated = await CustomerProfile.findByIdAndUpdate(id, data, {
94
+ new: true,
95
+ }).exec();
96
+
97
+ if (updated) {
98
+ updateCachedAllCustomerProfiles();
99
+ }
100
+
101
+ return updated;
102
+ };
103
+
104
+ const updateCustomerProfileByFilter = async (
105
+ filter,
106
+ data,
107
+ workspaceId = null
108
+ ) => {
109
+ const query = workspaceId ? { ...filter, workspaceId } : filter;
110
+
111
+ const updated = await CustomerProfile.findOneAndUpdate(query, data, {
112
+ new: true,
113
+ }).exec();
114
+
115
+ if (updated) {
116
+ updateCachedAllCustomerProfiles();
117
+ }
118
+
119
+ return updated;
120
+ };
121
+
122
+ const deleteCustomerProfile = async (id) => {
123
+ const deleted = await CustomerProfile.findByIdAndDelete(id).exec();
124
+
125
+ if (deleted) {
126
+ updateCachedAllCustomerProfiles();
127
+ }
128
+
129
+ return deleted;
130
+ };
131
+
132
+ const deleteAllCustomerProfilesByWorkspace = async (workspaceId) => {
133
+ await CustomerProfile.deleteMany({ workspaceId }).exec();
134
+ await updateCachedAllCustomerProfiles();
135
+ };
136
+
137
+ module.exports = {
138
+ createCustomerProfile,
139
+ findCustomerProfilesByWorkspaceId,
140
+ findCustomerProfilesByWorkspace,
141
+ findCustomerProfileByFilter,
142
+ updateCustomerProfile,
143
+ updateCustomerProfileByFilter,
144
+ deleteCustomerProfile,
145
+ deleteAllCustomerProfilesByWorkspace,
146
+ findAllCustomerProfilesByFilter,
147
+ };
@@ -0,0 +1,78 @@
1
+ const CustomerTimeline = require("../../models/CustomerTimeline");
2
+
3
+ const createCustomerTimeline = async (data) => {
4
+ const newTimeline = new CustomerTimeline(data);
5
+ return await newTimeline.save();
6
+ };
7
+
8
+ const findCustomerTimelinesByWorkspaceId = async (workspaceId) => {
9
+ return await CustomerTimeline.find({ workspaceId }).lean().exec();
10
+ };
11
+
12
+ const findCustomerTimelinesByWorkspace = async (workspaceId, filter = {}) => {
13
+ return await CustomerTimeline.find({ workspaceId, ...filter })
14
+ .lean()
15
+ .exec();
16
+ };
17
+
18
+ const findCustomerTimelineByFilter = async (filter = {}, bodyFilter = {}) => {
19
+ const all = await CustomerTimeline.find(filter).lean().exec();
20
+
21
+ return (
22
+ all.find((item) =>
23
+ Object.entries(bodyFilter).every(
24
+ ([key, value]) => item.body?.[key] === value
25
+ )
26
+ ) || null
27
+ );
28
+ };
29
+
30
+ const findAllCustomerTimelinesByFilter = async (
31
+ filter = {},
32
+ bodyFilter = {}
33
+ ) => {
34
+ const all = await CustomerTimeline.find(filter).lean().exec();
35
+ return all.filter((item) =>
36
+ Object.entries(bodyFilter).every(
37
+ ([key, value]) => item.body?.[key] === value
38
+ )
39
+ );
40
+ };
41
+
42
+ const updateCustomerTimeline = async (id, data) => {
43
+ return await CustomerTimeline.findByIdAndUpdate(id, data, {
44
+ new: true,
45
+ }).exec();
46
+ };
47
+ const updateManyCustomerTimelinesByFilter = async (filter = {}, data = {}) => {
48
+ await CustomerTimeline.updateMany(filter, data).exec();
49
+ const updatedDocs = await CustomerTimeline.find(filter).lean().exec();
50
+ return updatedDocs;
51
+ };
52
+
53
+ const updateCustomerTimelineByFilter = async (filter, data) => {
54
+ return await CustomerTimeline.findOneAndUpdate(filter, data, {
55
+ new: true,
56
+ }).exec();
57
+ };
58
+
59
+ const deleteCustomerTimeline = async (id) => {
60
+ return await CustomerTimeline.findByIdAndDelete(id).exec();
61
+ };
62
+
63
+ const deleteAllCustomerTimelinesByWorkspace = async (workspaceId) => {
64
+ return await CustomerTimeline.deleteMany({ workspaceId }).exec();
65
+ };
66
+
67
+ module.exports = {
68
+ createCustomerTimeline,
69
+ findCustomerTimelinesByWorkspaceId,
70
+ findCustomerTimelinesByWorkspace,
71
+ findCustomerTimelineByFilter,
72
+ updateCustomerTimeline,
73
+ updateCustomerTimelineByFilter,
74
+ deleteCustomerTimeline,
75
+ deleteAllCustomerTimelinesByWorkspace,
76
+ findAllCustomerTimelinesByFilter,
77
+ updateManyCustomerTimelinesByFilter,
78
+ };
@@ -0,0 +1,229 @@
1
+ const { getRedisData, setRedisData } = require("../../config/redis");
2
+ // const DescriptionTemplate = require("../../../models/DescriptionTemplate");
3
+ const DescriptionTemplate = require("../../models/DescriptionTemplate");
4
+
5
+ const CACHE_KEY_ALL = "description_templates_all";
6
+
7
+ /**
8
+ * Get cached descriptionTemplates for all workspaces.
9
+ */
10
+ const getCachedAllDescriptionTemplates = async () => {
11
+ let descriptionTemplates = await getRedisData(CACHE_KEY_ALL);
12
+ if (!descriptionTemplates) {
13
+ descriptionTemplates = await DescriptionTemplate.find({}).lean().exec();
14
+ await setRedisData(CACHE_KEY_ALL, descriptionTemplates);
15
+ }
16
+ return descriptionTemplates;
17
+ };
18
+
19
+ /**
20
+ * Update cached descriptionTemplates for all workspaces.
21
+ */
22
+ const updateCachedAllDescriptionTemplates = async () => {
23
+ const descriptionTemplates = await DescriptionTemplate.find({}).lean().exec();
24
+ await setRedisData(CACHE_KEY_ALL, descriptionTemplates);
25
+ };
26
+
27
+ /**
28
+ * Get cached descriptionTemplates for a specific workspace.
29
+ * Now uses the all descriptionTemplates cache and filters by workspaceId.
30
+ */
31
+
32
+ /**
33
+ * Create a new DescriptionTemplate and update cache.
34
+ */
35
+ const createTemplate = async (data) => {
36
+ const newTemplate = new DescriptionTemplate(data);
37
+ const saveDescriptionTemplate = await newTemplate.save();
38
+
39
+ // Update only the main cache
40
+ await updateCachedAllDescriptionTemplates();
41
+
42
+ return saveDescriptionTemplate;
43
+ };
44
+
45
+ /**
46
+ * Find an DescriptionTemplate by ID.
47
+ */
48
+ const findTemplatesByWorkspaceId = async (workspaceId) => {
49
+ const allDescriptionTemplates = await getCachedAllDescriptionTemplates();
50
+ return allDescriptionTemplates.filter(
51
+ (desc) => desc.workspaceId === workspaceId
52
+ );
53
+ };
54
+
55
+ /**
56
+ * Find DescriptionTemplate by filter (supports both workspace & non-workspace).
57
+ */
58
+ const findTemplateByFilter = async (
59
+ filter,
60
+ bodyFilter = {},
61
+ workspaceId = null
62
+ ) => {
63
+ const allTemplates = await getCachedAllDescriptionTemplates();
64
+
65
+ // Filter by workspaceId if provided
66
+ let filteredTemplates = workspaceId
67
+ ? allTemplates.filter((desc) => desc.workspaceId === workspaceId)
68
+ : allTemplates;
69
+
70
+ return (
71
+ filteredTemplates.find(
72
+ (item) =>
73
+ // Check direct properties of item
74
+ Object.entries(filter).every(([key, value]) => item[key] === value) &&
75
+ // If bodyFilter is provided, check inside item.body
76
+ Object.entries(bodyFilter).every(
77
+ ([key, value]) => item.body?.[key] === value
78
+ )
79
+ ) || null
80
+ );
81
+ };
82
+ const findAllTemplateByFilter = async (
83
+ filter,
84
+ bodyFilter = {},
85
+ workspaceId = null
86
+ ) => {
87
+ const allTemplates = await getCachedAllDescriptionTemplates();
88
+
89
+ // Filter by workspaceId if provided
90
+ let filteredTemplates = workspaceId
91
+ ? allTemplates.filter((desc) => desc.workspaceId === workspaceId)
92
+ : allTemplates;
93
+
94
+ return (
95
+ filteredTemplates.filter(
96
+ (item) =>
97
+ // Check direct properties of item
98
+ Object.entries(filter).every(([key, value]) => item[key] === value) &&
99
+ // If bodyFilter is provided, check inside item.body
100
+ Object.entries(bodyFilter).every(
101
+ ([key, value]) => item.body?.[key] === value
102
+ )
103
+ ) || null
104
+ );
105
+ };
106
+ const createOrUpdateTemplate = async (filter, data) => {
107
+ const template = await DescriptionTemplate.findOneAndUpdate(filter, data, {
108
+ upsert: true,
109
+ new: true,
110
+ });
111
+ if (template) {
112
+ // Update only the main cache
113
+ await updateCachedAllDescriptionTemplates();
114
+ }
115
+ return template;
116
+ };
117
+ const updateTemplateById = async (id, data) => {
118
+ const updatedTemplate = await DescriptionTemplate.findByIdAndUpdate(
119
+ id,
120
+ data,
121
+ {
122
+ new: true,
123
+ }
124
+ ).exec();
125
+
126
+ if (updatedTemplate) {
127
+ // Update only the main cache
128
+ await updateCachedAllDescriptionTemplates();
129
+ }
130
+
131
+ return updatedTemplate;
132
+ };
133
+
134
+ /**
135
+ * Update an DescriptionTemplate by filter.
136
+ */
137
+ const updateTemplateByFilter = async (filter, data, workspaceId = null) => {
138
+ // Add workspace filter if provided
139
+ const queryFilter = workspaceId ? { ...filter, workspaceId } : filter;
140
+
141
+ const updatedTemplate = await DescriptionTemplate.findOneAndUpdate(
142
+ queryFilter,
143
+ data,
144
+ {
145
+ new: true,
146
+ }
147
+ ).exec();
148
+
149
+ if (updatedTemplate) {
150
+ // Update only the main cache
151
+ await updateCachedAllDescriptionTemplates();
152
+ }
153
+
154
+ return updatedTemplate;
155
+ };
156
+ const updateManyTemplates = async (filter, data) => {
157
+ const queryFilter = workspaceId ? { ...filter, workspaceId } : filter;
158
+
159
+ const updatedTemplate = await DescriptionTemplate.updateMany(
160
+ queryFilter,
161
+ { $set: { ...data } },
162
+ {
163
+ new: true,
164
+ }
165
+ ).exec();
166
+
167
+ if (updatedTemplate) {
168
+ // Update only the main cache
169
+ await updateCachedAllDescriptionTemplates();
170
+ }
171
+
172
+ return updatedTemplate;
173
+ };
174
+ /**
175
+ * Delete an DescriptionTemplate by ID.
176
+ */
177
+ const deleteTemplate = async (id) => {
178
+ const deletedTemplate = await DescriptionTemplate.findByIdAndDelete(
179
+ id
180
+ ).exec();
181
+
182
+ if (deletedTemplate) {
183
+ await updateCachedAllDescriptionTemplates();
184
+ }
185
+
186
+ return deletedTemplate;
187
+ };
188
+
189
+ const deleteAllTemplatesByWorkspace = async (workspaceId) => {
190
+ await DescriptionTemplate.deleteMany({ workspaceId }).exec();
191
+
192
+ await updateCachedAllDescriptionTemplates();
193
+ };
194
+
195
+ const findTemplateById = async (id) => {
196
+ return await DescriptionTemplate.findById(id).lean().exec();
197
+ };
198
+
199
+ const deleteTemplatesByFilter = async (filter) => {
200
+ await DescriptionTemplate.deleteMany(filter).exec();
201
+
202
+ await updateCachedAllDescriptionTemplates();
203
+ };
204
+
205
+ const insertManyTemplates = async (templates) => {
206
+ const insertedTemplates = await DescriptionTemplate.insertMany(templates);
207
+
208
+ if (insertedTemplates?.length > 0) {
209
+ await updateCachedAllDescriptionTemplates();
210
+ }
211
+
212
+ return insertedTemplates;
213
+ };
214
+
215
+ module.exports = {
216
+ createTemplate,
217
+ createOrUpdateTemplate,
218
+ findTemplateByFilter,
219
+ findTemplatesByWorkspaceId,
220
+ updateTemplateById,
221
+ updateTemplateByFilter,
222
+ updateManyTemplates,
223
+ deleteTemplate,
224
+ deleteAllTemplatesByWorkspace,
225
+ findTemplateById,
226
+ deleteTemplatesByFilter,
227
+ insertManyTemplates,
228
+ findAllTemplateByFilter,
229
+ };
@@ -0,0 +1,34 @@
1
+ const workspaceRepository = require("./workspace.repository");
2
+ const integrationRepository = require("./integration.repository");
3
+ const descriptionTemplateRepository = require("./descriptionTemplates.repository");
4
+ const shipperRepository = require("./shipper.repository");
5
+ const labelRepository = require("./label.repository");
6
+ const userRepository = require("./user.repository");
7
+ const userRoleRepository = require("./userRole.repository");
8
+ const settingRepository = require("./settings.repository");
9
+ const socialMediaSettingRepository = require("./socialMediaSetting.repository");
10
+ const chatMemberRepository = require("./chatMember.repository");
11
+ const roleRepository = require("./role.repository");
12
+ const userPermissionRepository = require("./userPermission.repository");
13
+ const userRolePermissionRepository = require("./userRolePermission.repository");
14
+ const notificationSettingsRepository = require("./notificationSettings.repository");
15
+ const customerProfileRepository = require("./customerProfile.repository");
16
+ const customerTimelineRepository = require("./customerTimeline.repository");
17
+ exports.module = {
18
+ workspaceRepository,
19
+ integrationRepository,
20
+ descriptionTemplateRepository,
21
+ shipperRepository,
22
+ labelRepository,
23
+ userRepository,
24
+ userRoleRepository,
25
+ settingRepository,
26
+ socialMediaSettingRepository,
27
+ notificationSettingsRepository,
28
+ chatMemberRepository,
29
+ roleRepository,
30
+ userPermissionRepository,
31
+ userRolePermissionRepository,
32
+ customerProfileRepository,
33
+ customerTimelineRepository,
34
+ };
@@ -0,0 +1,210 @@
1
+ const { getRedisData, setRedisData } = require("../../config/redis");
2
+ const Integration = require("../../models/Integration");
3
+
4
+ const CACHE_KEY_ALL = "integrations_all";
5
+
6
+ /**
7
+ * Get cached integrations for all workspaces.
8
+ */
9
+ const getCachedAllIntegrations = async () => {
10
+ let integrations = await getRedisData(CACHE_KEY_ALL);
11
+ if (!integrations) {
12
+ integrations = await Integration.find({}).lean().exec();
13
+ await setRedisData(CACHE_KEY_ALL, integrations);
14
+ }
15
+ return integrations?.map((x) => {
16
+ return {
17
+ ...x,
18
+ _id: x?._id?.toString(),
19
+ };
20
+ });
21
+ };
22
+
23
+ /**
24
+ * Update cached integrations for all workspaces.
25
+ */
26
+ const updateCachedAllIntegrations = async () => {
27
+ const integrations = await Integration.find({}).lean().exec();
28
+ await setRedisData(CACHE_KEY_ALL, integrations);
29
+ };
30
+
31
+ /**
32
+ * Get cached integrations for a specific workspace.
33
+ * Now uses the all integrations cache and filters by workspaceId.
34
+ */
35
+ const getCachedIntegrations = async (workspaceId) => {
36
+ const allIntegrations = await getCachedAllIntegrations();
37
+ return allIntegrations.filter((intg) => intg.workspaceId === workspaceId);
38
+ };
39
+
40
+ /**
41
+ * Create a new integration and update cache.
42
+ */
43
+ const createIntegration = async (data) => {
44
+ const newIntegration = new Integration(data);
45
+ const savedIntegration = await newIntegration.save();
46
+
47
+ // Update only the main cache
48
+ await updateCachedAllIntegrations();
49
+
50
+ return savedIntegration;
51
+ };
52
+
53
+ /**
54
+ * Find an integration by ID.
55
+ */
56
+ const findIntegrationsByWorkspaceId = async (workspaceId) => {
57
+ return await getCachedIntegrations(workspaceId);
58
+ };
59
+
60
+ /**
61
+ * Find integrations by workspace.
62
+ */
63
+ const findIntegrationsByWorkspace = async (workspaceId, filter = {}) => {
64
+ const allIntegrations = await getCachedIntegrations(workspaceId);
65
+
66
+ return allIntegrations.filter(
67
+ (intg) =>
68
+ intg.workspaceId === workspaceId &&
69
+ Object.entries(filter).every(([key, value]) => intg[key] === value)
70
+ );
71
+ };
72
+
73
+ /**
74
+ * Find integration by filter (supports both workspace & non-workspace).
75
+ */
76
+ const findIntegrationByFilter = async (
77
+ filter,
78
+ bodyFilter = {},
79
+ workspaceId = null
80
+ ) => {
81
+ const allIntegrations = await getCachedAllIntegrations();
82
+
83
+ let filteredIntegrations = allIntegrations;
84
+ if (workspaceId) {
85
+ filteredIntegrations = allIntegrations.filter(
86
+ (intg) => intg.workspaceId === workspaceId
87
+ );
88
+ }
89
+
90
+ return (
91
+ filteredIntegrations.find(
92
+ (item) =>
93
+ Object.entries(filter).every(([key, value]) => item[key] === value) &&
94
+ Object.entries(bodyFilter).every(
95
+ ([key, value]) => item.body?.[key] === value
96
+ )
97
+ ) || null
98
+ );
99
+ };
100
+ const findAllIntegrationByFilter = async (
101
+ filter = {},
102
+ bodyFilter = {},
103
+ workspaceId = null
104
+ ) => {
105
+ const allIntegrations = await getCachedAllIntegrations();
106
+
107
+ let filteredIntegrations = allIntegrations;
108
+ if (workspaceId) {
109
+ filteredIntegrations = allIntegrations.filter(
110
+ (intg) => intg.workspaceId === workspaceId
111
+ );
112
+ }
113
+ return (
114
+ filteredIntegrations.filter(
115
+ (item) =>
116
+ Object.entries(filter).every(([key, value]) => item[key] === value) &&
117
+ Object.entries(bodyFilter).every(
118
+ ([key, value]) => item.body?.[key] === value
119
+ )
120
+ ) || []
121
+ );
122
+ };
123
+ const bulkCreateAndUpdateIntegrations = async (operations) => {
124
+ await Integration.bulkWrite(operations);
125
+ await updateCachedAllIntegrations();
126
+ return operations;
127
+ };
128
+ /**
129
+ * Find all integrations (without workspace filtering).
130
+ */
131
+ const findAllIntegrations = async () => {
132
+ return await getCachedAllIntegrations();
133
+ };
134
+
135
+ /**
136
+ * Update an integration by ID.
137
+ */
138
+ const updateIntegration = async (id, data) => {
139
+ const updatedIntegration = await Integration.findByIdAndUpdate(id, data, {
140
+ new: true,
141
+ }).exec();
142
+
143
+ if (updatedIntegration) {
144
+ // Update only the main cache
145
+ await updateCachedAllIntegrations();
146
+ }
147
+
148
+ return updatedIntegration;
149
+ };
150
+
151
+ /**
152
+ * Update an integration by filter.
153
+ */
154
+ const updateIntegrationByFilter = async (filter, data, workspaceId = null) => {
155
+ // Add workspace filter if provided
156
+ const queryFilter = workspaceId ? { ...filter, workspaceId } : filter;
157
+
158
+ const updatedIntegration = await Integration.findOneAndUpdate(
159
+ queryFilter,
160
+ data,
161
+ {
162
+ new: true,
163
+ }
164
+ ).exec();
165
+
166
+ if (updatedIntegration) {
167
+ // Update only the main cache
168
+ await updateCachedAllIntegrations();
169
+ }
170
+
171
+ return updatedIntegration;
172
+ };
173
+
174
+ /**
175
+ * Delete an integration by ID.
176
+ */
177
+ const deleteIntegration = async (id) => {
178
+ const deletedIntegration = await Integration.findByIdAndDelete(id).exec();
179
+
180
+ if (deletedIntegration) {
181
+ // Update only the main cache
182
+ await updateCachedAllIntegrations();
183
+ }
184
+
185
+ return deletedIntegration;
186
+ };
187
+
188
+ /**
189
+ * Delete all integrations for a specific workspace.
190
+ */
191
+ const deleteAllIntegrationsByWorkspace = async (workspaceId) => {
192
+ await Integration.deleteMany({ workspaceId }).exec();
193
+
194
+ // Update only the main cache
195
+ await updateCachedAllIntegrations();
196
+ };
197
+
198
+ module.exports = {
199
+ createIntegration,
200
+ findIntegrationsByWorkspaceId,
201
+ findIntegrationsByWorkspace,
202
+ findIntegrationByFilter,
203
+ findAllIntegrations,
204
+ updateIntegration,
205
+ updateIntegrationByFilter,
206
+ deleteIntegration,
207
+ deleteAllIntegrationsByWorkspace,
208
+ findAllIntegrationByFilter,
209
+ bulkCreateAndUpdateIntegrations,
210
+ };