@friggframework/core 2.0.0--canary.397.3862908.0 → 2.0.0--canary.398.e2147f7.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/handlers/app-handler-helpers.js +3 -0
- package/handlers/backend-utils.js +34 -35
- package/handlers/routers/auth.js +11 -14
- package/handlers/routers/integration-defined-routers.js +5 -8
- package/handlers/routers/middleware/loadUser.js +15 -0
- package/handlers/routers/middleware/requireLoggedInUser.js +12 -0
- package/handlers/routers/user.js +5 -25
- package/handlers/workers/integration-defined-workers.js +3 -6
- package/index.js +14 -1
- package/integrations/create-frigg-backend.js +31 -0
- package/integrations/index.js +5 -0
- package/integrations/integration-base.js +44 -42
- package/integrations/integration-factory.js +251 -0
- package/integrations/integration-router.js +171 -289
- package/integrations/integration-user.js +144 -0
- package/integrations/options.js +1 -1
- package/integrations/test/integration-base.test.js +6 -6
- package/module-plugin/auther.js +393 -0
- package/module-plugin/entity-manager.js +70 -0
- package/{modules → module-plugin}/index.js +6 -0
- package/module-plugin/module-factory.js +61 -0
- package/{modules → module-plugin}/test/mock-api/api.js +3 -8
- package/{modules → module-plugin}/test/mock-api/definition.js +8 -12
- package/package.json +5 -5
- package/types/integrations/index.d.ts +6 -2
- package/types/module-plugin/index.d.ts +21 -4
- package/credential/credential-repository.js +0 -42
- package/credential/use-cases/get-credential-for-user.js +0 -21
- package/credential/use-cases/update-authentication-status.js +0 -15
- package/handlers/app-definition-loader.js +0 -38
- package/integrations/integration-repository.js +0 -67
- package/integrations/integration.js +0 -233
- package/integrations/tests/doubles/dummy-integration-class.js +0 -11
- package/integrations/tests/doubles/test-integration-repository.js +0 -52
- package/integrations/tests/use-cases/create-integration.test.js +0 -27
- package/integrations/tests/use-cases/delete-integration-for-user.test.js +0 -20
- package/integrations/tests/use-cases/get-integration-for-user.test.js +0 -29
- package/integrations/tests/use-cases/get-integration-instance.test.js +0 -26
- package/integrations/tests/use-cases/get-integrations-for-user.test.js +0 -30
- package/integrations/tests/use-cases/get-possible-integrations.test.js +0 -12
- package/integrations/tests/use-cases/update-integration-messages.test.js +0 -15
- package/integrations/tests/use-cases/update-integration-status.test.js +0 -0
- package/integrations/tests/use-cases/update-integration.test.js +0 -24
- package/integrations/use-cases/create-integration.js +0 -57
- package/integrations/use-cases/delete-integration-for-user.js +0 -54
- package/integrations/use-cases/get-integration-for-user.js +0 -63
- package/integrations/use-cases/get-integration-instance.js +0 -73
- package/integrations/use-cases/get-integrations-for-user.js +0 -63
- package/integrations/use-cases/get-possible-integrations.js +0 -13
- package/integrations/use-cases/index.js +0 -11
- package/integrations/use-cases/update-integration-messages.js +0 -20
- package/integrations/use-cases/update-integration-status.js +0 -12
- package/integrations/use-cases/update-integration.js +0 -81
- package/integrations/utils/map-integration-dto.js +0 -36
- package/modules/module-factory.js +0 -54
- package/modules/module-repository.js +0 -72
- package/modules/module.js +0 -251
- package/modules/tests/doubles/test-module-factory.js +0 -16
- package/modules/tests/doubles/test-module-repository.js +0 -19
- package/modules/use-cases/get-entities-for-user.js +0 -32
- package/modules/use-cases/get-entity-options-by-id.js +0 -58
- package/modules/use-cases/get-entity-options-by-type.js +0 -34
- package/modules/use-cases/get-module-instance-from-type.js +0 -31
- package/modules/use-cases/get-module.js +0 -56
- package/modules/use-cases/refresh-entity-options.js +0 -58
- package/modules/use-cases/test-module-auth.js +0 -54
- package/modules/utils/map-module-dto.js +0 -18
- package/user/tests/doubles/test-user-repository.js +0 -72
- package/user/tests/use-cases/create-individual-user.test.js +0 -24
- package/user/tests/use-cases/create-organization-user.test.js +0 -28
- package/user/tests/use-cases/create-token-for-user-id.test.js +0 -19
- package/user/tests/use-cases/get-user-from-bearer-token.test.js +0 -64
- package/user/tests/use-cases/login-user.test.js +0 -140
- package/user/use-cases/create-individual-user.js +0 -61
- package/user/use-cases/create-organization-user.js +0 -47
- package/user/use-cases/create-token-for-user-id.js +0 -30
- package/user/use-cases/get-user-from-bearer-token.js +0 -77
- package/user/use-cases/login-user.js +0 -122
- package/user/user-repository.js +0 -62
- package/user/user.js +0 -77
- /package/{modules → module-plugin}/ModuleConstants.js +0 -0
- /package/{modules → module-plugin}/credential.js +0 -0
- /package/{modules → module-plugin}/entity.js +0 -0
- /package/{modules → module-plugin}/manager.js +0 -0
- /package/{modules → module-plugin}/requester/api-key.js +0 -0
- /package/{modules → module-plugin}/requester/basic.js +0 -0
- /package/{modules → module-plugin}/requester/oauth-2.js +0 -0
- /package/{modules → module-plugin}/requester/requester.js +0 -0
- /package/{modules → module-plugin}/requester/requester.test.js +0 -0
- /package/{modules → module-plugin}/test/auther.test.js +0 -0
- /package/{modules → module-plugin}/test/mock-api/mocks/hubspot.js +0 -0
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
const { Integration } = require('../integration');
|
|
2
|
-
const { mapIntegrationClassToIntegrationDTO } = require('../utils/map-integration-dto');
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
class UpdateIntegration {
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @class UpdateIntegration
|
|
9
|
-
* @description Use case for updating a single integration by ID and user.
|
|
10
|
-
* @param {Object} params
|
|
11
|
-
* @param {import('../integration-repository').IntegrationRepository} params.integrationRepository - Repository for integration data access
|
|
12
|
-
* @param {Array<import('../integration').Integration>} params.integrationClasses - Array of available integration classes
|
|
13
|
-
* @param {import('../../modules/module-factory').ModuleFactory} params.moduleFactory - Service for module instantiation and management
|
|
14
|
-
*/
|
|
15
|
-
constructor({
|
|
16
|
-
integrationRepository,
|
|
17
|
-
integrationClasses,
|
|
18
|
-
moduleFactory,
|
|
19
|
-
}) {
|
|
20
|
-
this.integrationRepository = integrationRepository;
|
|
21
|
-
this.integrationClasses = integrationClasses;
|
|
22
|
-
this.moduleFactory = moduleFactory;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async execute(integrationId, userId, config) {
|
|
26
|
-
// 1. Get integration record from repository
|
|
27
|
-
const integrationRecord = await this.integrationRepository.findIntegrationById(integrationId);
|
|
28
|
-
|
|
29
|
-
if (!integrationRecord) {
|
|
30
|
-
throw new Error(`No integration found by the ID of ${integrationId}`);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// 2. Get the correct Integration class by type
|
|
34
|
-
const integrationClass = this.integrationClasses.find(
|
|
35
|
-
(integrationClass) => integrationClass.Definition.name === integrationRecord.config.type
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
if (!integrationClass) {
|
|
39
|
-
throw new Error(`No integration class found for type: ${integrationRecord.config.type}`);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (integrationRecord.userId !== userId) {
|
|
43
|
-
throw new Error(
|
|
44
|
-
`Integration ${integrationId} does not belong to User ${userId}`
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
// 3. Load modules based on entity references
|
|
50
|
-
const modules = [];
|
|
51
|
-
for (const entityId of integrationRecord.entitiesIds) {
|
|
52
|
-
const moduleInstance = await this.moduleFactory.getModuleInstance(
|
|
53
|
-
entityId,
|
|
54
|
-
integrationRecord.userId
|
|
55
|
-
);
|
|
56
|
-
modules.push(moduleInstance);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// 4. Create the Integration domain entity with modules
|
|
60
|
-
const integrationInstance = new Integration({
|
|
61
|
-
id: integrationRecord.id,
|
|
62
|
-
userId: integrationRecord.userId,
|
|
63
|
-
entities: integrationRecord.entitiesIds,
|
|
64
|
-
config: integrationRecord.config,
|
|
65
|
-
status: integrationRecord.status,
|
|
66
|
-
version: integrationRecord.version,
|
|
67
|
-
messages: integrationRecord.messages,
|
|
68
|
-
integrationClass: integrationClass,
|
|
69
|
-
modules
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
// 6. Complete async initialization (load dynamic actions, register handlers)
|
|
74
|
-
await integrationInstance.initialize();
|
|
75
|
-
await integrationInstance.send('ON_UPDATE', { config });
|
|
76
|
-
|
|
77
|
-
return mapIntegrationClassToIntegrationDTO(integrationInstance);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
module.exports = { UpdateIntegration };
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @param {import('../integration').Integration} integration
|
|
3
|
-
* Convert an Integration domain instance to a plain DTO suitable for JSON responses.
|
|
4
|
-
*/
|
|
5
|
-
function mapIntegrationClassToIntegrationDTO(integration) {
|
|
6
|
-
if (!integration) return null;
|
|
7
|
-
|
|
8
|
-
return {
|
|
9
|
-
id: integration.id,
|
|
10
|
-
userId: integration.userId,
|
|
11
|
-
entities: integration.entities,
|
|
12
|
-
config: integration.config,
|
|
13
|
-
status: integration.status,
|
|
14
|
-
version: integration.version,
|
|
15
|
-
messages: integration.messages,
|
|
16
|
-
userActions: integration.userActions,
|
|
17
|
-
options: integration.getOptionDetails(),
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const getModulesDefinitionFromIntegrationClasses = (integrationClasses) => {
|
|
23
|
-
return [
|
|
24
|
-
...new Set(
|
|
25
|
-
integrationClasses
|
|
26
|
-
.map((integration) =>
|
|
27
|
-
Object.values(integration.Definition.modules).map(
|
|
28
|
-
(module) => module.definition
|
|
29
|
-
)
|
|
30
|
-
)
|
|
31
|
-
.flat()
|
|
32
|
-
),
|
|
33
|
-
];
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
module.exports = { mapIntegrationClassToIntegrationDTO, getModulesDefinitionFromIntegrationClasses };
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
const { Module } = require('./module');
|
|
2
|
-
|
|
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 {
|
|
9
|
-
/**
|
|
10
|
-
* @param {Object} params - Configuration parameters.
|
|
11
|
-
* @param {import('./module-repository').ModuleRepository} params.moduleRepository - Repository for module data operations.
|
|
12
|
-
* @param {Array<Object>} params.moduleDefinitions - Array of module definitions.
|
|
13
|
-
*/
|
|
14
|
-
constructor({ moduleRepository, moduleDefinitions }) {
|
|
15
|
-
this.moduleRepository = moduleRepository;
|
|
16
|
-
this.moduleDefinitions = moduleDefinitions;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async getModuleInstance(entityId, userId) {
|
|
20
|
-
const entity = await this.moduleRepository.findEntityById(
|
|
21
|
-
entityId,
|
|
22
|
-
userId
|
|
23
|
-
);
|
|
24
|
-
|
|
25
|
-
if (!entity) {
|
|
26
|
-
throw new Error(`Entity ${entityId} not found`);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (entity.userId !== userId) {
|
|
30
|
-
throw new Error(
|
|
31
|
-
`Entity ${entityId} does not belong to user ${userId}`
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const moduleName = entity.moduleName;
|
|
36
|
-
const moduleDefinition = this.moduleDefinitions.find((def) => {
|
|
37
|
-
return moduleName === def.modelName;
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
if (!moduleDefinition) {
|
|
41
|
-
throw new Error(
|
|
42
|
-
`Module definition not found for entity type: ${entityType}`
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return new Module({
|
|
47
|
-
userId,
|
|
48
|
-
entity,
|
|
49
|
-
definition: moduleDefinition,
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
module.exports = { ModuleFactory };
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
const { Entity } = require('./entity');
|
|
2
|
-
|
|
3
|
-
class ModuleRepository {
|
|
4
|
-
async findEntityById(entityId) {
|
|
5
|
-
const entity = await Entity.findById(entityId, undefined, { lean: true }).populate('credential');
|
|
6
|
-
if (!entity) {
|
|
7
|
-
throw new Error(`Entity ${entityId} not found`);
|
|
8
|
-
}
|
|
9
|
-
|
|
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
|
-
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
async findEntitiesByUserId(userId) {
|
|
23
|
-
const entitiesRecords = await Entity.find(
|
|
24
|
-
{ user: userId },
|
|
25
|
-
'',
|
|
26
|
-
{ lean: true }
|
|
27
|
-
).populate('credential');
|
|
28
|
-
|
|
29
|
-
return entitiesRecords.map(e => ({
|
|
30
|
-
id: e._id.toString(),
|
|
31
|
-
accountId: e.accountId,
|
|
32
|
-
credential: e.credential,
|
|
33
|
-
userId: e.user.toString(),
|
|
34
|
-
name: e.name,
|
|
35
|
-
externalId: e.externalId,
|
|
36
|
-
type: e.__t,
|
|
37
|
-
moduleName: e.moduleName,
|
|
38
|
-
}));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async findEntitiesByIds(entitiesIds) {
|
|
42
|
-
const entitiesRecords = await Entity.find(
|
|
43
|
-
{ _id: { $in: entitiesIds } },
|
|
44
|
-
'',
|
|
45
|
-
{ lean: true }
|
|
46
|
-
).populate('credential');
|
|
47
|
-
|
|
48
|
-
return entitiesRecords.map(e => ({
|
|
49
|
-
id: e._id.toString(),
|
|
50
|
-
accountId: e.accountId,
|
|
51
|
-
credential: e.credential,
|
|
52
|
-
userId: e.user.toString(),
|
|
53
|
-
name: e.name,
|
|
54
|
-
externalId: e.externalId,
|
|
55
|
-
type: e.__t,
|
|
56
|
-
moduleName: e.moduleName,
|
|
57
|
-
}));
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Remove the credential reference from an Entity document without loading a full Mongoose instance.
|
|
62
|
-
* Useful when a credential has been revoked/deleted (e.g. via Module.deauthorize).
|
|
63
|
-
* @param {string} entityId
|
|
64
|
-
* @returns {Promise<import('mongoose').UpdateWriteOpResult>}
|
|
65
|
-
*/
|
|
66
|
-
async unsetCredential(entityId) {
|
|
67
|
-
const result = await Entity.updateOne({ _id: entityId }, { $unset: { credential: "" } });
|
|
68
|
-
return result.acknowledged;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
module.exports = { ModuleRepository };
|
package/modules/module.js
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
const { Delegate } = require('../core');
|
|
2
|
-
const _ = require('lodash');
|
|
3
|
-
const { flushDebugLog } = require('../logs');
|
|
4
|
-
const { ModuleConstants } = require('./ModuleConstants');
|
|
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)
|
|
9
|
-
class Module extends Delegate {
|
|
10
|
-
|
|
11
|
-
//todo: entity should be replaced with actual entity properties
|
|
12
|
-
/**
|
|
13
|
-
*
|
|
14
|
-
* @param {Object} params
|
|
15
|
-
* @param {Object} params.definition The definition of the Api Module
|
|
16
|
-
* @param {string} params.userId The user id
|
|
17
|
-
* @param {Object} params.entity The entity record from the database
|
|
18
|
-
*/
|
|
19
|
-
constructor({ definition, userId = null, entity: entityObj = null }) {
|
|
20
|
-
super({ definition, userId, entity: entityObj });
|
|
21
|
-
|
|
22
|
-
this.validateDefinition(definition);
|
|
23
|
-
|
|
24
|
-
this.userId = userId;
|
|
25
|
-
this.entity = entityObj;
|
|
26
|
-
this.credential = entityObj?.credential;
|
|
27
|
-
this.definition = definition;
|
|
28
|
-
this.name = this.definition.moduleName;
|
|
29
|
-
this.modelName = this.definition.modelName;
|
|
30
|
-
this.apiClass = this.definition.API;
|
|
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();
|
|
39
|
-
|
|
40
|
-
Object.assign(this, this.definition.requiredAuthMethods);
|
|
41
|
-
|
|
42
|
-
const apiParams = {
|
|
43
|
-
...this.definition.env,
|
|
44
|
-
delegate: this,
|
|
45
|
-
...this.apiParamsFromCredential(this.credential),
|
|
46
|
-
...this.apiParamsFromEntity(this.entity),
|
|
47
|
-
};
|
|
48
|
-
this.api = new this.apiClass(apiParams);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
getName() {
|
|
52
|
-
return this.name;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
getEntityOptions() {
|
|
56
|
-
return this.definition.getEntityOptions()
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async refreshEntityOptions(options) {
|
|
60
|
-
await this.definition.refreshEntityOptions(options);
|
|
61
|
-
return this.getEntityOptions();
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
apiParamsFromCredential(credential) {
|
|
65
|
-
return _.pick(credential, ...this.apiPropertiesToPersist?.credential);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
apiParamsFromEntity(entity) {
|
|
69
|
-
return _.pick(entity, ...this.apiPropertiesToPersist?.entity);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async validateAuthorizationRequirements() {
|
|
73
|
-
const requirements = await this.getAuthorizationRequirements();
|
|
74
|
-
let valid = true;
|
|
75
|
-
if (
|
|
76
|
-
['oauth1', 'oauth2'].includes(requirements.type) &&
|
|
77
|
-
!requirements.url
|
|
78
|
-
) {
|
|
79
|
-
valid = false;
|
|
80
|
-
}
|
|
81
|
-
return valid;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async getAuthorizationRequirements(params) {
|
|
85
|
-
return this.api.getAuthorizationRequirements();
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async testAuth() {
|
|
89
|
-
let validAuth = false;
|
|
90
|
-
try {
|
|
91
|
-
if (await this.testAuthRequest(this.api)) validAuth = true;
|
|
92
|
-
} catch (e) {
|
|
93
|
-
flushDebugLog(e);
|
|
94
|
-
}
|
|
95
|
-
return validAuth;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
async processAuthorizationCallback(params) {
|
|
99
|
-
let tokenResponse;
|
|
100
|
-
if (this.apiClass.requesterType === ModuleConstants.authType.oauth2) {
|
|
101
|
-
tokenResponse = await this.getToken(this.api, params);
|
|
102
|
-
} else {
|
|
103
|
-
tokenResponse = await this.setAuthParams(this.api, params);
|
|
104
|
-
await this.onTokenUpdate();
|
|
105
|
-
}
|
|
106
|
-
const authRes = await this.testAuth();
|
|
107
|
-
if (!authRes) {
|
|
108
|
-
throw new Error('Authorization failed');
|
|
109
|
-
}
|
|
110
|
-
const entityDetails = await this.getEntityDetails(
|
|
111
|
-
this.api,
|
|
112
|
-
params,
|
|
113
|
-
tokenResponse,
|
|
114
|
-
this.userId
|
|
115
|
-
);
|
|
116
|
-
Object.assign(
|
|
117
|
-
entityDetails.details,
|
|
118
|
-
this.apiParamsFromEntity(this.api)
|
|
119
|
-
);
|
|
120
|
-
await this.findOrCreateEntity(entityDetails);
|
|
121
|
-
return {
|
|
122
|
-
credential_id: this.credential.id,
|
|
123
|
-
entity_id: this.entity.id,
|
|
124
|
-
type: this.getName(),
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
async onTokenUpdate() {
|
|
129
|
-
const credentialDetails = await this.getCredentialDetails(
|
|
130
|
-
this.api,
|
|
131
|
-
this.userId
|
|
132
|
-
);
|
|
133
|
-
Object.assign(
|
|
134
|
-
credentialDetails.details,
|
|
135
|
-
this.apiParamsFromCredential(this.api)
|
|
136
|
-
);
|
|
137
|
-
credentialDetails.details.auth_is_valid = true;
|
|
138
|
-
|
|
139
|
-
const persisted = await this.credentialRepository.upsertCredential(credentialDetails);
|
|
140
|
-
this.credential = persisted;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
async receiveNotification(notifier, delegateString, object = null) {
|
|
144
|
-
if (delegateString === this.api.DLGT_TOKEN_UPDATE) {
|
|
145
|
-
await this.onTokenUpdate();
|
|
146
|
-
} else if (delegateString === this.api.DLGT_TOKEN_DEAUTHORIZED) {
|
|
147
|
-
await this.deauthorize();
|
|
148
|
-
} else if (delegateString === this.api.DLGT_INVALID_AUTH) {
|
|
149
|
-
await this.markCredentialsInvalid();
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
async markCredentialsInvalid() {
|
|
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;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
async deauthorize() {
|
|
172
|
-
this.api = new this.apiClass();
|
|
173
|
-
|
|
174
|
-
// Remove persisted credential (if any)
|
|
175
|
-
if (this.entity?.credential) {
|
|
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
|
|
189
|
-
this.entity.credential = undefined;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// todo: check if all these props are still up to date
|
|
194
|
-
validateDefinition(definition) {
|
|
195
|
-
if (!definition) {
|
|
196
|
-
throw new Error('Module definition is required');
|
|
197
|
-
}
|
|
198
|
-
if (!definition.moduleName) {
|
|
199
|
-
throw new Error('Module definition requires moduleName');
|
|
200
|
-
}
|
|
201
|
-
if (!definition.API) {
|
|
202
|
-
throw new Error('Module definition requires API class');
|
|
203
|
-
}
|
|
204
|
-
if (!definition.requiredAuthMethods) {
|
|
205
|
-
throw new Error('Module definition requires requiredAuthMethods');
|
|
206
|
-
} else {
|
|
207
|
-
if (
|
|
208
|
-
definition.API.requesterType ===
|
|
209
|
-
ModuleConstants.authType.oauth2 &&
|
|
210
|
-
!definition.requiredAuthMethods.getToken
|
|
211
|
-
) {
|
|
212
|
-
throw new Error(
|
|
213
|
-
'Module definition requires requiredAuthMethods.getToken'
|
|
214
|
-
);
|
|
215
|
-
}
|
|
216
|
-
if (!definition.requiredAuthMethods.getEntityDetails) {
|
|
217
|
-
throw new Error(
|
|
218
|
-
'Module definition requires requiredAuthMethods.getEntityDetails'
|
|
219
|
-
);
|
|
220
|
-
}
|
|
221
|
-
if (!definition.requiredAuthMethods.getCredentialDetails) {
|
|
222
|
-
throw new Error(
|
|
223
|
-
'Module definition requires requiredAuthMethods.getCredentialDetails'
|
|
224
|
-
);
|
|
225
|
-
}
|
|
226
|
-
if (!definition.requiredAuthMethods.apiPropertiesToPersist) {
|
|
227
|
-
throw new Error(
|
|
228
|
-
'Module definition requires requiredAuthMethods.apiPropertiesToPersist'
|
|
229
|
-
);
|
|
230
|
-
} else if (definition.Credential) {
|
|
231
|
-
for (const prop of definition.requiredAuthMethods
|
|
232
|
-
.apiPropertiesToPersist?.credential) {
|
|
233
|
-
if (
|
|
234
|
-
!definition.Credential.schema.paths.hasOwnProperty(prop)
|
|
235
|
-
) {
|
|
236
|
-
throw new Error(
|
|
237
|
-
`Module definition requires Credential schema to have property ${prop}`
|
|
238
|
-
);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
if (!definition.requiredAuthMethods.testAuthRequest) {
|
|
243
|
-
throw new Error(
|
|
244
|
-
'Module definition requires requiredAuthMethods.testAuth'
|
|
245
|
-
);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
module.exports = { Module };
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
class TestModuleFactory {
|
|
2
|
-
constructor() { }
|
|
3
|
-
|
|
4
|
-
async getModuleInstance(entityId, userId) {
|
|
5
|
-
// return minimal stub module with getName and api property
|
|
6
|
-
return {
|
|
7
|
-
getName() { return 'stubModule'; },
|
|
8
|
-
api: {},
|
|
9
|
-
entityId,
|
|
10
|
-
userId,
|
|
11
|
-
testAuth: async () => true,
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
module.exports = { TestModuleFactory };
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
class TestModuleRepository {
|
|
2
|
-
constructor() {
|
|
3
|
-
this.entities = new Map();
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
addEntity(entity) {
|
|
7
|
-
this.entities.set(entity.id, entity);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
async findEntityById(id) {
|
|
11
|
-
return this.entities.get(id);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async findEntitiesByIds(ids) {
|
|
15
|
-
return ids.map((id) => this.entities.get(id));
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
module.exports = { TestModuleRepository };
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
const { Module } = require('../module');
|
|
2
|
-
const { mapModuleClassToModuleDTO } = require('../utils/map-module-dto');
|
|
3
|
-
|
|
4
|
-
class GetEntitiesForUser {
|
|
5
|
-
constructor({ moduleRepository, moduleDefinitions }) {
|
|
6
|
-
this.moduleRepository = moduleRepository;
|
|
7
|
-
|
|
8
|
-
this.definitionMap = new Map();
|
|
9
|
-
for (const definition of moduleDefinitions) {
|
|
10
|
-
this.definitionMap.set(definition.modelName, definition);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async execute(userId) {
|
|
15
|
-
const entities = await this.moduleRepository.findEntitiesByUserId(
|
|
16
|
-
userId
|
|
17
|
-
);
|
|
18
|
-
|
|
19
|
-
return entities.map((entity) => {
|
|
20
|
-
const definition = this.definitionMap.get(entity.moduleName);
|
|
21
|
-
|
|
22
|
-
const moduleInstance = new Module({
|
|
23
|
-
userId,
|
|
24
|
-
definition: definition,
|
|
25
|
-
entity: entity,
|
|
26
|
-
});
|
|
27
|
-
return mapModuleClassToModuleDTO(moduleInstance);
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
module.exports = { GetEntitiesForUser };
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
const { Module } = require('../module');
|
|
2
|
-
|
|
3
|
-
class GetEntityOptionsById {
|
|
4
|
-
/**
|
|
5
|
-
* @param {Object} params
|
|
6
|
-
* @param {import('../module-repository').ModuleRepository} params.moduleRepository
|
|
7
|
-
* @param {} params.moduleDefinitions
|
|
8
|
-
*/
|
|
9
|
-
constructor({ moduleRepository, moduleDefinitions }) {
|
|
10
|
-
this.moduleRepository = moduleRepository;
|
|
11
|
-
this.moduleDefinitions = moduleDefinitions;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Retrieve a Module instance for a given user and entity/module type.
|
|
16
|
-
* @param {string} userId
|
|
17
|
-
* @param {string} entityId
|
|
18
|
-
*/
|
|
19
|
-
async execute(entityId, userId) {
|
|
20
|
-
const entity = await this.moduleRepository.findEntityById(
|
|
21
|
-
entityId,
|
|
22
|
-
userId
|
|
23
|
-
);
|
|
24
|
-
|
|
25
|
-
if (!entity) {
|
|
26
|
-
throw new Error(`Entity ${entityId} not found`);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (entity.userId !== userId) {
|
|
30
|
-
throw new Error(
|
|
31
|
-
`Entity ${entityId} does not belong to user ${userId}`
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const entityType = entity.type;
|
|
36
|
-
const moduleDefinition = this.moduleDefinitions.find((def) => {
|
|
37
|
-
const modelName = Module.getEntityModelFromDefinition(def).modelName;
|
|
38
|
-
return entityType === modelName;
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
if (!moduleDefinition) {
|
|
42
|
-
throw new Error(
|
|
43
|
-
`Module definition not found for entity type: ${entityType}`
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const module = new Module({
|
|
48
|
-
userId,
|
|
49
|
-
entity,
|
|
50
|
-
definition: moduleDefinition,
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
const entityOptions = await module.getEntityOptions();
|
|
54
|
-
return entityOptions;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
module.exports = { GetEntityOptionsById };
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
const { Module } = require('../module');
|
|
2
|
-
|
|
3
|
-
class GetEntityOptionsByType {
|
|
4
|
-
/**
|
|
5
|
-
* @param {Object} params
|
|
6
|
-
* @param {} params.moduleDefinitions
|
|
7
|
-
*/
|
|
8
|
-
constructor({ moduleDefinitions }) {
|
|
9
|
-
this.moduleDefinitions = moduleDefinitions;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Retrieve a Module instance for a given user and entity/module type.
|
|
14
|
-
* @param {string} userId
|
|
15
|
-
* @param {string} type – human-readable module/entity type (e.g. "Hubspot")
|
|
16
|
-
*/
|
|
17
|
-
async execute(userId, type) {
|
|
18
|
-
const moduleDefinition = this.moduleDefinitions.find(
|
|
19
|
-
(def) => def.getName() === type
|
|
20
|
-
);
|
|
21
|
-
if (!moduleDefinition) {
|
|
22
|
-
throw new Error(`Module definition not found for type: ${type}`);
|
|
23
|
-
}
|
|
24
|
-
const moduleInstance = new Module({
|
|
25
|
-
userId,
|
|
26
|
-
definition: moduleDefinition,
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
const entityOptions = await moduleInstance.getEntityOptions();
|
|
30
|
-
return entityOptions;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
module.exports = { GetEntityOptionsByType };
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
const { Module } = require('../module');
|
|
2
|
-
|
|
3
|
-
class GetModuleInstanceFromType {
|
|
4
|
-
/**
|
|
5
|
-
* @param {Object} params
|
|
6
|
-
* @param {} params.moduleDefinitions
|
|
7
|
-
*/
|
|
8
|
-
constructor({ moduleDefinitions }) {
|
|
9
|
-
this.moduleDefinitions = moduleDefinitions;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Retrieve a Module instance for a given user and entity/module type.
|
|
14
|
-
* @param {string} userId
|
|
15
|
-
* @param {string} type – human-readable module/entity type (e.g. "Hubspot")
|
|
16
|
-
*/
|
|
17
|
-
async execute(userId, type) {
|
|
18
|
-
const moduleDefinition = this.moduleDefinitions.find(
|
|
19
|
-
(def) => def.getName() === type
|
|
20
|
-
);
|
|
21
|
-
if (!moduleDefinition) {
|
|
22
|
-
throw new Error(`Module definition not found for type: ${type}`);
|
|
23
|
-
}
|
|
24
|
-
return new Module({
|
|
25
|
-
userId,
|
|
26
|
-
definition: moduleDefinition,
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
module.exports = { GetModuleInstanceFromType };
|