@friggframework/core 2.0.0--canary.396.d50d765.0 → 2.0.0--canary.396.340648d.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,
@@ -12,6 +12,7 @@ const { GetCredentialForUser } = require('../credential/use-cases/get-credential
12
12
  const { CreateIntegration } = require('./use-cases/create-integration');
13
13
  const { ModuleService } = require('../module-plugin/module-service');
14
14
  const { ModuleRepository } = require('../module-plugin/module-repository');
15
+ const { GetEntitiesForUser } = require('../module-plugin/use-cases/get-entities-for-user');
15
16
  const {
16
17
  loadAppDefinition,
17
18
  } = require('../handlers/app-definition-loader');
@@ -70,14 +71,21 @@ function createIntegrationRouter(params) {
70
71
  moduleService,
71
72
  });
72
73
 
74
+ const getEntitiesForUserUseCase = new GetEntitiesForUser({
75
+ moduleRepository,
76
+ moduleDefinitions: getModules(),
77
+ });
78
+
73
79
  const router = get(params, 'router', express());
74
80
  const factory = get(params, 'factory');
75
81
  const getUserFromBearerToken = get(params, 'getUserFromBearerToken');
76
82
 
83
+ // todo: moduleFactory in factory is not used here anymore, remove it
77
84
  setIntegrationRoutes(router, factory, getUserFromBearerToken, {
78
85
  createIntegration,
79
86
  deleteIntegrationForUser,
80
87
  getIntegrationsForUser,
88
+ getEntitiesForUserUseCase,
81
89
  });
82
90
  setEntityRoutes(router, factory, getUserFromBearerToken, {
83
91
  getCredentialForUser,
@@ -116,11 +124,12 @@ function checkRequiredParams(params, requiredKeys) {
116
124
  * @param {import('../user/use-cases/get-user-from-bearer-token').GetUserFromBearerToken} getUserFromBearerToken - Use case for retrieving a user from a bearer token
117
125
  */
118
126
  function setIntegrationRoutes(router, factory, getUserFromBearerToken, useCases) {
119
- const { moduleFactory, integrationFactory } = factory;
127
+ const { integrationFactory } = factory;
120
128
  const {
121
129
  createIntegration,
122
130
  deleteIntegrationForUser,
123
131
  getIntegrationsForUser,
132
+ getEntitiesForUserUseCase,
124
133
  } = useCases;
125
134
 
126
135
  router.route('/api/integrations').get(
@@ -130,8 +139,7 @@ function setIntegrationRoutes(router, factory, getUserFromBearerToken, useCases)
130
139
  );
131
140
  const userId = user.getId();
132
141
  const results = await integrationFactory.getIntegrationOptions();
133
- results.entities.authorized =
134
- await moduleFactory.getEntitiesForUser(userId);
142
+ results.entities.authorized = await getEntitiesForUserUseCase.execute(userId);
135
143
  results.integrations =
136
144
  await getIntegrationsForUser.execute(userId);
137
145
 
@@ -454,7 +462,7 @@ function setEntityRoutes(router, factory, getUserFromBearerToken, useCases) {
454
462
  module.validateAuthorizationRequirements();
455
463
  if (!areRequirementsValid) {
456
464
  throw new Error(
457
- `Error: EntityManager of type ${params.entityType} requires a valid url`
465
+ `Error: Entity of type ${params.entityType} requires a valid url`
458
466
  );
459
467
  }
460
468
 
@@ -1,15 +1,11 @@
1
- const { GetIntegrationsForUserId } = require('./get-integrations-for-user');
2
- const { DeleteIntegrationForUserById } = require('./delete-integration-for-user-by-id');
3
- const { GetIntegrationById } = require('./get-integration-by-id');
4
- const { ListCredentials } = require('./list-credentials');
1
+ const { GetIntegrationsForUser } = require('./get-integrations-for-user');
2
+ const { DeleteIntegrationForUser } = require('./delete-integration-for-user');
5
3
  const { CreateIntegration } = require('./create-integration');
6
4
  const { GetIntegrationInstance } = require('./get-integration-instance');
7
5
 
8
6
  module.exports = {
9
- GetIntegrationsForUserId,
10
- DeleteIntegrationForUserById,
11
- GetIntegrationById,
12
- ListCredentials,
7
+ GetIntegrationsForUser,
8
+ DeleteIntegrationForUser,
13
9
  CreateIntegration,
14
10
  GetIntegrationInstance,
15
11
  };
@@ -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.d50d765.0",
4
+ "version": "2.0.0--canary.396.340648d.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.d50d765.0",
26
- "@friggframework/prettier-config": "2.0.0--canary.396.d50d765.0",
27
- "@friggframework/test": "2.0.0--canary.396.d50d765.0",
25
+ "@friggframework/eslint-config": "2.0.0--canary.396.340648d.0",
26
+ "@friggframework/prettier-config": "2.0.0--canary.396.340648d.0",
27
+ "@friggframework/test": "2.0.0--canary.396.340648d.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": "d50d765cf88cd18a604ded97837926ad2ab0bc87"
56
+ "gitHead": "340648d415ccfb50a16dcdcfa02ee7367b3ca64f"
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 };