@friggframework/core 2.0.0--canary.398.a314355.0 → 2.0.0--canary.397.4957a89.0

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 (97) hide show
  1. package/README.md +931 -50
  2. package/core/create-handler.js +1 -0
  3. package/credential/credential-repository.js +42 -0
  4. package/credential/use-cases/get-credential-for-user.js +21 -0
  5. package/credential/use-cases/update-authentication-status.js +15 -0
  6. package/database/models/WebsocketConnection.js +0 -5
  7. package/handlers/app-definition-loader.js +38 -0
  8. package/handlers/app-handler-helpers.js +0 -3
  9. package/handlers/backend-utils.js +35 -34
  10. package/handlers/routers/auth.js +3 -14
  11. package/handlers/routers/integration-defined-routers.js +8 -5
  12. package/handlers/routers/user.js +25 -5
  13. package/handlers/workers/integration-defined-workers.js +6 -3
  14. package/index.js +1 -16
  15. package/integrations/index.js +0 -5
  16. package/integrations/integration-base.js +42 -44
  17. package/integrations/integration-repository.js +67 -0
  18. package/integrations/integration-router.js +301 -178
  19. package/integrations/integration.js +233 -0
  20. package/integrations/options.js +1 -1
  21. package/integrations/tests/doubles/dummy-integration-class.js +90 -0
  22. package/integrations/tests/doubles/test-integration-repository.js +89 -0
  23. package/integrations/tests/use-cases/create-integration.test.js +124 -0
  24. package/integrations/tests/use-cases/delete-integration-for-user.test.js +143 -0
  25. package/integrations/tests/use-cases/get-integration-for-user.test.js +143 -0
  26. package/integrations/tests/use-cases/get-integration-instance.test.js +169 -0
  27. package/integrations/tests/use-cases/get-integrations-for-user.test.js +169 -0
  28. package/integrations/tests/use-cases/get-possible-integrations.test.js +188 -0
  29. package/integrations/tests/use-cases/update-integration-messages.test.js +142 -0
  30. package/integrations/tests/use-cases/update-integration-status.test.js +103 -0
  31. package/integrations/tests/use-cases/update-integration.test.js +134 -0
  32. package/integrations/use-cases/create-integration.js +72 -0
  33. package/integrations/use-cases/delete-integration-for-user.js +73 -0
  34. package/integrations/use-cases/get-integration-for-user.js +79 -0
  35. package/integrations/use-cases/get-integration-instance.js +84 -0
  36. package/integrations/use-cases/get-integrations-for-user.js +77 -0
  37. package/integrations/use-cases/get-possible-integrations.js +27 -0
  38. package/integrations/use-cases/index.js +11 -0
  39. package/integrations/use-cases/update-integration-messages.js +31 -0
  40. package/integrations/use-cases/update-integration-status.js +28 -0
  41. package/integrations/use-cases/update-integration.js +92 -0
  42. package/integrations/utils/map-integration-dto.js +36 -0
  43. package/{module-plugin → modules}/index.js +0 -8
  44. package/modules/module-factory.js +54 -0
  45. package/modules/module-repository.js +107 -0
  46. package/modules/module.js +221 -0
  47. package/{module-plugin → modules}/test/mock-api/api.js +8 -3
  48. package/{module-plugin → modules}/test/mock-api/definition.js +12 -8
  49. package/modules/tests/doubles/test-module-factory.js +16 -0
  50. package/modules/tests/doubles/test-module-repository.js +19 -0
  51. package/modules/use-cases/get-entities-for-user.js +32 -0
  52. package/modules/use-cases/get-entity-options-by-id.js +58 -0
  53. package/modules/use-cases/get-entity-options-by-type.js +34 -0
  54. package/modules/use-cases/get-module-instance-from-type.js +31 -0
  55. package/modules/use-cases/get-module.js +56 -0
  56. package/modules/use-cases/process-authorization-callback.js +114 -0
  57. package/modules/use-cases/refresh-entity-options.js +58 -0
  58. package/modules/use-cases/test-module-auth.js +54 -0
  59. package/modules/utils/map-module-dto.js +18 -0
  60. package/package.json +5 -5
  61. package/syncs/sync.js +0 -1
  62. package/types/integrations/index.d.ts +2 -6
  63. package/types/module-plugin/index.d.ts +4 -56
  64. package/types/syncs/index.d.ts +0 -2
  65. package/user/tests/doubles/test-user-repository.js +72 -0
  66. package/user/tests/use-cases/create-individual-user.test.js +24 -0
  67. package/user/tests/use-cases/create-organization-user.test.js +28 -0
  68. package/user/tests/use-cases/create-token-for-user-id.test.js +19 -0
  69. package/user/tests/use-cases/get-user-from-bearer-token.test.js +64 -0
  70. package/user/tests/use-cases/login-user.test.js +140 -0
  71. package/user/use-cases/create-individual-user.js +61 -0
  72. package/user/use-cases/create-organization-user.js +47 -0
  73. package/user/use-cases/create-token-for-user-id.js +30 -0
  74. package/user/use-cases/get-user-from-bearer-token.js +77 -0
  75. package/user/use-cases/login-user.js +122 -0
  76. package/user/user-repository.js +62 -0
  77. package/user/user.js +77 -0
  78. package/handlers/routers/middleware/loadUser.js +0 -15
  79. package/handlers/routers/middleware/requireLoggedInUser.js +0 -12
  80. package/integrations/create-frigg-backend.js +0 -31
  81. package/integrations/integration-factory.js +0 -251
  82. package/integrations/integration-user.js +0 -144
  83. package/integrations/test/integration-base.test.js +0 -144
  84. package/module-plugin/auther.js +0 -393
  85. package/module-plugin/entity-manager.js +0 -70
  86. package/module-plugin/manager.js +0 -169
  87. package/module-plugin/module-factory.js +0 -61
  88. /package/{module-plugin → modules}/ModuleConstants.js +0 -0
  89. /package/{module-plugin → modules}/credential.js +0 -0
  90. /package/{module-plugin → modules}/entity.js +0 -0
  91. /package/{module-plugin → modules}/requester/api-key.js +0 -0
  92. /package/{module-plugin → modules}/requester/basic.js +0 -0
  93. /package/{module-plugin → modules}/requester/oauth-2.js +0 -0
  94. /package/{module-plugin → modules}/requester/requester.js +0 -0
  95. /package/{module-plugin → modules}/requester/requester.test.js +0 -0
  96. /package/{module-plugin → modules}/test/auther.test.js +0 -0
  97. /package/{module-plugin → modules}/test/mock-api/mocks/hubspot.js +0 -0
@@ -0,0 +1,142 @@
1
+ const { UpdateIntegrationMessages } = require('../../use-cases/update-integration-messages');
2
+ const { TestIntegrationRepository } = require('../doubles/test-integration-repository');
3
+
4
+ describe('UpdateIntegrationMessages Use-Case', () => {
5
+ let integrationRepository;
6
+ let useCase;
7
+
8
+ beforeEach(() => {
9
+ integrationRepository = new TestIntegrationRepository();
10
+ useCase = new UpdateIntegrationMessages({ integrationRepository });
11
+ });
12
+
13
+ describe('happy path', () => {
14
+ it('adds message with correct details', async () => {
15
+ const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
16
+ const timestamp = Date.now();
17
+
18
+ await useCase.execute(record.id, 'errors', 'Test Error', 'Error details here', timestamp);
19
+
20
+ const fetched = await integrationRepository.findIntegrationById(record.id);
21
+ expect(fetched.messages.errors.length).toBe(1);
22
+ expect(fetched.messages.errors[0]).toEqual({
23
+ title: 'Test Error',
24
+ message: 'Error details here',
25
+ timestamp: timestamp
26
+ });
27
+ });
28
+
29
+ it('adds multiple messages to same type', async () => {
30
+ const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
31
+
32
+ await useCase.execute(record.id, 'errors', 'Error 1', 'First error', 1000);
33
+ await useCase.execute(record.id, 'errors', 'Error 2', 'Second error', 2000);
34
+
35
+ const fetched = await integrationRepository.findIntegrationById(record.id);
36
+ expect(fetched.messages.errors.length).toBe(2);
37
+ expect(fetched.messages.errors[0].title).toBe('Error 1');
38
+ expect(fetched.messages.errors[1].title).toBe('Error 2');
39
+ });
40
+
41
+ it('adds messages to different types', async () => {
42
+ const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
43
+
44
+ await useCase.execute(record.id, 'errors', 'Error Title', 'Error body', 1000);
45
+ await useCase.execute(record.id, 'warnings', 'Warning Title', 'Warning body', 2000);
46
+ await useCase.execute(record.id, 'info', 'Info Title', 'Info body', 3000);
47
+
48
+ const fetched = await integrationRepository.findIntegrationById(record.id);
49
+ expect(fetched.messages.errors.length).toBe(1);
50
+ expect(fetched.messages.warnings.length).toBe(1);
51
+ expect(fetched.messages.info.length).toBe(1);
52
+ });
53
+
54
+ it('tracks message update operation', async () => {
55
+ const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
56
+ integrationRepository.clearHistory();
57
+
58
+ await useCase.execute(record.id, 'logs', 'Log Entry', 'Log details', Date.now());
59
+
60
+ const history = integrationRepository.getOperationHistory();
61
+ const updateOperation = history.find(op => op.operation === 'updateMessages');
62
+ expect(updateOperation).toEqual({
63
+ operation: 'updateMessages',
64
+ id: record.id,
65
+ type: 'logs',
66
+ success: true
67
+ });
68
+ });
69
+ });
70
+
71
+ describe('error cases', () => {
72
+ it('returns false when integration not found', async () => {
73
+ const nonExistentId = 'non-existent-id';
74
+
75
+ const result = await useCase.execute(nonExistentId, 'errors', 'title', 'body', Date.now());
76
+
77
+ expect(result).toBe(false);
78
+ });
79
+
80
+ it('tracks failed message update operation', async () => {
81
+ const nonExistentId = 'non-existent-id';
82
+ integrationRepository.clearHistory();
83
+
84
+ await useCase.execute(nonExistentId, 'errors', 'title', 'body', Date.now());
85
+
86
+ const history = integrationRepository.getOperationHistory();
87
+ const updateOperation = history.find(op => op.operation === 'updateMessages');
88
+ expect(updateOperation).toEqual({
89
+ operation: 'updateMessages',
90
+ id: nonExistentId,
91
+ success: false
92
+ });
93
+ });
94
+ });
95
+
96
+ describe('edge cases', () => {
97
+ it('handles empty title and body', async () => {
98
+ const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
99
+
100
+ await useCase.execute(record.id, 'info', '', '', Date.now());
101
+
102
+ const fetched = await integrationRepository.findIntegrationById(record.id);
103
+ expect(fetched.messages.info[0].title).toBe('');
104
+ expect(fetched.messages.info[0].message).toBe('');
105
+ });
106
+
107
+ it('handles null and undefined values', async () => {
108
+ const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
109
+
110
+ await useCase.execute(record.id, 'warnings', null, undefined, null);
111
+
112
+ const fetched = await integrationRepository.findIntegrationById(record.id);
113
+ expect(fetched.messages.warnings[0].title).toBeNull();
114
+ expect(fetched.messages.warnings[0].message).toBeUndefined();
115
+ expect(fetched.messages.warnings[0].timestamp).toBeNull();
116
+ });
117
+
118
+ it('handles very long message content', async () => {
119
+ const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
120
+ const longTitle = 'A'.repeat(1000);
121
+ const longBody = 'B'.repeat(5000);
122
+
123
+ await useCase.execute(record.id, 'errors', longTitle, longBody, Date.now());
124
+
125
+ const fetched = await integrationRepository.findIntegrationById(record.id);
126
+ expect(fetched.messages.errors[0].title).toBe(longTitle);
127
+ expect(fetched.messages.errors[0].message).toBe(longBody);
128
+ });
129
+
130
+ it('handles special characters in messages', async () => {
131
+ const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
132
+ const specialTitle = '🚨 Error with émojis & spëcial chars';
133
+ const specialBody = 'Body with\nnewlines\tand\ttabs';
134
+
135
+ await useCase.execute(record.id, 'errors', specialTitle, specialBody, Date.now());
136
+
137
+ const fetched = await integrationRepository.findIntegrationById(record.id);
138
+ expect(fetched.messages.errors[0].title).toBe(specialTitle);
139
+ expect(fetched.messages.errors[0].message).toBe(specialBody);
140
+ });
141
+ });
142
+ });
@@ -0,0 +1,103 @@
1
+ const { UpdateIntegrationStatus } = require('../../use-cases/update-integration-status');
2
+ const { TestIntegrationRepository } = require('../doubles/test-integration-repository');
3
+
4
+ describe('UpdateIntegrationStatus Use-Case', () => {
5
+ let integrationRepository;
6
+ let useCase;
7
+
8
+ beforeEach(() => {
9
+ integrationRepository = new TestIntegrationRepository();
10
+ useCase = new UpdateIntegrationStatus({
11
+ integrationRepository,
12
+ });
13
+ });
14
+
15
+ describe('happy path', () => {
16
+ it('updates integration status', async () => {
17
+ const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'dummy' });
18
+
19
+ const result = await useCase.execute(record.id, 'ACTIVE');
20
+
21
+ expect(result).toBe(true);
22
+
23
+ const updatedRecord = await integrationRepository.findIntegrationById(record.id);
24
+ expect(updatedRecord.status).toBe('ACTIVE');
25
+ });
26
+
27
+ it('tracks status update operation', async () => {
28
+ const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'dummy' });
29
+ integrationRepository.clearHistory();
30
+
31
+ await useCase.execute(record.id, 'PAUSED');
32
+
33
+ const history = integrationRepository.getOperationHistory();
34
+ const updateOperation = history.find(op => op.operation === 'updateStatus');
35
+ expect(updateOperation).toEqual({
36
+ operation: 'updateStatus',
37
+ id: record.id,
38
+ status: 'PAUSED',
39
+ success: true
40
+ });
41
+ });
42
+
43
+ it('handles different status values', async () => {
44
+ const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'dummy' });
45
+
46
+ const statuses = ['ACTIVE', 'PAUSED', 'ERROR', 'DISABLED'];
47
+
48
+ for (const status of statuses) {
49
+ await useCase.execute(record.id, status);
50
+ const updatedRecord = await integrationRepository.findIntegrationById(record.id);
51
+ expect(updatedRecord.status).toBe(status);
52
+ }
53
+ });
54
+ });
55
+
56
+ describe('error cases', () => {
57
+ it('returns false when integration not found', async () => {
58
+ const nonExistentId = 'non-existent-id';
59
+
60
+ const result = await useCase.execute(nonExistentId, 'ACTIVE');
61
+
62
+ expect(result).toBe(false);
63
+ });
64
+
65
+ it('tracks failed update operation', async () => {
66
+ const nonExistentId = 'non-existent-id';
67
+ integrationRepository.clearHistory();
68
+
69
+ await useCase.execute(nonExistentId, 'ACTIVE');
70
+
71
+ const history = integrationRepository.getOperationHistory();
72
+ const updateOperation = history.find(op => op.operation === 'updateStatus');
73
+ expect(updateOperation).toEqual({
74
+ operation: 'updateStatus',
75
+ id: nonExistentId,
76
+ status: 'ACTIVE',
77
+ success: false
78
+ });
79
+ });
80
+ });
81
+
82
+ describe('edge cases', () => {
83
+ it('handles null status value', async () => {
84
+ const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'dummy' });
85
+
86
+ const result = await useCase.execute(record.id, null);
87
+
88
+ expect(result).toBe(true);
89
+ const updatedRecord = await integrationRepository.findIntegrationById(record.id);
90
+ expect(updatedRecord.status).toBeNull();
91
+ });
92
+
93
+ it('handles empty string status', async () => {
94
+ const record = await integrationRepository.createIntegration(['entity-1'], 'user-1', { type: 'dummy' });
95
+
96
+ const result = await useCase.execute(record.id, '');
97
+
98
+ expect(result).toBe(true);
99
+ const updatedRecord = await integrationRepository.findIntegrationById(record.id);
100
+ expect(updatedRecord.status).toBe('');
101
+ });
102
+ });
103
+ });
@@ -0,0 +1,134 @@
1
+ const { UpdateIntegration } = require('../../use-cases/update-integration');
2
+ const { TestIntegrationRepository } = require('../doubles/test-integration-repository');
3
+ const { TestModuleFactory } = require('../../../modules/tests/doubles/test-module-factory');
4
+ const { DummyIntegration } = require('../doubles/dummy-integration-class');
5
+
6
+ describe('UpdateIntegration Use-Case', () => {
7
+ let integrationRepository;
8
+ let moduleFactory;
9
+ let useCase;
10
+
11
+ beforeEach(() => {
12
+ integrationRepository = new TestIntegrationRepository();
13
+ moduleFactory = new TestModuleFactory();
14
+ useCase = new UpdateIntegration({
15
+ integrationRepository,
16
+ integrationClasses: [DummyIntegration],
17
+ moduleFactory,
18
+ });
19
+ });
20
+
21
+ describe('happy path', () => {
22
+ it('calls on update and returns dto', async () => {
23
+ const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy', foo: 'bar' });
24
+
25
+ const newConfig = { type: 'dummy', foo: 'baz' };
26
+ const dto = await useCase.execute(record.id, 'user-1', newConfig);
27
+
28
+ expect(dto.config.foo).toBe('baz');
29
+ expect(dto.id).toBe(record.id);
30
+ expect(dto.userId).toBe('user-1');
31
+ });
32
+
33
+ it('triggers ON_UPDATE event with correct payload', async () => {
34
+ const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy', foo: 'bar' });
35
+ integrationRepository.clearHistory();
36
+
37
+ const newConfig = { type: 'dummy', foo: 'updated' };
38
+ await useCase.execute(record.id, 'user-1', newConfig);
39
+
40
+ const history = integrationRepository.getOperationHistory();
41
+ const findOperation = history.find(op => op.operation === 'findById');
42
+ expect(findOperation).toEqual({
43
+ operation: 'findById',
44
+ id: record.id,
45
+ found: true
46
+ });
47
+ });
48
+
49
+ it('updates integration with multiple entities', async () => {
50
+ const record = await integrationRepository.createIntegration(['e1', 'e2'], 'user-1', { type: 'dummy' });
51
+
52
+ const newConfig = { type: 'dummy', updated: true };
53
+ const dto = await useCase.execute(record.id, 'user-1', newConfig);
54
+
55
+ expect(dto.entities).toEqual(['e1', 'e2']);
56
+ expect(dto.config.updated).toBe(true);
57
+ });
58
+ });
59
+
60
+ describe('error cases', () => {
61
+ it('throws error when integration not found', async () => {
62
+ const nonExistentId = 'non-existent-id';
63
+ const newConfig = { type: 'dummy', foo: 'baz' };
64
+
65
+ await expect(useCase.execute(nonExistentId, 'user-1', newConfig))
66
+ .rejects
67
+ .toThrow(`No integration found by the ID of ${nonExistentId}`);
68
+ });
69
+
70
+ it('throws error when integration class not found', async () => {
71
+ const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'unknown-type' });
72
+
73
+ const newConfig = { type: 'unknown-type', foo: 'baz' };
74
+
75
+ await expect(useCase.execute(record.id, 'user-1', newConfig))
76
+ .rejects
77
+ .toThrow('No integration class found for type: unknown-type');
78
+ });
79
+
80
+ it('throws error when user does not own integration', async () => {
81
+ const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
82
+
83
+ const newConfig = { type: 'dummy', foo: 'baz' };
84
+
85
+ await expect(useCase.execute(record.id, 'different-user', newConfig))
86
+ .rejects
87
+ .toThrow(`Integration ${record.id} does not belong to User different-user`);
88
+ });
89
+
90
+ it('throws error when no integration classes provided', async () => {
91
+ const useCaseWithoutClasses = new UpdateIntegration({
92
+ integrationRepository,
93
+ integrationClasses: [],
94
+ moduleFactory,
95
+ });
96
+
97
+ const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy' });
98
+ const newConfig = { type: 'dummy', foo: 'baz' };
99
+
100
+ await expect(useCaseWithoutClasses.execute(record.id, 'user-1', newConfig))
101
+ .rejects
102
+ .toThrow('No integration class found for type: dummy');
103
+ });
104
+ });
105
+
106
+ describe('edge cases', () => {
107
+ it('handles config with null values', async () => {
108
+ const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy', foo: 'bar' });
109
+
110
+ const newConfig = { type: 'dummy', foo: null, bar: undefined };
111
+ const dto = await useCase.execute(record.id, 'user-1', newConfig);
112
+
113
+ expect(dto.config.foo).toBeNull();
114
+ expect(dto.config.bar).toBeUndefined();
115
+ });
116
+
117
+ it('handles deeply nested config updates', async () => {
118
+ const record = await integrationRepository.createIntegration(['e1'], 'user-1', { type: 'dummy', nested: { old: 'value' } });
119
+
120
+ const newConfig = {
121
+ type: 'dummy',
122
+ nested: {
123
+ new: 'value',
124
+ deep: { level: 'test' }
125
+ }
126
+ };
127
+ const dto = await useCase.execute(record.id, 'user-1', newConfig);
128
+
129
+ expect(dto.config.nested.new).toBe('value');
130
+ expect(dto.config.nested.deep.level).toBe('test');
131
+ expect(dto.config.nested.old).toBeUndefined();
132
+ });
133
+ });
134
+ });
@@ -0,0 +1,72 @@
1
+ const { Integration } = require('../integration');
2
+ const { mapIntegrationClassToIntegrationDTO } = require('../utils/map-integration-dto');
3
+
4
+ /**
5
+ * Use case for creating a new integration instance.
6
+ * @class CreateIntegration
7
+ */
8
+ class CreateIntegration {
9
+ /**
10
+ * Creates a new CreateIntegration instance.
11
+ * @param {Object} params - Configuration parameters.
12
+ * @param {import('../integration-repository').IntegrationRepository} params.integrationRepository - Repository for integration data operations.
13
+ * @param {import('../integration-classes').IntegrationClasses} params.integrationClasses - Array of available integration classes.
14
+ * @param {import('../../modules/module-factory').ModuleFactory} params.moduleFactory - Service for module instantiation and management.
15
+ */
16
+ constructor({ integrationRepository, integrationClasses, moduleFactory }) {
17
+ this.integrationRepository = integrationRepository;
18
+ this.integrationClasses = integrationClasses;
19
+ this.moduleFactory = moduleFactory;
20
+ }
21
+
22
+ /**
23
+ * Executes the integration creation process.
24
+ * @async
25
+ * @param {string[]} entities - Array of entity IDs to associate with the integration.
26
+ * @param {string} userId - ID of the user creating the integration.
27
+ * @param {Object} config - Configuration object for the integration.
28
+ * @param {string} config.type - Type of integration to create.
29
+ * @returns {Promise<Object>} The created integration DTO.
30
+ * @throws {Error} When integration class is not found for the specified type.
31
+ */
32
+ async execute(entities, userId, config) {
33
+ const integrationRecord = await this.integrationRepository.createIntegration(entities, userId, config);
34
+
35
+
36
+ const integrationClass = this.integrationClasses.find(
37
+ (integrationClass) => integrationClass.Definition.name === integrationRecord.config.type
38
+ );
39
+
40
+ if (!integrationClass) {
41
+ throw new Error(`No integration class found for type: ${integrationRecord.config.type}`);
42
+ }
43
+
44
+ const modules = [];
45
+ for (const entityId of integrationRecord.entitiesIds) {
46
+ const moduleInstance = await this.moduleFactory.getModuleInstance(
47
+ entityId,
48
+ integrationRecord.userId
49
+ );
50
+ modules.push(moduleInstance);
51
+ }
52
+
53
+ const integrationInstance = new Integration({
54
+ id: integrationRecord.id,
55
+ userId: integrationRecord.userId,
56
+ entities: integrationRecord.entitiesIds,
57
+ config: integrationRecord.config,
58
+ status: integrationRecord.status,
59
+ version: integrationRecord.version,
60
+ messages: integrationRecord.messages,
61
+ integrationClass: integrationClass,
62
+ modules
63
+ });
64
+
65
+ await integrationInstance.initialize();
66
+ await integrationInstance.send('ON_CREATE', { integrationId: integrationRecord.id });
67
+
68
+ return mapIntegrationClassToIntegrationDTO(integrationInstance);
69
+ }
70
+ }
71
+
72
+ module.exports = { CreateIntegration };
@@ -0,0 +1,73 @@
1
+ const Boom = require('@hapi/boom');
2
+ const { Integration } = require('../integration');
3
+
4
+ /**
5
+ * Use case for deleting an integration for a specific user.
6
+ * @class DeleteIntegrationForUser
7
+ */
8
+ class DeleteIntegrationForUser {
9
+ /**
10
+ * Creates a new DeleteIntegrationForUser instance.
11
+ * @param {Object} params - Configuration parameters.
12
+ * @param {import('../integration-repository').IntegrationRepository} params.integrationRepository - Repository for integration data operations.
13
+ * @param {Array<import('../integration').Integration>} params.integrationClasses - Array of available integration classes.
14
+ */
15
+ constructor({ integrationRepository, integrationClasses }) {
16
+
17
+ /**
18
+ * @type {import('../integration-repository').IntegrationRepository}
19
+ */
20
+ this.integrationRepository = integrationRepository;
21
+ this.integrationClasses = integrationClasses;
22
+ }
23
+
24
+ /**
25
+ * Executes the deletion of an integration for a user.
26
+ * @async
27
+ * @param {string} integrationId - ID of the integration to delete.
28
+ * @param {string} userId - ID of the user requesting the deletion.
29
+ * @returns {Promise<void>} Resolves when the integration is successfully deleted.
30
+ * @throws {Boom.notFound} When integration with the specified ID does not exist.
31
+ * @throws {Error} When the integration doesn't belong to the specified user.
32
+ */
33
+ async execute(integrationId, userId) {
34
+ const integrationRecord = await this.integrationRepository.findIntegrationById(integrationId);
35
+
36
+ if (!integrationRecord) {
37
+ throw Boom.notFound(
38
+ `Integration with id of ${integrationId} does not exist`
39
+ );
40
+ }
41
+
42
+ const integrationClass = this.integrationClasses.find(
43
+ (integrationClass) => integrationClass.Definition.name === integrationRecord.config.type
44
+ );
45
+
46
+ if (integrationRecord.userId !== userId) {
47
+ throw new Error(
48
+ `Integration ${integrationId} does not belong to User ${userId}`
49
+ );
50
+ }
51
+
52
+ const integrationInstance = new Integration({
53
+ id: integrationRecord.id,
54
+ userId: integrationRecord.userId,
55
+ entities: integrationRecord.entitiesIds,
56
+ config: integrationRecord.config,
57
+ status: integrationRecord.status,
58
+ version: integrationRecord.version,
59
+ messages: integrationRecord.messages,
60
+ integrationClass: integrationClass,
61
+ modules: [],
62
+ });
63
+
64
+ // 6. Complete async initialization (load dynamic actions, register handlers)
65
+ await integrationInstance.initialize();
66
+ await integrationInstance.send('ON_DELETE');
67
+
68
+ await this.integrationRepository.deleteIntegrationById(integrationId);
69
+
70
+ }
71
+ }
72
+
73
+ module.exports = { DeleteIntegrationForUser };
@@ -0,0 +1,79 @@
1
+ const { Integration } = require('../integration');
2
+ const { mapIntegrationClassToIntegrationDTO } = require('../utils/map-integration-dto');
3
+
4
+ /**
5
+ * Use case for retrieving a single integration for a specific user.
6
+ * @class GetIntegrationForUser
7
+ */
8
+ class GetIntegrationForUser {
9
+ /**
10
+ * Creates a new GetIntegrationForUser instance.
11
+ * @param {Object} params - Configuration parameters.
12
+ * @param {import('../integration-repository').IntegrationRepository} params.integrationRepository - Repository for integration data operations.
13
+ * @param {Array<import('../integration').Integration>} params.integrationClasses - Array of available integration classes.
14
+ * @param {import('../../modules/module-factory').ModuleFactory} params.moduleFactory - Service for module instantiation and management.
15
+ * @param {import('../../modules/module-repository').ModuleRepository} params.moduleRepository - Repository for module and entity data operations.
16
+ */
17
+ constructor({ integrationRepository, integrationClasses, moduleFactory, moduleRepository }) {
18
+
19
+ /**
20
+ * @type {import('../integration-repository').IntegrationRepository}
21
+ */
22
+ this.integrationRepository = integrationRepository;
23
+ this.integrationClasses = integrationClasses;
24
+ this.moduleFactory = moduleFactory;
25
+ this.moduleRepository = moduleRepository;
26
+ }
27
+
28
+ /**
29
+ * Executes the retrieval of a single integration for a user.
30
+ * @async
31
+ * @param {string} integrationId - ID of the integration to retrieve.
32
+ * @param {string} userId - ID of the user requesting the integration.
33
+ * @returns {Promise<Object>} The integration DTO for the specified user.
34
+ * @throws {Boom.notFound} When integration with the specified ID does not exist.
35
+ * @throws {Boom.forbidden} When user does not have access to the integration.
36
+ */
37
+ async execute(integrationId, userId) {
38
+ const integrationRecord = await this.integrationRepository.findIntegrationById(integrationId);
39
+ const entities = await this.moduleRepository.findEntitiesByIds(integrationRecord.entitiesIds);
40
+
41
+ if (!integrationRecord) {
42
+ throw Boom.notFound(`Integration with id of ${integrationId} does not exist`);
43
+ }
44
+
45
+ if (integrationRecord.userId.toString() !== userId.toString()) {
46
+ throw Boom.forbidden('User does not have access to this integration');
47
+ }
48
+
49
+ const integrationClass = this.integrationClasses.find(
50
+ (integrationClass) => integrationClass.Definition.name === integrationRecord.config.type
51
+ );
52
+
53
+ const modules = [];
54
+ for (const entity of entities) {
55
+ const moduleInstance = await this.moduleFactory.getModuleInstance(
56
+ entity._id,
57
+ integrationRecord.userId
58
+ );
59
+ modules.push(moduleInstance);
60
+ }
61
+
62
+ const integrationInstance = new Integration({
63
+ id: integrationRecord._id,
64
+ userId: integrationRecord.userId,
65
+ entities: entities,
66
+ config: integrationRecord.config,
67
+ status: integrationRecord.status,
68
+ version: integrationRecord.version,
69
+ messages: integrationRecord.messages,
70
+ entityReference: integrationRecord.entityReference,
71
+ integrationClass: integrationClass,
72
+ modules
73
+ });
74
+
75
+ return mapIntegrationClassToIntegrationDTO(integrationInstance);
76
+ }
77
+ }
78
+
79
+ module.exports = { GetIntegrationForUser };