@friggframework/core 2.0.0--canary.686.df38e8e.0 → 2.0.0--canary.710.9045bbc.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.
Files changed (217) hide show
  1. package/CHANGELOG.md +0 -16
  2. package/CLAUDE.md +41 -29
  3. package/README.md +0 -105
  4. package/application/commands/README.md +17 -11
  5. package/application/commands/entity-commands.js +18 -60
  6. package/application/commands/integration-commands.js +73 -20
  7. package/application/commands/scheduler-commands.js +7 -11
  8. package/application/commands/user-commands.js +5 -17
  9. package/application/index.js +2 -6
  10. package/core/Worker.js +7 -31
  11. package/core/create-handler.js +0 -9
  12. package/credential/repositories/credential-repository-documentdb.js +0 -34
  13. package/credential/repositories/credential-repository-interface.js +0 -15
  14. package/credential/repositories/credential-repository-mongo.js +0 -23
  15. package/credential/repositories/credential-repository-postgres.js +0 -28
  16. package/database/config.js +24 -48
  17. package/database/documentdb-encryption-service.js +22 -9
  18. package/database/documentdb-utils.js +0 -2
  19. package/database/encryption/README.md +17 -81
  20. package/database/encryption/documentdb-encryption-service.md +1 -78
  21. package/database/encryption/encryption-schema-registry.js +46 -123
  22. package/database/encryption/field-encryption-service.js +5 -69
  23. package/database/encryption/integration-mapping-encryption.js +1 -1
  24. package/database/prisma.js +38 -15
  25. package/database/use-cases/check-encryption-health-use-case.js +52 -43
  26. package/errors/index.js +4 -0
  27. package/errors/mapping-already-exists-error.js +14 -0
  28. package/generated/prisma-mongodb/edge.js +4 -43
  29. package/generated/prisma-mongodb/index-browser.js +1 -40
  30. package/generated/prisma-mongodb/index.d.ts +111 -4739
  31. package/generated/prisma-mongodb/index.js +4 -43
  32. package/generated/prisma-mongodb/package.json +1 -1
  33. package/generated/prisma-mongodb/schema.prisma +0 -92
  34. package/generated/prisma-mongodb/wasm.js +4 -43
  35. package/generated/prisma-postgresql/edge.js +4 -43
  36. package/generated/prisma-postgresql/index-browser.js +1 -40
  37. package/generated/prisma-postgresql/index.d.ts +33 -4966
  38. package/generated/prisma-postgresql/index.js +4 -43
  39. package/generated/prisma-postgresql/package.json +1 -1
  40. package/generated/prisma-postgresql/schema.prisma +0 -87
  41. package/generated/prisma-postgresql/wasm.js +4 -43
  42. package/handlers/WEBHOOKS.md +29 -0
  43. package/handlers/app-definition-loader.js +19 -121
  44. package/handlers/app-handler-helpers.js +3 -25
  45. package/handlers/backend-utils.js +25 -31
  46. package/handlers/middleware/admin-auth.js +1 -14
  47. package/handlers/routers/auth.js +10 -46
  48. package/handlers/routers/health.js +4 -13
  49. package/handlers/routers/integration-defined-routers.js +92 -144
  50. package/handlers/routers/integration-webhook-routers.js +64 -102
  51. package/handlers/workers/integration-defined-workers.js +2 -5
  52. package/index.js +3 -65
  53. package/infrastructure/scheduler/eventbridge-scheduler-adapter.js +67 -13
  54. package/infrastructure/scheduler/index.js +6 -19
  55. package/infrastructure/scheduler/scheduler-service-factory.js +7 -55
  56. package/integrations/index.js +0 -1
  57. package/integrations/integration-base.js +29 -0
  58. package/integrations/integration-router.js +96 -131
  59. package/integrations/repositories/integration-mapping-repository-documentdb.js +50 -0
  60. package/integrations/repositories/integration-mapping-repository-interface.js +15 -0
  61. package/integrations/repositories/integration-mapping-repository-mongo.js +29 -0
  62. package/integrations/repositories/integration-mapping-repository-postgres.js +30 -0
  63. package/integrations/repositories/integration-mapping-repository.js +29 -0
  64. package/integrations/repositories/integration-repository-documentdb.js +35 -35
  65. package/integrations/repositories/integration-repository-interface.js +31 -17
  66. package/integrations/repositories/integration-repository-mongo.js +129 -60
  67. package/integrations/repositories/integration-repository-postgres.js +134 -62
  68. package/integrations/repositories/process-repository-mongo.js +1 -1
  69. package/integrations/tests/doubles/test-integration-repository.js +1 -12
  70. package/integrations/use-cases/add-entities-to-integration.js +92 -0
  71. package/integrations/use-cases/create-integration.js +75 -72
  72. package/integrations/use-cases/get-integrations-for-user.js +40 -80
  73. package/integrations/use-cases/index.js +1 -7
  74. package/integrations/use-cases/remove-entities-from-integration.js +70 -0
  75. package/integrations/utils/assert-entity-ids-input.js +42 -0
  76. package/integrations/utils/map-integration-dto.js +14 -43
  77. package/jest-setup.js +13 -2
  78. package/jest-teardown.js +5 -2
  79. package/jest.config.js +4 -0
  80. package/jest.db.config.js +23 -0
  81. package/modules/index.js +0 -14
  82. package/modules/module-factory.js +0 -12
  83. package/modules/module.js +2 -53
  84. package/modules/repositories/module-repository-documentdb.js +6 -51
  85. package/modules/repositories/module-repository-interface.js +2 -19
  86. package/modules/repositories/module-repository-mongo.js +74 -67
  87. package/modules/repositories/module-repository-postgres.js +74 -66
  88. package/modules/repositories/module-repository.js +1 -2
  89. package/modules/requester/requester.js +2 -27
  90. package/modules/use-cases/get-entities-for-user.js +13 -26
  91. package/modules/use-cases/get-entity-options-by-id.js +4 -10
  92. package/modules/use-cases/get-entity-options-by-type.js +1 -1
  93. package/modules/use-cases/get-module-instance-from-type.js +1 -1
  94. package/modules/use-cases/get-module.js +4 -10
  95. package/modules/use-cases/process-authorization-callback.js +19 -53
  96. package/modules/use-cases/refresh-entity-options.js +4 -10
  97. package/modules/use-cases/test-module-auth.js +4 -10
  98. package/modules/utils/map-module-dto.js +5 -3
  99. package/modules/utils/to-credential-id.js +14 -0
  100. package/package.json +7 -6
  101. package/prisma-mongodb/schema.prisma +0 -92
  102. package/prisma-postgresql/schema.prisma +0 -87
  103. package/queues/index.js +0 -25
  104. package/queues/queuer-util.js +6 -5
  105. package/types/index.d.ts +0 -2
  106. package/user/repositories/user-repository-documentdb.js +2 -27
  107. package/user/repositories/user-repository-interface.js +0 -18
  108. package/user/repositories/user-repository-mongo.js +0 -24
  109. package/user/repositories/user-repository-postgres.js +0 -18
  110. package/user/tests/doubles/test-user-repository.js +1 -1
  111. package/user/use-cases/authenticate-user.js +1 -7
  112. package/user/use-cases/get-user-from-adopter-jwt.js +124 -95
  113. package/user/use-cases/get-user-from-bearer-token.js +7 -2
  114. package/user/use-cases/get-user-from-x-frigg-headers.js +80 -83
  115. package/user/user.js +1 -64
  116. package/credential/use-cases/credential-operations-for-user.js +0 -202
  117. package/credential/use-cases/reauthorize-credential-for-user.js +0 -73
  118. package/database/encryption/encryption-config.js +0 -219
  119. package/database/encryption/test-support/fake-ciphertext.js +0 -15
  120. package/database/organization-where.js +0 -18
  121. package/database/unique-violation.js +0 -25
  122. package/extensions/bootstrap-runner.js +0 -43
  123. package/extensions/extension-loader.js +0 -187
  124. package/extensions/index.js +0 -41
  125. package/extensions/initialize-app.js +0 -123
  126. package/extensions/route-mounter.js +0 -103
  127. package/extensions/schema-composer.js +0 -188
  128. package/infrastructure/scheduler/netlify-scheduler-adapter.js +0 -220
  129. package/infrastructure/scheduler/scheduled-job-repository.js +0 -118
  130. package/integrations/repositories/integration-not-found.js +0 -14
  131. package/integrations/schedule-config.js +0 -80
  132. package/integrations/use-cases/dispatch-ongoing-syncs.js +0 -100
  133. package/integrations/use-cases/integration-operations-for-user.js +0 -256
  134. package/integrations/use-cases/resolve-integration-ownership.js +0 -177
  135. package/management-api/create-management-api-router.js +0 -49
  136. package/management-api/error-response.js +0 -116
  137. package/management-api/management-api-config.js +0 -151
  138. package/management-api/meta-router.js +0 -76
  139. package/management-api/openapi/build-openapi.js +0 -144
  140. package/management-api/openapi/components.js +0 -408
  141. package/management-api/openapi/v1-operations.js +0 -83
  142. package/management-api/openapi/v2-operations.js +0 -264
  143. package/management-api/route-registry.js +0 -411
  144. package/management-api/test-support/http-client.js +0 -39
  145. package/management-api/use-cases/get-management-api-meta.js +0 -57
  146. package/management-api/v1-deprecation.js +0 -120
  147. package/management-api/v2/create-v2-router.js +0 -85
  148. package/management-api/v2/dependencies.js +0 -281
  149. package/management-api/v2/dto.js +0 -143
  150. package/management-api/v2/handlers/authorization.js +0 -94
  151. package/management-api/v2/handlers/credentials.js +0 -33
  152. package/management-api/v2/handlers/entities.js +0 -77
  153. package/management-api/v2/handlers/integrations.js +0 -128
  154. package/management-api/v2/handlers/proxy.js +0 -34
  155. package/management-api/v2/request.js +0 -85
  156. package/management-api/v2/test-support/fake-dependencies.js +0 -103
  157. package/modules/domain/authorization-session.js +0 -144
  158. package/modules/entity-scope.js +0 -15
  159. package/modules/entity-type-registry.js +0 -311
  160. package/modules/proxy/proxy-errors.js +0 -20
  161. package/modules/proxy/proxy-path.js +0 -179
  162. package/modules/proxy/proxy-policy.js +0 -248
  163. package/modules/repositories/authorization-session-repository-documentdb.js +0 -112
  164. package/modules/repositories/authorization-session-repository-factory.js +0 -31
  165. package/modules/repositories/authorization-session-repository-interface.js +0 -47
  166. package/modules/repositories/authorization-session-repository-prisma.js +0 -87
  167. package/modules/repositories/entity-not-found.js +0 -18
  168. package/modules/requester/proxy-request.js +0 -244
  169. package/modules/scope-config.js +0 -192
  170. package/modules/scope-policy.js +0 -112
  171. package/modules/scope-resolution.js +0 -288
  172. package/modules/scope-visibility.js +0 -247
  173. package/modules/tests/doubles/in-memory-authorization-session-repository.js +0 -45
  174. package/modules/tests/doubles/ownership-matching.js +0 -18
  175. package/modules/use-cases/authorize-entity-for-user.js +0 -61
  176. package/modules/use-cases/create-entity-from-credential.js +0 -113
  177. package/modules/use-cases/entity-operations-for-user.js +0 -162
  178. package/modules/use-cases/entity-types.js +0 -139
  179. package/modules/use-cases/execute-entity-proxy-request.js +0 -286
  180. package/modules/use-cases/get-authorization-requirements.js +0 -100
  181. package/modules/use-cases/get-entity-module-for-user.js +0 -113
  182. package/modules/use-cases/manage-access.js +0 -21
  183. package/modules/use-cases/multi-step-authorization.js +0 -329
  184. package/prisma-postgresql/migrations/20261003000000_add_authorization_session/migration.sql +0 -28
  185. package/prisma-postgresql/migrations/20261015000000_add_organization_membership/migration.sql +0 -26
  186. package/prisma-postgresql/migrations/20261016000000_add_entity_integration_organization/migration.sql +0 -25
  187. package/prisma-postgresql/migrations/20261017000000_add_scheduled_job/migration.sql +0 -28
  188. package/providers/index.js +0 -23
  189. package/providers/resolve-provider.js +0 -93
  190. package/providers/runtime-detection.js +0 -124
  191. package/queues/providers/index.js +0 -11
  192. package/queues/providers/netlify-background-provider.js +0 -187
  193. package/queues/providers/qstash-queue-provider.js +0 -219
  194. package/queues/providers/sqs-queue-provider.js +0 -70
  195. package/queues/queue-client-interface.js +0 -55
  196. package/queues/queue-message-auth.js +0 -193
  197. package/queues/queue-provider-factory.js +0 -101
  198. package/queues/queue-provider.js +0 -65
  199. package/queues/sqs-queue-client.js +0 -81
  200. package/types/modules/index.d.ts +0 -197
  201. package/types/user/index.d.ts +0 -134
  202. package/user/adopter-jwt-config.js +0 -177
  203. package/user/adopter-jwt-errors.js +0 -59
  204. package/user/adopter-token-verifier.js +0 -122
  205. package/user/app-user-helpers.js +0 -97
  206. package/user/create-user-authentication.js +0 -64
  207. package/user/membership.js +0 -51
  208. package/user/repositories/organization-membership-record.js +0 -19
  209. package/user/repositories/organization-membership-repository-documentdb.js +0 -106
  210. package/user/repositories/organization-membership-repository-factory.js +0 -42
  211. package/user/repositories/organization-membership-repository-interface.js +0 -57
  212. package/user/repositories/organization-membership-repository-mongo.js +0 -13
  213. package/user/repositories/organization-membership-repository-postgres.js +0 -29
  214. package/user/repositories/organization-membership-repository-prisma.js +0 -81
  215. package/user/tests/doubles/adopter-jwt-fixtures.js +0 -220
  216. package/user/use-cases/ensure-organization-membership.js +0 -92
  217. package/user/use-cases/find-individual-users-by-organization.js +0 -54
@@ -3,14 +3,12 @@ const {
3
3
  toObjectId,
4
4
  fromObjectId,
5
5
  findMany,
6
- findManyDrained,
7
6
  findOne,
8
7
  insertOne,
9
8
  updateOne,
10
9
  deleteOne,
11
10
  } = require('../../database/documentdb-utils');
12
11
  const { ModuleRepositoryInterface } = require('./module-repository-interface');
13
- const { entityNotFoundError } = require('./entity-not-found');
14
12
  const { DocumentDBEncryptionService } = require('../../database/documentdb-encryption-service');
15
13
 
16
14
  /**
@@ -35,11 +33,11 @@ class ModuleRepositoryDocumentDB extends ModuleRepositoryInterface {
35
33
  async findEntityById(entityId) {
36
34
  const objectId = toObjectId(entityId);
37
35
  if (!objectId) {
38
- throw entityNotFoundError(entityId);
36
+ throw new Error(`Entity ${entityId} not found`);
39
37
  }
40
38
  const doc = await findOne(this.prisma, 'Entity', { _id: objectId });
41
39
  if (!doc) {
42
- throw entityNotFoundError(entityId);
40
+ throw new Error(`Entity ${entityId} not found`);
43
41
  }
44
42
  const credential = await this._fetchCredential(doc.credentialId);
45
43
  return this._mapEntity(doc, credential);
@@ -78,29 +76,6 @@ class ModuleRepositoryDocumentDB extends ModuleRepositoryInterface {
78
76
  return docs.map((doc) => this._mapEntity(doc, credentialMap.get(fromObjectId(doc.credentialId)) || null));
79
77
  }
80
78
 
81
- /**
82
- * Find entities matching any of the owner/organization pairs, in one query.
83
- * `organizationId: null` matches rows with no organization (a raw MongoDB
84
- * `null` also matches a missing field). The caller (a use case) decides
85
- * which pairs; this only runs them.
86
- *
87
- * @param {Array<{userId: string, organizationId: (string|null)}>} clauses
88
- * @returns {Promise<Array>}
89
- */
90
- async findEntitiesByOwnership(clauses) {
91
- if (!Array.isArray(clauses) || clauses.length === 0) return [];
92
- // Drained: a list can hold more than the first cursor batch (~101).
93
- const docs = await findManyDrained(this.prisma, 'Entity', {
94
- $or: clauses.map(({ userId, organizationId }) => ({
95
- userId: toObjectId(userId),
96
- organizationId:
97
- organizationId === null ? null : toObjectId(organizationId),
98
- })),
99
- });
100
- const credentialMap = await this._fetchCredentialsBulk(docs.map((doc) => doc.credentialId));
101
- return docs.map((doc) => this._mapEntity(doc, credentialMap.get(fromObjectId(doc.credentialId)) || null));
102
- }
103
-
104
79
  async unsetCredential(entityId) {
105
80
  const objectId = toObjectId(entityId);
106
81
  if (!objectId) return false;
@@ -140,14 +115,7 @@ class ModuleRepositoryDocumentDB extends ModuleRepositoryInterface {
140
115
  );
141
116
  }
142
117
 
143
- /**
144
- * @param {Object} entityData
145
- * @param {{organizationId?: (string|null)}} [tenancy] - the organization the entity
146
- * belongs to (ADR-056 §3.6). A separate argument, not a field of `entityData`,
147
- * because `entityData` carries provider fields and one named `organizationId`
148
- * must not be taken for ours.
149
- */
150
- async createEntity(entityData, tenancy = {}) {
118
+ async createEntity(entityData) {
151
119
  const {
152
120
  user,
153
121
  userId,
@@ -165,7 +133,6 @@ class ModuleRepositoryDocumentDB extends ModuleRepositoryInterface {
165
133
  name: name ?? null,
166
134
  moduleName: moduleName ?? null,
167
135
  externalId: externalId ?? null,
168
- organizationId: toObjectId(tenancy.organizationId) || null,
169
136
  data: dynamicData,
170
137
  };
171
138
  const insertedId = await insertOne(this.prisma, 'Entity', document);
@@ -280,9 +247,8 @@ class ModuleRepositoryDocumentDB extends ModuleRepositoryInterface {
280
247
  const objectIds = ids.map(id => toObjectId(id)).filter(Boolean);
281
248
  if (objectIds.length === 0) return new Map();
282
249
 
283
- // Raw read to bypass the Prisma encryption extension. Drained:
284
- // more ids than the first cursor batch must all come back.
285
- const rawCredentials = await findManyDrained(this.prisma, 'Credential', {
250
+ // Use raw findMany to bypass Prisma encryption extension
251
+ const rawCredentials = await findMany(this.prisma, 'Credential', {
286
252
  _id: { $in: objectIds }
287
253
  });
288
254
 
@@ -363,30 +329,19 @@ class ModuleRepositoryDocumentDB extends ModuleRepositoryInterface {
363
329
  if (filter.name) query.name = filter.name;
364
330
  if (filter.moduleName) query.moduleName = filter.moduleName;
365
331
  if (filter.externalId) query.externalId = filter.externalId;
366
- // `null` means "no organization"; absent means "any". A raw MongoDB
367
- // `{ field: null }` matches both a null value and a missing field.
368
- if (filter.organizationId !== undefined) {
369
- query.organizationId =
370
- filter.organizationId === null
371
- ? null
372
- : toObjectId(filter.organizationId);
373
- }
374
332
  return query;
375
333
  }
376
334
 
377
335
  _mapEntity(doc, credential) {
378
336
  const dynamicData = doc?.data || {};
379
337
  return {
380
- // First, so no stored provider field can replace a column below
381
- // (identity, owner or organization decide who sees the entity).
382
- ...dynamicData,
383
338
  id: fromObjectId(doc?._id),
384
339
  credential,
385
340
  userId: fromObjectId(doc?.userId),
386
341
  name: doc?.name ?? null,
387
342
  externalId: doc?.externalId ?? null,
388
343
  moduleName: doc?.moduleName ?? null,
389
- organizationId: fromObjectId(doc?.organizationId) ?? null,
344
+ ...dynamicData,
390
345
  };
391
346
  }
392
347
  }
@@ -67,21 +67,6 @@ class ModuleRepositoryInterface {
67
67
  );
68
68
  }
69
69
 
70
- /**
71
- * Find entities matching any of the owner/organization pairs, in one query
72
- * (ADR-056 §3.6). `organizationId: null` matches rows with no organization.
73
- * Which pairs is decided by the caller (modules/entity-scope.js), not here.
74
- *
75
- * @param {Array<{userId: string|number, organizationId: (string|number|null)}>} clauses
76
- * @returns {Promise<Array>} Array of entity objects
77
- * @abstract
78
- */
79
- async findEntitiesByOwnership(clauses) {
80
- throw new Error(
81
- 'Method findEntitiesByOwnership must be implemented by subclass'
82
- );
83
- }
84
-
85
70
  /**
86
71
  * Unset credential from entity
87
72
  *
@@ -125,13 +110,11 @@ class ModuleRepositoryInterface {
125
110
  /**
126
111
  * Create a new entity
127
112
  *
128
- * @param {Object} entityData - Entity data (provider fields included)
129
- * @param {{organizationId?: (string|number|null)}} [tenancy] - the organization
130
- * the entity belongs to (ADR-056 §3.6), kept apart from `entityData`
113
+ * @param {Object} entityData - Entity data
131
114
  * @returns {Promise<Object>} Created entity object
132
115
  * @abstract
133
116
  */
134
- async createEntity(entityData, tenancy) {
117
+ async createEntity(entityData) {
135
118
  throw new Error('Method createEntity must be implemented by subclass');
136
119
  }
137
120
 
@@ -1,7 +1,5 @@
1
1
  const { prisma } = require('../../database/prisma');
2
- const { organizationWhere } = require('../../database/organization-where');
3
2
  const { ModuleRepositoryInterface } = require('./module-repository-interface');
4
- const { entityNotFoundError } = require('./entity-not-found');
5
3
 
6
4
  /**
7
5
  * MongoDB Module Repository Adapter
@@ -78,26 +76,6 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
78
76
  return credentialMap;
79
77
  }
80
78
 
81
- /**
82
- * The one place a stored entity row becomes the entity the application sees.
83
- * The provider's `data` is spread FIRST and every column is pinned after it:
84
- * a stored `id`, `userId` or `organizationId` in `data` must not decide who
85
- * owns the entity or who sees it.
86
- * @private
87
- */
88
- _mapEntity(row, credential) {
89
- return {
90
- ...(row.data || {}),
91
- id: row.id,
92
- credential,
93
- userId: row.userId,
94
- name: row.name,
95
- externalId: row.externalId,
96
- moduleName: row.moduleName,
97
- organizationId: row.organizationId ?? null,
98
- };
99
- }
100
-
101
79
  /**
102
80
  * Find entity by ID with credential
103
81
  * Replaces: Entity.findById(entityId).populate('credential')
@@ -112,12 +90,20 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
112
90
  });
113
91
 
114
92
  if (!entity) {
115
- throw entityNotFoundError(entityId);
93
+ throw new Error(`Entity ${entityId} not found`);
116
94
  }
117
95
 
118
96
  const credential = await this._fetchCredential(entity.credentialId);
119
97
 
120
- return this._mapEntity(entity, credential);
98
+ return {
99
+ id: entity.id,
100
+ credential,
101
+ userId: entity.userId,
102
+ name: entity.name,
103
+ externalId: entity.externalId,
104
+ moduleName: entity.moduleName,
105
+ ...(entity.data || {}),
106
+ };
121
107
  }
122
108
 
123
109
  /**
@@ -135,7 +121,15 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
135
121
  const credentialIds = entities.map(e => e.credentialId).filter(Boolean);
136
122
  const credentialMap = await this._fetchCredentialsBulk(credentialIds);
137
123
 
138
- return entities.map((e) => (this._mapEntity(e, credentialMap.get(e.credentialId) || null)));
124
+ return entities.map((e) => ({
125
+ id: e.id,
126
+ credential: credentialMap.get(e.credentialId) || null,
127
+ userId: e.userId,
128
+ name: e.name,
129
+ externalId: e.externalId,
130
+ moduleName: e.moduleName,
131
+ ...(e.data || {}),
132
+ }));
139
133
  }
140
134
 
141
135
  /**
@@ -153,7 +147,15 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
153
147
  const credentialIds = entities.map(e => e.credentialId).filter(Boolean);
154
148
  const credentialMap = await this._fetchCredentialsBulk(credentialIds);
155
149
 
156
- return entities.map((e) => (this._mapEntity(e, credentialMap.get(e.credentialId) || null)));
150
+ return entities.map((e) => ({
151
+ id: e.id,
152
+ credential: credentialMap.get(e.credentialId) || null,
153
+ userId: e.userId,
154
+ name: e.name,
155
+ externalId: e.externalId,
156
+ moduleName: e.moduleName,
157
+ ...(e.data || {}),
158
+ }));
157
159
  }
158
160
 
159
161
  /**
@@ -175,32 +177,15 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
175
177
  const credentialIds = entities.map(e => e.credentialId).filter(Boolean);
176
178
  const credentialMap = await this._fetchCredentialsBulk(credentialIds);
177
179
 
178
- return entities.map((e) => (this._mapEntity(e, credentialMap.get(e.credentialId) || null)));
179
- }
180
-
181
- /**
182
- * Find entities matching any of the owner/organization pairs, in one query.
183
- * `organizationId: null` matches rows with no organization. The caller
184
- * (a use case) decides which pairs; this only runs them.
185
- *
186
- * @param {Array<{userId: string, organizationId: (string|null)}>} clauses
187
- * @returns {Promise<Array>} Array of entity objects with string IDs
188
- */
189
- async findEntitiesByOwnership(clauses) {
190
- if (!Array.isArray(clauses) || clauses.length === 0) return [];
191
- const entities = await this.prisma.entity.findMany({
192
- where: {
193
- OR: clauses.map(({ userId, organizationId }) => ({
194
- userId,
195
- ...organizationWhere(organizationId),
196
- })),
197
- },
198
- });
199
-
200
- const credentialIds = entities.map(e => e.credentialId).filter(Boolean);
201
- const credentialMap = await this._fetchCredentialsBulk(credentialIds);
202
-
203
- return entities.map((e) => (this._mapEntity(e, credentialMap.get(e.credentialId) || null)));
180
+ return entities.map((e) => ({
181
+ id: e.id,
182
+ credential: credentialMap.get(e.credentialId) || null,
183
+ userId: e.userId,
184
+ name: e.name,
185
+ externalId: e.externalId,
186
+ moduleName: e.moduleName,
187
+ ...(e.data || {}),
188
+ }));
204
189
  }
205
190
 
206
191
  /**
@@ -238,7 +223,15 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
238
223
 
239
224
  const credential = await this._fetchCredential(entity.credentialId);
240
225
 
241
- return this._mapEntity(entity, credential);
226
+ return {
227
+ id: entity.id,
228
+ credential,
229
+ userId: entity.userId,
230
+ name: entity.name,
231
+ externalId: entity.externalId,
232
+ moduleName: entity.moduleName,
233
+ ...(entity.data || {}),
234
+ };
242
235
  }
243
236
 
244
237
  /**
@@ -258,7 +251,15 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
258
251
  .filter(Boolean);
259
252
  const credentialMap = await this._fetchCredentialsBulk(credentialIds);
260
253
 
261
- return entities.map((e) => (this._mapEntity(e, credentialMap.get(e.credentialId) || null)));
254
+ return entities.map((e) => ({
255
+ id: e.id,
256
+ credential: credentialMap.get(e.credentialId) || null,
257
+ userId: e.userId,
258
+ name: e.name,
259
+ externalId: e.externalId,
260
+ moduleName: e.moduleName,
261
+ ...(e.data || {}),
262
+ }));
262
263
  }
263
264
 
264
265
  /**
@@ -266,13 +267,9 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
266
267
  * Replaces: Entity.create(entityData)
267
268
  *
268
269
  * @param {Object} entityData - Entity data
269
- * @param {{organizationId?: (string|null)}} [tenancy] - the organization the entity
270
- * belongs to (ADR-056 §3.6). A separate argument, not a field of `entityData`,
271
- * because `entityData` carries provider fields and one named `organizationId`
272
- * must not be taken for ours.
273
270
  * @returns {Promise<Object>} Created entity object with string IDs
274
271
  */
275
- async createEntity(entityData, tenancy = {}) {
272
+ async createEntity(entityData) {
276
273
  const {
277
274
  user,
278
275
  userId,
@@ -290,7 +287,6 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
290
287
  name,
291
288
  moduleName,
292
289
  externalId,
293
- organizationId: tenancy.organizationId ?? null,
294
290
  data: dynamicData,
295
291
  };
296
292
 
@@ -300,7 +296,15 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
300
296
 
301
297
  const credentialObj = await this._fetchCredential(entity.credentialId);
302
298
 
303
- return this._mapEntity(entity, credentialObj);
299
+ return {
300
+ id: entity.id,
301
+ credential: credentialObj,
302
+ userId: entity.userId,
303
+ name: entity.name,
304
+ externalId: entity.externalId,
305
+ moduleName: entity.moduleName,
306
+ ...(entity.data || {}),
307
+ };
304
308
  }
305
309
 
306
310
  /**
@@ -354,7 +358,15 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
354
358
 
355
359
  const credentialObj = await this._fetchCredential(entity.credentialId);
356
360
 
357
- return this._mapEntity(entity, credentialObj);
361
+ return {
362
+ id: entity.id,
363
+ credential: credentialObj,
364
+ userId: entity.userId,
365
+ name: entity.name,
366
+ externalId: entity.externalId,
367
+ moduleName: entity.moduleName,
368
+ ...(entity.data || {}),
369
+ };
358
370
  } catch (error) {
359
371
  if (error.code === 'P2025') {
360
372
  return null;
@@ -417,11 +429,6 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
417
429
  if (filter.moduleName) where.moduleName = filter.moduleName;
418
430
  if (filter.externalId) where.externalId = this._toString(filter.externalId);
419
431
 
420
- // `null` means "no organization"; absent means "any".
421
- if (filter.organizationId !== undefined) {
422
- Object.assign(where, organizationWhere(filter.organizationId));
423
- }
424
-
425
432
  return where;
426
433
  }
427
434
  }
@@ -1,6 +1,5 @@
1
1
  const { prisma } = require('../../database/prisma');
2
2
  const { ModuleRepositoryInterface } = require('./module-repository-interface');
3
- const { entityNotFoundError } = require('./entity-not-found');
4
3
 
5
4
  /**
6
5
  * PostgreSQL Module Repository Adapter
@@ -109,26 +108,6 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
109
108
  return credentialMap;
110
109
  }
111
110
 
112
- /**
113
- * The one place a stored entity row becomes the entity the application sees.
114
- * The provider's `data` is spread FIRST and every column is pinned after it:
115
- * a stored `id`, `userId` or `organizationId` in `data` must not decide who
116
- * owns the entity or who sees it.
117
- * @private
118
- */
119
- _mapEntity(row, credential) {
120
- return {
121
- ...(row.data || {}),
122
- id: row.id.toString(),
123
- credential,
124
- userId: row.userId?.toString(),
125
- name: row.name,
126
- externalId: row.externalId,
127
- moduleName: row.moduleName,
128
- organizationId: row.organizationId?.toString() ?? null,
129
- };
130
- }
131
-
132
111
  /**
133
112
  * Find entity by ID with credential
134
113
  * Replaces: Entity.findById(entityId).populate('credential')
@@ -145,12 +124,20 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
145
124
  });
146
125
 
147
126
  if (!entity) {
148
- throw entityNotFoundError(entityId);
127
+ throw new Error(`Entity ${entityId} not found`);
149
128
  }
150
129
 
151
130
  const credential = await this._fetchCredential(entity.credentialId);
152
131
 
153
- return this._mapEntity(entity, credential);
132
+ return {
133
+ id: entity.id.toString(),
134
+ credential,
135
+ userId: entity.userId?.toString(),
136
+ name: entity.name,
137
+ externalId: entity.externalId,
138
+ moduleName: entity.moduleName,
139
+ ...(entity.data || {}),
140
+ };
154
141
  }
155
142
 
156
143
  /**
@@ -170,7 +157,15 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
170
157
  const credentialIds = entities.map(e => e.credentialId).filter(Boolean);
171
158
  const credentialMap = await this._fetchCredentialsBulk(credentialIds);
172
159
 
173
- return entities.map((e) => (this._mapEntity(e, credentialMap.get(e.credentialId) || null)));
160
+ return entities.map((e) => ({
161
+ id: e.id.toString(),
162
+ credential: credentialMap.get(e.credentialId) || null,
163
+ userId: e.userId?.toString(),
164
+ name: e.name,
165
+ externalId: e.externalId,
166
+ moduleName: e.moduleName,
167
+ ...(e.data || {}),
168
+ }));
174
169
  }
175
170
 
176
171
  /**
@@ -190,7 +185,15 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
190
185
  const credentialIds = entities.map(e => e.credentialId).filter(Boolean);
191
186
  const credentialMap = await this._fetchCredentialsBulk(credentialIds);
192
187
 
193
- return entities.map((e) => (this._mapEntity(e, credentialMap.get(e.credentialId) || null)));
188
+ return entities.map((e) => ({
189
+ id: e.id.toString(),
190
+ credential: credentialMap.get(e.credentialId) || null,
191
+ userId: e.userId?.toString(),
192
+ name: e.name,
193
+ externalId: e.externalId,
194
+ moduleName: e.moduleName,
195
+ ...(e.data || {}),
196
+ }));
194
197
  }
195
198
 
196
199
  /**
@@ -214,32 +217,15 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
214
217
  const credentialIds = entities.map(e => e.credentialId).filter(Boolean);
215
218
  const credentialMap = await this._fetchCredentialsBulk(credentialIds);
216
219
 
217
- return entities.map((e) => (this._mapEntity(e, credentialMap.get(e.credentialId) || null)));
218
- }
219
-
220
- /**
221
- * Find entities matching any of the owner/organization pairs, in one query.
222
- * `organizationId: null` matches rows with no organization. The caller
223
- * (a use case) decides which pairs; this only runs them.
224
- *
225
- * @param {Array<{userId: string, organizationId: (string|null)}>} clauses
226
- * @returns {Promise<Array>} Array of entity objects with string IDs
227
- */
228
- async findEntitiesByOwnership(clauses) {
229
- if (!Array.isArray(clauses) || clauses.length === 0) return [];
230
- const entities = await this.prisma.entity.findMany({
231
- where: {
232
- OR: clauses.map(({ userId, organizationId }) => ({
233
- userId: this._convertId(userId),
234
- organizationId: this._convertId(organizationId),
235
- })),
236
- },
237
- });
238
-
239
- const credentialIds = entities.map(e => e.credentialId).filter(Boolean);
240
- const credentialMap = await this._fetchCredentialsBulk(credentialIds);
241
-
242
- return entities.map((e) => (this._mapEntity(e, credentialMap.get(e.credentialId) || null)));
220
+ return entities.map((e) => ({
221
+ id: e.id.toString(),
222
+ credential: credentialMap.get(e.credentialId) || null,
223
+ userId: e.userId?.toString(),
224
+ name: e.name,
225
+ externalId: e.externalId,
226
+ moduleName: e.moduleName,
227
+ ...(e.data || {}),
228
+ }));
243
229
  }
244
230
 
245
231
  /**
@@ -278,7 +264,15 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
278
264
 
279
265
  const credential = await this._fetchCredential(entity.credentialId);
280
266
 
281
- return this._mapEntity(entity, credential);
267
+ return {
268
+ id: entity.id.toString(),
269
+ credential,
270
+ userId: entity.userId?.toString(),
271
+ name: entity.name,
272
+ externalId: entity.externalId,
273
+ moduleName: entity.moduleName,
274
+ ...(entity.data || {}),
275
+ };
282
276
  }
283
277
 
284
278
  /**
@@ -298,7 +292,15 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
298
292
  .filter(Boolean);
299
293
  const credentialMap = await this._fetchCredentialsBulk(credentialIds);
300
294
 
301
- return entities.map((e) => (this._mapEntity(e, credentialMap.get(e.credentialId) || null)));
295
+ return entities.map((e) => ({
296
+ id: e.id.toString(),
297
+ credential: credentialMap.get(e.credentialId) || null,
298
+ userId: e.userId?.toString(),
299
+ name: e.name,
300
+ externalId: e.externalId,
301
+ moduleName: e.moduleName,
302
+ ...(e.data || {}),
303
+ }));
302
304
  }
303
305
 
304
306
  /**
@@ -306,13 +308,9 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
306
308
  * Replaces: Entity.create(entityData)
307
309
  *
308
310
  * @param {Object} entityData - Entity data (with string IDs from application layer)
309
- * @param {{organizationId?: (string|null)}} [tenancy] - the organization the entity
310
- * belongs to (ADR-056 §3.6). A separate argument, not a field of `entityData`,
311
- * because `entityData` carries provider fields and one named `organizationId`
312
- * must not be taken for ours.
313
311
  * @returns {Promise<Object>} Created entity object with string IDs
314
312
  */
315
- async createEntity(entityData, tenancy = {}) {
313
+ async createEntity(entityData) {
316
314
  const {
317
315
  user,
318
316
  userId,
@@ -330,7 +328,6 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
330
328
  name,
331
329
  moduleName,
332
330
  externalId,
333
- organizationId: this._convertId(tenancy.organizationId ?? null),
334
331
  data: dynamicData,
335
332
  };
336
333
 
@@ -340,7 +337,15 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
340
337
 
341
338
  const credentialObj = await this._fetchCredential(entity.credentialId);
342
339
 
343
- return this._mapEntity(entity, credentialObj);
340
+ return {
341
+ id: entity.id.toString(),
342
+ credential: credentialObj,
343
+ userId: entity.userId?.toString(),
344
+ name: entity.name,
345
+ externalId: entity.externalId,
346
+ moduleName: entity.moduleName,
347
+ ...(entity.data || {}),
348
+ };
344
349
  }
345
350
 
346
351
  /**
@@ -396,7 +401,15 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
396
401
 
397
402
  const credentialObj = await this._fetchCredential(entity.credentialId);
398
403
 
399
- return this._mapEntity(entity, credentialObj);
404
+ return {
405
+ id: entity.id.toString(),
406
+ credential: credentialObj,
407
+ userId: entity.userId?.toString(),
408
+ name: entity.name,
409
+ externalId: entity.externalId,
410
+ moduleName: entity.moduleName,
411
+ ...(entity.data || {}),
412
+ };
400
413
  } catch (error) {
401
414
  if (error.code === 'P2025') {
402
415
  return null;
@@ -461,11 +474,6 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
461
474
  if (filter.moduleName) where.moduleName = filter.moduleName;
462
475
  if (filter.externalId) where.externalId = this._toString(filter.externalId);
463
476
 
464
- // `null` means "no organization"; absent means "any".
465
- if (filter.organizationId !== undefined) {
466
- where.organizationId = this._convertId(filter.organizationId);
467
- }
468
-
469
477
  return where;
470
478
  }
471
479
  }
@@ -1,6 +1,5 @@
1
1
  const { prisma } = require('../../database/prisma');
2
2
  const { ModuleRepositoryInterface } = require('./module-repository-interface');
3
- const { entityNotFoundError } = require('./entity-not-found');
4
3
 
5
4
  /**
6
5
  * Prisma-based Module Repository
@@ -38,7 +37,7 @@ class ModuleRepository extends ModuleRepositoryInterface {
38
37
  });
39
38
 
40
39
  if (!entity) {
41
- throw entityNotFoundError(entityId);
40
+ throw new Error(`Entity ${entityId} not found`);
42
41
  }
43
42
 
44
43
  return {