@friggframework/core 2.0.0--canary.396.ed41aed.0 → 2.0.0--canary.396.20d0301.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.
@@ -60,7 +60,7 @@ class IntegrationBase {
60
60
  const { definition } =
61
61
  this.constructor.Definition.modules[moduleName];
62
62
  if (typeof definition.API === 'function') {
63
- this[moduleName] = { api: new definition.API() };
63
+ this[moduleName] = { api: new definition.API({}) };
64
64
  } else {
65
65
  throw new Error(
66
66
  `Module ${moduleName} must be a function that extends IntegrationModule`
@@ -2,7 +2,7 @@ const { IntegrationModel } = require('./integration-model');
2
2
 
3
3
  class IntegrationRepository {
4
4
  async findIntegrationsByUserId(userId) {
5
- return IntegrationModel.find({ user: userId });
5
+ return IntegrationModel.find({ user: userId }, '', { lean: true }).populate('entities');
6
6
  }
7
7
 
8
8
  async deleteIntegrationById(integrationId) {
@@ -136,6 +136,7 @@ function setIntegrationRoutes(router, factory, getUserFromBearerToken, integrati
136
136
  req.headers.authorization
137
137
  );
138
138
  const userId = user.getId();
139
+ const integrations = await getIntegrationsForUser.execute(userId);
139
140
  const results = {
140
141
  entities: {
141
142
  options: integrationClasses.map((IntegrationClass) =>
@@ -143,7 +144,7 @@ function setIntegrationRoutes(router, factory, getUserFromBearerToken, integrati
143
144
  ),
144
145
  authorized: await getEntitiesForUserUseCase.execute(userId),
145
146
  },
146
- integrations: await getIntegrationsForUser.execute(userId),
147
+ integrations: integrations.map((integration) => integration.toJSON()),
147
148
  }
148
149
 
149
150
  res.json(results);
@@ -151,6 +151,26 @@ class Integration {
151
151
  hasMethod(methodName) {
152
152
  return methodName in this || (this.behavior && methodName in this.behavior);
153
153
  }
154
+
155
+ /**
156
+ * Custom JSON serializer to prevent circular references (e.g. Module → Api → delegate)
157
+ * and to keep API responses lightweight.
158
+ * Only primitive, serialisable data needed by clients is returned.
159
+ */
160
+ toJSON() {
161
+ return {
162
+ id: this.id,
163
+ userId: this.userId,
164
+ entities: this.entities,
165
+ config: this.config,
166
+ status: this.status,
167
+ version: this.version,
168
+ messages: this.messages,
169
+ entityReference: this.entityReference,
170
+ // Expose userActions if they were loaded/attached elsewhere
171
+ userActions: this.userActions,
172
+ };
173
+ }
154
174
  }
155
175
 
156
176
  module.exports = { Integration };
@@ -17,9 +17,9 @@ class CreateIntegration {
17
17
  const integrationRecord = await this.integrationRepository.createIntegration(entities, userId, config);
18
18
 
19
19
  const modules = {};
20
- for (const [entityId, key] of Object.entries(integrationRecord.entityReference)) {
20
+ for (const [key, entity] of Object.entries(integrationRecord.entities)) {
21
21
  const moduleInstance = await this.moduleService.getModuleInstance(
22
- entityId,
22
+ entity._id,
23
23
  integrationRecord.user
24
24
  );
25
25
  modules[key] = moduleInstance;
@@ -1,3 +1,5 @@
1
+ const { Integration } = require('../integration');
2
+
1
3
  class GetIntegrationForUser {
2
4
  constructor({ integrationRepository, integrationClasses, moduleService }) {
3
5
 
@@ -25,10 +27,14 @@ class GetIntegrationForUser {
25
27
  throw Boom.forbidden('User does not have access to this integration');
26
28
  }
27
29
 
30
+ const integrationClass = this.integrationClasses.find(
31
+ (integrationClass) => integrationClass.Definition.name === integrationRecord.config.type
32
+ );
33
+
28
34
  const modules = {};
29
- for (const [entityId, key] of Object.entries(integrationRecord.entityReference)) {
35
+ for (const [key, entity] of Object.entries(integrationRecord.entities)) {
30
36
  const moduleInstance = await this.moduleService.getModuleInstance(
31
- entityId,
37
+ entity._id,
32
38
  integrationRecord.user
33
39
  );
34
40
  modules[key] = moduleInstance;
@@ -43,7 +49,7 @@ class GetIntegrationForUser {
43
49
  version: integrationRecord.version,
44
50
  messages: integrationRecord.messages,
45
51
  entityReference: integrationRecord.entityReference,
46
- integrationClass: IntegrationClass,
52
+ integrationClass: integrationClass,
47
53
  modules
48
54
  });
49
55
 
@@ -11,15 +11,8 @@ class GetIntegration {
11
11
  this.integrationRepository = integrationRepository;
12
12
  this.integrationClasses = integrationClasses;
13
13
  this.moduleService = moduleService;
14
-
15
- // Build type mapping for quick lookup
16
- this.integrationTypeMap = new Map();
17
- this.integrationClasses.forEach((IntegrationClass) => {
18
- this.integrationTypeMap.set(IntegrationClass.getName(), IntegrationClass);
19
- });
20
14
  }
21
15
 
22
- // todo: check if this is really all we need to instantiate an integration instance
23
16
  async execute(integrationId, userId) {
24
17
  // 1. Get integration record from repository
25
18
  const integrationRecord = await this.integrationRepository.findIntegrationById(integrationId);
@@ -28,10 +21,7 @@ class GetIntegration {
28
21
  throw new Error(`No integration found by the ID of ${integrationId}`);
29
22
  }
30
23
 
31
- // 2. Get the correct Integration class by type
32
- const IntegrationClass = this.integrationTypeMap.get(integrationRecord.config.type);
33
-
34
- if (!IntegrationClass) {
24
+ if (!integrationClass) {
35
25
  throw new Error(`No integration class found for type: ${integrationRecord.config.type}`);
36
26
  }
37
27
 
@@ -41,11 +31,17 @@ class GetIntegration {
41
31
  );
42
32
  }
43
33
 
34
+ // 2. Get the correct Integration class by type
35
+ const integrationClass = this.integrationClasses.find(
36
+ (integrationClass) => integrationClass.Definition.name === integrationRecord.config.type
37
+ );
38
+
39
+
44
40
  // 3. Load modules based on entity references
45
41
  const modules = {};
46
- for (const [entityId, key] of Object.entries(integrationRecord.entityReference)) {
42
+ for (const [key, entity] of Object.entries(integrationRecord.entities)) {
47
43
  const moduleInstance = await this.moduleService.getModuleInstance(
48
- entityId,
44
+ entity._id,
49
45
  integrationRecord.user
50
46
  );
51
47
  modules[key] = moduleInstance;
@@ -61,7 +57,7 @@ class GetIntegration {
61
57
  version: integrationRecord.version,
62
58
  messages: integrationRecord.messages,
63
59
  entityReference: integrationRecord.entityReference,
64
- integrationClass: IntegrationClass,
60
+ integrationClass: integrationClass,
65
61
  modules
66
62
  });
67
63
 
@@ -1,3 +1,5 @@
1
+ const { Integration } = require('../integration');
2
+
1
3
  class GetIntegrationsForUser {
2
4
  constructor({ integrationRepository, integrationClasses, moduleService }) {
3
5
 
@@ -16,19 +18,23 @@ class GetIntegrationsForUser {
16
18
  async execute(userId) {
17
19
  const integrationRecords = await this.integrationRepository.findIntegrationsByUserId(userId);
18
20
 
19
-
20
21
  const integrations = []
21
22
 
22
23
  for (const integrationRecord of integrationRecords) {
23
24
 
25
+ const integrationClass = this.integrationClasses.find(
26
+ (integrationClass) => integrationClass.Definition.name === integrationRecord.config.type
27
+ );
28
+
24
29
  const modules = {};
25
- for (const [entityId, key] of Object.entries(integrationRecord.entityReference)) {
30
+ for (const [key, entity] of Object.entries(integrationRecord.entities)) {
26
31
  const moduleInstance = await this.moduleService.getModuleInstance(
27
- entityId,
32
+ entity._id,
28
33
  integrationRecord.user
29
34
  );
30
35
  modules[key] = moduleInstance;
31
36
  }
37
+
32
38
  integrations.push(new Integration({
33
39
  id: integrationRecord.id,
34
40
  userId: integrationRecord.user,
@@ -38,7 +44,7 @@ class GetIntegrationsForUser {
38
44
  version: integrationRecord.version,
39
45
  messages: integrationRecord.messages,
40
46
  entityReference: integrationRecord.entityReference,
41
- integrationClass: IntegrationClass,
47
+ integrationClass: integrationClass,
42
48
  modules
43
49
  }));
44
50
 
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.ed41aed.0",
4
+ "version": "2.0.0--canary.396.20d0301.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.ed41aed.0",
26
- "@friggframework/prettier-config": "2.0.0--canary.396.ed41aed.0",
27
- "@friggframework/test": "2.0.0--canary.396.ed41aed.0",
25
+ "@friggframework/eslint-config": "2.0.0--canary.396.20d0301.0",
26
+ "@friggframework/prettier-config": "2.0.0--canary.396.20d0301.0",
27
+ "@friggframework/test": "2.0.0--canary.396.20d0301.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": "ed41aede348ecbdeb5c2a944137bb01fa76e8aac"
56
+ "gitHead": "20d0301107157627ac80db74ae19d806ac9d7bc2"
57
57
  }