@friggframework/core 2.0.0--canary.522.cbd3d5a.0 → 2.0.0--canary.517.35ee143.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 (225) hide show
  1. package/CHANGELOG.md +67 -66
  2. package/CLAUDE.md +124 -148
  3. package/README.md +130 -165
  4. package/admin-scripts/index.js +16 -30
  5. package/admin-scripts/repositories/{admin-api-key-repository-documentdb.js → admin-process-repository-documentdb.js} +5 -5
  6. package/admin-scripts/repositories/{admin-api-key-repository-factory.js → admin-process-repository-factory.js} +15 -19
  7. package/admin-scripts/repositories/admin-process-repository-interface.js +150 -0
  8. package/admin-scripts/repositories/admin-process-repository-mongo.js +213 -0
  9. package/admin-scripts/repositories/admin-process-repository-postgres.js +251 -0
  10. package/admin-scripts/repositories/script-schedule-repository-factory.js +2 -6
  11. package/admin-scripts/repositories/script-schedule-repository-interface.js +14 -36
  12. package/admin-scripts/repositories/script-schedule-repository-mongo.js +13 -25
  13. package/admin-scripts/repositories/script-schedule-repository-postgres.js +13 -25
  14. package/application/commands/admin-script-commands.js +102 -266
  15. package/application/commands/integration-commands.js +6 -12
  16. package/application/index.js +3 -1
  17. package/assertions/get.js +4 -1
  18. package/associations/association.js +57 -59
  19. package/associations/model.js +41 -41
  20. package/core/CLAUDE.md +156 -222
  21. package/core/Worker.js +1 -5
  22. package/credential/repositories/credential-repository-documentdb.js +1 -71
  23. package/credential/repositories/credential-repository-interface.js +2 -8
  24. package/credential/repositories/credential-repository-mongo.js +4 -45
  25. package/credential/repositories/credential-repository-postgres.js +3 -44
  26. package/credential/repositories/credential-repository.js +4 -6
  27. package/credential/use-cases/get-credential-for-user.js +3 -21
  28. package/credential/use-cases/update-authentication-status.js +2 -5
  29. package/database/MONGODB_TRANSACTION_FIX.md +52 -72
  30. package/database/adapters/lambda-invoker.js +7 -8
  31. package/database/config.js +13 -15
  32. package/database/documentdb-encryption-service.js +13 -29
  33. package/database/documentdb-utils.js +12 -21
  34. package/database/encryption/README.md +36 -53
  35. package/database/encryption/encryption-schema-registry.js +9 -21
  36. package/database/encryption/field-encryption-service.js +4 -15
  37. package/database/encryption/logger.js +2 -3
  38. package/database/encryption/prisma-encryption-extension.js +8 -5
  39. package/database/index.js +2 -4
  40. package/database/models/IndividualUser.js +15 -15
  41. package/database/models/OrganizationUser.js +4 -6
  42. package/database/models/UserModel.js +2 -2
  43. package/database/models/WebsocketConnection.js +1 -4
  44. package/database/mongoose.js +2 -2
  45. package/database/prisma.js +14 -22
  46. package/database/repositories/health-check-repository-documentdb.js +7 -22
  47. package/database/repositories/health-check-repository-factory.js +3 -9
  48. package/database/repositories/health-check-repository-interface.js +5 -15
  49. package/database/repositories/health-check-repository-mongodb.js +6 -9
  50. package/database/repositories/health-check-repository-postgres.js +1 -1
  51. package/database/repositories/migration-status-repository-s3.js +5 -10
  52. package/database/use-cases/check-database-health-use-case.js +2 -3
  53. package/database/use-cases/check-database-state-use-case.js +7 -9
  54. package/database/use-cases/get-database-state-via-worker-use-case.js +12 -15
  55. package/database/use-cases/get-migration-status-use-case.js +2 -4
  56. package/database/use-cases/run-database-migration-use-case.js +9 -41
  57. package/database/use-cases/test-encryption-use-case.js +5 -4
  58. package/database/use-cases/trigger-database-migration-use-case.js +8 -9
  59. package/database/utils/mongodb-collection-utils.js +3 -11
  60. package/database/utils/mongodb-schema-init.js +4 -10
  61. package/database/utils/prisma-runner.js +74 -144
  62. package/database/utils/prisma-schema-parser.js +2 -2
  63. package/docs/PROCESS_MANAGEMENT_QUEUE_SPEC.md +80 -135
  64. package/encrypt/Cryptor.js +2 -8
  65. package/errors/fetch-error.js +1 -1
  66. package/generated/prisma-mongodb/edge.js +16 -59
  67. package/generated/prisma-mongodb/index-browser.js +12 -62
  68. package/generated/prisma-mongodb/index.d.ts +4850 -8136
  69. package/generated/prisma-mongodb/index.js +16 -59
  70. package/generated/prisma-mongodb/package.json +1 -1
  71. package/generated/prisma-mongodb/schema.prisma +33 -75
  72. package/generated/prisma-mongodb/wasm.js +16 -59
  73. package/generated/prisma-postgresql/edge.js +15 -59
  74. package/generated/prisma-postgresql/index-browser.js +11 -62
  75. package/generated/prisma-postgresql/index.d.ts +5963 -8930
  76. package/generated/prisma-postgresql/index.js +15 -59
  77. package/generated/prisma-postgresql/package.json +1 -1
  78. package/generated/prisma-postgresql/schema.prisma +33 -73
  79. package/generated/prisma-postgresql/wasm.js +15 -59
  80. package/handlers/WEBHOOKS.md +81 -104
  81. package/handlers/app-definition-loader.js +1 -1
  82. package/handlers/app-handler-helpers.js +2 -14
  83. package/handlers/backend-utils.js +1 -0
  84. package/handlers/database-migration-handler.js +45 -64
  85. package/handlers/middleware/admin-auth.js +53 -0
  86. package/handlers/routers/HEALTHCHECK.md +135 -170
  87. package/handlers/routers/auth.js +2 -23
  88. package/handlers/routers/db-migration.handler.js +3 -2
  89. package/handlers/routers/db-migration.js +33 -69
  90. package/handlers/routers/health.js +8 -23
  91. package/handlers/routers/integration-defined-routers.js +4 -4
  92. package/handlers/routers/integration-webhook-routers.js +7 -17
  93. package/handlers/routers/websocket.js +3 -9
  94. package/handlers/use-cases/check-integrations-health-use-case.js +6 -13
  95. package/handlers/workers/db-migration.js +26 -54
  96. package/index.js +9 -5
  97. package/integrations/WEBHOOK-QUICKSTART.md +14 -21
  98. package/integrations/index.js +0 -4
  99. package/integrations/integration-base.js +6 -9
  100. package/integrations/integration-router.js +146 -1817
  101. package/integrations/options.js +20 -47
  102. package/integrations/repositories/integration-repository-documentdb.js +24 -53
  103. package/integrations/repositories/integration-repository-factory.js +2 -6
  104. package/integrations/repositories/integration-repository-interface.js +9 -27
  105. package/integrations/repositories/integration-repository-postgres.js +3 -3
  106. package/integrations/repositories/process-repository-factory.js +1 -0
  107. package/integrations/repositories/process-repository-interface.js +5 -9
  108. package/integrations/repositories/process-repository-mongo.js +3 -7
  109. package/integrations/repositories/process-repository-postgres.js +2 -2
  110. package/integrations/tests/doubles/config-capturing-integration.js +8 -9
  111. package/integrations/tests/doubles/dummy-integration-class.js +8 -81
  112. package/integrations/tests/doubles/test-integration-repository.js +11 -51
  113. package/integrations/use-cases/create-integration.js +32 -36
  114. package/integrations/use-cases/create-process.js +9 -20
  115. package/integrations/use-cases/delete-integration-for-user.js +4 -5
  116. package/integrations/use-cases/get-integration-for-user.js +10 -25
  117. package/integrations/use-cases/get-integration-instance-by-definition.js +7 -13
  118. package/integrations/use-cases/get-integrations-for-user.js +1 -4
  119. package/integrations/use-cases/get-possible-integrations.js +1 -1
  120. package/integrations/use-cases/get-process.js +6 -5
  121. package/integrations/use-cases/index.js +1 -3
  122. package/integrations/use-cases/load-integration-context.js +5 -1
  123. package/integrations/use-cases/update-process-state.js +8 -10
  124. package/integrations/utils/map-integration-dto.js +3 -9
  125. package/jest-setup.js +1 -1
  126. package/jest-teardown.js +1 -1
  127. package/lambda/TimeoutCatcher.js +30 -32
  128. package/lambda/index.js +1 -1
  129. package/logs/index.js +3 -3
  130. package/modules/module.js +1 -3
  131. package/modules/repositories/module-repository-documentdb.js +48 -114
  132. package/modules/repositories/module-repository-interface.js +0 -13
  133. package/modules/repositories/module-repository-mongo.js +6 -46
  134. package/modules/repositories/module-repository-postgres.js +6 -48
  135. package/modules/repositories/module-repository.js +1 -34
  136. package/modules/requester/api-key.js +2 -0
  137. package/modules/requester/basic.js +1 -0
  138. package/modules/requester/oauth-2.js +2 -3
  139. package/modules/test/mock-api/definition.js +10 -15
  140. package/modules/test/mock-api/mocks/hubspot.js +36 -36
  141. package/modules/tests/doubles/test-module-factory.js +4 -10
  142. package/modules/tests/doubles/test-module-repository.js +1 -1
  143. package/modules/use-cases/get-entities-for-user.js +1 -1
  144. package/modules/use-cases/get-entity-options-by-id.js +6 -8
  145. package/modules/use-cases/get-module.js +9 -12
  146. package/modules/use-cases/process-authorization-callback.js +6 -12
  147. package/modules/use-cases/refresh-entity-options.js +6 -8
  148. package/modules/use-cases/test-module-auth.js +6 -8
  149. package/modules/utils/map-module-dto.js +2 -2
  150. package/package.json +5 -7
  151. package/prisma-mongodb/schema.prisma +33 -75
  152. package/prisma-postgresql/schema.prisma +33 -73
  153. package/queues/queuer-util.js +1 -5
  154. package/syncs/model.js +51 -51
  155. package/syncs/repositories/sync-repository-documentdb.js +19 -39
  156. package/syncs/repositories/sync-repository-interface.js +2 -6
  157. package/syncs/repositories/sync-repository-postgres.js +13 -19
  158. package/syncs/sync.js +100 -104
  159. package/token/repositories/token-repository-factory.js +3 -1
  160. package/types/assertions/index.d.ts +72 -72
  161. package/types/associations/index.d.ts +57 -57
  162. package/types/core/index.d.ts +53 -59
  163. package/types/database/index.d.ts +2 -2
  164. package/types/encrypt/index.d.ts +3 -3
  165. package/types/errors/index.d.ts +54 -61
  166. package/types/eslint-config/index.d.ts +40 -40
  167. package/types/integrations/index.d.ts +183 -185
  168. package/types/lambda/index.d.ts +26 -26
  169. package/types/logs/index.d.ts +4 -4
  170. package/types/module-plugin/index.d.ts +237 -239
  171. package/types/prettier-config/index.d.ts +5 -5
  172. package/types/syncs/index.d.ts +108 -102
  173. package/types/test-environment/index.d.ts +14 -14
  174. package/types/tsconfig.json +93 -95
  175. package/user/tests/doubles/test-user-repository.js +2 -5
  176. package/user/use-cases/authenticate-user.js +4 -2
  177. package/user/use-cases/authenticate-with-shared-secret.js +1 -1
  178. package/user/use-cases/create-individual-user.js +7 -8
  179. package/user/use-cases/create-organization-user.js +1 -1
  180. package/user/use-cases/create-token-for-user-id.js +1 -1
  181. package/user/use-cases/get-user-from-adopter-jwt.js +2 -0
  182. package/user/use-cases/get-user-from-bearer-token.js +3 -7
  183. package/user/use-cases/get-user-from-x-frigg-headers.js +11 -14
  184. package/user/use-cases/login-user.js +8 -8
  185. package/user/user.js +4 -18
  186. package/utils/backend-path.js +2 -2
  187. package/utils/index.js +1 -4
  188. package/websocket/repositories/websocket-connection-repository-documentdb.js +8 -21
  189. package/websocket/repositories/websocket-connection-repository-mongo.js +4 -8
  190. package/websocket/repositories/websocket-connection-repository-postgres.js +4 -8
  191. package/websocket/repositories/websocket-connection-repository.js +4 -8
  192. package/admin-scripts/repositories/admin-api-key-repository-interface.js +0 -121
  193. package/admin-scripts/repositories/admin-api-key-repository-mongo.js +0 -155
  194. package/admin-scripts/repositories/admin-api-key-repository-postgres.js +0 -189
  195. package/admin-scripts/repositories/script-execution-repository-documentdb.js +0 -21
  196. package/admin-scripts/repositories/script-execution-repository-factory.js +0 -55
  197. package/admin-scripts/repositories/script-execution-repository-interface.js +0 -193
  198. package/admin-scripts/repositories/script-execution-repository-mongo.js +0 -278
  199. package/admin-scripts/repositories/script-execution-repository-postgres.js +0 -320
  200. package/credential/credential-router.js +0 -262
  201. package/credential/use-cases/delete-credential-for-user.js +0 -50
  202. package/credential/use-cases/list-credentials-for-user.js +0 -31
  203. package/credential/use-cases/reauthorize-credential.js +0 -103
  204. package/handlers/routers/admin.js +0 -450
  205. package/handlers/routers/docs.js +0 -180
  206. package/handlers/routers/middleware/loadUser.js +0 -39
  207. package/handlers/routers/middleware/requireAdmin.js +0 -41
  208. package/handlers/routers/middleware/requireLoggedInUser.js +0 -19
  209. package/integrations/use-cases/execute-proxy-request.js +0 -564
  210. package/modules/domain/entities/AuthorizationSession.js +0 -131
  211. package/modules/domain/entities/index.js +0 -10
  212. package/modules/repositories/authorization-session-repository-factory.js +0 -55
  213. package/modules/repositories/authorization-session-repository-interface.js +0 -75
  214. package/modules/repositories/authorization-session-repository-mongo.js +0 -169
  215. package/modules/repositories/authorization-session-repository-postgres.js +0 -170
  216. package/modules/use-cases/delete-module-entity.js +0 -23
  217. package/modules/use-cases/get-authorization-requirements.js +0 -110
  218. package/modules/use-cases/get-module-entity-by-id.js +0 -16
  219. package/modules/use-cases/process-authorization-step.js +0 -160
  220. package/modules/use-cases/start-authorization-session.js +0 -86
  221. package/modules/use-cases/update-module-entity.js +0 -27
  222. package/openapi/openapi-spec-generator.js +0 -370
  223. package/openapi/openapi-v1.yaml +0 -1360
  224. package/openapi/openapi-v2.yaml +0 -1535
  225. package/user/use-cases/delete-user.js +0 -47
@@ -78,26 +78,6 @@ model Token {
78
78
  @@index([expires])
79
79
  }
80
80
 
81
- /// Multi-step authorization session tracking
82
- /// Supports OTP flows and multi-stage authentication (e.g., Nagaris)
83
- model AuthorizationSession {
84
- id Int @id @default(autoincrement())
85
- sessionId String @unique
86
- userId String
87
- entityType String
88
- currentStep Int @default(1)
89
- maxSteps Int
90
- stepData Json @default("{}")
91
- expiresAt DateTime
92
- completed Boolean @default(false)
93
- createdAt DateTime @default(now())
94
- updatedAt DateTime @updatedAt
95
-
96
- @@index([sessionId])
97
- @@index([userId, entityType])
98
- @@index([expiresAt])
99
- }
100
-
101
81
  // ============================================================================
102
82
  // CREDENTIAL & ENTITY MODELS
103
83
  // ============================================================================
@@ -135,7 +115,6 @@ model Entity {
135
115
  name String?
136
116
  moduleName String?
137
117
  externalId String?
138
- isGlobal Boolean @default(false)
139
118
 
140
119
  createdAt DateTime @default(now())
141
120
  updatedAt DateTime @updatedAt
@@ -151,8 +130,6 @@ model Entity {
151
130
  @@index([externalId])
152
131
  @@index([moduleName])
153
132
  @@index([credentialId])
154
- @@index([isGlobal])
155
- @@index([isGlobal, moduleName])
156
133
  }
157
134
 
158
135
  // ============================================================================
@@ -366,68 +343,51 @@ model WebsocketConnection {
366
343
  }
367
344
 
368
345
  // ============================================================================
369
- // ADMIN SCRIPT RUNNER MODELS
346
+ // ADMIN PROCESS MODELS
370
347
  // ============================================================================
371
348
 
372
- enum ScriptExecutionStatus {
349
+ /// Admin process state machine
350
+ enum AdminProcessState {
373
351
  PENDING
374
352
  RUNNING
375
353
  COMPLETED
376
354
  FAILED
377
- TIMEOUT
378
- CANCELLED
379
355
  }
380
356
 
381
- enum ScriptTrigger {
357
+ /// Admin trigger types
358
+ enum AdminTrigger {
382
359
  MANUAL
383
360
  SCHEDULED
384
361
  QUEUE
385
362
  WEBHOOK
386
363
  }
387
364
 
388
- /// Admin API keys for script execution authentication
389
- /// Key hashes stored with bcrypt
390
- model AdminApiKey {
391
- id Int @id @default(autoincrement())
392
- keyHash String @unique
393
- keyLast4 String
394
- name String
395
- scopes String[]
396
- expiresAt DateTime?
397
- createdBy String?
398
- lastUsedAt DateTime?
399
- isActive Boolean @default(true)
400
- createdAt DateTime @default(now())
401
- updatedAt DateTime @updatedAt
402
-
403
- @@index([isActive])
404
- }
365
+ /// Admin process tracking (like Process but without user/integration FK)
366
+ /// Used for: admin scripts, db migrations, system maintenance tasks
367
+ model AdminProcess {
368
+ id Int @id @default(autoincrement())
369
+ name String // e.g., "oauth-token-refresh", "db-migration-xyz"
370
+ type String // e.g., "ADMIN_SCRIPT", "DB_MIGRATION"
405
371
 
406
- /// Script execution tracking and audit log
407
- model ScriptExecution {
408
- id Int @id @default(autoincrement())
409
- scriptName String
410
- scriptVersion String?
411
- status ScriptExecutionStatus @default(PENDING)
412
- trigger ScriptTrigger
413
- mode String @default("async")
414
- input Json?
415
- output Json?
416
- logs Json[]
417
- metricsStartTime DateTime?
418
- metricsEndTime DateTime?
419
- metricsDurationMs Int?
420
- errorName String?
421
- errorMessage String?
422
- errorStack String?
423
- auditApiKeyName String?
424
- auditApiKeyLast4 String?
425
- auditIpAddress String?
426
- createdAt DateTime @default(now())
427
- updatedAt DateTime @updatedAt
428
-
429
- @@index([scriptName, createdAt(sort: Desc)])
430
- @@index([status])
372
+ // State machine
373
+ state AdminProcessState @default(PENDING)
374
+
375
+ // Flexible storage (mirrors Process model pattern)
376
+ context Json @default("{}") // input, trigger, audit info, script version
377
+ results Json @default("{}") // output, logs, metrics, errors
378
+
379
+ // Hierarchy support - self-referential relation
380
+ parentProcessId Int?
381
+ parentProcess AdminProcess? @relation("AdminProcessHierarchy", fields: [parentProcessId], references: [id], onDelete: SetNull)
382
+ childProcesses AdminProcess[] @relation("AdminProcessHierarchy")
383
+
384
+ // Timestamps
385
+ createdAt DateTime @default(now())
386
+ updatedAt DateTime @updatedAt
387
+
388
+ @@index([name, createdAt(sort: Desc)])
389
+ @@index([state])
390
+ @@index([type])
431
391
  }
432
392
 
433
393
  /// Script scheduling configuration for hybrid scheduling (SQS + EventBridge)
@@ -440,9 +400,9 @@ model ScriptSchedule {
440
400
  lastTriggeredAt DateTime?
441
401
  nextTriggerAt DateTime?
442
402
 
443
- // AWS EventBridge Schedule (if provisioned)
444
- awsScheduleArn String?
445
- awsScheduleName String?
403
+ // External Scheduler (e.g., AWS EventBridge Scheduler)
404
+ externalScheduleId String?
405
+ externalScheduleName String?
446
406
 
447
407
  createdAt DateTime @default(now())
448
408
  updatedAt DateTime @updatedAt
@@ -1,9 +1,5 @@
1
1
  const { v4: uuid } = require('uuid');
2
- const {
3
- SQSClient,
4
- SendMessageCommand,
5
- SendMessageBatchCommand,
6
- } = require('@aws-sdk/client-sqs');
2
+ const { SQSClient, SendMessageCommand, SendMessageBatchCommand } = require('@aws-sdk/client-sqs');
7
3
 
8
4
  const awsConfigOptions = () => {
9
5
  const config = {};
package/syncs/model.js CHANGED
@@ -1,62 +1,62 @@
1
- const mongoose = require('mongoose');
1
+ const mongoose = require("mongoose");
2
2
 
3
3
  const schema = new mongoose.Schema({
4
- entities: [
5
- { type: mongoose.Schema.Types.ObjectId, ref: 'Entity', required: true },
6
- ],
7
- hash: { type: String, required: true },
8
- name: { type: String, required: true },
9
- dataIdentifiers: [
10
- {
11
- entity: {
12
- type: mongoose.Schema.Types.ObjectId,
13
- ref: 'Entity',
14
- required: true,
15
- },
16
- id: { type: Object, required: true },
17
- hash: { type: String, required: true },
18
- },
19
- ],
4
+ entities: [
5
+ { type: mongoose.Schema.Types.ObjectId, ref: "Entity", required: true },
6
+ ],
7
+ hash: { type: String, required: true },
8
+ name: { type: String, required: true },
9
+ dataIdentifiers: [
10
+ {
11
+ entity: {
12
+ type: mongoose.Schema.Types.ObjectId,
13
+ ref: "Entity",
14
+ required: true,
15
+ },
16
+ id: { type: Object, required: true },
17
+ hash: { type: String, required: true },
18
+ },
19
+ ],
20
20
  });
21
21
 
22
22
  schema.statics({
23
- getSyncObject: async function (name, dataIdentifier, entity) {
24
- // const syncList = await this.list({name:name,entities: {"$in": entities}, "entityIds.idHash":entityIdHash });
25
- const syncList = await this.find({
26
- name: name,
27
- dataIdentifiers: { $elemMatch: { id: dataIdentifier, entity } },
28
- });
23
+ getSyncObject: async function (name, dataIdentifier, entity) {
24
+ // const syncList = await this.list({name:name,entities: {"$in": entities}, "entityIds.idHash":entityIdHash });
25
+ const syncList = await this.find({
26
+ name: name,
27
+ dataIdentifiers: { $elemMatch: { id: dataIdentifier, entity } },
28
+ });
29
29
 
30
- if (syncList.length === 1) {
31
- return syncList[0];
32
- } else if (syncList.length === 0) {
33
- return null;
34
- } else {
35
- throw new Error(
36
- `There are multiple sync objects with the name ${name}, for entities [${syncList[0].entities}] [${syncList[1].entities}]`
37
- );
38
- }
39
- },
30
+ if (syncList.length === 1) {
31
+ return syncList[0];
32
+ } else if (syncList.length === 0) {
33
+ return null;
34
+ } else {
35
+ throw new Error(
36
+ `There are multiple sync objects with the name ${name}, for entities [${syncList[0].entities}] [${syncList[1].entities}]`
37
+ );
38
+ }
39
+ },
40
40
 
41
- addDataIdentifier: async function (id, dataIdentifier) {
42
- return await this.update(
43
- { _id: id },
44
- {},
45
- { dataIdentifiers: dataIdentifier }
46
- );
47
- },
41
+ addDataIdentifier: async function (id, dataIdentifier) {
42
+ return await this.update(
43
+ { _id: id },
44
+ {},
45
+ { dataIdentifiers: dataIdentifier }
46
+ );
47
+ },
48
48
 
49
- getEntityObjIdForEntityIdFromObject: function (syncObj, entityId) {
50
- for (let dataIdentifier of syncObj.dataIdentifiers) {
51
- if (dataIdentifier.entity.toString() === entityId) {
52
- return dataIdentifier.id;
53
- }
54
- }
55
- throw new Error(
56
- `Sync object does not have DataIdentifier for entityId: ${entityId}`
57
- );
58
- },
49
+ getEntityObjIdForEntityIdFromObject: function (syncObj, entityId) {
50
+ for (let dataIdentifier of syncObj.dataIdentifiers) {
51
+ if (dataIdentifier.entity.toString() === entityId) {
52
+ return dataIdentifier.id;
53
+ }
54
+ }
55
+ throw new Error(
56
+ `Sync object does not have DataIdentifier for entityId: ${entityId}`
57
+ );
58
+ },
59
59
  });
60
60
 
61
- const Sync = mongoose.models.Sync || mongoose.model('Sync', schema);
61
+ const Sync = mongoose.models.Sync || mongoose.model("Sync", schema);
62
62
  module.exports = { Sync };
@@ -69,9 +69,7 @@ class SyncRepositoryDocumentDB extends SyncRepositoryInterface {
69
69
  $set: documentData,
70
70
  }
71
71
  );
72
- const updated = await findOne(this.prisma, 'Sync', {
73
- _id: existing._id,
74
- });
72
+ const updated = await findOne(this.prisma, 'Sync', { _id: existing._id });
75
73
  return this._mapSync(updated);
76
74
  }
77
75
 
@@ -105,9 +103,7 @@ class SyncRepositoryDocumentDB extends SyncRepositoryInterface {
105
103
  const doc = await findOne(this.prisma, 'Sync', { _id: syncObjectId });
106
104
  if (!doc) return null;
107
105
 
108
- const identifiers = Array.isArray(doc.dataIdentifiers)
109
- ? [...doc.dataIdentifiers]
110
- : [];
106
+ const identifiers = Array.isArray(doc.dataIdentifiers) ? [...doc.dataIdentifiers] : [];
111
107
  identifiers.push({
112
108
  syncId: syncObjectId,
113
109
  entityId: toObjectId(dataIdentifier.entity),
@@ -128,9 +124,7 @@ class SyncRepositoryDocumentDB extends SyncRepositoryInterface {
128
124
  }
129
125
  );
130
126
 
131
- const updated = await findOne(this.prisma, 'Sync', {
132
- _id: syncObjectId,
133
- });
127
+ const updated = await findOne(this.prisma, 'Sync', { _id: syncObjectId });
134
128
  return updated ? this._mapSync(updated) : null;
135
129
  }
136
130
 
@@ -140,8 +134,7 @@ class SyncRepositoryDocumentDB extends SyncRepositoryInterface {
140
134
  }
141
135
 
142
136
  const entry = syncObj.dataIdentifiers.find(
143
- (identifier) =>
144
- fromObjectId(identifier.entityId) === String(entityId)
137
+ (identifier) => fromObjectId(identifier.entityId) === String(entityId)
145
138
  );
146
139
 
147
140
  if (entry) {
@@ -185,9 +178,7 @@ class SyncRepositoryDocumentDB extends SyncRepositoryInterface {
185
178
  query.integrationId = toObjectId(filter.integrationId);
186
179
  }
187
180
  if (filter.entities) {
188
- query.entityIds = (filter.entities || [])
189
- .map((id) => toObjectId(id))
190
- .filter(Boolean);
181
+ query.entityIds = (filter.entities || []).map((id) => toObjectId(id)).filter(Boolean);
191
182
  delete query.entities;
192
183
  }
193
184
  return query;
@@ -199,11 +190,8 @@ class SyncRepositoryDocumentDB extends SyncRepositoryInterface {
199
190
  prepared.integrationId = toObjectId(data.integrationId);
200
191
  }
201
192
  if (data.entities !== undefined || data.entityIds !== undefined) {
202
- const list =
203
- data.entities !== undefined ? data.entities : data.entityIds;
204
- prepared.entityIds = (list || [])
205
- .map((id) => toObjectId(id))
206
- .filter(Boolean);
193
+ const list = data.entities !== undefined ? data.entities : data.entityIds;
194
+ prepared.entityIds = (list || []).map((id) => toObjectId(id)).filter(Boolean);
207
195
  }
208
196
  if (data.hash !== undefined) prepared.hash = data.hash;
209
197
  if (data.name !== undefined) prepared.name = data.name;
@@ -211,17 +199,13 @@ class SyncRepositoryDocumentDB extends SyncRepositoryInterface {
211
199
  if (data.results !== undefined) prepared.results = data.results;
212
200
  if (timestamp) prepared.updatedAt = timestamp;
213
201
  if (data.dataIdentifiers !== undefined) {
214
- prepared.dataIdentifiers = (data.dataIdentifiers || []).map(
215
- (identifier) => ({
216
- syncId: toObjectId(identifier.syncId),
217
- entityId: toObjectId(identifier.entityId),
218
- idData: identifier.idData,
219
- hash: identifier.hash,
220
- createdAt: identifier.createdAt
221
- ? new Date(identifier.createdAt)
222
- : new Date(),
223
- })
224
- );
202
+ prepared.dataIdentifiers = (data.dataIdentifiers || []).map((identifier) => ({
203
+ syncId: toObjectId(identifier.syncId),
204
+ entityId: toObjectId(identifier.entityId),
205
+ idData: identifier.idData,
206
+ hash: identifier.hash,
207
+ createdAt: identifier.createdAt ? new Date(identifier.createdAt) : new Date(),
208
+ }));
225
209
  }
226
210
  return prepared;
227
211
  }
@@ -230,9 +214,7 @@ class SyncRepositoryDocumentDB extends SyncRepositoryInterface {
230
214
  if (!doc) return null;
231
215
  return {
232
216
  id: fromObjectId(doc._id),
233
- integrationId: doc.integrationId
234
- ? fromObjectId(doc.integrationId)
235
- : null,
217
+ integrationId: doc.integrationId ? fromObjectId(doc.integrationId) : null,
236
218
  entities: Array.isArray(doc.entityIds)
237
219
  ? doc.entityIds.map((id) => fromObjectId(id))
238
220
  : [],
@@ -243,12 +225,8 @@ class SyncRepositoryDocumentDB extends SyncRepositoryInterface {
243
225
  name: doc.name ?? null,
244
226
  dataIdentifiers: Array.isArray(doc.dataIdentifiers)
245
227
  ? doc.dataIdentifiers.map((identifier) => ({
246
- syncId: identifier.syncId
247
- ? fromObjectId(identifier.syncId)
248
- : null,
249
- entityId: identifier.entityId
250
- ? fromObjectId(identifier.entityId)
251
- : null,
228
+ syncId: identifier.syncId ? fromObjectId(identifier.syncId) : null,
229
+ entityId: identifier.entityId ? fromObjectId(identifier.entityId) : null,
252
230
  idData: identifier.idData,
253
231
  hash: identifier.hash,
254
232
  }))
@@ -258,3 +236,5 @@ class SyncRepositoryDocumentDB extends SyncRepositoryInterface {
258
236
  }
259
237
 
260
238
  module.exports = { SyncRepositoryDocumentDB };
239
+
240
+
@@ -56,9 +56,7 @@ class SyncRepositoryInterface {
56
56
  * @abstract
57
57
  */
58
58
  async addDataIdentifier(syncId, dataIdentifier) {
59
- throw new Error(
60
- 'Method addDataIdentifier must be implemented by subclass'
61
- );
59
+ throw new Error('Method addDataIdentifier must be implemented by subclass');
62
60
  }
63
61
 
64
62
  /**
@@ -71,9 +69,7 @@ class SyncRepositoryInterface {
71
69
  * @abstract
72
70
  */
73
71
  getEntityObjIdForEntityIdFromObject(syncObj, entityId) {
74
- throw new Error(
75
- 'Method getEntityObjIdForEntityIdFromObject must be implemented by subclass'
76
- );
72
+ throw new Error('Method getEntityObjIdForEntityIdFromObject must be implemented by subclass');
77
73
  }
78
74
 
79
75
  /**
@@ -46,26 +46,24 @@ class SyncRepositoryPostgres extends SyncRepositoryInterface {
46
46
  ...sync,
47
47
  id: sync.id?.toString(),
48
48
  integrationId: sync.integrationId?.toString(),
49
- entities: sync.entities?.map((e) => ({
49
+ entities: sync.entities?.map(e => ({
50
50
  ...e,
51
51
  id: e.id?.toString(),
52
52
  userId: e.userId?.toString(),
53
- credentialId: e.credentialId?.toString(),
53
+ credentialId: e.credentialId?.toString()
54
54
  })),
55
- dataIdentifiers: sync.dataIdentifiers?.map((di) => ({
55
+ dataIdentifiers: sync.dataIdentifiers?.map(di => ({
56
56
  ...di,
57
57
  id: di.id?.toString(),
58
58
  syncId: di.syncId?.toString(),
59
59
  entityId: di.entityId?.toString(),
60
- entity: di.entity
61
- ? {
62
- ...di.entity,
63
- id: di.entity.id?.toString(),
64
- userId: di.entity.userId?.toString(),
65
- credentialId: di.entity.credentialId?.toString(),
66
- }
67
- : di.entity,
68
- })),
60
+ entity: di.entity ? {
61
+ ...di.entity,
62
+ id: di.entity.id?.toString(),
63
+ userId: di.entity.userId?.toString(),
64
+ credentialId: di.entity.credentialId?.toString()
65
+ } : di.entity
66
+ }))
69
67
  };
70
68
  }
71
69
 
@@ -125,9 +123,7 @@ class SyncRepositoryPostgres extends SyncRepositoryInterface {
125
123
  // Convert IDs in syncData if present
126
124
  const convertedData = { ...syncData };
127
125
  if (convertedData.integrationId) {
128
- convertedData.integrationId = this._convertId(
129
- convertedData.integrationId
130
- );
126
+ convertedData.integrationId = this._convertId(convertedData.integrationId);
131
127
  }
132
128
 
133
129
  if (existing) {
@@ -159,9 +155,7 @@ class SyncRepositoryPostgres extends SyncRepositoryInterface {
159
155
  // Convert IDs in updates if present
160
156
  const convertedUpdates = { ...updates };
161
157
  if (convertedUpdates.integrationId) {
162
- convertedUpdates.integrationId = this._convertId(
163
- convertedUpdates.integrationId
164
- );
158
+ convertedUpdates.integrationId = this._convertId(convertedUpdates.integrationId);
165
159
  }
166
160
 
167
161
  const updated = await this.prisma.sync.update({
@@ -247,7 +241,7 @@ class SyncRepositoryPostgres extends SyncRepositoryInterface {
247
241
  },
248
242
  },
249
243
  });
250
- return syncs.map((sync) => this._convertSyncIds(sync));
244
+ return syncs.map(sync => this._convertSyncIds(sync));
251
245
  }
252
246
 
253
247
  /**