@friggframework/core 2.0.0--canary.397.17c930a.0 → 2.0.0--canary.397.fadaaf6.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.
@@ -1,7 +1,8 @@
1
1
  const { IntegrationMapping } = require('./integration-mapping');
2
2
  const { Options } = require('./options');
3
- const { EnableIntegration } = require('./use-cases/enable-integration');
3
+ const { UpdateIntegrationStatus } = require('./use-cases/update-integration-status');
4
4
  const { IntegrationRepository } = require('./integration-repository');
5
+ const { UpdateIntegrationMessages } = require('./use-cases/update-integration-messages');
5
6
 
6
7
  const constantsToBeMigrated = {
7
8
  defaultEvents: {
@@ -23,6 +24,11 @@ const constantsToBeMigrated = {
23
24
 
24
25
  class IntegrationBase {
25
26
 
27
+ // todo: maybe we can pass this as Dependency Injection in the sub-class constructor
28
+ integrationRepository = new IntegrationRepository();
29
+ updateIntegrationStatus = new UpdateIntegrationStatus({ integrationRepository: this.integrationRepository });
30
+ updateIntegrationMessages = new UpdateIntegrationMessages({ integrationRepository: this.integrationRepository });
31
+
26
32
  static getOptionDetails() {
27
33
  const options = new Options({
28
34
  module: Object.values(this.Definition.modules)[0], // This is a placeholder until we revamp the frontend
@@ -79,12 +85,6 @@ class IntegrationBase {
79
85
 
80
86
  constructor(params) {
81
87
 
82
- // todo: maybe we can pass this as Dependency Injection in the sub-class constructor
83
- const { integrationRepository } = new IntegrationRepository();
84
- this.enableIntegration = new EnableIntegration({
85
- integrationRepository,
86
- });
87
-
88
88
  this.defaultEvents = {
89
89
  [constantsToBeMigrated.defaultEvents.ON_CREATE]: {
90
90
  type: constantsToBeMigrated.types.LIFE_CYCLE_EVENT,
@@ -133,7 +133,7 @@ class IntegrationBase {
133
133
 
134
134
  async validateConfig() {
135
135
  const configOptions = await this.getConfigOptions();
136
- const currentConfig = this.record.config;
136
+ const currentConfig = this.getConfig();
137
137
  let needsConfig = false;
138
138
  for (const option of configOptions) {
139
139
  if (option.required) {
@@ -145,17 +145,18 @@ class IntegrationBase {
145
145
  )
146
146
  ) {
147
147
  needsConfig = true;
148
- this.record.messages.warnings.push({
149
- title: 'Config Validation Error',
150
- message: `Missing required field of ${option.label}`,
151
- timestamp: Date.now(),
152
- });
148
+ await this.updateIntegrationMessages.execute(
149
+ this.id,
150
+ 'warnings',
151
+ 'Config Validation Error',
152
+ `Missing required field of ${option.label}`,
153
+ Date.now()
154
+ );
153
155
  }
154
156
  }
155
157
  }
156
158
  if (needsConfig) {
157
- this.record.status = 'NEEDS_CONFIG';
158
- await this.record.save();
159
+ await this.updateIntegrationStatus.execute(this.id, 'NEEDS_CONFIG');
159
160
  }
160
161
  }
161
162
 
@@ -167,34 +168,36 @@ class IntegrationBase {
167
168
  await this[module].testAuth();
168
169
  } catch {
169
170
  didAuthPass = false;
170
- this.record.messages.errors.push({
171
- title: 'Authentication Error',
172
- message: `There was an error with your ${this[
171
+ await this.updateIntegrationMessages.execute(
172
+ this.id,
173
+ 'errors',
174
+ 'Authentication Error',
175
+ `There was an error with your ${this[
173
176
  module
174
177
  ].constructor.getName()} Entity.
175
178
  Please reconnect/re-authenticate, or reach out to Support for assistance.`,
176
- timestamp: Date.now(),
177
- });
179
+ Date.now()
180
+ );
178
181
  }
179
182
  }
180
183
 
181
184
  if (!didAuthPass) {
182
- this.record.status = 'ERROR';
183
- this.record.markModified('messages.error');
184
- await this.record.save();
185
+ await this.updateIntegrationStatus.execute(this.id, 'ERROR');
185
186
  }
186
187
  }
187
188
 
188
189
  async getMapping(sourceId) {
189
- return IntegrationMapping.findBy(this.record.id, sourceId);
190
+ // todo: this should be a use case
191
+ return IntegrationMapping.findBy(this.id, sourceId);
190
192
  }
191
193
 
192
194
  async upsertMapping(sourceId, mapping) {
193
195
  if (!sourceId) {
194
196
  throw new Error(`sourceId must be set`);
195
197
  }
198
+ // todo: this should be a use case
196
199
  return await IntegrationMapping.upsert(
197
- this.record.id,
200
+ this.id,
198
201
  sourceId,
199
202
  mapping
200
203
  );
@@ -204,7 +207,7 @@ class IntegrationBase {
204
207
  * CHILDREN CAN OVERRIDE THESE CONFIGURATION METHODS
205
208
  */
206
209
  async onCreate({ integrationId }) {
207
- await this.enableIntegration.execute(integrationId);
210
+ await this.updateIntegrationStatus.execute(integrationId, 'ENABLED');
208
211
  }
209
212
 
210
213
  async onUpdate(params) { }
@@ -31,17 +31,17 @@ class IntegrationRepository {
31
31
  }
32
32
  }
33
33
 
34
- async updateIntegration(integrationId, status) {
34
+ async updateIntegrationStatus(integrationId, status) {
35
35
  const integrationRecord = await IntegrationModel.updateOne({ _id: integrationId }, { status });
36
- return {
37
- id: integrationRecord._id,
38
- entitiesIds: integrationRecord.entities.map(e => e._id),
39
- userId: integrationRecord.user.toString(),
40
- config: integrationRecord.config,
41
- version: integrationRecord.version,
42
- status: integrationRecord.status,
43
- messages: integrationRecord.messages,
44
- };
36
+ return integrationRecord.acknowledged;
37
+ }
38
+
39
+ async updateIntegrationMessages(integrationId, messageType, messageTitle, messageBody, messageTimestamp) {
40
+ const integrationRecord = await IntegrationModel.updateOne(
41
+ { _id: integrationId },
42
+ { $push: { [`messages.${messageType}`]: { title: messageTitle, message: messageBody, timestamp: messageTimestamp } } }
43
+ );
44
+ return integrationRecord.acknowledged;
45
45
  }
46
46
 
47
47
  async createIntegration(entities, userId, config) {
@@ -148,6 +148,10 @@ class Integration {
148
148
  // Core methods that should always be on Integration entity
149
149
  // These override any behavior methods with the same name
150
150
 
151
+ getConfig() {
152
+ return this.config;
153
+ }
154
+
151
155
  // Module access helpers
152
156
  getModule(key) {
153
157
  return this.modules[key];
@@ -48,7 +48,7 @@ class CreateIntegration {
48
48
  });
49
49
 
50
50
  await integrationInstance.initialize();
51
- await integrationInstance.send('ON_CREATE', {});
51
+ await integrationInstance.send('ON_CREATE', { integrationId: integrationRecord.id });
52
52
 
53
53
  return mapIntegrationClassToIntegrationDTO(integrationInstance);
54
54
  }
@@ -0,0 +1,20 @@
1
+ class UpdateIntegrationMessages {
2
+ constructor({ integrationRepository }) {
3
+ this.integrationRepository = integrationRepository;
4
+ }
5
+
6
+ /**
7
+ * @param {string} integrationId
8
+ * @param {string} messageType - 'errors', 'warnings', 'info' or 'logs'
9
+ * @param {string} messageTitle,
10
+ * @param {string} messageBody,
11
+ * @param {string} messageTimestamp,
12
+ * @returns {Promise<Object>}
13
+ */
14
+ async execute(integrationId, messageType, messageTitle, messageBody, messageTimestamp) {
15
+ const integration = await this.integrationRepository.updateIntegrationMessages(integrationId, messageType, messageTitle, messageBody, messageTimestamp);
16
+ return integration;
17
+ }
18
+ }
19
+
20
+ module.exports = { UpdateIntegrationMessages };
@@ -1,12 +1,12 @@
1
- class EnableIntegration {
1
+ class UpdateIntegrationStatus {
2
2
  constructor({ integrationRepository }) {
3
3
  this.integrationRepository = integrationRepository;
4
4
  }
5
5
 
6
- async execute(integrationId) {
7
- const integration = await this.integrationRepository.updateIntegration(integrationId, 'ENABLED');
6
+ async execute(integrationId, status) {
7
+ const integration = await this.integrationRepository.updateIntegrationStatus(integrationId, status);
8
8
  return integration;
9
9
  }
10
10
  }
11
11
 
12
- module.exports = { EnableIntegration };
12
+ module.exports = { UpdateIntegrationStatus };
@@ -40,24 +40,35 @@ class ModuleRepository {
40
40
  }
41
41
 
42
42
  async findEntitiesByUserId(userId) {
43
- return Entity.find(
43
+ const entitiesRecords = await Entity.find(
44
44
  { user: userId },
45
45
  '-dateCreated -dateUpdated -user -credentials -credential -__t -__v',
46
46
  { lean: true }
47
- );
47
+ ).populate('credential');
48
+
49
+ return entitiesRecords.map(e => ({
50
+ id: e._id.toString(),
51
+ accountId: e.accountId,
52
+ credential: e.credential,
53
+ userId: e.user.toString(),
54
+ name: e.name,
55
+ externalId: e.externalId,
56
+ type: e.__t,
57
+ moduleName: e.moduleName,
58
+ }));
48
59
  }
49
60
 
50
- async findEntitiesByIds(entityIds) {
51
- const entities = await Entity.find(
52
- { _id: { $in: entityIds } },
61
+ async findEntitiesByIds(entitiesIds) {
62
+ const entitiesRecords = await Entity.find(
63
+ { _id: { $in: entitiesIds } },
53
64
  '',
54
65
  { lean: true }
55
- );
66
+ ).populate('credential');
56
67
 
57
- return entities.map(e => ({
68
+ return entitiesRecords.map(e => ({
58
69
  id: e._id.toString(),
59
70
  accountId: e.accountId,
60
- credentialId: e.credential.toString(),
71
+ credential: e.credential,
61
72
  userId: e.user.toString(),
62
73
  name: e.name,
63
74
  externalId: e.externalId,
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.397.17c930a.0",
4
+ "version": "2.0.0--canary.397.fadaaf6.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.397.17c930a.0",
26
- "@friggframework/prettier-config": "2.0.0--canary.397.17c930a.0",
27
- "@friggframework/test": "2.0.0--canary.397.17c930a.0",
25
+ "@friggframework/eslint-config": "2.0.0--canary.397.fadaaf6.0",
26
+ "@friggframework/prettier-config": "2.0.0--canary.397.fadaaf6.0",
27
+ "@friggframework/test": "2.0.0--canary.397.fadaaf6.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": "17c930a2dfe417f16b61c1facd6c04b17a6b7d83"
56
+ "gitHead": "fadaaf6990abc87d718814725fbd94173b9ca2bc"
57
57
  }