@friggframework/core 2.0.0--canary.396.607ab7a.0 → 2.0.0--canary.396.cb56ae3.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 CHANGED
@@ -46,7 +46,6 @@ const { TimeoutCatcher } = require('./lambda/index');
46
46
  const { debug, initDebugLog, flushDebugLog } = require('./logs/index');
47
47
  const {
48
48
  Credential,
49
- EntityManager,
50
49
  Entity,
51
50
  ModuleManager,
52
51
  ApiKeyRequester,
@@ -120,7 +119,6 @@ module.exports = {
120
119
 
121
120
  // module plugin
122
121
  Credential,
123
- EntityManager,
124
122
  Entity,
125
123
  ModuleManager,
126
124
  ApiKeyRequester,
@@ -16,37 +16,6 @@ const {
16
16
  loadAppDefinition,
17
17
  } = require('../handlers/app-definition-loader');
18
18
 
19
-
20
- const { integrations } = loadAppDefinition();
21
- const moduleRepository = new ModuleRepository();
22
- const integrationRepository = new IntegrationRepository();
23
- const credentialRepository = new CredentialRepository();
24
- const moduleService = new ModuleService({ moduleRepository, moduleDefinitions: { ...getModules() } });
25
- const deleteIntegrationForUser = new DeleteIntegrationForUser({ integrationRepository });
26
- const getIntegrationForUser = new GetIntegrationForUser({ integrationRepository });
27
- const getIntegrationsForUser = new GetIntegrationsForUser({ integrationRepository });
28
- const getCredentialForUser = new GetCredentialForUser({ credentialRepository });
29
- const createIntegration = new CreateIntegration({
30
- integrationRepository,
31
- integrationClasses: integrations,
32
- moduleService,
33
- });
34
-
35
- // todo: move this into a utils file
36
- const getModules = () => {
37
- return [
38
- ...new Set(
39
- integrations
40
- .map((integration) =>
41
- Object.values(integration.Definition.modules).map(
42
- (module) => module.definition
43
- )
44
- )
45
- .flat()
46
- ),
47
- ];
48
- }
49
-
50
19
  // todo: dont send moduleFactory and integrationFactory as a factory object, instead send them as separate params.
51
20
  // todo: this could be a use case class
52
21
  /**
@@ -60,12 +29,66 @@ const getModules = () => {
60
29
  * @returns {express.Router} Configured Express router with integration and entity routes
61
30
  */
62
31
  function createIntegrationRouter(params) {
32
+ const { integrations } = loadAppDefinition();
33
+ const moduleRepository = new ModuleRepository();
34
+ const integrationRepository = new IntegrationRepository();
35
+ const credentialRepository = new CredentialRepository();
36
+
37
+ // todo: move this into a utils file
38
+ const getModules = () => {
39
+ return [
40
+ ...new Set(
41
+ integrations
42
+ .map((integration) =>
43
+ Object.values(integration.Definition.modules).map(
44
+ (module) => module.definition
45
+ )
46
+ )
47
+ .flat()
48
+ ),
49
+ ];
50
+ };
51
+ const moduleService = new ModuleService({
52
+ moduleRepository,
53
+ moduleDefinitions: getModules(),
54
+ });
55
+ const deleteIntegrationForUser = new DeleteIntegrationForUser({
56
+ integrationRepository,
57
+ });
58
+ const getIntegrationForUser = new GetIntegrationForUser({
59
+ integrationRepository,
60
+ });
61
+ const getIntegrationsForUser = new GetIntegrationsForUser({
62
+ integrationRepository,
63
+ });
64
+ const getCredentialForUser = new GetCredentialForUser({
65
+ credentialRepository,
66
+ });
67
+ const createIntegration = new CreateIntegration({
68
+ integrationRepository,
69
+ integrationClasses: integrations,
70
+ moduleService,
71
+ });
72
+
73
+ const getEntitiesForUserUseCase = new GetEntitiesForUser({
74
+ moduleRepository,
75
+ moduleDefinitions: getModules(),
76
+ });
77
+
63
78
  const router = get(params, 'router', express());
64
79
  const factory = get(params, 'factory');
65
80
  const getUserFromBearerToken = get(params, 'getUserFromBearerToken');
66
81
 
67
- setIntegrationRoutes(router, factory, getUserFromBearerToken);
68
- setEntityRoutes(router, factory, getUserFromBearerToken);
82
+ // todo: moduleFactory in factory is not used here anymore, remove it
83
+ setIntegrationRoutes(router, factory, getUserFromBearerToken, {
84
+ createIntegration,
85
+ deleteIntegrationForUser,
86
+ getIntegrationsForUser,
87
+ getEntitiesForUserUseCase,
88
+ });
89
+ setEntityRoutes(router, factory, getUserFromBearerToken, {
90
+ getCredentialForUser,
91
+ });
69
92
  return router;
70
93
  }
71
94
 
@@ -99,8 +122,14 @@ function checkRequiredParams(params, requiredKeys) {
99
122
  * @param {Object} factory.integrationFactory - Factory for creating and managing integrations
100
123
  * @param {import('../user/use-cases/get-user-from-bearer-token').GetUserFromBearerToken} getUserFromBearerToken - Use case for retrieving a user from a bearer token
101
124
  */
102
- function setIntegrationRoutes(router, factory, getUserFromBearerToken) {
103
- const { moduleFactory, integrationFactory } = factory;
125
+ function setIntegrationRoutes(router, factory, getUserFromBearerToken, useCases) {
126
+ const { integrationFactory } = factory;
127
+ const {
128
+ createIntegration,
129
+ deleteIntegrationForUser,
130
+ getIntegrationsForUser,
131
+ getEntitiesForUserUseCase,
132
+ } = useCases;
104
133
 
105
134
  router.route('/api/integrations').get(
106
135
  catchAsyncError(async (req, res) => {
@@ -109,8 +138,7 @@ function setIntegrationRoutes(router, factory, getUserFromBearerToken) {
109
138
  );
110
139
  const userId = user.getId();
111
140
  const results = await integrationFactory.getIntegrationOptions();
112
- results.entities.authorized =
113
- await moduleFactory.getEntitiesForUser(userId);
141
+ results.entities.authorized = await getEntitiesForUserUseCase.execute(userId);
114
142
  results.integrations =
115
143
  await getIntegrationsForUser.execute(userId);
116
144
 
@@ -404,8 +432,9 @@ function setIntegrationRoutes(router, factory, getUserFromBearerToken) {
404
432
  * @param {Object} factory - Factory object containing moduleFactory
405
433
  * @param {import('../user/use-cases/get-user-from-bearer-token').GetUserFromBearerToken} getUserFromBearerToken - Use case for retrieving a user from a bearer token
406
434
  */
407
- function setEntityRoutes(router, factory, getUserFromBearerToken) {
435
+ function setEntityRoutes(router, factory, getUserFromBearerToken, useCases) {
408
436
  const { moduleFactory } = factory;
437
+ const { getCredentialForUser } = useCases;
409
438
  const getModuleInstance = async (userId, entityType) => {
410
439
  if (!moduleFactory.checkIsValidType(entityType)) {
411
440
  throw Boom.badRequest(
@@ -432,7 +461,7 @@ function setEntityRoutes(router, factory, getUserFromBearerToken) {
432
461
  module.validateAuthorizationRequirements();
433
462
  if (!areRequirementsValid) {
434
463
  throw new Error(
435
- `Error: EntityManager of type ${params.entityType} requires a valid url`
464
+ `Error: Entity of type ${params.entityType} requires a valid url`
436
465
  );
437
466
  }
438
467
 
@@ -1,5 +1,4 @@
1
1
  const { Credential } = require('./credential');
2
- const { EntityManager } = require('./entity-manager');
3
2
  const { Entity } = require('./entity');
4
3
  const { ModuleManager } = require('./manager');
5
4
  const { ApiKeyRequester } = require('./requester/api-key');
@@ -11,7 +10,6 @@ const { ModuleFactory } = require('./module-factory');
11
10
 
12
11
  module.exports = {
13
12
  Credential,
14
- EntityManager,
15
13
  Entity,
16
14
  ModuleManager,
17
15
  ApiKeyRequester,
@@ -13,19 +13,6 @@ class ModuleFactory {
13
13
  });
14
14
  }
15
15
 
16
- async getEntitiesForUser(userId) {
17
- let results = [];
18
- for (const moduleDefinition of this.moduleDefinitions) {
19
- const moduleInstance = new Module({
20
- userId,
21
- definition: moduleDefinition,
22
- });
23
- const list = await moduleInstance.getEntitiesForUserId(userId);
24
- results.push(...list);
25
- }
26
- return results;
27
- }
28
-
29
16
  checkIsValidType(entityType) {
30
17
  return this.moduleTypes.includes(entityType);
31
18
  }
@@ -9,6 +9,14 @@ class ModuleRepository {
9
9
 
10
10
  return entity;
11
11
  }
12
+
13
+ async findEntitiesByUserId(userId) {
14
+ return Entity.find(
15
+ { user: userId },
16
+ '-dateCreated -dateUpdated -user -credentials -credential -__t -__v',
17
+ { lean: true }
18
+ );
19
+ }
12
20
  }
13
21
 
14
22
  module.exports = { ModuleRepository };
@@ -103,20 +103,21 @@ class Module extends Delegate {
103
103
  return this.CredentialModel;
104
104
  }
105
105
 
106
- async getEntitiesForUserId(userId) {
107
- // Only return non-internal fields. Leverages "select" and "options" to non-excepted fields and a pure object.
108
- const list = await this.EntityModel.find(
109
- { user: userId },
110
- '-dateCreated -dateUpdated -user -credentials -credential -__t -__v',
111
- { lean: true }
112
- );
113
- console.log('getEntitiesForUserId list', list, userId);
114
- return list.map((entity) => ({
115
- id: entity._id,
116
- type: this.getName(),
117
- ...entity,
118
- }));
119
- }
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
+ // }
120
121
 
121
122
  async validateAuthorizationRequirements() {
122
123
  const requirements = await this.getAuthorizationRequirements();
@@ -0,0 +1,35 @@
1
+ const { Module } = require('../module');
2
+
3
+ class GetEntitiesForUser {
4
+ constructor({ moduleRepository, moduleDefinitions }) {
5
+ this.moduleRepository = moduleRepository;
6
+
7
+ this.definitionMap = new Map();
8
+ for (const definition of moduleDefinitions) {
9
+ const modelName =
10
+ Module.getEntityModelFromDefinition(definition).modelName;
11
+ this.definitionMap.set(modelName, definition);
12
+ }
13
+ }
14
+
15
+ async execute(userId) {
16
+ const entities = await this.moduleRepository.findEntitiesByUserId(
17
+ userId
18
+ );
19
+
20
+ return entities.map((entity) => {
21
+ const definition = this.definitionMap.get(entity.__t);
22
+ // todo: check if dont need to use this.
23
+ const type = definition ? definition.getName() : 'unknown';
24
+
25
+ const moduleInstance = new Module({
26
+ userId,
27
+ definition: definition,
28
+ entity: entity,
29
+ });
30
+ return moduleInstance;
31
+ });
32
+ }
33
+ }
34
+
35
+ module.exports = { GetEntitiesForUser };
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.396.607ab7a.0",
4
+ "version": "2.0.0--canary.396.cb56ae3.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.396.607ab7a.0",
26
- "@friggframework/prettier-config": "2.0.0--canary.396.607ab7a.0",
27
- "@friggframework/test": "2.0.0--canary.396.607ab7a.0",
25
+ "@friggframework/eslint-config": "2.0.0--canary.396.cb56ae3.0",
26
+ "@friggframework/prettier-config": "2.0.0--canary.396.cb56ae3.0",
27
+ "@friggframework/test": "2.0.0--canary.396.cb56ae3.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": "607ab7a7440562858c6d7440b8983a00931be283"
56
+ "gitHead": "cb56ae344c7fed00baa31a00970d27ea59d3dce5"
57
57
  }
@@ -1,7 +1,6 @@
1
1
  declare module "@friggframework/integrations" {
2
2
  import { Delegate, IFriggDelegate } from "@friggframework/core";
3
3
  import { Model } from "mongoose";
4
- import { EntityManager } from "@friggframework/module-plugin";
5
4
 
6
5
  export class Integration extends Model {
7
6
  entities: any[];
@@ -19,8 +18,7 @@ declare module "@friggframework/integrations" {
19
18
 
20
19
  export class IntegrationManager
21
20
  extends Delegate
22
- implements IFriggIntegrationManager
23
- {
21
+ implements IFriggIntegrationManager {
24
22
  integration: Integration;
25
23
  primaryInstance: any;
26
24
  targetInstance: any;
@@ -56,7 +54,6 @@ declare module "@friggframework/integrations" {
56
54
  entities: { id: string; user: any },
57
55
  userId: string,
58
56
  config: any,
59
- EntityManager: EntityManager
60
57
  ): Promise<any>;
61
58
 
62
59
  static getFormattedIntegration(
@@ -116,8 +113,7 @@ declare module "@friggframework/integrations" {
116
113
  }
117
114
 
118
115
  export class IntegrationConfigManager
119
- implements IFriggIntegrationConfigManager
120
- {
116
+ implements IFriggIntegrationConfigManager {
121
117
  options: IntegrationOptions[];
122
118
  primary: any;
123
119
 
@@ -9,21 +9,7 @@ declare module "@friggframework/module-plugin" {
9
9
  externalId: string;
10
10
  }
11
11
 
12
- export class EntityManager implements IFriggEntityManager {
13
- static primaryEntityClass: any;
14
- static entityManagerClasses: any[];
15
- static entityTypes: string[];
16
- static getEntitiesForUser(userId: string): Promise<any[]>;
17
- static checkIsValidType(entityType: string): boolean;
18
- static getEntityManagerClass(entityType?: string): any;
19
-
20
- static getEntityManagerInstanceFromEntityId(
21
- entityId: string,
22
- userId: string
23
- ): Promise<any>;
24
- }
25
-
26
- interface IFriggEntityManager {}
12
+ interface IFriggEntityManager { }
27
13
 
28
14
  export class Entity extends Model {
29
15
  credentialId: string;
@@ -138,8 +124,7 @@ declare module "@friggframework/module-plugin" {
138
124
 
139
125
  export class ApiKeyRequester
140
126
  extends Requester
141
- implements IFriggApiKeyRequester
142
- {
127
+ implements IFriggApiKeyRequester {
143
128
  API_KEY_NAME: string;
144
129
  API_KEY_VALUE: any;
145
130
 
@@ -160,8 +145,7 @@ declare module "@friggframework/module-plugin" {
160
145
 
161
146
  export class BasicAuthRequester
162
147
  extends Requester
163
- implements IFriggBasicAuthRequester
164
- {
148
+ implements IFriggBasicAuthRequester {
165
149
  password: string;
166
150
  username: string;
167
151
 
@@ -189,8 +173,7 @@ declare module "@friggframework/module-plugin" {
189
173
 
190
174
  export class OAuth2Requester
191
175
  extends Requester
192
- implements IFriggOAuth2Requester
193
- {
176
+ implements IFriggOAuth2Requester {
194
177
  DLGT_TOKEN_DEAUTHORIZED: string;
195
178
  DLGT_TOKEN_UPDATE: string;
196
179
  accessTokenExpire: any;
@@ -1,70 +0,0 @@
1
- const { loadInstalledModules, Delegate } = require('../core');
2
-
3
- const { Entity } = require('./entity');
4
- const { ModuleManager } = require('./manager');
5
-
6
- class EntityManager {
7
- static primaryEntityClass = null; //primaryEntity;
8
-
9
- static entityManagerClasses = loadInstalledModules().map(
10
- (m) => m.EntityManager
11
- );
12
-
13
- static entityTypes = EntityManager.entityManagerClasses.map(
14
- (ManagerClass) => ManagerClass.getName()
15
- );
16
-
17
- static async getEntitiesForUser(userId) {
18
- const results = [];
19
- for (const Manager of this.entityManagerClasses) {
20
- results.push(...(await Manager.getEntitiesForUserId(userId)));
21
- }
22
- return results;
23
- }
24
-
25
- static checkIsValidType(entityType) {
26
- const indexOfEntity = EntityManager.entityTypes.indexOf(entityType);
27
- return indexOfEntity >= 0;
28
- }
29
-
30
- static getEntityManagerClass(entityType = '') {
31
- const normalizedType = entityType.toLowerCase();
32
-
33
- const indexOfEntityType =
34
- EntityManager.entityTypes.indexOf(normalizedType);
35
- if (!EntityManager.checkIsValidType(normalizedType)) {
36
- throw new Error(
37
- `Error: Invalid entity type of ${normalizedType}, options are ${EntityManager.entityTypes.join(
38
- ', '
39
- )}`
40
- );
41
- }
42
-
43
- const managerClass =
44
- EntityManager.entityManagerClasses[indexOfEntityType];
45
-
46
- if (!(managerClass.prototype instanceof ModuleManager)) {
47
- throw new Error('The Entity is not an instance of ModuleManager');
48
- }
49
-
50
- return managerClass;
51
- }
52
-
53
- static async getEntityManagerInstanceFromEntityId(entityId, userId) {
54
- const entityMO = new Entity();
55
- const entity = await entityMO.get(entityId);
56
- let entityManagerClass;
57
- for (const Manager of this.entityManagerClasses) {
58
- if (entity instanceof Manager.Entity.Model) {
59
- entityManagerClass = Manager;
60
- }
61
- }
62
- const instance = await entityManagerClass.getInstance({
63
- userId,
64
- entityId,
65
- });
66
- return instance;
67
- }
68
- }
69
-
70
- module.exports = { EntityManager };