@friggframework/core 2.0.0-next.117 → 2.0.0-next.119
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 +40 -0
- package/application/commands/entity-commands.js +18 -60
- package/application/commands/integration-commands.js +63 -0
- package/integrations/repositories/integration-repository-documentdb.js +32 -0
- package/integrations/repositories/integration-repository-interface.js +30 -0
- package/integrations/repositories/integration-repository-mongo.js +52 -0
- package/integrations/repositories/integration-repository-postgres.js +58 -0
- package/integrations/use-cases/add-entities-to-integration.js +92 -0
- package/integrations/use-cases/create-integration.js +57 -19
- package/integrations/use-cases/remove-entities-from-integration.js +70 -0
- package/integrations/utils/assert-entity-ids-input.js +42 -0
- package/modules/utils/map-module-dto.js +5 -3
- package/modules/utils/to-credential-id.js +14 -0
- package/package.json +5 -5
package/CLAUDE.md
CHANGED
|
@@ -206,11 +206,51 @@ packages/core/
|
|
|
206
206
|
- `delete-integration-for-user.js` - Remove integration
|
|
207
207
|
- `get-integration-instance.js` - Load integration with modules
|
|
208
208
|
- `load-integration-context.js` - Full integration context loading
|
|
209
|
+
- `add-entities-to-integration.js` / `remove-entities-from-integration.js` - Attach or detach entities on an existing integration
|
|
209
210
|
|
|
210
211
|
**Repositories**:
|
|
211
212
|
- `integration-repository-factory.js` - Creates database-specific repositories
|
|
212
213
|
- `integration-repository-mongo.js` - MongoDB implementation
|
|
213
214
|
- `integration-repository-postgres.js` - PostgreSQL implementation
|
|
215
|
+
- `addEntitiesToIntegration(integrationId, entityIds)` /
|
|
216
|
+
`removeEntitiesFromIntegration(integrationId, entityIds)` change the entity
|
|
217
|
+
set of an existing integration (all three adapters; `null` for an unknown
|
|
218
|
+
integration). Reach them through the `addEntitiesToIntegration` /
|
|
219
|
+
`removeEntitiesFromIntegration` commands. The add use case
|
|
220
|
+
(`add-entities-to-integration.js`) rejects unknown entities
|
|
221
|
+
(`ENTITIES_NOT_FOUND`, 404), entities of another user
|
|
222
|
+
(`ENTITY_USER_MISMATCH`, 403) and entities of a module the integration does
|
|
223
|
+
not already hold (`ENTITY_MODULE_MISMATCH`, 400). The remove use case
|
|
224
|
+
(`remove-entities-from-integration.js`) refuses to leave a module with no
|
|
225
|
+
entity (`LAST_ENTITY_OF_MODULE`, 409). Re-adding an attached entity or
|
|
226
|
+
removing a detached one changes nothing; a detached entity is not deleted.
|
|
227
|
+
`integration-repository-entities-parity.test.js` runs every adapter against
|
|
228
|
+
real databases when `INTEGRATION_ENTITIES_PARITY_MONGO_URL` /
|
|
229
|
+
`INTEGRATION_ENTITIES_PARITY_POSTGRES_URL` are set. The DocumentDB adapter
|
|
230
|
+
writes only `Integration.entityIds` (as its `createIntegration` does), not
|
|
231
|
+
`Entity.integrationIds`.
|
|
232
|
+
**One credential, many accounts**: when one credential covers several
|
|
233
|
+
provider accounts, give each account its own entity (externalId = account
|
|
234
|
+
id) and attach them all to one integration; webhooks then route with
|
|
235
|
+
`findIntegrationByEntityExternalId`. Traps:
|
|
236
|
+
(1) `IntegrationBase` keys modules by module name, so `this.<moduleKey>`
|
|
237
|
+
holds only one entity's module, and on the queue/webhook path
|
|
238
|
+
(`get-integration-instance.js`) `this.entities` holds entity **ids**, not
|
|
239
|
+
records. For per-account work (a webhook for one account, teardown of every
|
|
240
|
+
account in `onDelete`), load
|
|
241
|
+
`this.commands.loadIntegrationContextById(this.id)` and use
|
|
242
|
+
`context.modules`: one module per entity, each with its record on
|
|
243
|
+
`module.entity` (pick by `module.entity.externalId`).
|
|
244
|
+
(2) `findIntegrationByEntityExternalId` counts every entity with that
|
|
245
|
+
externalId, attached or not, across users: reuse an existing entity
|
|
246
|
+
(`findEntity`) when an account comes back, or delete it when it is revoked,
|
|
247
|
+
never create a second one.
|
|
248
|
+
(3) `createIntegration` reuses an existing integration of the same user
|
|
249
|
+
and type whose entities include all requested ones (an exact match wins),
|
|
250
|
+
so a reconnect that sends the original entities finds the integration that
|
|
251
|
+
now holds more.
|
|
252
|
+
(4) Every module's delegate is the integration: an auth failure on any
|
|
253
|
+
account marks the shared credential invalid and the integration ERROR.
|
|
214
254
|
- `integration-mapping-repository-*.js` - Mapping data persistence.
|
|
215
255
|
`queryMappings(integrationId, { where, orderBy, skip, take, omit })`
|
|
216
256
|
returns `{ mappings, total }`: one filtered, ordered page of an
|
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
const {
|
|
2
2
|
createModuleRepository,
|
|
3
3
|
} = require('../../modules/repositories/module-repository-factory');
|
|
4
|
+
const { toCredentialId } = require('../../modules/utils/to-credential-id');
|
|
4
5
|
|
|
5
6
|
const ERROR_CODE_MAP = {
|
|
6
7
|
ENTITY_NOT_FOUND: 404,
|
|
7
8
|
INVALID_ENTITY_DATA: 400,
|
|
8
9
|
};
|
|
9
10
|
|
|
11
|
+
function toEntityDto(entity) {
|
|
12
|
+
return {
|
|
13
|
+
id: entity.id,
|
|
14
|
+
userId: entity.userId,
|
|
15
|
+
externalId: entity.externalId,
|
|
16
|
+
name: entity.name,
|
|
17
|
+
moduleName: entity.moduleName,
|
|
18
|
+
credentialId: toCredentialId(entity.credential),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
10
22
|
function mapErrorToResponse(error) {
|
|
11
23
|
const status = ERROR_CODE_MAP[error?.code] || 500;
|
|
12
24
|
return {
|
|
@@ -66,16 +78,7 @@ function createEntityCommands() {
|
|
|
66
78
|
|
|
67
79
|
const entity = await moduleRepo.createEntity(entityData);
|
|
68
80
|
|
|
69
|
-
return
|
|
70
|
-
id: entity.id,
|
|
71
|
-
userId: entity.userId,
|
|
72
|
-
externalId: entity.externalId,
|
|
73
|
-
name: entity.name,
|
|
74
|
-
moduleName: entity.moduleName,
|
|
75
|
-
credentialId: entity.credential?._id
|
|
76
|
-
? entity.credential._id.toString()
|
|
77
|
-
: entity.credential,
|
|
78
|
-
};
|
|
81
|
+
return toEntityDto(entity);
|
|
79
82
|
} catch (error) {
|
|
80
83
|
return mapErrorToResponse(error);
|
|
81
84
|
}
|
|
@@ -109,16 +112,7 @@ function createEntityCommands() {
|
|
|
109
112
|
return null;
|
|
110
113
|
}
|
|
111
114
|
|
|
112
|
-
return
|
|
113
|
-
id: entity.id,
|
|
114
|
-
userId: entity.userId,
|
|
115
|
-
externalId: entity.externalId,
|
|
116
|
-
name: entity.name,
|
|
117
|
-
moduleName: entity.moduleName,
|
|
118
|
-
credentialId: entity.credential?._id
|
|
119
|
-
? entity.credential._id.toString()
|
|
120
|
-
: entity.credential,
|
|
121
|
-
};
|
|
115
|
+
return toEntityDto(entity);
|
|
122
116
|
} catch (error) {
|
|
123
117
|
return mapErrorToResponse(error);
|
|
124
118
|
}
|
|
@@ -139,16 +133,7 @@ function createEntityCommands() {
|
|
|
139
133
|
|
|
140
134
|
const entities = await moduleRepo.findEntitiesByUserId(userId);
|
|
141
135
|
|
|
142
|
-
return entities.map((entity) => (
|
|
143
|
-
id: entity.id,
|
|
144
|
-
userId: entity.userId,
|
|
145
|
-
externalId: entity.externalId,
|
|
146
|
-
name: entity.name,
|
|
147
|
-
moduleName: entity.moduleName,
|
|
148
|
-
credentialId: entity.credential?._id
|
|
149
|
-
? entity.credential._id.toString()
|
|
150
|
-
: entity.credential,
|
|
151
|
-
}));
|
|
136
|
+
return entities.map((entity) => toEntityDto(entity));
|
|
152
137
|
} catch (error) {
|
|
153
138
|
if (error.code) {
|
|
154
139
|
return mapErrorToResponse(error);
|
|
@@ -180,16 +165,7 @@ function createEntityCommands() {
|
|
|
180
165
|
moduleName
|
|
181
166
|
);
|
|
182
167
|
|
|
183
|
-
return entities.map((entity) => (
|
|
184
|
-
id: entity.id,
|
|
185
|
-
userId: entity.userId,
|
|
186
|
-
externalId: entity.externalId,
|
|
187
|
-
name: entity.name,
|
|
188
|
-
moduleName: entity.moduleName,
|
|
189
|
-
credentialId: entity.credential?._id
|
|
190
|
-
? entity.credential._id.toString()
|
|
191
|
-
: entity.credential,
|
|
192
|
-
}));
|
|
168
|
+
return entities.map((entity) => toEntityDto(entity));
|
|
193
169
|
} catch (error) {
|
|
194
170
|
if (error.code) {
|
|
195
171
|
return mapErrorToResponse(error);
|
|
@@ -213,16 +189,7 @@ function createEntityCommands() {
|
|
|
213
189
|
|
|
214
190
|
const entity = await moduleRepo.findEntityById(entityId);
|
|
215
191
|
|
|
216
|
-
return
|
|
217
|
-
id: entity.id,
|
|
218
|
-
userId: entity.userId,
|
|
219
|
-
externalId: entity.externalId,
|
|
220
|
-
name: entity.name,
|
|
221
|
-
moduleName: entity.moduleName,
|
|
222
|
-
credentialId: entity.credential?._id
|
|
223
|
-
? entity.credential._id.toString()
|
|
224
|
-
: entity.credential,
|
|
225
|
-
};
|
|
192
|
+
return toEntityDto(entity);
|
|
226
193
|
} catch (error) {
|
|
227
194
|
return mapErrorToResponse(error);
|
|
228
195
|
}
|
|
@@ -250,16 +217,7 @@ function createEntityCommands() {
|
|
|
250
217
|
throw error;
|
|
251
218
|
}
|
|
252
219
|
|
|
253
|
-
return
|
|
254
|
-
id: entity.id,
|
|
255
|
-
userId: entity.userId,
|
|
256
|
-
externalId: entity.externalId,
|
|
257
|
-
name: entity.name,
|
|
258
|
-
moduleName: entity.moduleName,
|
|
259
|
-
credentialId: entity.credential?._id
|
|
260
|
-
? entity.credential._id.toString()
|
|
261
|
-
: entity.credential,
|
|
262
|
-
};
|
|
220
|
+
return toEntityDto(entity);
|
|
263
221
|
} catch (error) {
|
|
264
222
|
return mapErrorToResponse(error);
|
|
265
223
|
}
|
|
@@ -29,6 +29,12 @@ const {
|
|
|
29
29
|
const {
|
|
30
30
|
PatchIntegrationConfig,
|
|
31
31
|
} = require('../../integrations/use-cases/patch-integration-config');
|
|
32
|
+
const {
|
|
33
|
+
AddEntitiesToIntegration,
|
|
34
|
+
} = require('../../integrations/use-cases/add-entities-to-integration');
|
|
35
|
+
const {
|
|
36
|
+
RemoveEntitiesFromIntegration,
|
|
37
|
+
} = require('../../integrations/use-cases/remove-entities-from-integration');
|
|
32
38
|
const {
|
|
33
39
|
getModulesDefinitionFromIntegrationClasses,
|
|
34
40
|
} = require('../../integrations/utils/map-integration-dto');
|
|
@@ -40,6 +46,11 @@ const ERROR_CODE_MAP = {
|
|
|
40
46
|
EXTERNAL_ID_REQUIRED: 400,
|
|
41
47
|
TYPE_REQUIRED: 400,
|
|
42
48
|
INTEGRATION_RECORD_NOT_FOUND: 404,
|
|
49
|
+
INVALID_INTEGRATION_DATA: 400,
|
|
50
|
+
ENTITIES_NOT_FOUND: 404,
|
|
51
|
+
ENTITY_USER_MISMATCH: 403,
|
|
52
|
+
ENTITY_MODULE_MISMATCH: 400,
|
|
53
|
+
LAST_ENTITY_OF_MODULE: 409,
|
|
43
54
|
};
|
|
44
55
|
|
|
45
56
|
function mapErrorToResponse(error) {
|
|
@@ -163,6 +174,17 @@ function createIntegrationCommands({ integrationClass } = {}) {
|
|
|
163
174
|
integrationRepository,
|
|
164
175
|
});
|
|
165
176
|
|
|
177
|
+
const addEntitiesToIntegrationUseCase = new AddEntitiesToIntegration({
|
|
178
|
+
integrationRepository,
|
|
179
|
+
moduleRepository,
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
const removeEntitiesFromIntegrationUseCase =
|
|
183
|
+
new RemoveEntitiesFromIntegration({
|
|
184
|
+
integrationRepository,
|
|
185
|
+
moduleRepository,
|
|
186
|
+
});
|
|
187
|
+
|
|
166
188
|
return {
|
|
167
189
|
findIntegrationById,
|
|
168
190
|
listIntegrations,
|
|
@@ -307,6 +329,47 @@ function createIntegrationCommands({ integrationClass } = {}) {
|
|
|
307
329
|
}
|
|
308
330
|
},
|
|
309
331
|
|
|
332
|
+
/**
|
|
333
|
+
* Attach entities to an existing integration. Every entity must
|
|
334
|
+
* exist, belong to the integration's user, and be of a module the
|
|
335
|
+
* integration already holds; entities that are already attached are
|
|
336
|
+
* left as they are.
|
|
337
|
+
* @param {Object} params
|
|
338
|
+
* @param {string} params.integrationId - Integration ID
|
|
339
|
+
* @param {Array<string>} params.entityIds - Entity IDs to attach
|
|
340
|
+
* @returns {Promise<Object>} Updated integration
|
|
341
|
+
*/
|
|
342
|
+
async addEntitiesToIntegration({ integrationId, entityIds }) {
|
|
343
|
+
try {
|
|
344
|
+
return await addEntitiesToIntegrationUseCase.execute(
|
|
345
|
+
integrationId,
|
|
346
|
+
entityIds
|
|
347
|
+
);
|
|
348
|
+
} catch (error) {
|
|
349
|
+
return mapErrorToResponse(error);
|
|
350
|
+
}
|
|
351
|
+
},
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Detach entities from an integration. Entities that are not
|
|
355
|
+
* attached are ignored; detached entities are not deleted. Each
|
|
356
|
+
* module must keep at least one entity.
|
|
357
|
+
* @param {Object} params
|
|
358
|
+
* @param {string} params.integrationId - Integration ID
|
|
359
|
+
* @param {Array<string>} params.entityIds - Entity IDs to detach
|
|
360
|
+
* @returns {Promise<Object>} Updated integration
|
|
361
|
+
*/
|
|
362
|
+
async removeEntitiesFromIntegration({ integrationId, entityIds }) {
|
|
363
|
+
try {
|
|
364
|
+
return await removeEntitiesFromIntegrationUseCase.execute(
|
|
365
|
+
integrationId,
|
|
366
|
+
entityIds
|
|
367
|
+
);
|
|
368
|
+
} catch (error) {
|
|
369
|
+
return mapErrorToResponse(error);
|
|
370
|
+
}
|
|
371
|
+
},
|
|
372
|
+
|
|
310
373
|
/**
|
|
311
374
|
* Delete an integration by ID
|
|
312
375
|
* @param {string} integrationId - Integration ID to delete
|
|
@@ -164,6 +164,38 @@ class IntegrationRepositoryDocumentDB extends IntegrationRepositoryInterface {
|
|
|
164
164
|
return this._mapIntegration(created);
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
+
async addEntitiesToIntegration(integrationId, entityIds) {
|
|
168
|
+
const objectId = toObjectId(integrationId);
|
|
169
|
+
if (!objectId) return null;
|
|
170
|
+
const result = await updateOne(
|
|
171
|
+
this.prisma,
|
|
172
|
+
'Integration',
|
|
173
|
+
{ _id: objectId },
|
|
174
|
+
{
|
|
175
|
+
$addToSet: { entityIds: { $each: toObjectIdArray(entityIds) } },
|
|
176
|
+
$set: { updatedAt: { $date: new Date().toISOString() } },
|
|
177
|
+
}
|
|
178
|
+
);
|
|
179
|
+
if (!result?.n) return null;
|
|
180
|
+
return this.findIntegrationById(integrationId);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async removeEntitiesFromIntegration(integrationId, entityIds) {
|
|
184
|
+
const objectId = toObjectId(integrationId);
|
|
185
|
+
if (!objectId) return null;
|
|
186
|
+
const result = await updateOne(
|
|
187
|
+
this.prisma,
|
|
188
|
+
'Integration',
|
|
189
|
+
{ _id: objectId },
|
|
190
|
+
{
|
|
191
|
+
$pullAll: { entityIds: toObjectIdArray(entityIds) },
|
|
192
|
+
$set: { updatedAt: { $date: new Date().toISOString() } },
|
|
193
|
+
}
|
|
194
|
+
);
|
|
195
|
+
if (!result?.n) return null;
|
|
196
|
+
return this.findIntegrationById(integrationId);
|
|
197
|
+
}
|
|
198
|
+
|
|
167
199
|
async findIntegrationByUserId(userId) {
|
|
168
200
|
const objectId = toObjectId(userId);
|
|
169
201
|
if (!objectId) return null;
|
|
@@ -184,6 +184,36 @@ class IntegrationRepositoryInterface {
|
|
|
184
184
|
'Method findIntegrationsByEntityId must be implemented by subclass'
|
|
185
185
|
);
|
|
186
186
|
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Attach entities to an existing integration. Entities that are already
|
|
190
|
+
* attached are left as they are.
|
|
191
|
+
*
|
|
192
|
+
* @param {string|number} integrationId - Integration ID
|
|
193
|
+
* @param {Array<string|number>} entityIds - Entity IDs to attach
|
|
194
|
+
* @returns {Promise<Object|null>} Updated integration, or null when the integration does not exist
|
|
195
|
+
* @abstract
|
|
196
|
+
*/
|
|
197
|
+
async addEntitiesToIntegration(integrationId, entityIds) {
|
|
198
|
+
throw new Error(
|
|
199
|
+
'Method addEntitiesToIntegration must be implemented by subclass'
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Detach entities from an integration. Entities that are not attached
|
|
205
|
+
* are ignored; the entities themselves are not deleted.
|
|
206
|
+
*
|
|
207
|
+
* @param {string|number} integrationId - Integration ID
|
|
208
|
+
* @param {Array<string|number>} entityIds - Entity IDs to detach
|
|
209
|
+
* @returns {Promise<Object|null>} Updated integration, or null when the integration does not exist
|
|
210
|
+
* @abstract
|
|
211
|
+
*/
|
|
212
|
+
async removeEntitiesFromIntegration(integrationId, entityIds) {
|
|
213
|
+
throw new Error(
|
|
214
|
+
'Method removeEntitiesFromIntegration must be implemented by subclass'
|
|
215
|
+
);
|
|
216
|
+
}
|
|
187
217
|
}
|
|
188
218
|
|
|
189
219
|
module.exports = { IntegrationRepositoryInterface };
|
|
@@ -338,6 +338,58 @@ class IntegrationRepositoryMongo extends IntegrationRepositoryInterface {
|
|
|
338
338
|
};
|
|
339
339
|
}
|
|
340
340
|
|
|
341
|
+
/**
|
|
342
|
+
* Attach entities to an existing integration. Entities already attached
|
|
343
|
+
* are left as they are.
|
|
344
|
+
*
|
|
345
|
+
* @param {string} integrationId - Integration ID
|
|
346
|
+
* @param {Array<string>} entityIds - Entity IDs to attach
|
|
347
|
+
* @returns {Promise<Object|null>} Updated integration, or null when the integration does not exist
|
|
348
|
+
*/
|
|
349
|
+
async addEntitiesToIntegration(integrationId, entityIds) {
|
|
350
|
+
|
|
351
|
+
try {
|
|
352
|
+
await this.prisma.integration.update({
|
|
353
|
+
where: { id: integrationId },
|
|
354
|
+
data: {
|
|
355
|
+
entities: {
|
|
356
|
+
connect: entityIds.map((id) => ({ id })),
|
|
357
|
+
},
|
|
358
|
+
},
|
|
359
|
+
});
|
|
360
|
+
} catch (error) {
|
|
361
|
+
if (error.code === 'P2025') return null;
|
|
362
|
+
throw error;
|
|
363
|
+
}
|
|
364
|
+
return this.findIntegrationById(integrationId);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Detach entities from an integration. Entities that are not attached
|
|
369
|
+
* are ignored. The entities themselves are not deleted.
|
|
370
|
+
*
|
|
371
|
+
* @param {string} integrationId - Integration ID
|
|
372
|
+
* @param {Array<string>} entityIds - Entity IDs to detach
|
|
373
|
+
* @returns {Promise<Object|null>} Updated integration, or null when the integration does not exist
|
|
374
|
+
*/
|
|
375
|
+
async removeEntitiesFromIntegration(integrationId, entityIds) {
|
|
376
|
+
|
|
377
|
+
try {
|
|
378
|
+
await this.prisma.integration.update({
|
|
379
|
+
where: { id: integrationId },
|
|
380
|
+
data: {
|
|
381
|
+
entities: {
|
|
382
|
+
disconnect: entityIds.map((id) => ({ id })),
|
|
383
|
+
},
|
|
384
|
+
},
|
|
385
|
+
});
|
|
386
|
+
} catch (error) {
|
|
387
|
+
if (error.code === 'P2025') return null;
|
|
388
|
+
throw error;
|
|
389
|
+
}
|
|
390
|
+
return this.findIntegrationById(integrationId);
|
|
391
|
+
}
|
|
392
|
+
|
|
341
393
|
/**
|
|
342
394
|
* Find integration by user ID (returns single integration)
|
|
343
395
|
* Replaces: IntegrationModel.findOne({ user: userId }).populate('entities')
|
|
@@ -365,6 +365,64 @@ class IntegrationRepositoryPostgres extends IntegrationRepositoryInterface {
|
|
|
365
365
|
};
|
|
366
366
|
}
|
|
367
367
|
|
|
368
|
+
/**
|
|
369
|
+
* Attach entities to an existing integration. Entities already attached
|
|
370
|
+
* are left as they are.
|
|
371
|
+
*
|
|
372
|
+
* @param {string} integrationId - Integration ID (string from application layer)
|
|
373
|
+
* @param {Array<string>} entityIds - Entity IDs to attach
|
|
374
|
+
* @returns {Promise<Object|null>} Updated integration, or null when the integration does not exist
|
|
375
|
+
*/
|
|
376
|
+
async addEntitiesToIntegration(integrationId, entityIds) {
|
|
377
|
+
const intId = this._convertId(integrationId);
|
|
378
|
+
|
|
379
|
+
try {
|
|
380
|
+
await this.prisma.integration.update({
|
|
381
|
+
where: { id: intId },
|
|
382
|
+
data: {
|
|
383
|
+
entities: {
|
|
384
|
+
connect: entityIds.map((id) => ({
|
|
385
|
+
id: this._convertId(id),
|
|
386
|
+
})),
|
|
387
|
+
},
|
|
388
|
+
},
|
|
389
|
+
});
|
|
390
|
+
} catch (error) {
|
|
391
|
+
if (error.code === 'P2025') return null;
|
|
392
|
+
throw error;
|
|
393
|
+
}
|
|
394
|
+
return this.findIntegrationById(integrationId);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Detach entities from an integration. Entities that are not attached
|
|
399
|
+
* are ignored. The entities themselves are not deleted.
|
|
400
|
+
*
|
|
401
|
+
* @param {string} integrationId - Integration ID (string from application layer)
|
|
402
|
+
* @param {Array<string>} entityIds - Entity IDs to detach
|
|
403
|
+
* @returns {Promise<Object|null>} Updated integration, or null when the integration does not exist
|
|
404
|
+
*/
|
|
405
|
+
async removeEntitiesFromIntegration(integrationId, entityIds) {
|
|
406
|
+
const intId = this._convertId(integrationId);
|
|
407
|
+
|
|
408
|
+
try {
|
|
409
|
+
await this.prisma.integration.update({
|
|
410
|
+
where: { id: intId },
|
|
411
|
+
data: {
|
|
412
|
+
entities: {
|
|
413
|
+
disconnect: entityIds.map((id) => ({
|
|
414
|
+
id: this._convertId(id),
|
|
415
|
+
})),
|
|
416
|
+
},
|
|
417
|
+
},
|
|
418
|
+
});
|
|
419
|
+
} catch (error) {
|
|
420
|
+
if (error.code === 'P2025') return null;
|
|
421
|
+
throw error;
|
|
422
|
+
}
|
|
423
|
+
return this.findIntegrationById(integrationId);
|
|
424
|
+
}
|
|
425
|
+
|
|
368
426
|
/**
|
|
369
427
|
* Find integration by user ID (returns single integration)
|
|
370
428
|
*
|
|
@@ -0,0 +1,92 @@
|
|
|
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 };
|
|
@@ -62,7 +62,7 @@ class CreateIntegration {
|
|
|
62
62
|
|
|
63
63
|
// A concurrent request may have created a matching row between the
|
|
64
64
|
// lookup above and this insert; re-check before any side effects run.
|
|
65
|
-
const survivor = await this.
|
|
65
|
+
const survivor = await this._findSurvivor(
|
|
66
66
|
userId,
|
|
67
67
|
config?.type,
|
|
68
68
|
entities
|
|
@@ -135,30 +135,68 @@ class CreateIntegration {
|
|
|
135
135
|
return mapIntegrationClassToIntegrationDTO(integrationInstance);
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
async
|
|
138
|
+
async _findMatches(userId, type, entityIds) {
|
|
139
139
|
const candidates =
|
|
140
140
|
await this.integrationRepository.findIntegrationsByUserId(userId);
|
|
141
|
-
const target =
|
|
142
|
-
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
.
|
|
155
|
-
|
|
141
|
+
const target = new Set((entityIds ?? []).map(String));
|
|
142
|
+
|
|
143
|
+
const live = candidates.filter(
|
|
144
|
+
(integration) =>
|
|
145
|
+
// An IN_DELETION row is either mid-teardown or a zombie left
|
|
146
|
+
// behind by a failed deleteIntegrationById call — never a live
|
|
147
|
+
// integration. Treating it as a duplicate would hand a
|
|
148
|
+
// reinstall attempt a row nothing will ever move out of
|
|
149
|
+
// IN_DELETION, silently discarded by the queue worker forever.
|
|
150
|
+
// Let a fresh create proceed instead; the zombie is cleaned up
|
|
151
|
+
// out-of-band, matching how teardown failures are already
|
|
152
|
+
// handled here.
|
|
153
|
+
integration.status !== 'IN_DELETION' &&
|
|
154
|
+
integration.config?.type === type
|
|
155
|
+
);
|
|
156
|
+
const entitySet = (integration) =>
|
|
157
|
+
new Set((integration.entitiesIds ?? []).map(String));
|
|
158
|
+
|
|
159
|
+
const exact = live.filter((integration) => {
|
|
160
|
+
const current = entitySet(integration);
|
|
156
161
|
return (
|
|
157
|
-
current.
|
|
158
|
-
|
|
162
|
+
current.size === target.size &&
|
|
163
|
+
[...target].every((id) => current.has(id))
|
|
159
164
|
);
|
|
160
165
|
});
|
|
161
166
|
|
|
167
|
+
// Entities may have been attached after create (addEntitiesToIntegration),
|
|
168
|
+
// so a reconnect that sends the original entities must still find the
|
|
169
|
+
// integration that now holds them, instead of creating a second one
|
|
170
|
+
// that would share those entities.
|
|
171
|
+
const supersets = live.filter((integration) => {
|
|
172
|
+
const current = entitySet(integration);
|
|
173
|
+
return [...target].every((id) => current.has(id));
|
|
174
|
+
});
|
|
175
|
+
return { exact, supersets: target.size ? supersets : [] };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async _findDuplicate(userId, type, entityIds) {
|
|
179
|
+
const { exact, supersets } = await this._findMatches(
|
|
180
|
+
userId,
|
|
181
|
+
type,
|
|
182
|
+
entityIds
|
|
183
|
+
);
|
|
184
|
+
if (exact.length) return this._pickOldest(exact);
|
|
185
|
+
return supersets.length ? this._pickOldest(supersets) : null;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// After an insert the new row is itself an exact match, so the survivor
|
|
189
|
+
// is the oldest of every exact or superset match (the new row included):
|
|
190
|
+
// an older integration that already holds these entities always wins.
|
|
191
|
+
async _findSurvivor(userId, type, entityIds) {
|
|
192
|
+
const { exact, supersets } = await this._findMatches(
|
|
193
|
+
userId,
|
|
194
|
+
type,
|
|
195
|
+
entityIds
|
|
196
|
+
);
|
|
197
|
+
// supersets includes the exact matches; it is empty only for an
|
|
198
|
+
// empty entity set, where exact matching alone applies.
|
|
199
|
+
const matches = supersets.length ? supersets : exact;
|
|
162
200
|
return matches.length ? this._pickOldest(matches) : null;
|
|
163
201
|
}
|
|
164
202
|
|
|
@@ -0,0 +1,70 @@
|
|
|
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 };
|
|
@@ -0,0 +1,42 @@
|
|
|
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,3 +1,5 @@
|
|
|
1
|
+
const { toCredentialId } = require('./to-credential-id');
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* @param {import('../module').Module} moduleInstance
|
|
3
5
|
* Convert a Module domain instance to a plain DTO suitable for JSON responses.
|
|
@@ -10,9 +12,9 @@ function mapModuleClassToModuleDTO(moduleInstance) {
|
|
|
10
12
|
name: moduleInstance.name,
|
|
11
13
|
userId: moduleInstance.userId,
|
|
12
14
|
entity: moduleInstance.entity,
|
|
13
|
-
credentialId: moduleInstance.credential
|
|
14
|
-
type: moduleInstance.getName()
|
|
15
|
+
credentialId: toCredentialId(moduleInstance.credential),
|
|
16
|
+
type: moduleInstance.getName(),
|
|
15
17
|
};
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
module.exports = { mapModuleClassToModuleDTO };
|
|
20
|
+
module.exports = { mapModuleClassToModuleDTO };
|
|
@@ -0,0 +1,14 @@
|
|
|
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 };
|
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-next.
|
|
4
|
+
"version": "2.0.0-next.119",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@aws-sdk/client-apigatewaymanagementapi": "^3.588.0",
|
|
7
7
|
"@aws-sdk/client-kms": "^3.588.0",
|
|
@@ -47,9 +47,9 @@
|
|
|
47
47
|
}
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@friggframework/eslint-config": "2.0.0-next.
|
|
51
|
-
"@friggframework/prettier-config": "2.0.0-next.
|
|
52
|
-
"@friggframework/test": "2.0.0-next.
|
|
50
|
+
"@friggframework/eslint-config": "2.0.0-next.119",
|
|
51
|
+
"@friggframework/prettier-config": "2.0.0-next.119",
|
|
52
|
+
"@friggframework/test": "2.0.0-next.119",
|
|
53
53
|
"@prisma/client": "^6.19.3",
|
|
54
54
|
"@types/lodash": "4.17.15",
|
|
55
55
|
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
@@ -89,5 +89,5 @@
|
|
|
89
89
|
"publishConfig": {
|
|
90
90
|
"access": "public"
|
|
91
91
|
},
|
|
92
|
-
"gitHead": "
|
|
92
|
+
"gitHead": "470470c1956365ae1e8fffd24ab013d7906ffc10"
|
|
93
93
|
}
|