@friggframework/core 2.0.0--canary.711.f8b6445.0 → 2.0.0--canary.667.f74cf3b.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/CLAUDE.md +0 -40
- package/application/commands/entity-commands.js +60 -18
- package/application/commands/integration-commands.js +0 -63
- package/database/documentdb-utils.js +1 -4
- package/errors/index.js +0 -4
- package/handlers/WEBHOOKS.md +0 -29
- package/index.js +0 -2
- package/infrastructure/scheduler/eventbridge-scheduler-adapter.js +13 -67
- package/integrations/integration-base.js +0 -29
- package/integrations/integration-router.js +18 -22
- package/integrations/repositories/integration-mapping-repository-documentdb.js +0 -50
- package/integrations/repositories/integration-mapping-repository-interface.js +0 -15
- package/integrations/repositories/integration-mapping-repository-mongo.js +0 -29
- package/integrations/repositories/integration-mapping-repository-postgres.js +0 -30
- package/integrations/repositories/integration-mapping-repository.js +0 -29
- package/integrations/repositories/integration-repository-documentdb.js +0 -32
- package/integrations/repositories/integration-repository-interface.js +0 -30
- package/integrations/repositories/integration-repository-mongo.js +1 -53
- package/integrations/repositories/integration-repository-postgres.js +0 -58
- package/integrations/repositories/process-repository-mongo.js +1 -1
- package/integrations/use-cases/create-integration.js +19 -57
- package/modules/use-cases/create-entity-from-credential.js +105 -0
- package/modules/use-cases/get-entity-options-by-id.js +7 -5
- package/modules/use-cases/get-module.js +29 -8
- package/modules/use-cases/refresh-entity-options.js +7 -5
- package/modules/use-cases/test-module-auth.js +7 -5
- package/modules/utils/find-module-definition.js +23 -0
- package/modules/utils/map-module-dto.js +3 -5
- package/package.json +5 -6
- package/user/tests/doubles/test-user-repository.js +1 -1
- package/user/use-cases/get-user-from-bearer-token.js +2 -7
- package/errors/mapping-already-exists-error.js +0 -14
- package/integrations/use-cases/add-entities-to-integration.js +0 -92
- package/integrations/use-cases/remove-entities-from-integration.js +0 -70
- package/integrations/utils/assert-entity-ids-input.js +0 -42
- package/modules/utils/to-credential-id.js +0 -14
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
const { Module } = require('../module');
|
|
2
|
+
const {
|
|
3
|
+
findModuleDefinitionForEntity,
|
|
4
|
+
} = require('../utils/find-module-definition');
|
|
2
5
|
|
|
3
6
|
class TestModuleAuth {
|
|
4
7
|
/**
|
|
@@ -45,11 +48,10 @@ class TestModuleAuth {
|
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
const entityType = entity.moduleName;
|
|
48
|
-
const moduleDefinition =
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
});
|
|
51
|
+
const moduleDefinition = findModuleDefinitionForEntity(
|
|
52
|
+
this.moduleDefinitions,
|
|
53
|
+
entity
|
|
54
|
+
);
|
|
53
55
|
|
|
54
56
|
if (!moduleDefinition) {
|
|
55
57
|
throw new Error(
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The API module definition a stored entity belongs to.
|
|
3
|
+
*
|
|
4
|
+
* Entities are persisted with `moduleName` set to the definition's
|
|
5
|
+
* `moduleName` (see ProcessAuthorizationCallback.findOrCreateEntity). A
|
|
6
|
+
* definition's legacy `modelName` is accepted as well, for entities written
|
|
7
|
+
* before the two were the same.
|
|
8
|
+
*
|
|
9
|
+
* @param {Array<Object>} moduleDefinitions
|
|
10
|
+
* @param {Object} entity - entity record with a `moduleName`
|
|
11
|
+
* @returns {Object|undefined}
|
|
12
|
+
*/
|
|
13
|
+
function findModuleDefinitionForEntity(moduleDefinitions, entity) {
|
|
14
|
+
const entityType = entity?.moduleName;
|
|
15
|
+
if (!entityType) return undefined;
|
|
16
|
+
return (moduleDefinitions || []).find(
|
|
17
|
+
(def) =>
|
|
18
|
+
def &&
|
|
19
|
+
(def.moduleName === entityType || def.modelName === entityType)
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
module.exports = { findModuleDefinitionForEntity };
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
const { toCredentialId } = require('./to-credential-id');
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* @param {import('../module').Module} moduleInstance
|
|
5
3
|
* Convert a Module domain instance to a plain DTO suitable for JSON responses.
|
|
@@ -12,9 +10,9 @@ function mapModuleClassToModuleDTO(moduleInstance) {
|
|
|
12
10
|
name: moduleInstance.name,
|
|
13
11
|
userId: moduleInstance.userId,
|
|
14
12
|
entity: moduleInstance.entity,
|
|
15
|
-
credentialId:
|
|
16
|
-
type: moduleInstance.getName()
|
|
13
|
+
credentialId: moduleInstance.credential?._id?.toString(),
|
|
14
|
+
type: moduleInstance.getName()
|
|
17
15
|
};
|
|
18
16
|
}
|
|
19
17
|
|
|
20
|
-
module.exports = { mapModuleClassToModuleDTO };
|
|
18
|
+
module.exports = { mapModuleClassToModuleDTO };
|
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@friggframework/core",
|
|
3
3
|
"prettier": "@friggframework/prettier-config",
|
|
4
|
-
"version": "2.0.0--canary.
|
|
4
|
+
"version": "2.0.0--canary.667.f74cf3b.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@aws-sdk/client-apigatewaymanagementapi": "^3.588.0",
|
|
7
7
|
"@aws-sdk/client-kms": "^3.588.0",
|
|
8
8
|
"@aws-sdk/client-lambda": "^3.714.0",
|
|
9
9
|
"@aws-sdk/client-s3": "^3.588.0",
|
|
10
|
-
"@aws-sdk/client-scheduler": "^3.588.0",
|
|
11
10
|
"@aws-sdk/client-sqs": "^3.588.0",
|
|
12
11
|
"@aws-sdk/client-ssm": "^3.588.0",
|
|
13
12
|
"@aws-sdk/s3-request-presigner": "^3.588.0",
|
|
@@ -48,9 +47,9 @@
|
|
|
48
47
|
}
|
|
49
48
|
},
|
|
50
49
|
"devDependencies": {
|
|
51
|
-
"@friggframework/eslint-config": "2.0.0--canary.
|
|
52
|
-
"@friggframework/prettier-config": "2.0.0--canary.
|
|
53
|
-
"@friggframework/test": "2.0.0--canary.
|
|
50
|
+
"@friggframework/eslint-config": "2.0.0--canary.667.f74cf3b.0",
|
|
51
|
+
"@friggframework/prettier-config": "2.0.0--canary.667.f74cf3b.0",
|
|
52
|
+
"@friggframework/test": "2.0.0--canary.667.f74cf3b.0",
|
|
54
53
|
"@prisma/client": "^6.19.3",
|
|
55
54
|
"@types/lodash": "4.17.15",
|
|
56
55
|
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
@@ -90,5 +89,5 @@
|
|
|
90
89
|
"publishConfig": {
|
|
91
90
|
"access": "public"
|
|
92
91
|
},
|
|
93
|
-
"gitHead": "
|
|
92
|
+
"gitHead": "f74cf3b1f04195508cb0ee6d4086b1cb2d70d913"
|
|
94
93
|
}
|
|
@@ -23,7 +23,7 @@ class TestUserRepository {
|
|
|
23
23
|
|
|
24
24
|
async createToken(userId, rawToken, minutes = 120) {
|
|
25
25
|
const token = `token-for-${userId}-for-${minutes}-mins`;
|
|
26
|
-
this.tokens.set(token, {
|
|
26
|
+
this.tokens.set(token, { user: userId, rawToken });
|
|
27
27
|
return token;
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -40,13 +40,8 @@ class GetUserFromBearerToken {
|
|
|
40
40
|
throw Boom.unauthorized('Session Token Not Found');
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
const tokenUserId = sessionToken.userId;
|
|
44
|
-
if (!tokenUserId) {
|
|
45
|
-
throw Boom.unauthorized('Session Token Has No User');
|
|
46
|
-
}
|
|
47
|
-
|
|
48
43
|
if (this.userConfig.primary === 'organization') {
|
|
49
|
-
const organizationUserData = await this.userRepository.findOrganizationUserById(
|
|
44
|
+
const organizationUserData = await this.userRepository.findOrganizationUserById(sessionToken.user);
|
|
50
45
|
|
|
51
46
|
if (!organizationUserData) {
|
|
52
47
|
throw Boom.unauthorized('Organization User Not Found');
|
|
@@ -62,7 +57,7 @@ class GetUserFromBearerToken {
|
|
|
62
57
|
);
|
|
63
58
|
}
|
|
64
59
|
|
|
65
|
-
const individualUserData = await this.userRepository.findIndividualUserById(
|
|
60
|
+
const individualUserData = await this.userRepository.findIndividualUserById(sessionToken.user);
|
|
66
61
|
|
|
67
62
|
if (!individualUserData) {
|
|
68
63
|
throw Boom.unauthorized('Individual User Not Found');
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
const { BaseError } = require('./base-error');
|
|
2
|
-
|
|
3
|
-
class MappingAlreadyExistsError extends BaseError {
|
|
4
|
-
constructor(integrationId, sourceId, ...errorOptions) {
|
|
5
|
-
super(
|
|
6
|
-
`Mapping already exists for integration ${integrationId} and sourceId ${sourceId}`,
|
|
7
|
-
...errorOptions
|
|
8
|
-
);
|
|
9
|
-
this.integrationId = integrationId;
|
|
10
|
-
this.sourceId = sourceId;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
module.exports = { MappingAlreadyExistsError };
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
const {
|
|
2
|
-
assertEntityIdsInput,
|
|
3
|
-
integrationNotFound,
|
|
4
|
-
loadIntegration,
|
|
5
|
-
} = require('../utils/assert-entity-ids-input');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Attaches entities to an existing integration, e.g. when a user grants
|
|
9
|
-
* access to more accounts after the integration was created. Every entity
|
|
10
|
-
* must exist, belong to the integration's user, and be of a module the
|
|
11
|
-
* integration already holds (so hydration can load it). Entities that are
|
|
12
|
-
* already attached are left as they are.
|
|
13
|
-
* @class AddEntitiesToIntegration
|
|
14
|
-
*/
|
|
15
|
-
class AddEntitiesToIntegration {
|
|
16
|
-
/**
|
|
17
|
-
* @param {Object} params
|
|
18
|
-
* @param {import('../repositories/integration-repository-interface').IntegrationRepositoryInterface} params.integrationRepository
|
|
19
|
-
* @param {import('../../modules/repositories/module-repository-interface').ModuleRepositoryInterface} params.moduleRepository
|
|
20
|
-
*/
|
|
21
|
-
constructor({ integrationRepository, moduleRepository }) {
|
|
22
|
-
this.integrationRepository = integrationRepository;
|
|
23
|
-
this.moduleRepository = moduleRepository;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* @param {string} integrationId
|
|
28
|
-
* @param {string[]} entityIds
|
|
29
|
-
* @returns {Promise<Object>} The updated integration record.
|
|
30
|
-
*/
|
|
31
|
-
async execute(integrationId, entityIds) {
|
|
32
|
-
assertEntityIdsInput(integrationId, entityIds);
|
|
33
|
-
|
|
34
|
-
const integration = await loadIntegration(
|
|
35
|
-
this.integrationRepository,
|
|
36
|
-
integrationId
|
|
37
|
-
);
|
|
38
|
-
|
|
39
|
-
const entities = await this.moduleRepository.findEntitiesByIds(
|
|
40
|
-
entityIds
|
|
41
|
-
);
|
|
42
|
-
const foundIds = new Set(entities.map((e) => String(e.id)));
|
|
43
|
-
const missing = entityIds.filter((id) => !foundIds.has(String(id)));
|
|
44
|
-
if (missing.length > 0) {
|
|
45
|
-
const error = new Error(
|
|
46
|
-
`Entities not found: ${missing.join(', ')}`
|
|
47
|
-
);
|
|
48
|
-
error.code = 'ENTITIES_NOT_FOUND';
|
|
49
|
-
throw error;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const foreign = entities.filter(
|
|
53
|
-
(e) => String(e.userId) !== String(integration.userId)
|
|
54
|
-
);
|
|
55
|
-
if (foreign.length > 0) {
|
|
56
|
-
const error = new Error(
|
|
57
|
-
`Entities do not belong to the integration's user: ${foreign
|
|
58
|
-
.map((e) => e.id)
|
|
59
|
-
.join(', ')}`
|
|
60
|
-
);
|
|
61
|
-
error.code = 'ENTITY_USER_MISMATCH';
|
|
62
|
-
throw error;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const attached = await this.moduleRepository.findEntitiesByIds(
|
|
66
|
-
integration.entitiesIds ?? []
|
|
67
|
-
);
|
|
68
|
-
const heldModules = new Set(attached.map((e) => e.moduleName));
|
|
69
|
-
const otherModules = entities.filter(
|
|
70
|
-
(e) => !heldModules.has(e.moduleName)
|
|
71
|
-
);
|
|
72
|
-
if (otherModules.length > 0) {
|
|
73
|
-
const error = new Error(
|
|
74
|
-
`Entities belong to a module the integration does not hold: ${otherModules
|
|
75
|
-
.map((e) => `${e.id} (${e.moduleName})`)
|
|
76
|
-
.join(', ')}`
|
|
77
|
-
);
|
|
78
|
-
error.code = 'ENTITY_MODULE_MISMATCH';
|
|
79
|
-
throw error;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const updated =
|
|
83
|
-
await this.integrationRepository.addEntitiesToIntegration(
|
|
84
|
-
integrationId,
|
|
85
|
-
entityIds
|
|
86
|
-
);
|
|
87
|
-
if (!updated) throw integrationNotFound(integrationId);
|
|
88
|
-
return updated;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
module.exports = { AddEntitiesToIntegration };
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
const {
|
|
2
|
-
assertEntityIdsInput,
|
|
3
|
-
integrationNotFound,
|
|
4
|
-
loadIntegration,
|
|
5
|
-
} = require('../utils/assert-entity-ids-input');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Detaches entities from an integration. Entities that are not attached are
|
|
9
|
-
* ignored, and detached entities are not deleted. Every module the
|
|
10
|
-
* integration holds must keep at least one entity, so the integration can
|
|
11
|
-
* still hydrate each of its modules.
|
|
12
|
-
* @class RemoveEntitiesFromIntegration
|
|
13
|
-
*/
|
|
14
|
-
class RemoveEntitiesFromIntegration {
|
|
15
|
-
/**
|
|
16
|
-
* @param {Object} params
|
|
17
|
-
* @param {import('../repositories/integration-repository-interface').IntegrationRepositoryInterface} params.integrationRepository
|
|
18
|
-
* @param {import('../../modules/repositories/module-repository-interface').ModuleRepositoryInterface} params.moduleRepository
|
|
19
|
-
*/
|
|
20
|
-
constructor({ integrationRepository, moduleRepository }) {
|
|
21
|
-
this.integrationRepository = integrationRepository;
|
|
22
|
-
this.moduleRepository = moduleRepository;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* @param {string} integrationId
|
|
27
|
-
* @param {string[]} entityIds
|
|
28
|
-
* @returns {Promise<Object>} The updated integration record.
|
|
29
|
-
*/
|
|
30
|
-
async execute(integrationId, entityIds) {
|
|
31
|
-
assertEntityIdsInput(integrationId, entityIds);
|
|
32
|
-
|
|
33
|
-
const integration = await loadIntegration(
|
|
34
|
-
this.integrationRepository,
|
|
35
|
-
integrationId
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
const removing = new Set(entityIds.map(String));
|
|
39
|
-
const attached = await this.moduleRepository.findEntitiesByIds(
|
|
40
|
-
integration.entitiesIds ?? []
|
|
41
|
-
);
|
|
42
|
-
const emptied = [...new Set(attached.map((e) => e.moduleName))].filter(
|
|
43
|
-
(moduleName) =>
|
|
44
|
-
!attached.some(
|
|
45
|
-
(e) =>
|
|
46
|
-
e.moduleName === moduleName &&
|
|
47
|
-
!removing.has(String(e.id))
|
|
48
|
-
)
|
|
49
|
-
);
|
|
50
|
-
if (emptied.length > 0) {
|
|
51
|
-
const error = new Error(
|
|
52
|
-
`Removing these entities would leave no entity for module(s): ${emptied.join(
|
|
53
|
-
', '
|
|
54
|
-
)}`
|
|
55
|
-
);
|
|
56
|
-
error.code = 'LAST_ENTITY_OF_MODULE';
|
|
57
|
-
throw error;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const updated =
|
|
61
|
-
await this.integrationRepository.removeEntitiesFromIntegration(
|
|
62
|
-
integrationId,
|
|
63
|
-
entityIds
|
|
64
|
-
);
|
|
65
|
-
if (!updated) throw integrationNotFound(integrationId);
|
|
66
|
-
return updated;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
module.exports = { RemoveEntitiesFromIntegration };
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Throws INVALID_INTEGRATION_DATA unless an integration id and a non-empty
|
|
3
|
-
* list of entity ids are given.
|
|
4
|
-
* @param {string} integrationId
|
|
5
|
-
* @param {string[]} entityIds
|
|
6
|
-
*/
|
|
7
|
-
function assertEntityIdsInput(integrationId, entityIds) {
|
|
8
|
-
if (!integrationId || !Array.isArray(entityIds) || entityIds.length === 0) {
|
|
9
|
-
const error = new Error(
|
|
10
|
-
'integrationId and a non-empty entityIds array are required'
|
|
11
|
-
);
|
|
12
|
-
error.code = 'INVALID_INTEGRATION_DATA';
|
|
13
|
-
throw error;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function integrationNotFound(integrationId) {
|
|
18
|
-
const error = new Error(`Integration ${integrationId} not found`);
|
|
19
|
-
error.code = 'INTEGRATION_NOT_FOUND';
|
|
20
|
-
return error;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Loads an integration, mapping the repositories' "not found" error to
|
|
25
|
-
* INTEGRATION_NOT_FOUND. Any other failure (connection, invalid id) is
|
|
26
|
-
* rethrown unchanged.
|
|
27
|
-
* @param {Object} integrationRepository
|
|
28
|
-
* @param {string} integrationId
|
|
29
|
-
* @returns {Promise<Object>} The integration record.
|
|
30
|
-
*/
|
|
31
|
-
async function loadIntegration(integrationRepository, integrationId) {
|
|
32
|
-
try {
|
|
33
|
-
return await integrationRepository.findIntegrationById(integrationId);
|
|
34
|
-
} catch (error) {
|
|
35
|
-
if (/^Integration with id .* not found$/.test(error?.message)) {
|
|
36
|
-
throw integrationNotFound(integrationId);
|
|
37
|
-
}
|
|
38
|
-
throw error;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
module.exports = { assertEntityIdsInput, integrationNotFound, loadIntegration };
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Resolve the credential id from an entity's or module's `credential`, which
|
|
3
|
-
* repositories return as the decrypted credential object (or a bare id).
|
|
4
|
-
* @param {Object|string|number|null|undefined} credential
|
|
5
|
-
* @returns {string|null}
|
|
6
|
-
*/
|
|
7
|
-
function toCredentialId(credential) {
|
|
8
|
-
if (credential && typeof credential === 'object') {
|
|
9
|
-
return credential.id?.toString() ?? null;
|
|
10
|
-
}
|
|
11
|
-
return credential?.toString() ?? null;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
module.exports = { toCredentialId };
|