@friggframework/core 2.0.0--canary.397.05c9b19.0 → 2.0.0--canary.397.1b51778.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.
@@ -4,6 +4,39 @@ class CredentialRepository {
4
4
  async findCredentialById(id) {
5
5
  return Credential.findById(id);
6
6
  }
7
+
8
+ async updateAuthenticationStatus(credentialId, authIsValid) {
9
+ return Credential.updateOne({ _id: credentialId }, { $set: { auth_is_valid: authIsValid } });
10
+ }
11
+
12
+ /**
13
+ * Permanently remove a credential document.
14
+ * @param {string} credentialId
15
+ * @returns {Promise<import('mongoose').DeleteResult>}
16
+ */
17
+ async deleteCredentialById(credentialId) {
18
+ return Credential.deleteOne({ _id: credentialId });
19
+ }
20
+
21
+ /**
22
+ * Create a new credential or update an existing one matching the identifiers.
23
+ * `credentialDetails` format: { identifiers: { ... }, details: { ... } }
24
+ * Identifiers are used in the query filter; details are merged into the document.
25
+ * @param {{identifiers: Object, details: Object}} credentialDetails
26
+ * @returns {Promise<Object>} The persisted credential (lean object)
27
+ */
28
+ async upsertCredential(credentialDetails) {
29
+ const { identifiers, details } = credentialDetails;
30
+ if (!identifiers) throw new Error('identifiers required to upsert credential');
31
+
32
+ const query = { ...identifiers };
33
+
34
+ const update = { $set: { ...details } };
35
+
36
+ const options = { upsert: true, new: true, setDefaultsOnInsert: true, lean: true };
37
+
38
+ return Credential.findOneAndUpdate(query, update, options);
39
+ }
7
40
  }
8
41
 
9
42
  module.exports = { CredentialRepository };
@@ -0,0 +1,15 @@
1
+ class UpdateAuthenticationStatus {
2
+ constructor({ credentialRepository }) {
3
+ this.credentialRepository = credentialRepository;
4
+ }
5
+
6
+ /**
7
+ * @param {string} credentialId
8
+ * @param {boolean} authIsValid
9
+ */
10
+ async execute(credentialId, authIsValid) {
11
+ await this.credentialRepository.updateAuthenticationStatus(credentialId, authIsValid);
12
+ }
13
+ }
14
+
15
+ module.exports = { UpdateAuthenticationStatus };
@@ -2,8 +2,10 @@ const { Router } = require('express');
2
2
  const { Worker } = require('@friggframework/core');
3
3
  const { loadAppDefinition } = require('./app-definition-loader');
4
4
  const { IntegrationRepository } = require('../integrations/integration-repository');
5
- const { ModuleService } = require('../modules/module-service');
5
+ const { ModuleFactory } = require('../modules/module-factory');
6
6
  const { GetIntegrationInstance } = require('../integrations/use-cases/get-integration-instance');
7
+ const { getModulesDefinitionFromIntegrationClasses } = require('../integrations/utils/map-integration-dto');
8
+ const { ModuleRepository } = require('../modules/module-repository');
7
9
 
8
10
  const loadRouterFromObject = (IntegrationClass, routerObject) => {
9
11
  const router = Router();
@@ -42,12 +44,16 @@ const createQueueWorker = (integrationClass) => {
42
44
  } else {
43
45
  const { integrations: integrationClasses } = loadAppDefinition();
44
46
  const integrationRepository = new IntegrationRepository();
45
- const moduleService = new ModuleService();
47
+ const moduleRepository = new ModuleRepository();
48
+ const moduleFactory = new ModuleFactory({
49
+ moduleRepository,
50
+ moduleDefinitions: getModulesDefinitionFromIntegrationClasses(integrationClasses),
51
+ });
46
52
 
47
53
  const getIntegrationInstance = new GetIntegrationInstance({
48
54
  integrationRepository,
49
55
  integrationClasses,
50
- moduleService,
56
+ moduleFactory,
51
57
  });
52
58
 
53
59
  // todo: are we going to have the userId available here?
@@ -8,7 +8,7 @@ const { GetIntegrationsForUser } = require('./use-cases/get-integrations-for-use
8
8
  const { CredentialRepository } = require('../credential/credential-repository');
9
9
  const { GetCredentialForUser } = require('../credential/use-cases/get-credential-for-user');
10
10
  const { CreateIntegration } = require('./use-cases/create-integration');
11
- const { ModuleService } = require('../modules/module-service');
11
+ const { ModuleFactory } = require('../modules/module-factory');
12
12
  const { ModuleRepository } = require('../modules/module-repository');
13
13
  const { GetEntitiesForUser } = require('../modules/use-cases/get-entities-for-user');
14
14
  const { loadAppDefinition } = require('../handlers/app-definition-loader');
@@ -36,7 +36,7 @@ function createIntegrationRouter(params) {
36
36
  const integrationRepository = new IntegrationRepository();
37
37
  const credentialRepository = new CredentialRepository();
38
38
 
39
- const moduleService = new ModuleService({
39
+ const moduleFactory = new ModuleFactory({
40
40
  moduleRepository,
41
41
  moduleDefinitions: getModulesDefinitionFromIntegrationClasses(integrationClasses),
42
42
  });
@@ -48,7 +48,7 @@ function createIntegrationRouter(params) {
48
48
  const getIntegrationsForUser = new GetIntegrationsForUser({
49
49
  integrationRepository,
50
50
  integrationClasses,
51
- moduleService,
51
+ moduleFactory,
52
52
  moduleRepository,
53
53
  });
54
54
 
@@ -59,7 +59,7 @@ function createIntegrationRouter(params) {
59
59
  const createIntegration = new CreateIntegration({
60
60
  integrationRepository,
61
61
  integrationClasses,
62
- moduleService,
62
+ moduleFactory,
63
63
  });
64
64
 
65
65
  const getEntitiesForUser = new GetEntitiesForUser({
@@ -70,13 +70,13 @@ function createIntegrationRouter(params) {
70
70
  const getIntegrationInstance = new GetIntegrationInstance({
71
71
  integrationRepository,
72
72
  integrationClasses,
73
- moduleService,
73
+ moduleFactory,
74
74
  });
75
75
 
76
76
  const updateIntegration = new UpdateIntegration({
77
77
  integrationRepository,
78
78
  integrationClasses,
79
- moduleService,
79
+ moduleFactory,
80
80
  });
81
81
 
82
82
  const getModuleInstanceFromType = new GetModuleInstanceFromType({
@@ -119,7 +119,7 @@ class Integration {
119
119
  // -----------------------------------------------------------------
120
120
  // Inject the real Module instances (with credentials) so that any
121
121
  // behaviour code accessing `this.<moduleName>.api` hits the
122
- // correctly authenticated requester created by ModuleService.
122
+ // correctly authenticated requester created by ModuleFactory.
123
123
  // -----------------------------------------------------------------
124
124
  for (const mod of this._moduleInstances) {
125
125
  const key = typeof mod.getName === 'function' ? mod.getName() : mod.name;
@@ -6,12 +6,12 @@ class CreateIntegration {
6
6
  * @param {Object} params
7
7
  * @param {import('../integration-repository').IntegrationRepository} params.integrationRepository
8
8
  * @param {import('../integration-classes').IntegrationClasses} params.integrationClasses
9
- * @param {import('../../modules/module-service').ModuleService} params.moduleService
9
+ * @param {import('../../modules/module-factory').ModuleFactory} params.moduleFactory
10
10
  */
11
- constructor({ integrationRepository, integrationClasses, moduleService }) {
11
+ constructor({ integrationRepository, integrationClasses, moduleFactory }) {
12
12
  this.integrationRepository = integrationRepository;
13
13
  this.integrationClasses = integrationClasses;
14
- this.moduleService = moduleService;
14
+ this.moduleFactory = moduleFactory;
15
15
  }
16
16
 
17
17
  async execute(entities, userId, config) {
@@ -28,7 +28,7 @@ class CreateIntegration {
28
28
 
29
29
  const modules = [];
30
30
  for (const entityId of integrationRecord.entitiesIds) {
31
- const moduleInstance = await this.moduleService.getModuleInstance(
31
+ const moduleInstance = await this.moduleFactory.getModuleInstance(
32
32
  entityId,
33
33
  integrationRecord.userId
34
34
  );
@@ -2,14 +2,14 @@ const { Integration } = require('../integration');
2
2
  const { mapIntegrationClassToIntegrationDTO } = require('../utils/map-integration-dto');
3
3
 
4
4
  class GetIntegrationForUser {
5
- constructor({ integrationRepository, integrationClasses, moduleService, moduleRepository }) {
5
+ constructor({ integrationRepository, integrationClasses, moduleFactory, moduleRepository }) {
6
6
 
7
7
  /**
8
8
  * @type {import('../integration-repository').IntegrationRepository}
9
9
  */
10
10
  this.integrationRepository = integrationRepository;
11
11
  this.integrationClasses = integrationClasses;
12
- this.moduleService = moduleService;
12
+ this.moduleFactory = moduleFactory;
13
13
  this.moduleRepository = moduleRepository;
14
14
  }
15
15
 
@@ -36,7 +36,7 @@ class GetIntegrationForUser {
36
36
 
37
37
  const modules = [];
38
38
  for (const entity of entities) {
39
- const moduleInstance = await this.moduleService.getModuleInstance(
39
+ const moduleInstance = await this.moduleFactory.getModuleInstance(
40
40
  entity._id,
41
41
  integrationRecord.user
42
42
  );
@@ -8,16 +8,16 @@ class GetIntegrationInstance {
8
8
  * @param {Object} params
9
9
  * @param {import('../integration-repository').IntegrationRepository} params.integrationRepository - Repository for integration data access
10
10
  * @param {Array<import('../integration').Integration>} params.integrationClasses - Array of available integration classes
11
- * @param {import('../module-plugin/module-service').ModuleService} params.moduleService - Service for module instantiation and management
11
+ * @param {import('../../modules/module-factory').ModuleFactory} params.moduleFactory - Service for module instantiation and management
12
12
  */
13
13
  constructor({
14
14
  integrationRepository,
15
15
  integrationClasses,
16
- moduleService,
16
+ moduleFactory,
17
17
  }) {
18
18
  this.integrationRepository = integrationRepository;
19
19
  this.integrationClasses = integrationClasses;
20
- this.moduleService = moduleService;
20
+ this.moduleFactory = moduleFactory;
21
21
  }
22
22
 
23
23
  async execute(integrationId, userId) {
@@ -44,7 +44,7 @@ class GetIntegrationInstance {
44
44
 
45
45
  const modules = [];
46
46
  for (const entityId of integrationRecord.entitiesIds) {
47
- const moduleInstance = await this.moduleService.getModuleInstance(
47
+ const moduleInstance = await this.moduleFactory.getModuleInstance(
48
48
  entityId,
49
49
  integrationRecord.userId
50
50
  );
@@ -2,14 +2,14 @@ const { Integration } = require('../integration');
2
2
  const { mapIntegrationClassToIntegrationDTO } = require('../utils/map-integration-dto');
3
3
 
4
4
  class GetIntegrationsForUser {
5
- constructor({ integrationRepository, integrationClasses, moduleService, moduleRepository }) {
5
+ constructor({ integrationRepository, integrationClasses, moduleFactory, moduleRepository }) {
6
6
 
7
7
  /**
8
8
  * @type {import('../integration-repository').IntegrationRepository}
9
9
  */
10
10
  this.integrationRepository = integrationRepository;
11
11
  this.integrationClasses = integrationClasses;
12
- this.moduleService = moduleService;
12
+ this.moduleFactory = moduleFactory;
13
13
  this.moduleRepository = moduleRepository;
14
14
  }
15
15
 
@@ -31,7 +31,7 @@ class GetIntegrationsForUser {
31
31
 
32
32
  const modules = [];
33
33
  for (const entity of entities) {
34
- const moduleInstance = await this.moduleService.getModuleInstance(
34
+ const moduleInstance = await this.moduleFactory.getModuleInstance(
35
35
  entity.id,
36
36
  integrationRecord.userId
37
37
  );
@@ -10,16 +10,16 @@ class UpdateIntegration {
10
10
  * @param {Object} params
11
11
  * @param {import('../integration-repository').IntegrationRepository} params.integrationRepository - Repository for integration data access
12
12
  * @param {Array<import('../integration').Integration>} params.integrationClasses - Array of available integration classes
13
- * @param {import('../module-plugin/module-service').ModuleService} params.moduleService - Service for module instantiation and management
13
+ * @param {import('../../modules/module-factory').ModuleFactory} params.moduleFactory - Service for module instantiation and management
14
14
  */
15
15
  constructor({
16
16
  integrationRepository,
17
17
  integrationClasses,
18
- moduleService,
18
+ moduleFactory,
19
19
  }) {
20
20
  this.integrationRepository = integrationRepository;
21
21
  this.integrationClasses = integrationClasses;
22
- this.moduleService = moduleService;
22
+ this.moduleFactory = moduleFactory;
23
23
  }
24
24
 
25
25
  async execute(integrationId, userId, config) {
@@ -49,7 +49,7 @@ class UpdateIntegration {
49
49
  // 3. Load modules based on entity references
50
50
  const modules = [];
51
51
  for (const entityId of integrationRecord.entitiesIds) {
52
- const moduleInstance = await this.moduleService.getModuleInstance(
52
+ const moduleInstance = await this.moduleFactory.getModuleInstance(
53
53
  entityId,
54
54
  integrationRecord.userId
55
55
  );
@@ -1,6 +1,11 @@
1
1
  const { Module } = require('./module');
2
2
 
3
- class ModuleService {
3
+ /**
4
+ * Acts as a factory for fully-hydrated domain Module instances.
5
+ * Provides methods to retrieve and construct Module objects with their associated
6
+ * entity and definition.
7
+ */
8
+ class ModuleFactory {
4
9
  /**
5
10
  * @param {Object} params - Configuration parameters.
6
11
  * @param {import('./module-repository').ModuleRepository} params.moduleRepository - Repository for module data operations.
@@ -46,4 +51,4 @@ class ModuleService {
46
51
  }
47
52
  }
48
53
 
49
- module.exports = { ModuleService };
54
+ module.exports = { ModuleFactory };
@@ -42,7 +42,7 @@ class ModuleRepository {
42
42
  async findEntitiesByUserId(userId) {
43
43
  const entitiesRecords = await Entity.find(
44
44
  { user: userId },
45
- '-dateCreated -dateUpdated -user -credentials -credential -__t -__v',
45
+ '',
46
46
  { lean: true }
47
47
  ).populate('credential');
48
48
 
@@ -76,6 +76,16 @@ class ModuleRepository {
76
76
  moduleName: e.moduleName,
77
77
  }));
78
78
  }
79
+
80
+ /**
81
+ * Remove the credential reference from an Entity document without loading a full Mongoose instance.
82
+ * Useful when a credential has been revoked/deleted (e.g. via Module.deauthorize).
83
+ * @param {string} entityId
84
+ * @returns {Promise<import('mongoose').UpdateWriteOpResult>}
85
+ */
86
+ async unsetCredential(entityId) {
87
+ return Entity.updateOne({ _id: entityId }, { $unset: { credential: "" } });
88
+ }
79
89
  }
80
90
 
81
91
  module.exports = { ModuleRepository };
package/modules/module.js CHANGED
@@ -3,6 +3,9 @@ const _ = require('lodash');
3
3
  const { flushDebugLog } = require('../logs');
4
4
  const { ModuleConstants } = require('./ModuleConstants');
5
5
 
6
+ // todo: this class should be a Domain class, and the Delegate function is preventing us from
7
+ // doing that, we probably have to get rid of the Delegate class as well as the event based
8
+ // calls since they go against the Domain Driven Design principles (eg. a domain class should not call repository methods or use cases)
6
9
  class Module extends Delegate {
7
10
 
8
11
  //todo: entity should be replaced with actual entity properties
@@ -26,6 +29,13 @@ class Module extends Delegate {
26
29
  this.modelName = this.definition.modelName;
27
30
  this.apiClass = this.definition.API;
28
31
 
32
+ // Repository used for persistence operations related to credentials.
33
+ const { CredentialRepository } = require('../credential/credential-repository');
34
+ this.credentialRepository = new CredentialRepository();
35
+
36
+ // Repository responsible for entity persistence actions
37
+ const { ModuleRepository } = require('./module-repository');
38
+ this.moduleRepository = new ModuleRepository();
29
39
 
30
40
  Object.assign(this, this.definition.requiredAuthMethods);
31
41
 
@@ -125,7 +135,9 @@ class Module extends Delegate {
125
135
  this.apiParamsFromCredential(this.api)
126
136
  );
127
137
  credentialDetails.details.auth_is_valid = true;
128
- await this.updateOrCreateCredential(credentialDetails);
138
+
139
+ const persisted = await this.credentialRepository.upsertCredential(credentialDetails);
140
+ this.credential = persisted;
129
141
  }
130
142
 
131
143
  async receiveNotification(notifier, delegateString, object = null) {
@@ -139,20 +151,42 @@ class Module extends Delegate {
139
151
  }
140
152
 
141
153
  async markCredentialsInvalid() {
142
- if (this.credential) {
143
- this.credential.auth_is_valid = false;
144
- await this.credential.save();
145
- }
154
+ if (!this.credential) return;
155
+
156
+ // Persist flag change through repository – works even when the
157
+ // credential object is a plain JavaScript object (lean query).
158
+ const credentialId = this.credential._id || this.credential.id;
159
+ if (!credentialId) return;
160
+
161
+ await this.credentialRepository.updateAuthenticationStatus(
162
+ credentialId,
163
+ false
164
+ );
165
+
166
+ // Keep the in-memory snapshot consistent so that callers can read the
167
+ // updated state without another fetch.
168
+ this.credential.auth_is_valid = false;
146
169
  }
147
170
 
148
171
  async deauthorize() {
149
172
  this.api = new this.apiClass();
173
+
174
+ // Remove persisted credential (if any)
150
175
  if (this.entity?.credential) {
151
- await this.CredentialModel.deleteOne({
152
- _id: this.entity.credential,
153
- });
176
+ const credentialId =
177
+ this.entity.credential._id || this.entity.credential.id || this.entity.credential;
178
+
179
+ // Delete credential via repository
180
+ await this.credentialRepository.deleteCredentialById(credentialId);
181
+
182
+ // Unset credential reference on the Entity document
183
+ const entityId = this.entity._id || this.entity.id;
184
+ if (entityId) {
185
+ await this.moduleRepository.unsetCredential(entityId);
186
+ }
187
+
188
+ // Keep in-memory snapshot consistent
154
189
  this.entity.credential = undefined;
155
- await this.entity.save();
156
190
  }
157
191
  }
158
192
 
@@ -6,7 +6,7 @@ function mapModuleClassToModuleDTO(moduleInstance) {
6
6
  if (!moduleInstance) return null;
7
7
 
8
8
  return {
9
- id: moduleInstance.entity._id.toString(),
9
+ id: moduleInstance.entity.id,
10
10
  name: moduleInstance.name,
11
11
  userId: moduleInstance.userId,
12
12
  entity: moduleInstance.entity,
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.05c9b19.0",
4
+ "version": "2.0.0--canary.397.1b51778.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.05c9b19.0",
26
- "@friggframework/prettier-config": "2.0.0--canary.397.05c9b19.0",
27
- "@friggframework/test": "2.0.0--canary.397.05c9b19.0",
25
+ "@friggframework/eslint-config": "2.0.0--canary.397.1b51778.0",
26
+ "@friggframework/prettier-config": "2.0.0--canary.397.1b51778.0",
27
+ "@friggframework/test": "2.0.0--canary.397.1b51778.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": "05c9b19a8f1f2d8e7ef49983786c1fd622fa376a"
56
+ "gitHead": "1b51778697149cbb26c6ba4de50e507461663f48"
57
57
  }