@friggframework/core 2.0.0--canary.710.91495fa.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 +1 -41
- package/application/commands/README.md +11 -17
- package/application/commands/entity-commands.js +60 -18
- package/application/commands/integration-commands.js +0 -63
- package/database/documentdb-utils.js +1 -4
- package/database/encryption/README.md +3 -3
- package/database/encryption/encryption-schema-registry.js +7 -27
- 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/jest-setup.js +2 -13
- package/jest-teardown.js +2 -5
- package/jest.config.js +0 -4
- 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 -7
- package/queues/queuer-util.js +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/jest.db.config.js +0 -23
- package/modules/utils/to-credential-id.js +0 -14
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
const Boom = require('@hapi/boom');
|
|
2
|
+
const { Module } = require('../module');
|
|
3
|
+
|
|
4
|
+
const sameId = (a, b) =>
|
|
5
|
+
a !== undefined &&
|
|
6
|
+
a !== null &&
|
|
7
|
+
b !== undefined &&
|
|
8
|
+
b !== null &&
|
|
9
|
+
String(a) === String(b);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Creates (or finds) the entity for a credential the caller already holds:
|
|
13
|
+
* the v1 `POST /api/entity`.
|
|
14
|
+
*
|
|
15
|
+
* The module is built with the stored credential loaded, so the API can call
|
|
16
|
+
* the provider and a token refresh during `getEntityDetails` updates that
|
|
17
|
+
* credential. Persistence reuses `ProcessAuthorizationCallback.findOrCreateEntity`,
|
|
18
|
+
* the same find-or-create (and credential repointing) the authorization flow
|
|
19
|
+
* uses, so the two paths cannot drift.
|
|
20
|
+
*/
|
|
21
|
+
class CreateEntityFromCredential {
|
|
22
|
+
/**
|
|
23
|
+
* @param {Object} params
|
|
24
|
+
* @param {import('./process-authorization-callback').ProcessAuthorizationCallback} params.processAuthorizationCallback
|
|
25
|
+
* @param {Object} params.credentialRepository
|
|
26
|
+
* @param {Array<Object>} params.moduleDefinitions
|
|
27
|
+
* @param {Function} [params.createModule] - builds a Module; injectable for tests
|
|
28
|
+
*/
|
|
29
|
+
constructor({
|
|
30
|
+
processAuthorizationCallback,
|
|
31
|
+
credentialRepository,
|
|
32
|
+
moduleDefinitions,
|
|
33
|
+
createModule,
|
|
34
|
+
}) {
|
|
35
|
+
this.processAuthorizationCallback = processAuthorizationCallback;
|
|
36
|
+
this.credentialRepository = credentialRepository;
|
|
37
|
+
this.moduleDefinitions = moduleDefinitions || [];
|
|
38
|
+
this.createModule = createModule || ((params) => new Module(params));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @param {Object} params
|
|
43
|
+
* @param {Object} params.user - authenticated User
|
|
44
|
+
* @param {string} params.entityType
|
|
45
|
+
* @param {string} params.credentialId
|
|
46
|
+
* @returns {Promise<Object>} the persisted entity
|
|
47
|
+
*/
|
|
48
|
+
async execute({ user, entityType, credentialId }) {
|
|
49
|
+
const userId = user?.getId?.();
|
|
50
|
+
if (userId === undefined || userId === null) {
|
|
51
|
+
throw Boom.unauthorized('No authenticated user');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const definition = this.moduleDefinitions.find(
|
|
55
|
+
(def) => def && def.moduleName === entityType
|
|
56
|
+
);
|
|
57
|
+
if (!definition) {
|
|
58
|
+
throw Boom.badRequest(`Unknown entity type ${entityType}`, {
|
|
59
|
+
code: 'ENTITY_TYPE_NOT_FOUND',
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const credential =
|
|
64
|
+
await this.credentialRepository.findCredentialById(credentialId);
|
|
65
|
+
// A missing credential and someone else's get the same answer, so
|
|
66
|
+
// credential ids cannot be probed.
|
|
67
|
+
if (!credential || !sameId(credential.userId, userId)) {
|
|
68
|
+
throw Boom.badRequest('Invalid credential ID', {
|
|
69
|
+
code: 'INVALID_CREDENTIAL',
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// The repository returns the tokens flat; a Module reads them from
|
|
74
|
+
// `credential.data`.
|
|
75
|
+
const { id, userId: ownerId, externalId, authIsValid, ...data } =
|
|
76
|
+
credential;
|
|
77
|
+
const module = this.createModule({
|
|
78
|
+
userId,
|
|
79
|
+
definition,
|
|
80
|
+
entity: {
|
|
81
|
+
credential: { id, userId: ownerId, externalId, authIsValid, data },
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const entityDetails =
|
|
86
|
+
await definition.requiredAuthMethods.getEntityDetails(
|
|
87
|
+
module.api,
|
|
88
|
+
null,
|
|
89
|
+
null,
|
|
90
|
+
userId
|
|
91
|
+
);
|
|
92
|
+
Object.assign(
|
|
93
|
+
entityDetails.details,
|
|
94
|
+
module.apiParamsFromEntity(module.api)
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
return this.processAuthorizationCallback.findOrCreateEntity(
|
|
98
|
+
entityDetails,
|
|
99
|
+
definition.moduleName,
|
|
100
|
+
credential.id
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
module.exports = { CreateEntityFromCredential };
|
|
@@ -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 GetEntityOptionsById {
|
|
4
7
|
/**
|
|
@@ -45,11 +48,10 @@ class GetEntityOptionsById {
|
|
|
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(
|
|
@@ -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 GetModule {
|
|
4
7
|
constructor({ moduleRepository, moduleDefinitions }) {
|
|
@@ -42,10 +45,10 @@ class GetModule {
|
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
const entityType = entity.moduleName;
|
|
45
|
-
const moduleDefinition =
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
48
|
+
const moduleDefinition = findModuleDefinitionForEntity(
|
|
49
|
+
this.moduleDefinitions,
|
|
50
|
+
entity
|
|
51
|
+
);
|
|
49
52
|
|
|
50
53
|
if (!moduleDefinition) {
|
|
51
54
|
throw new Error(
|
|
@@ -59,15 +62,33 @@ class GetModule {
|
|
|
59
62
|
definition: moduleDefinition,
|
|
60
63
|
});
|
|
61
64
|
|
|
62
|
-
//
|
|
65
|
+
// The credential itself (tokens, API keys) never leaves the server:
|
|
66
|
+
// the response names it and says whether it still works, as the v2
|
|
67
|
+
// entity DTO does.
|
|
68
|
+
const credential = module.credential;
|
|
69
|
+
const credentialId =
|
|
70
|
+
credential && typeof credential === 'object'
|
|
71
|
+
? credential.id
|
|
72
|
+
: credential;
|
|
73
|
+
const ownerId = module.entity.userId ?? module.entity.user;
|
|
63
74
|
return {
|
|
64
75
|
id: module.entity.id,
|
|
65
76
|
name: module.entity.name,
|
|
66
77
|
moduleName: module.entity.moduleName,
|
|
67
|
-
|
|
78
|
+
credentialId:
|
|
79
|
+
credentialId === undefined || credentialId === null
|
|
80
|
+
? null
|
|
81
|
+
: String(credentialId),
|
|
82
|
+
authIsValid:
|
|
83
|
+
typeof credential?.authIsValid === 'boolean'
|
|
84
|
+
? credential.authIsValid
|
|
85
|
+
: null,
|
|
68
86
|
externalId: module.entity.externalId,
|
|
69
|
-
userId:
|
|
70
|
-
|
|
87
|
+
userId:
|
|
88
|
+
ownerId === undefined || ownerId === null
|
|
89
|
+
? null
|
|
90
|
+
: String(ownerId),
|
|
91
|
+
};
|
|
71
92
|
}
|
|
72
93
|
}
|
|
73
94
|
|
|
@@ -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 RefreshEntityOptions {
|
|
4
7
|
/**
|
|
@@ -46,11 +49,10 @@ class RefreshEntityOptions {
|
|
|
46
49
|
}
|
|
47
50
|
|
|
48
51
|
const entityType = entity.moduleName;
|
|
49
|
-
const moduleDefinition =
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
});
|
|
52
|
+
const moduleDefinition = findModuleDefinitionForEntity(
|
|
53
|
+
this.moduleDefinitions,
|
|
54
|
+
entity
|
|
55
|
+
);
|
|
54
56
|
|
|
55
57
|
if (!moduleDefinition) {
|
|
56
58
|
throw new Error(
|
|
@@ -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",
|
|
@@ -68,7 +67,6 @@
|
|
|
68
67
|
"scripts": {
|
|
69
68
|
"lint:fix": "prettier --write --loglevel error . && eslint . --fix",
|
|
70
69
|
"test": "jest --passWithNoTests # TODO",
|
|
71
|
-
"test:db": "jest --config jest.db.config.js --runInBand",
|
|
72
70
|
"prisma:generate:mongo": "npx prisma generate --schema ./prisma-mongodb/schema.prisma",
|
|
73
71
|
"prisma:generate:postgres": "npx prisma generate --schema ./prisma-postgresql/schema.prisma",
|
|
74
72
|
"prisma:generate": "npm run prisma:generate:mongo && npm run prisma:generate:postgres",
|
|
@@ -91,5 +89,5 @@
|
|
|
91
89
|
"publishConfig": {
|
|
92
90
|
"access": "public"
|
|
93
91
|
},
|
|
94
|
-
"gitHead": "
|
|
92
|
+
"gitHead": "f74cf3b1f04195508cb0ee6d4086b1cb2d70d913"
|
|
95
93
|
}
|
package/queues/queuer-util.js
CHANGED
|
@@ -97,17 +97,16 @@ const QueuerUtil = {
|
|
|
97
97
|
Id: uuid(),
|
|
98
98
|
MessageBody: JSON.stringify(entry),
|
|
99
99
|
});
|
|
100
|
-
// Sends 10, then
|
|
101
|
-
// array: clearing the shared buffer in place would also empty the
|
|
102
|
-
// Entries of a command that was already sent.
|
|
100
|
+
// Sends 10, then purges the buffer
|
|
103
101
|
if (buffer.length === batchSize) {
|
|
104
|
-
const batch = buffer.splice(0, buffer.length);
|
|
105
102
|
const command = new SendMessageBatchCommand({
|
|
106
|
-
Entries:
|
|
103
|
+
Entries: buffer,
|
|
107
104
|
QueueUrl: queueUrl,
|
|
108
105
|
});
|
|
109
106
|
const result = await sqs.send(command);
|
|
110
|
-
inspectBatchResult(result, queueUrl,
|
|
107
|
+
inspectBatchResult(result, queueUrl, buffer);
|
|
108
|
+
// Purge the buffer
|
|
109
|
+
buffer.splice(0, buffer.length);
|
|
111
110
|
}
|
|
112
111
|
}
|
|
113
112
|
|
|
@@ -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 };
|
package/jest.db.config.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Database integration suites (`*.db.test.js`).
|
|
3
|
-
*
|
|
4
|
-
* These talk to real databases instead of mocking Prisma, so they are kept
|
|
5
|
-
* out of the default `npm test` run and executed with `npm run test:db`.
|
|
6
|
-
* They need generated Prisma clients (`npm run prisma:generate`) and:
|
|
7
|
-
* - a MongoDB replica set (Prisma requires one for MongoDB writes):
|
|
8
|
-
* FRIGG_TEST_MONGODB_URL, default
|
|
9
|
-
* mongodb://localhost:27017/frigg?replicaSet=rs0
|
|
10
|
-
* - PostgreSQL with the prisma-postgresql migrations applied:
|
|
11
|
-
* FRIGG_TEST_POSTGRES_URL, default
|
|
12
|
-
* postgresql://postgres:postgres@localhost:5432/frigg?schema=public
|
|
13
|
-
* The repository parity suites skip themselves unless their own URLs are
|
|
14
|
-
* set (INTEGRATION_ENTITIES_PARITY_* / QUERY_MAPPINGS_PARITY_*).
|
|
15
|
-
*/
|
|
16
|
-
const base = require('./jest.config');
|
|
17
|
-
|
|
18
|
-
module.exports = {
|
|
19
|
-
...base,
|
|
20
|
-
coverageThreshold: undefined,
|
|
21
|
-
testMatch: ['**/*.db.test.js'],
|
|
22
|
-
testPathIgnorePatterns: ['/node_modules/'],
|
|
23
|
-
};
|
|
@@ -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 };
|