@friggframework/core 2.0.0--canary.396.55167ab.0 → 2.0.0--canary.396.0dd37aa.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.
@@ -6,7 +6,7 @@ class IntegrationRepository {
6
6
  return integrationRecords.map(integrationRecord => ({
7
7
  id: integrationRecord._id,
8
8
  entitiesIds: integrationRecord.entities.map(e => e._id),
9
- userId: integrationRecord.user,
9
+ userId: integrationRecord.user.toString(),
10
10
  config: integrationRecord.config,
11
11
  version: integrationRecord.version,
12
12
  status: integrationRecord.status,
@@ -23,7 +23,7 @@ class IntegrationRepository {
23
23
  return {
24
24
  id: integrationRecord._id,
25
25
  entitiesIds: integrationRecord.entities.map(e => e._id),
26
- userId: integrationRecord.user,
26
+ userId: integrationRecord.user.toString(),
27
27
  config: integrationRecord.config,
28
28
  version: integrationRecord.version,
29
29
  status: integrationRecord.status,
@@ -15,6 +15,7 @@ const { GetEntitiesForUser } = require('../module-plugin/use-cases/get-entities-
15
15
  const {
16
16
  loadAppDefinition,
17
17
  } = require('../handlers/app-definition-loader');
18
+ const { GetIntegration } = require('./use-cases/get-integration');
18
19
 
19
20
  // todo: dont send moduleFactory and integrationFactory as a factory object, instead send them as separate params.
20
21
  // todo: this could be a use case class
@@ -76,6 +77,12 @@ function createIntegrationRouter(params) {
76
77
  moduleDefinitions: getModules(),
77
78
  });
78
79
 
80
+ const getIntegration = new GetIntegration({
81
+ integrationRepository,
82
+ integrationClasses: integrations,
83
+ moduleService,
84
+ });
85
+
79
86
  const router = get(params, 'router', express());
80
87
  const factory = get(params, 'factory');
81
88
  const getUserFromBearerToken = get(params, 'getUserFromBearerToken');
@@ -86,6 +93,7 @@ function createIntegrationRouter(params) {
86
93
  deleteIntegrationForUser,
87
94
  getIntegrationsForUser,
88
95
  getEntitiesForUserUseCase,
96
+ getIntegration,
89
97
  });
90
98
  setEntityRoutes(router, factory, getUserFromBearerToken, {
91
99
  getCredentialForUser,
@@ -130,6 +138,7 @@ function setIntegrationRoutes(router, factory, getUserFromBearerToken, integrati
130
138
  deleteIntegrationForUser,
131
139
  getIntegrationsForUser,
132
140
  getEntitiesForUserUseCase,
141
+ getIntegration,
133
142
  } = useCases;
134
143
  router.route('/api/integrations').get(
135
144
  catchAsyncError(async (req, res) => {
@@ -280,11 +289,7 @@ function setIntegrationRoutes(router, factory, getUserFromBearerToken, integrati
280
289
  req.headers.authorization
281
290
  );
282
291
  const params = checkRequiredParams(req.params, ['integrationId']);
283
- const integration =
284
- await integrationFactory.getInstanceFromIntegrationId({
285
- integrationId: params.integrationId,
286
- userId: user.getId(),
287
- });
292
+ const integration = await getIntegration.execute(params.integrationId, user.getId());
288
293
  res.json(await integration.send('GET_USER_ACTIONS', req.body));
289
294
  })
290
295
  );
@@ -34,13 +34,13 @@ class GetIntegrationForUser {
34
34
  (integrationClass) => integrationClass.Definition.name === integrationRecord.config.type
35
35
  );
36
36
 
37
- const modules = {};
38
- for (const [key, entity] of Object.entries(entities)) {
37
+ const modules = [];
38
+ for (const entity of entities) {
39
39
  const moduleInstance = await this.moduleService.getModuleInstance(
40
- entity.id,
40
+ entity._id,
41
41
  integrationRecord.user
42
42
  );
43
- modules[key] = moduleInstance;
43
+ modules.push(moduleInstance);
44
44
  }
45
45
 
46
46
  const integrationInstance = new Integration({
@@ -4,6 +4,15 @@ const { mapIntegrationClassToIntegrationDTO } = require('../utils/map-integratio
4
4
 
5
5
  // todo: remove this use case
6
6
  class GetIntegration {
7
+
8
+ /**
9
+ * @class GetIntegration
10
+ * @description Use case for retrieving a single integration by ID and user.
11
+ * @param {Object} params
12
+ * @param {import('../integration-repository').IntegrationRepository} params.integrationRepository - Repository for integration data access
13
+ * @param {Array<import('../integration').Integration>} params.integrationClasses - Array of available integration classes
14
+ * @param {import('../module-plugin/module-service').ModuleService} params.moduleService - Service for module instantiation and management
15
+ */
7
16
  constructor({
8
17
  integrationRepository,
9
18
  integrationClasses,
@@ -26,7 +35,7 @@ class GetIntegration {
26
35
  throw new Error(`No integration class found for type: ${integrationRecord.config.type}`);
27
36
  }
28
37
 
29
- if (!integrationRecord.user.equals(userId)) {
38
+ if (integrationRecord.userId !== userId) {
30
39
  throw new Error(
31
40
  `Integration ${integrationId} does not belong to User ${userId}`
32
41
  );
@@ -39,20 +48,20 @@ class GetIntegration {
39
48
 
40
49
 
41
50
  // 3. Load modules based on entity references
42
- const modules = {};
43
- for (const [key, entity] of Object.entries(integrationRecord.entities)) {
51
+ const modules = [];
52
+ for (const entityId of integrationRecord.entitiesIds) {
44
53
  const moduleInstance = await this.moduleService.getModuleInstance(
45
- entity._id,
46
- integrationRecord.user
54
+ entityId,
55
+ integrationRecord.userId
47
56
  );
48
- modules[key] = moduleInstance;
57
+ modules.push(moduleInstance);
49
58
  }
50
59
 
51
60
  // 4. Create the Integration domain entity with modules
52
61
  const integrationInstance = new Integration({
53
62
  id: integrationRecord._id.toString(),
54
- userId: integrationRecord.user,
55
- entities: integrationRecord.entities,
63
+ userId: integrationRecord.userId,
64
+ entities: integrationRecord.entitiesIds,
56
65
  config: integrationRecord.config,
57
66
  status: integrationRecord.status,
58
67
  version: integrationRecord.version,
@@ -23,19 +23,19 @@ class GetIntegrationsForUser {
23
23
  const integrations = []
24
24
 
25
25
  for (const integrationRecord of integrationRecords) {
26
- const entities = await this.moduleRepository.findEntitiesByIds(integrationRecords.entitiesIds);
26
+ const entities = await this.moduleRepository.findEntitiesByIds(integrationRecord.entitiesIds);
27
27
 
28
28
  const integrationClass = this.integrationClasses.find(
29
29
  (integrationClass) => integrationClass.Definition.name === integrationRecord.config.type
30
30
  );
31
31
 
32
- const modules = {};
33
- for (const [key, entity] of Object.entries(entities)) {
32
+ const modules = [];
33
+ for (const entity of entities) {
34
34
  const moduleInstance = await this.moduleService.getModuleInstance(
35
- entity.id,
36
- integrationRecord.user
35
+ entity._id,
36
+ integrationRecord.userId
37
37
  );
38
- modules[key] = moduleInstance;
38
+ modules.push(moduleInstance);
39
39
  }
40
40
 
41
41
  const integrationInstance = new Integration({
@@ -7,16 +7,31 @@ class ModuleRepository {
7
7
  throw new Error(`Entity ${entityId} not found`);
8
8
  }
9
9
 
10
- return entity;
10
+ return {
11
+ id: entity._id,
12
+ accountId: entity.accountId,
13
+ credential: entity.credential,
14
+ userId: entity.user.toString(),
15
+ name: entity.name,
16
+ externalId: entity.externalId,
17
+ type: entity.__t,
18
+ moduleName: entity.moduleName,
19
+ };
11
20
  }
12
21
 
13
22
  async findEntitiesByIds(entitiesIds) {
14
23
  const entitiesRecords = await Entity.find({ _id: { $in: entitiesIds } }, '', { lean: true }).populate('credential');
24
+
25
+ // todo: this is a workaround needed while we create an integration with the same entity twice
26
+ if (entitiesRecords.length !== entitiesIds.length && entitiesIds[0] !== entitiesIds[1]) {
27
+ throw new Error(`Some entities not found`);
28
+ }
29
+
15
30
  return entitiesRecords.map(e => ({
16
31
  id: e._id,
17
32
  accountId: e.accountId,
18
33
  credential: e.credential,
19
- userId: e.user,
34
+ userId: e.user.toString(),
20
35
  name: e.name,
21
36
  externalId: e.externalId,
22
37
  type: e.__t,
@@ -31,6 +46,14 @@ class ModuleRepository {
31
46
  { lean: true }
32
47
  );
33
48
  }
49
+
50
+ async findEntitiesByIds(entityIds) {
51
+ return Entity.find(
52
+ { _id: { $in: entityIds } },
53
+ '-dateCreated -dateUpdated -user -credentials -credential -__t -__v',
54
+ { lean: true }
55
+ );
56
+ }
34
57
  }
35
58
 
36
59
  module.exports = { ModuleRepository };
@@ -21,13 +21,13 @@ class ModuleService {
21
21
  throw new Error(`Entity ${entityId} not found`);
22
22
  }
23
23
 
24
- if (entity.user.toString() !== userId.toString()) {
24
+ if (entity.userId !== userId) {
25
25
  throw new Error(
26
26
  `Entity ${entityId} does not belong to user ${userId}`
27
27
  );
28
28
  }
29
29
 
30
- const entityType = entity.__t;
30
+ const entityType = entity.type;
31
31
  const moduleDefinition = this.moduleDefinitions.find((def) => {
32
32
  const modelName = Module.getEntityModelFromDefinition(def).modelName;
33
33
  return entityType === modelName;
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.55167ab.0",
4
+ "version": "2.0.0--canary.396.0dd37aa.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.55167ab.0",
26
- "@friggframework/prettier-config": "2.0.0--canary.396.55167ab.0",
27
- "@friggframework/test": "2.0.0--canary.396.55167ab.0",
25
+ "@friggframework/eslint-config": "2.0.0--canary.396.0dd37aa.0",
26
+ "@friggframework/prettier-config": "2.0.0--canary.396.0dd37aa.0",
27
+ "@friggframework/test": "2.0.0--canary.396.0dd37aa.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": "55167ab7bbeabe9d61924cb7ba8b62edaa2e1dd4"
56
+ "gitHead": "0dd37aa78998c9291f90e3fdeb0e07096e2e140a"
57
57
  }