@friggframework/core 2.0.0--canary.397.155fecd.0 → 2.0.0--canary.398.e2147f7.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 (93) hide show
  1. package/README.md +50 -933
  2. package/core/create-handler.js +0 -1
  3. package/handlers/app-handler-helpers.js +3 -0
  4. package/handlers/backend-utils.js +34 -35
  5. package/handlers/routers/auth.js +14 -3
  6. package/handlers/routers/integration-defined-routers.js +5 -8
  7. package/handlers/routers/middleware/loadUser.js +15 -0
  8. package/handlers/routers/middleware/requireLoggedInUser.js +12 -0
  9. package/handlers/routers/user.js +5 -25
  10. package/handlers/workers/integration-defined-workers.js +3 -6
  11. package/index.js +14 -1
  12. package/integrations/create-frigg-backend.js +31 -0
  13. package/integrations/index.js +5 -0
  14. package/integrations/integration-base.js +44 -42
  15. package/integrations/integration-factory.js +251 -0
  16. package/integrations/integration-router.js +173 -291
  17. package/integrations/integration-user.js +144 -0
  18. package/integrations/options.js +1 -1
  19. package/integrations/test/integration-base.test.js +144 -0
  20. package/module-plugin/auther.js +393 -0
  21. package/module-plugin/entity-manager.js +70 -0
  22. package/{modules → module-plugin}/index.js +6 -0
  23. package/module-plugin/module-factory.js +61 -0
  24. package/{modules → module-plugin}/test/mock-api/api.js +3 -8
  25. package/{modules → module-plugin}/test/mock-api/definition.js +8 -12
  26. package/package.json +5 -5
  27. package/types/integrations/index.d.ts +6 -2
  28. package/types/module-plugin/index.d.ts +21 -4
  29. package/credential/credential-repository.js +0 -42
  30. package/credential/use-cases/get-credential-for-user.js +0 -21
  31. package/credential/use-cases/update-authentication-status.js +0 -15
  32. package/handlers/app-definition-loader.js +0 -38
  33. package/integrations/integration-repository.js +0 -67
  34. package/integrations/integration.js +0 -233
  35. package/integrations/tests/doubles/dummy-integration-class.js +0 -90
  36. package/integrations/tests/doubles/test-integration-repository.js +0 -89
  37. package/integrations/tests/use-cases/create-integration.test.js +0 -124
  38. package/integrations/tests/use-cases/delete-integration-for-user.test.js +0 -143
  39. package/integrations/tests/use-cases/get-integration-for-user.test.js +0 -143
  40. package/integrations/tests/use-cases/get-integration-instance.test.js +0 -169
  41. package/integrations/tests/use-cases/get-integrations-for-user.test.js +0 -169
  42. package/integrations/tests/use-cases/get-possible-integrations.test.js +0 -188
  43. package/integrations/tests/use-cases/update-integration-messages.test.js +0 -142
  44. package/integrations/tests/use-cases/update-integration-status.test.js +0 -103
  45. package/integrations/tests/use-cases/update-integration.test.js +0 -134
  46. package/integrations/use-cases/create-integration.js +0 -72
  47. package/integrations/use-cases/delete-integration-for-user.js +0 -73
  48. package/integrations/use-cases/get-integration-for-user.js +0 -79
  49. package/integrations/use-cases/get-integration-instance.js +0 -84
  50. package/integrations/use-cases/get-integrations-for-user.js +0 -77
  51. package/integrations/use-cases/get-possible-integrations.js +0 -27
  52. package/integrations/use-cases/index.js +0 -11
  53. package/integrations/use-cases/update-integration-messages.js +0 -31
  54. package/integrations/use-cases/update-integration-status.js +0 -28
  55. package/integrations/use-cases/update-integration.js +0 -92
  56. package/integrations/utils/map-integration-dto.js +0 -36
  57. package/modules/module-factory.js +0 -54
  58. package/modules/module-repository.js +0 -72
  59. package/modules/module.js +0 -251
  60. package/modules/tests/doubles/test-module-factory.js +0 -16
  61. package/modules/tests/doubles/test-module-repository.js +0 -19
  62. package/modules/use-cases/get-entities-for-user.js +0 -32
  63. package/modules/use-cases/get-entity-options-by-id.js +0 -58
  64. package/modules/use-cases/get-entity-options-by-type.js +0 -34
  65. package/modules/use-cases/get-module-instance-from-type.js +0 -31
  66. package/modules/use-cases/get-module.js +0 -56
  67. package/modules/use-cases/refresh-entity-options.js +0 -58
  68. package/modules/use-cases/test-module-auth.js +0 -54
  69. package/modules/utils/map-module-dto.js +0 -18
  70. package/user/tests/doubles/test-user-repository.js +0 -72
  71. package/user/tests/use-cases/create-individual-user.test.js +0 -24
  72. package/user/tests/use-cases/create-organization-user.test.js +0 -28
  73. package/user/tests/use-cases/create-token-for-user-id.test.js +0 -19
  74. package/user/tests/use-cases/get-user-from-bearer-token.test.js +0 -64
  75. package/user/tests/use-cases/login-user.test.js +0 -140
  76. package/user/use-cases/create-individual-user.js +0 -61
  77. package/user/use-cases/create-organization-user.js +0 -47
  78. package/user/use-cases/create-token-for-user-id.js +0 -30
  79. package/user/use-cases/get-user-from-bearer-token.js +0 -77
  80. package/user/use-cases/login-user.js +0 -122
  81. package/user/user-repository.js +0 -62
  82. package/user/user.js +0 -77
  83. /package/{modules → module-plugin}/ModuleConstants.js +0 -0
  84. /package/{modules → module-plugin}/credential.js +0 -0
  85. /package/{modules → module-plugin}/entity.js +0 -0
  86. /package/{modules → module-plugin}/manager.js +0 -0
  87. /package/{modules → module-plugin}/requester/api-key.js +0 -0
  88. /package/{modules → module-plugin}/requester/basic.js +0 -0
  89. /package/{modules → module-plugin}/requester/oauth-2.js +0 -0
  90. /package/{modules → module-plugin}/requester/requester.js +0 -0
  91. /package/{modules → module-plugin}/requester/requester.test.js +0 -0
  92. /package/{modules → module-plugin}/test/auther.test.js +0 -0
  93. /package/{modules → module-plugin}/test/mock-api/mocks/hubspot.js +0 -0
package/modules/module.js DELETED
@@ -1,251 +0,0 @@
1
- const { Delegate } = require('../core');
2
- const _ = require('lodash');
3
- const { flushDebugLog } = require('../logs');
4
- const { ModuleConstants } = require('./ModuleConstants');
5
-
6
- // todo: this class should be a Domain class, and the Delegate function is preventing us from
7
- // doing that, we probably have to get rid of the Delegate class as well as the event based
8
- // calls since they go against the Domain Driven Design principles (eg. a domain class should not call repository methods or use cases)
9
- class Module extends Delegate {
10
-
11
- //todo: entity should be replaced with actual entity properties
12
- /**
13
- *
14
- * @param {Object} params
15
- * @param {Object} params.definition The definition of the Api Module
16
- * @param {string} params.userId The user id
17
- * @param {Object} params.entity The entity record from the database
18
- */
19
- constructor({ definition, userId = null, entity: entityObj = null }) {
20
- super({ definition, userId, entity: entityObj });
21
-
22
- this.validateDefinition(definition);
23
-
24
- this.userId = userId;
25
- this.entity = entityObj;
26
- this.credential = entityObj?.credential;
27
- this.definition = definition;
28
- this.name = this.definition.moduleName;
29
- this.modelName = this.definition.modelName;
30
- this.apiClass = this.definition.API;
31
-
32
- // Repository used for persistence operations related to credentials.
33
- const { CredentialRepository } = require('../credential/credential-repository');
34
- this.credentialRepository = new CredentialRepository();
35
-
36
- // Repository responsible for entity persistence actions
37
- const { ModuleRepository } = require('./module-repository');
38
- this.moduleRepository = new ModuleRepository();
39
-
40
- Object.assign(this, this.definition.requiredAuthMethods);
41
-
42
- const apiParams = {
43
- ...this.definition.env,
44
- delegate: this,
45
- ...this.apiParamsFromCredential(this.credential),
46
- ...this.apiParamsFromEntity(this.entity),
47
- };
48
- this.api = new this.apiClass(apiParams);
49
- }
50
-
51
- getName() {
52
- return this.name;
53
- }
54
-
55
- getEntityOptions() {
56
- return this.definition.getEntityOptions()
57
- }
58
-
59
- async refreshEntityOptions(options) {
60
- await this.definition.refreshEntityOptions(options);
61
- return this.getEntityOptions();
62
- }
63
-
64
- apiParamsFromCredential(credential) {
65
- return _.pick(credential, ...this.apiPropertiesToPersist?.credential);
66
- }
67
-
68
- apiParamsFromEntity(entity) {
69
- return _.pick(entity, ...this.apiPropertiesToPersist?.entity);
70
- }
71
-
72
- async validateAuthorizationRequirements() {
73
- const requirements = await this.getAuthorizationRequirements();
74
- let valid = true;
75
- if (
76
- ['oauth1', 'oauth2'].includes(requirements.type) &&
77
- !requirements.url
78
- ) {
79
- valid = false;
80
- }
81
- return valid;
82
- }
83
-
84
- async getAuthorizationRequirements(params) {
85
- return this.api.getAuthorizationRequirements();
86
- }
87
-
88
- async testAuth() {
89
- let validAuth = false;
90
- try {
91
- if (await this.testAuthRequest(this.api)) validAuth = true;
92
- } catch (e) {
93
- flushDebugLog(e);
94
- }
95
- return validAuth;
96
- }
97
-
98
- async processAuthorizationCallback(params) {
99
- let tokenResponse;
100
- if (this.apiClass.requesterType === ModuleConstants.authType.oauth2) {
101
- tokenResponse = await this.getToken(this.api, params);
102
- } else {
103
- tokenResponse = await this.setAuthParams(this.api, params);
104
- await this.onTokenUpdate();
105
- }
106
- const authRes = await this.testAuth();
107
- if (!authRes) {
108
- throw new Error('Authorization failed');
109
- }
110
- const entityDetails = await this.getEntityDetails(
111
- this.api,
112
- params,
113
- tokenResponse,
114
- this.userId
115
- );
116
- Object.assign(
117
- entityDetails.details,
118
- this.apiParamsFromEntity(this.api)
119
- );
120
- await this.findOrCreateEntity(entityDetails);
121
- return {
122
- credential_id: this.credential.id,
123
- entity_id: this.entity.id,
124
- type: this.getName(),
125
- };
126
- }
127
-
128
- async onTokenUpdate() {
129
- const credentialDetails = await this.getCredentialDetails(
130
- this.api,
131
- this.userId
132
- );
133
- Object.assign(
134
- credentialDetails.details,
135
- this.apiParamsFromCredential(this.api)
136
- );
137
- credentialDetails.details.auth_is_valid = true;
138
-
139
- const persisted = await this.credentialRepository.upsertCredential(credentialDetails);
140
- this.credential = persisted;
141
- }
142
-
143
- async receiveNotification(notifier, delegateString, object = null) {
144
- if (delegateString === this.api.DLGT_TOKEN_UPDATE) {
145
- await this.onTokenUpdate();
146
- } else if (delegateString === this.api.DLGT_TOKEN_DEAUTHORIZED) {
147
- await this.deauthorize();
148
- } else if (delegateString === this.api.DLGT_INVALID_AUTH) {
149
- await this.markCredentialsInvalid();
150
- }
151
- }
152
-
153
- async markCredentialsInvalid() {
154
- if (!this.credential) return;
155
-
156
- // Persist flag change through repository – works even when the
157
- // credential object is a plain JavaScript object (lean query).
158
- const credentialId = this.credential._id || this.credential.id;
159
- if (!credentialId) return;
160
-
161
- await this.credentialRepository.updateAuthenticationStatus(
162
- credentialId,
163
- false
164
- );
165
-
166
- // Keep the in-memory snapshot consistent so that callers can read the
167
- // updated state without another fetch.
168
- this.credential.auth_is_valid = false;
169
- }
170
-
171
- async deauthorize() {
172
- this.api = new this.apiClass();
173
-
174
- // Remove persisted credential (if any)
175
- if (this.entity?.credential) {
176
- const credentialId =
177
- this.entity.credential._id || this.entity.credential.id || this.entity.credential;
178
-
179
- // Delete credential via repository
180
- await this.credentialRepository.deleteCredentialById(credentialId);
181
-
182
- // Unset credential reference on the Entity document
183
- const entityId = this.entity._id || this.entity.id;
184
- if (entityId) {
185
- await this.moduleRepository.unsetCredential(entityId);
186
- }
187
-
188
- // Keep in-memory snapshot consistent
189
- this.entity.credential = undefined;
190
- }
191
- }
192
-
193
- // todo: check if all these props are still up to date
194
- validateDefinition(definition) {
195
- if (!definition) {
196
- throw new Error('Module definition is required');
197
- }
198
- if (!definition.moduleName) {
199
- throw new Error('Module definition requires moduleName');
200
- }
201
- if (!definition.API) {
202
- throw new Error('Module definition requires API class');
203
- }
204
- if (!definition.requiredAuthMethods) {
205
- throw new Error('Module definition requires requiredAuthMethods');
206
- } else {
207
- if (
208
- definition.API.requesterType ===
209
- ModuleConstants.authType.oauth2 &&
210
- !definition.requiredAuthMethods.getToken
211
- ) {
212
- throw new Error(
213
- 'Module definition requires requiredAuthMethods.getToken'
214
- );
215
- }
216
- if (!definition.requiredAuthMethods.getEntityDetails) {
217
- throw new Error(
218
- 'Module definition requires requiredAuthMethods.getEntityDetails'
219
- );
220
- }
221
- if (!definition.requiredAuthMethods.getCredentialDetails) {
222
- throw new Error(
223
- 'Module definition requires requiredAuthMethods.getCredentialDetails'
224
- );
225
- }
226
- if (!definition.requiredAuthMethods.apiPropertiesToPersist) {
227
- throw new Error(
228
- 'Module definition requires requiredAuthMethods.apiPropertiesToPersist'
229
- );
230
- } else if (definition.Credential) {
231
- for (const prop of definition.requiredAuthMethods
232
- .apiPropertiesToPersist?.credential) {
233
- if (
234
- !definition.Credential.schema.paths.hasOwnProperty(prop)
235
- ) {
236
- throw new Error(
237
- `Module definition requires Credential schema to have property ${prop}`
238
- );
239
- }
240
- }
241
- }
242
- if (!definition.requiredAuthMethods.testAuthRequest) {
243
- throw new Error(
244
- 'Module definition requires requiredAuthMethods.testAuth'
245
- );
246
- }
247
- }
248
- }
249
- }
250
-
251
- module.exports = { Module };
@@ -1,16 +0,0 @@
1
- class TestModuleFactory {
2
- constructor() { }
3
-
4
- async getModuleInstance(entityId, userId) {
5
- // return minimal stub module with getName and api property
6
- return {
7
- getName() { return 'stubModule'; },
8
- api: {},
9
- entityId,
10
- userId,
11
- testAuth: async () => true,
12
- };
13
- }
14
- }
15
-
16
- module.exports = { TestModuleFactory };
@@ -1,19 +0,0 @@
1
- class TestModuleRepository {
2
- constructor() {
3
- this.entities = new Map();
4
- }
5
-
6
- addEntity(entity) {
7
- this.entities.set(entity.id, entity);
8
- }
9
-
10
- async findEntityById(id) {
11
- return this.entities.get(id);
12
- }
13
-
14
- async findEntitiesByIds(ids) {
15
- return ids.map((id) => this.entities.get(id));
16
- }
17
- }
18
-
19
- module.exports = { TestModuleRepository };
@@ -1,32 +0,0 @@
1
- const { Module } = require('../module');
2
- const { mapModuleClassToModuleDTO } = require('../utils/map-module-dto');
3
-
4
- class GetEntitiesForUser {
5
- constructor({ moduleRepository, moduleDefinitions }) {
6
- this.moduleRepository = moduleRepository;
7
-
8
- this.definitionMap = new Map();
9
- for (const definition of moduleDefinitions) {
10
- this.definitionMap.set(definition.modelName, definition);
11
- }
12
- }
13
-
14
- async execute(userId) {
15
- const entities = await this.moduleRepository.findEntitiesByUserId(
16
- userId
17
- );
18
-
19
- return entities.map((entity) => {
20
- const definition = this.definitionMap.get(entity.moduleName);
21
-
22
- const moduleInstance = new Module({
23
- userId,
24
- definition: definition,
25
- entity: entity,
26
- });
27
- return mapModuleClassToModuleDTO(moduleInstance);
28
- });
29
- }
30
- }
31
-
32
- module.exports = { GetEntitiesForUser };
@@ -1,58 +0,0 @@
1
- const { Module } = require('../module');
2
-
3
- class GetEntityOptionsById {
4
- /**
5
- * @param {Object} params
6
- * @param {import('../module-repository').ModuleRepository} params.moduleRepository
7
- * @param {} params.moduleDefinitions
8
- */
9
- constructor({ moduleRepository, moduleDefinitions }) {
10
- this.moduleRepository = moduleRepository;
11
- this.moduleDefinitions = moduleDefinitions;
12
- }
13
-
14
- /**
15
- * Retrieve a Module instance for a given user and entity/module type.
16
- * @param {string} userId
17
- * @param {string} entityId
18
- */
19
- async execute(entityId, userId) {
20
- const entity = await this.moduleRepository.findEntityById(
21
- entityId,
22
- userId
23
- );
24
-
25
- if (!entity) {
26
- throw new Error(`Entity ${entityId} not found`);
27
- }
28
-
29
- if (entity.userId !== userId) {
30
- throw new Error(
31
- `Entity ${entityId} does not belong to user ${userId}`
32
- );
33
- }
34
-
35
- const entityType = entity.type;
36
- const moduleDefinition = this.moduleDefinitions.find((def) => {
37
- const modelName = Module.getEntityModelFromDefinition(def).modelName;
38
- return entityType === modelName;
39
- });
40
-
41
- if (!moduleDefinition) {
42
- throw new Error(
43
- `Module definition not found for entity type: ${entityType}`
44
- );
45
- }
46
-
47
- const module = new Module({
48
- userId,
49
- entity,
50
- definition: moduleDefinition,
51
- });
52
-
53
- const entityOptions = await module.getEntityOptions();
54
- return entityOptions;
55
- }
56
- }
57
-
58
- module.exports = { GetEntityOptionsById };
@@ -1,34 +0,0 @@
1
- const { Module } = require('../module');
2
-
3
- class GetEntityOptionsByType {
4
- /**
5
- * @param {Object} params
6
- * @param {} params.moduleDefinitions
7
- */
8
- constructor({ moduleDefinitions }) {
9
- this.moduleDefinitions = moduleDefinitions;
10
- }
11
-
12
- /**
13
- * Retrieve a Module instance for a given user and entity/module type.
14
- * @param {string} userId
15
- * @param {string} type – human-readable module/entity type (e.g. "Hubspot")
16
- */
17
- async execute(userId, type) {
18
- const moduleDefinition = this.moduleDefinitions.find(
19
- (def) => def.getName() === type
20
- );
21
- if (!moduleDefinition) {
22
- throw new Error(`Module definition not found for type: ${type}`);
23
- }
24
- const moduleInstance = new Module({
25
- userId,
26
- definition: moduleDefinition,
27
- });
28
-
29
- const entityOptions = await moduleInstance.getEntityOptions();
30
- return entityOptions;
31
- }
32
- }
33
-
34
- module.exports = { GetEntityOptionsByType };
@@ -1,31 +0,0 @@
1
- const { Module } = require('../module');
2
-
3
- class GetModuleInstanceFromType {
4
- /**
5
- * @param {Object} params
6
- * @param {} params.moduleDefinitions
7
- */
8
- constructor({ moduleDefinitions }) {
9
- this.moduleDefinitions = moduleDefinitions;
10
- }
11
-
12
- /**
13
- * Retrieve a Module instance for a given user and entity/module type.
14
- * @param {string} userId
15
- * @param {string} type – human-readable module/entity type (e.g. "Hubspot")
16
- */
17
- async execute(userId, type) {
18
- const moduleDefinition = this.moduleDefinitions.find(
19
- (def) => def.getName() === type
20
- );
21
- if (!moduleDefinition) {
22
- throw new Error(`Module definition not found for type: ${type}`);
23
- }
24
- return new Module({
25
- userId,
26
- definition: moduleDefinition,
27
- });
28
- }
29
- }
30
-
31
- module.exports = { GetModuleInstanceFromType };
@@ -1,56 +0,0 @@
1
- const { Module } = require('../module');
2
-
3
- class GetModule {
4
- constructor({ moduleRepository, moduleDefinitions }) {
5
- this.moduleRepository = moduleRepository;
6
- this.moduleDefinitions = moduleDefinitions;
7
- }
8
-
9
- async execute(entityId, userId) {
10
- const entity = await this.moduleRepository.findEntityById(
11
- entityId,
12
- userId
13
- );
14
-
15
- if (!entity) {
16
- throw new Error(`Entity ${entityId} not found`);
17
- }
18
-
19
- if (entity.userId !== userId) {
20
- throw new Error(
21
- `Entity ${entityId} does not belong to user ${userId}`
22
- );
23
- }
24
-
25
- const entityType = entity.type;
26
- const moduleDefinition = this.moduleDefinitions.find((def) => {
27
- const modelName = Module.getEntityModelFromDefinition(def).modelName;
28
- return entityType === modelName;
29
- });
30
-
31
- if (!moduleDefinition) {
32
- throw new Error(
33
- `Module definition not found for entity type: ${entityType}`
34
- );
35
- }
36
-
37
- const module = new Module({
38
- userId,
39
- entity,
40
- definition: moduleDefinition,
41
- });
42
-
43
- // todo: this properties should be methods in the Module class
44
- return {
45
- id: module.entity.id,
46
- name: module.entity.name,
47
- type: module.entity.moduleName,
48
- moduleName: module.entity.moduleName,
49
- credential: module.credential,
50
- externalId: module.entity.externalId,
51
- userId: module.entity.user.toString(),
52
- }
53
- }
54
- }
55
-
56
- module.exports = { GetModule };
@@ -1,58 +0,0 @@
1
- const { Module } = require('../module');
2
-
3
- class RefreshEntityOptions {
4
- /**
5
- * @param {Object} params
6
- * @param {import('../module-repository').ModuleRepository} params.moduleRepository
7
- * @param {} params.moduleDefinitions
8
- */
9
- constructor({ moduleRepository, moduleDefinitions }) {
10
- this.moduleRepository = moduleRepository;
11
- this.moduleDefinitions = moduleDefinitions;
12
- }
13
-
14
- /**
15
- * Retrieve a Module instance for a given user and entity/module type.
16
- * @param {string} userId
17
- * @param {string} entityId
18
- */
19
- async execute(entityId, userId, options) {
20
- const entity = await this.moduleRepository.findEntityById(
21
- entityId,
22
- userId
23
- );
24
-
25
- if (!entity) {
26
- throw new Error(`Entity ${entityId} not found`);
27
- }
28
-
29
- if (entity.userId !== userId) {
30
- throw new Error(
31
- `Entity ${entityId} does not belong to user ${userId}`
32
- );
33
- }
34
-
35
- const entityType = entity.type;
36
- const moduleDefinition = this.moduleDefinitions.find((def) => {
37
- const modelName = Module.getEntityModelFromDefinition(def).modelName;
38
- return entityType === modelName;
39
- });
40
-
41
- if (!moduleDefinition) {
42
- throw new Error(
43
- `Module definition not found for entity type: ${entityType}`
44
- );
45
- }
46
-
47
- const module = new Module({
48
- userId,
49
- entity,
50
- definition: moduleDefinition,
51
- });
52
-
53
- await module.refreshEntityOptions(options);
54
- return module.getEntityOptions();
55
- }
56
- }
57
-
58
- module.exports = { RefreshEntityOptions };
@@ -1,54 +0,0 @@
1
- const { Module } = require('../module');
2
-
3
- class TestModuleAuth {
4
- /**
5
- * @param {Object} params - Configuration parameters.
6
- * @param {import('./module-repository').ModuleRepository} params.moduleRepository - Repository for module data operations.
7
- * @param {Array<Object>} params.moduleDefinitions - Array of module definitions.
8
- */
9
- constructor({ moduleRepository, moduleDefinitions }) {
10
- this.moduleRepository = moduleRepository;
11
- this.moduleDefinitions = moduleDefinitions;
12
- }
13
-
14
- async execute(entityId, userId) {
15
- const entity = await this.moduleRepository.findEntityById(
16
- entityId,
17
- userId
18
- );
19
-
20
- if (!entity) {
21
- throw new Error(`Entity ${entityId} not found`);
22
- }
23
-
24
- if (entity.userId !== userId) {
25
- throw new Error(
26
- `Entity ${entityId} does not belong to user ${userId}`
27
- );
28
- }
29
-
30
- const entityType = entity.type;
31
- const moduleDefinition = this.moduleDefinitions.find((def) => {
32
- const modelName = Module.getEntityModelFromDefinition(def).modelName;
33
- return entityType === modelName;
34
- });
35
-
36
- if (!moduleDefinition) {
37
- throw new Error(
38
- `Module definition not found for entity type: ${entityType}`
39
- );
40
- }
41
-
42
- const module = new Module({
43
- userId,
44
- entity,
45
- definition: moduleDefinition,
46
- });
47
-
48
- const testAuthResponse = await module.testAuth();
49
-
50
- return testAuthResponse;
51
- }
52
- }
53
-
54
- module.exports = { TestModuleAuth };
@@ -1,18 +0,0 @@
1
- /**
2
- * @param {import('../module').Module} moduleInstance
3
- * Convert a Module domain instance to a plain DTO suitable for JSON responses.
4
- */
5
- function mapModuleClassToModuleDTO(moduleInstance) {
6
- if (!moduleInstance) return null;
7
-
8
- return {
9
- id: moduleInstance.entity.id,
10
- name: moduleInstance.name,
11
- userId: moduleInstance.userId,
12
- entity: moduleInstance.entity,
13
- credentialId: moduleInstance.credential?._id?.toString(),
14
- type: moduleInstance.getName()
15
- };
16
- }
17
-
18
- module.exports = { mapModuleClassToModuleDTO };