@friggframework/core 2.0.0--canary.396.469364a.0 → 2.0.0--canary.397.3d4865d.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.
- package/index.js +0 -2
- package/integrations/integration-router.js +65 -52
- package/integrations/integration.js +8 -6
- package/module-plugin/index.js +0 -2
- package/module-plugin/module.js +10 -18
- package/module-plugin/use-cases/get-entity-options-by-id.js +58 -0
- package/module-plugin/use-cases/get-entity-options-by-type.js +34 -0
- package/module-plugin/use-cases/get-module-instance-from-type.js +31 -0
- package/module-plugin/use-cases/get-module.js +56 -0
- package/module-plugin/use-cases/refresh-entity-options.js +58 -0
- package/module-plugin/use-cases/test-module-auth.js +54 -0
- package/package.json +5 -5
- package/module-plugin/module-factory.js +0 -42
package/index.js
CHANGED
|
@@ -52,7 +52,6 @@ const {
|
|
|
52
52
|
OAuth2Requester,
|
|
53
53
|
Requester,
|
|
54
54
|
ModuleConstants,
|
|
55
|
-
ModuleFactory,
|
|
56
55
|
} = require('./module-plugin/index');
|
|
57
56
|
const utils = require('./utils');
|
|
58
57
|
|
|
@@ -124,7 +123,6 @@ module.exports = {
|
|
|
124
123
|
OAuth2Requester,
|
|
125
124
|
Requester,
|
|
126
125
|
ModuleConstants,
|
|
127
|
-
ModuleFactory,
|
|
128
126
|
// queues
|
|
129
127
|
QueuerUtil,
|
|
130
128
|
|
|
@@ -15,6 +15,12 @@ const { loadAppDefinition } = require('../handlers/app-definition-loader');
|
|
|
15
15
|
const { GetIntegrationInstance } = require('./use-cases/get-integration-instance');
|
|
16
16
|
const { UpdateIntegration } = require('./use-cases/update-integration');
|
|
17
17
|
const { getModulesDefinitionFromIntegrationClasses } = require('./utils/map-integration-dto');
|
|
18
|
+
const { GetModuleInstanceFromType } = require('../module-plugin/use-cases/get-module-instance-from-type');
|
|
19
|
+
const { GetEntityOptionsByType } = require('../module-plugin/use-cases/get-entity-options-by-type');
|
|
20
|
+
const { TestModuleAuth } = require('../module-plugin/use-cases/test-module-auth');
|
|
21
|
+
const { GetModule } = require('../module-plugin/use-cases/get-module');
|
|
22
|
+
const { GetEntityOptionsById } = require('../module-plugin/use-cases/get-entity-options-by-id');
|
|
23
|
+
const { RefreshEntityOptions } = require('../module-plugin/use-cases/refresh-entity-options');
|
|
18
24
|
|
|
19
25
|
/**
|
|
20
26
|
* Creates an Express router with integration and entity routes configured
|
|
@@ -70,8 +76,35 @@ function createIntegrationRouter(params) {
|
|
|
70
76
|
moduleService,
|
|
71
77
|
});
|
|
72
78
|
|
|
79
|
+
const getModuleInstanceFromType = new GetModuleInstanceFromType({
|
|
80
|
+
moduleDefinitions: getModulesDefinitionFromIntegrationClasses(integrationClasses),
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const getEntityOptionsByType = new GetEntityOptionsByType({
|
|
84
|
+
moduleDefinitions: getModulesDefinitionFromIntegrationClasses(integrationClasses),
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const testModuleAuth = new TestModuleAuth({
|
|
88
|
+
moduleRepository,
|
|
89
|
+
moduleDefinitions: getModulesDefinitionFromIntegrationClasses(integrationClasses),
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const getModule = new GetModule({
|
|
93
|
+
moduleRepository,
|
|
94
|
+
moduleDefinitions: getModulesDefinitionFromIntegrationClasses(integrationClasses),
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const getEntityOptionsById = new GetEntityOptionsById({
|
|
98
|
+
moduleRepository,
|
|
99
|
+
moduleDefinitions: getModulesDefinitionFromIntegrationClasses(integrationClasses),
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const refreshEntityOptions = new RefreshEntityOptions({
|
|
103
|
+
moduleRepository,
|
|
104
|
+
moduleDefinitions: getModulesDefinitionFromIntegrationClasses(integrationClasses),
|
|
105
|
+
});
|
|
106
|
+
|
|
73
107
|
const router = get(params, 'router', express());
|
|
74
|
-
const factory = get(params, 'factory');
|
|
75
108
|
const getUserFromBearerToken = get(params, 'getUserFromBearerToken');
|
|
76
109
|
|
|
77
110
|
setIntegrationRoutes(router, getUserFromBearerToken, {
|
|
@@ -82,8 +115,14 @@ function createIntegrationRouter(params) {
|
|
|
82
115
|
getIntegrationInstance,
|
|
83
116
|
updateIntegration,
|
|
84
117
|
});
|
|
85
|
-
setEntityRoutes(router,
|
|
118
|
+
setEntityRoutes(router, getUserFromBearerToken, {
|
|
86
119
|
getCredentialForUser,
|
|
120
|
+
getModuleInstanceFromType,
|
|
121
|
+
getEntityOptionsByType,
|
|
122
|
+
testModuleAuth,
|
|
123
|
+
getModule,
|
|
124
|
+
getEntityOptionsById,
|
|
125
|
+
refreshEntityOptions,
|
|
87
126
|
});
|
|
88
127
|
return router;
|
|
89
128
|
}
|
|
@@ -348,25 +387,18 @@ function setIntegrationRoutes(router, getUserFromBearerToken, useCases) {
|
|
|
348
387
|
/**
|
|
349
388
|
* Sets up entity-related routes for the integration router
|
|
350
389
|
* @param {Object} router - Express router instance
|
|
351
|
-
* @param {Object} factory - Factory object containing moduleFactory
|
|
352
390
|
* @param {import('../user/use-cases/get-user-from-bearer-token').GetUserFromBearerToken} getUserFromBearerToken - Use case for retrieving a user from a bearer token
|
|
353
391
|
*/
|
|
354
|
-
function setEntityRoutes(router,
|
|
355
|
-
const {
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
}
|
|
365
|
-
return await moduleFactory.getInstanceFromTypeName(
|
|
366
|
-
entityType,
|
|
367
|
-
userId
|
|
368
|
-
);
|
|
369
|
-
};
|
|
392
|
+
function setEntityRoutes(router, getUserFromBearerToken, useCases) {
|
|
393
|
+
const {
|
|
394
|
+
getCredentialForUser,
|
|
395
|
+
getModuleInstanceFromType,
|
|
396
|
+
getEntityOptionsByType,
|
|
397
|
+
testModuleAuth,
|
|
398
|
+
getModule,
|
|
399
|
+
getEntityOptionsById,
|
|
400
|
+
refreshEntityOptions,
|
|
401
|
+
} = useCases;
|
|
370
402
|
|
|
371
403
|
router.route('/api/authorize').get(
|
|
372
404
|
catchAsyncError(async (req, res) => {
|
|
@@ -375,7 +407,7 @@ function setEntityRoutes(router, factory, getUserFromBearerToken, useCases) {
|
|
|
375
407
|
);
|
|
376
408
|
const userId = user.getId();
|
|
377
409
|
const params = checkRequiredParams(req.query, ['entityType']);
|
|
378
|
-
const module = await
|
|
410
|
+
const module = await getModuleInstanceFromType.execute(userId, params.entityType);
|
|
379
411
|
const areRequirementsValid =
|
|
380
412
|
module.validateAuthorizationRequirements();
|
|
381
413
|
if (!areRequirementsValid) {
|
|
@@ -398,7 +430,7 @@ function setEntityRoutes(router, factory, getUserFromBearerToken, useCases) {
|
|
|
398
430
|
'entityType',
|
|
399
431
|
'data',
|
|
400
432
|
]);
|
|
401
|
-
const module = await
|
|
433
|
+
const module = await getModuleInstanceFromType.execute(userId, params.entityType);
|
|
402
434
|
|
|
403
435
|
res.json(
|
|
404
436
|
await module.processAuthorizationCallback({
|
|
@@ -431,7 +463,7 @@ function setEntityRoutes(router, factory, getUserFromBearerToken, useCases) {
|
|
|
431
463
|
throw Boom.badRequest('Invalid credential ID');
|
|
432
464
|
}
|
|
433
465
|
|
|
434
|
-
const module = await
|
|
466
|
+
const module = await getModuleInstanceFromType.execute(userId, params.entityType);
|
|
435
467
|
const entityDetails = await module.getEntityDetails(
|
|
436
468
|
module.api,
|
|
437
469
|
null,
|
|
@@ -460,9 +492,9 @@ function setEntityRoutes(router, factory, getUserFromBearerToken, useCases) {
|
|
|
460
492
|
}
|
|
461
493
|
|
|
462
494
|
const params = checkRequiredParams(req.query, ['entityType']);
|
|
463
|
-
const
|
|
495
|
+
const entityOptions = await getEntityOptionsByType.execute(userId, params.entityType);
|
|
464
496
|
|
|
465
|
-
res.json(
|
|
497
|
+
res.json(entityOptions);
|
|
466
498
|
})
|
|
467
499
|
);
|
|
468
500
|
|
|
@@ -473,17 +505,11 @@ function setEntityRoutes(router, factory, getUserFromBearerToken, useCases) {
|
|
|
473
505
|
);
|
|
474
506
|
const userId = user.getId();
|
|
475
507
|
const params = checkRequiredParams(req.params, ['entityId']);
|
|
476
|
-
const
|
|
508
|
+
const testAuthResponse = await testModuleAuth.execute(
|
|
477
509
|
params.entityId,
|
|
478
510
|
userId
|
|
479
511
|
);
|
|
480
512
|
|
|
481
|
-
if (!module) {
|
|
482
|
-
throw Boom.notFound();
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
const testAuthResponse = await module.testAuth();
|
|
486
|
-
|
|
487
513
|
if (!testAuthResponse) {
|
|
488
514
|
res.status(400);
|
|
489
515
|
res.json({
|
|
@@ -508,16 +534,12 @@ function setEntityRoutes(router, factory, getUserFromBearerToken, useCases) {
|
|
|
508
534
|
);
|
|
509
535
|
const userId = user.getId();
|
|
510
536
|
const params = checkRequiredParams(req.params, ['entityId']);
|
|
511
|
-
const module = await
|
|
537
|
+
const module = await getModule.execute(
|
|
512
538
|
params.entityId,
|
|
513
539
|
userId
|
|
514
540
|
);
|
|
515
541
|
|
|
516
|
-
|
|
517
|
-
throw Boom.notFound();
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
res.json(module.entity);
|
|
542
|
+
res.json(module);
|
|
521
543
|
})
|
|
522
544
|
);
|
|
523
545
|
|
|
@@ -530,16 +552,10 @@ function setEntityRoutes(router, factory, getUserFromBearerToken, useCases) {
|
|
|
530
552
|
const params = checkRequiredParams(req.params, [
|
|
531
553
|
'entityId',
|
|
532
554
|
]);
|
|
533
|
-
const module = await moduleFactory.getModuleInstanceFromEntityId(
|
|
534
|
-
params.entityId,
|
|
535
|
-
userId
|
|
536
|
-
);
|
|
537
555
|
|
|
538
|
-
|
|
539
|
-
throw Boom.notFound();
|
|
540
|
-
}
|
|
556
|
+
const entityOptions = await getEntityOptionsById.execute(params.entityId, userId);
|
|
541
557
|
|
|
542
|
-
res.json(
|
|
558
|
+
res.json(entityOptions);
|
|
543
559
|
})
|
|
544
560
|
);
|
|
545
561
|
|
|
@@ -552,16 +568,13 @@ function setEntityRoutes(router, factory, getUserFromBearerToken, useCases) {
|
|
|
552
568
|
const params = checkRequiredParams(req.params, [
|
|
553
569
|
'entityId',
|
|
554
570
|
]);
|
|
555
|
-
const
|
|
571
|
+
const updatedOptions = await refreshEntityOptions.execute(
|
|
556
572
|
params.entityId,
|
|
557
|
-
userId
|
|
573
|
+
userId,
|
|
574
|
+
req.body
|
|
558
575
|
);
|
|
559
576
|
|
|
560
|
-
|
|
561
|
-
throw Boom.notFound();
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
res.json(await module.refreshEntityOptions(req.body));
|
|
577
|
+
res.json(updatedOptions);
|
|
565
578
|
})
|
|
566
579
|
);
|
|
567
580
|
}
|
|
@@ -116,13 +116,15 @@ class Integration {
|
|
|
116
116
|
// access them without falling back through the Proxy. This prevents
|
|
117
117
|
// `undefined` errors for methods like `loadDynamicUserActions` that
|
|
118
118
|
// may be invoked inside default event-handlers.
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
119
|
+
let proto = Object.getPrototypeOf(this.behavior);
|
|
120
|
+
while (proto && proto !== Object.prototype) {
|
|
121
|
+
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
122
|
+
if (key === 'constructor') continue;
|
|
123
|
+
if (typeof proto[key] === 'function' && this[key] === undefined) {
|
|
124
|
+
this[key] = proto[key].bind(this.behavior);
|
|
125
|
+
}
|
|
125
126
|
}
|
|
127
|
+
proto = Object.getPrototypeOf(proto);
|
|
126
128
|
}
|
|
127
129
|
}
|
|
128
130
|
}
|
package/module-plugin/index.js
CHANGED
|
@@ -6,7 +6,6 @@ const { BasicAuthRequester } = require('./requester/basic');
|
|
|
6
6
|
const { OAuth2Requester } = require('./requester/oauth-2');
|
|
7
7
|
const { Requester } = require('./requester/requester');
|
|
8
8
|
const { ModuleConstants } = require('./ModuleConstants');
|
|
9
|
-
const { ModuleFactory } = require('./module-factory');
|
|
10
9
|
|
|
11
10
|
module.exports = {
|
|
12
11
|
Credential,
|
|
@@ -17,5 +16,4 @@ module.exports = {
|
|
|
17
16
|
OAuth2Requester,
|
|
18
17
|
Requester,
|
|
19
18
|
ModuleConstants,
|
|
20
|
-
ModuleFactory,
|
|
21
19
|
};
|
package/module-plugin/module.js
CHANGED
|
@@ -9,6 +9,7 @@ const { ModuleConstants } = require('./ModuleConstants');
|
|
|
9
9
|
|
|
10
10
|
class Module extends Delegate {
|
|
11
11
|
|
|
12
|
+
//todo: entity should be replaced with actual entity properties
|
|
12
13
|
/**
|
|
13
14
|
*
|
|
14
15
|
* @param {Object} params
|
|
@@ -25,8 +26,6 @@ class Module extends Delegate {
|
|
|
25
26
|
this.entity = entityObj;
|
|
26
27
|
this.credential = entityObj?.credential;
|
|
27
28
|
this.definition = definition;
|
|
28
|
-
this.getEntityOptions = this.definition.getEntityOptions;
|
|
29
|
-
this.refreshEntityOptions = this.definition.refreshEntityOptions;
|
|
30
29
|
this.name = this.definition.moduleName;
|
|
31
30
|
this.modelName = this.definition.modelName;
|
|
32
31
|
this.apiClass = this.definition.API;
|
|
@@ -56,6 +55,15 @@ class Module extends Delegate {
|
|
|
56
55
|
return this.name;
|
|
57
56
|
}
|
|
58
57
|
|
|
58
|
+
getEntityOptions() {
|
|
59
|
+
return this.definition.getEntityOptions()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async refreshEntityOptions(options) {
|
|
63
|
+
await this.definition.refreshEntityOptions(options);
|
|
64
|
+
return this.getEntityOptions();
|
|
65
|
+
}
|
|
66
|
+
|
|
59
67
|
apiParamsFromCredential(credential) {
|
|
60
68
|
return _.pick(credential, ...this.apiPropertiesToPersist?.credential);
|
|
61
69
|
}
|
|
@@ -103,22 +111,6 @@ class Module extends Delegate {
|
|
|
103
111
|
return this.CredentialModel;
|
|
104
112
|
}
|
|
105
113
|
|
|
106
|
-
// todo: remove this method from all places
|
|
107
|
-
// async getEntitiesForUserId(userId) {
|
|
108
|
-
// // Only return non-internal fields. Leverages "select" and "options" to non-excepted fields and a pure object.
|
|
109
|
-
// const list = await this.EntityModel.find(
|
|
110
|
-
// { user: userId },
|
|
111
|
-
// '-dateCreated -dateUpdated -user -credentials -credential -__t -__v',
|
|
112
|
-
// { lean: true }
|
|
113
|
-
// );
|
|
114
|
-
// console.log('getEntitiesForUserId list', list, userId);
|
|
115
|
-
// return list.map((entity) => ({
|
|
116
|
-
// id: entity._id,
|
|
117
|
-
// type: this.getName(),
|
|
118
|
-
// ...entity,
|
|
119
|
-
// }));
|
|
120
|
-
// }
|
|
121
|
-
|
|
122
114
|
async validateAuthorizationRequirements() {
|
|
123
115
|
const requirements = await this.getAuthorizationRequirements();
|
|
124
116
|
let valid = true;
|
|
@@ -0,0 +1,58 @@
|
|
|
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 };
|
|
@@ -0,0 +1,34 @@
|
|
|
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 };
|
|
@@ -0,0 +1,31 @@
|
|
|
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 };
|
|
@@ -0,0 +1,56 @@
|
|
|
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 };
|
|
@@ -0,0 +1,58 @@
|
|
|
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 };
|
|
@@ -0,0 +1,54 @@
|
|
|
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 };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@friggframework/core",
|
|
3
3
|
"prettier": "@friggframework/prettier-config",
|
|
4
|
-
"version": "2.0.0--canary.
|
|
4
|
+
"version": "2.0.0--canary.397.3d4865d.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@hapi/boom": "^10.0.1",
|
|
7
7
|
"aws-sdk": "^2.1200.0",
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"uuid": "^9.0.1"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@friggframework/eslint-config": "2.0.0--canary.
|
|
26
|
-
"@friggframework/prettier-config": "2.0.0--canary.
|
|
27
|
-
"@friggframework/test": "2.0.0--canary.
|
|
25
|
+
"@friggframework/eslint-config": "2.0.0--canary.397.3d4865d.0",
|
|
26
|
+
"@friggframework/prettier-config": "2.0.0--canary.397.3d4865d.0",
|
|
27
|
+
"@friggframework/test": "2.0.0--canary.397.3d4865d.0",
|
|
28
28
|
"@types/lodash": "4.17.15",
|
|
29
29
|
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
30
30
|
"chai": "^4.3.6",
|
|
@@ -53,5 +53,5 @@
|
|
|
53
53
|
},
|
|
54
54
|
"homepage": "https://github.com/friggframework/frigg#readme",
|
|
55
55
|
"description": "",
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "3d4865d4a752858baaa1f723ca9d07b38bfd7e5e"
|
|
57
57
|
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
const { ModuleRepository } = require('./module-repository');
|
|
2
|
-
const { ModuleService } = require('./module-service');
|
|
3
|
-
const { Module } = require('./module');
|
|
4
|
-
|
|
5
|
-
class ModuleFactory {
|
|
6
|
-
constructor(...params) {
|
|
7
|
-
this.moduleDefinitions = params;
|
|
8
|
-
this.moduleTypes = this.moduleDefinitions.map((def) => def.moduleName);
|
|
9
|
-
this.moduleRepository = new ModuleRepository();
|
|
10
|
-
this.moduleService = new ModuleService({
|
|
11
|
-
moduleRepository: this.moduleRepository,
|
|
12
|
-
moduleDefinitions: this.moduleDefinitions,
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
checkIsValidType(entityType) {
|
|
17
|
-
return this.moduleTypes.includes(entityType);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
getModuleDefinitionFromTypeName(typeName) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async getModuleInstanceFromEntityId(entityId, userId) {
|
|
25
|
-
return this.moduleService.getModuleInstance(entityId, userId);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async getInstanceFromTypeName(typeName, userId) {
|
|
29
|
-
const moduleDefinition = this.moduleDefinitions.find(
|
|
30
|
-
(def) => def.getName() === typeName
|
|
31
|
-
);
|
|
32
|
-
if (!moduleDefinition) {
|
|
33
|
-
throw new Error(`Module definition not found for type: ${typeName}`);
|
|
34
|
-
}
|
|
35
|
-
return new Module({
|
|
36
|
-
userId,
|
|
37
|
-
definition: moduleDefinition,
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
module.exports = { ModuleFactory };
|