@friggframework/core 2.0.0-next.44 → 2.0.0-next.46

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 (166) hide show
  1. package/README.md +28 -0
  2. package/application/commands/integration-commands.js +19 -0
  3. package/core/Worker.js +8 -21
  4. package/credential/repositories/credential-repository-mongo.js +14 -8
  5. package/credential/repositories/credential-repository-postgres.js +14 -8
  6. package/credential/repositories/credential-repository.js +3 -8
  7. package/database/MONGODB_TRANSACTION_FIX.md +198 -0
  8. package/database/adapters/lambda-invoker.js +97 -0
  9. package/database/config.js +11 -2
  10. package/database/models/WebsocketConnection.js +11 -10
  11. package/database/prisma.js +63 -3
  12. package/database/repositories/health-check-repository-mongodb.js +3 -0
  13. package/database/repositories/migration-status-repository-s3.js +137 -0
  14. package/database/use-cases/check-database-state-use-case.js +81 -0
  15. package/database/use-cases/check-encryption-health-use-case.js +3 -2
  16. package/database/use-cases/get-database-state-via-worker-use-case.js +61 -0
  17. package/database/use-cases/get-migration-status-use-case.js +93 -0
  18. package/database/use-cases/run-database-migration-use-case.js +137 -0
  19. package/database/use-cases/trigger-database-migration-use-case.js +157 -0
  20. package/database/utils/mongodb-collection-utils.js +91 -0
  21. package/database/utils/mongodb-schema-init.js +106 -0
  22. package/database/utils/prisma-runner.js +400 -0
  23. package/database/utils/prisma-schema-parser.js +182 -0
  24. package/encrypt/Cryptor.js +14 -16
  25. package/generated/prisma-mongodb/client.d.ts +1 -0
  26. package/generated/prisma-mongodb/client.js +4 -0
  27. package/generated/prisma-mongodb/default.d.ts +1 -0
  28. package/generated/prisma-mongodb/default.js +4 -0
  29. package/generated/prisma-mongodb/edge.d.ts +1 -0
  30. package/generated/prisma-mongodb/edge.js +334 -0
  31. package/generated/prisma-mongodb/index-browser.js +316 -0
  32. package/generated/prisma-mongodb/index.d.ts +22897 -0
  33. package/generated/prisma-mongodb/index.js +359 -0
  34. package/generated/prisma-mongodb/package.json +183 -0
  35. package/generated/prisma-mongodb/query-engine-debian-openssl-3.0.x +0 -0
  36. package/generated/prisma-mongodb/query-engine-rhel-openssl-3.0.x +0 -0
  37. package/generated/prisma-mongodb/runtime/binary.d.ts +1 -0
  38. package/generated/prisma-mongodb/runtime/binary.js +289 -0
  39. package/generated/prisma-mongodb/runtime/edge-esm.js +34 -0
  40. package/generated/prisma-mongodb/runtime/edge.js +34 -0
  41. package/generated/prisma-mongodb/runtime/index-browser.d.ts +370 -0
  42. package/generated/prisma-mongodb/runtime/index-browser.js +16 -0
  43. package/generated/prisma-mongodb/runtime/library.d.ts +3977 -0
  44. package/generated/prisma-mongodb/runtime/react-native.js +83 -0
  45. package/generated/prisma-mongodb/runtime/wasm-compiler-edge.js +84 -0
  46. package/generated/prisma-mongodb/runtime/wasm-engine-edge.js +36 -0
  47. package/generated/prisma-mongodb/schema.prisma +362 -0
  48. package/generated/prisma-mongodb/wasm-edge-light-loader.mjs +4 -0
  49. package/generated/prisma-mongodb/wasm-worker-loader.mjs +4 -0
  50. package/generated/prisma-mongodb/wasm.d.ts +1 -0
  51. package/generated/prisma-mongodb/wasm.js +341 -0
  52. package/generated/prisma-postgresql/client.d.ts +1 -0
  53. package/generated/prisma-postgresql/client.js +4 -0
  54. package/generated/prisma-postgresql/default.d.ts +1 -0
  55. package/generated/prisma-postgresql/default.js +4 -0
  56. package/generated/prisma-postgresql/edge.d.ts +1 -0
  57. package/generated/prisma-postgresql/edge.js +356 -0
  58. package/generated/prisma-postgresql/index-browser.js +338 -0
  59. package/generated/prisma-postgresql/index.d.ts +25071 -0
  60. package/generated/prisma-postgresql/index.js +381 -0
  61. package/generated/prisma-postgresql/package.json +183 -0
  62. package/generated/prisma-postgresql/query-engine-debian-openssl-3.0.x +0 -0
  63. package/generated/prisma-postgresql/query-engine-rhel-openssl-3.0.x +0 -0
  64. package/generated/prisma-postgresql/query_engine_bg.js +2 -0
  65. package/generated/prisma-postgresql/query_engine_bg.wasm +0 -0
  66. package/generated/prisma-postgresql/runtime/binary.d.ts +1 -0
  67. package/generated/prisma-postgresql/runtime/binary.js +289 -0
  68. package/generated/prisma-postgresql/runtime/edge-esm.js +34 -0
  69. package/generated/prisma-postgresql/runtime/edge.js +34 -0
  70. package/generated/prisma-postgresql/runtime/index-browser.d.ts +370 -0
  71. package/generated/prisma-postgresql/runtime/index-browser.js +16 -0
  72. package/generated/prisma-postgresql/runtime/library.d.ts +3977 -0
  73. package/generated/prisma-postgresql/runtime/react-native.js +83 -0
  74. package/generated/prisma-postgresql/runtime/wasm-compiler-edge.js +84 -0
  75. package/generated/prisma-postgresql/runtime/wasm-engine-edge.js +36 -0
  76. package/generated/prisma-postgresql/schema.prisma +345 -0
  77. package/generated/prisma-postgresql/wasm-edge-light-loader.mjs +4 -0
  78. package/generated/prisma-postgresql/wasm-worker-loader.mjs +4 -0
  79. package/generated/prisma-postgresql/wasm.d.ts +1 -0
  80. package/generated/prisma-postgresql/wasm.js +363 -0
  81. package/handlers/WEBHOOKS.md +653 -0
  82. package/handlers/backend-utils.js +118 -3
  83. package/handlers/database-migration-handler.js +227 -0
  84. package/handlers/routers/auth.js +1 -1
  85. package/handlers/routers/db-migration.handler.js +29 -0
  86. package/handlers/routers/db-migration.js +256 -0
  87. package/handlers/routers/health.js +41 -6
  88. package/handlers/routers/integration-webhook-routers.js +67 -0
  89. package/handlers/use-cases/check-integrations-health-use-case.js +22 -10
  90. package/handlers/workers/db-migration.js +352 -0
  91. package/index.js +28 -0
  92. package/integrations/WEBHOOK-QUICKSTART.md +151 -0
  93. package/integrations/integration-base.js +74 -3
  94. package/integrations/integration-router.js +60 -70
  95. package/integrations/repositories/integration-repository-interface.js +12 -0
  96. package/integrations/repositories/integration-repository-mongo.js +32 -0
  97. package/integrations/repositories/integration-repository-postgres.js +33 -0
  98. package/integrations/repositories/process-repository-postgres.js +43 -20
  99. package/integrations/tests/doubles/dummy-integration-class.js +1 -8
  100. package/integrations/tests/doubles/test-integration-repository.js +2 -2
  101. package/logs/logger.js +0 -4
  102. package/modules/entity.js +0 -1
  103. package/modules/repositories/module-repository-mongo.js +3 -12
  104. package/modules/repositories/module-repository-postgres.js +0 -11
  105. package/modules/repositories/module-repository.js +1 -12
  106. package/modules/use-cases/get-entity-options-by-id.js +1 -1
  107. package/modules/use-cases/get-module.js +1 -2
  108. package/modules/use-cases/refresh-entity-options.js +1 -1
  109. package/modules/use-cases/test-module-auth.js +1 -1
  110. package/package.json +82 -66
  111. package/prisma-mongodb/schema.prisma +21 -21
  112. package/prisma-postgresql/schema.prisma +15 -15
  113. package/queues/queuer-util.js +28 -15
  114. package/types/core/index.d.ts +2 -2
  115. package/types/module-plugin/index.d.ts +0 -2
  116. package/user/repositories/user-repository-mongo.js +53 -12
  117. package/user/repositories/user-repository-postgres.js +53 -14
  118. package/user/use-cases/authenticate-user.js +127 -0
  119. package/user/use-cases/authenticate-with-shared-secret.js +48 -0
  120. package/user/use-cases/get-user-from-adopter-jwt.js +149 -0
  121. package/user/use-cases/get-user-from-x-frigg-headers.js +106 -0
  122. package/user/use-cases/login-user.js +1 -1
  123. package/user/user.js +18 -2
  124. package/websocket/repositories/websocket-connection-repository-mongo.js +11 -10
  125. package/websocket/repositories/websocket-connection-repository-postgres.js +11 -10
  126. package/websocket/repositories/websocket-connection-repository.js +11 -10
  127. package/application/commands/integration-commands.test.js +0 -123
  128. package/database/encryption/encryption-integration.test.js +0 -553
  129. package/database/encryption/encryption-schema-registry.test.js +0 -392
  130. package/database/encryption/field-encryption-service.test.js +0 -525
  131. package/database/encryption/mongo-decryption-fix-verification.test.js +0 -348
  132. package/database/encryption/postgres-decryption-fix-verification.test.js +0 -371
  133. package/database/encryption/postgres-relation-decryption.test.js +0 -245
  134. package/database/encryption/prisma-encryption-extension.test.js +0 -439
  135. package/errors/base-error.test.js +0 -32
  136. package/errors/fetch-error.test.js +0 -79
  137. package/errors/halt-error.test.js +0 -11
  138. package/errors/validation-errors.test.js +0 -120
  139. package/handlers/auth-flow.integration.test.js +0 -147
  140. package/handlers/integration-event-dispatcher.test.js +0 -141
  141. package/handlers/routers/health.test.js +0 -210
  142. package/integrations/tests/use-cases/create-integration.test.js +0 -131
  143. package/integrations/tests/use-cases/delete-integration-for-user.test.js +0 -150
  144. package/integrations/tests/use-cases/find-integration-context-by-external-entity-id.test.js +0 -92
  145. package/integrations/tests/use-cases/get-integration-for-user.test.js +0 -150
  146. package/integrations/tests/use-cases/get-integration-instance.test.js +0 -176
  147. package/integrations/tests/use-cases/get-integrations-for-user.test.js +0 -176
  148. package/integrations/tests/use-cases/get-possible-integrations.test.js +0 -188
  149. package/integrations/tests/use-cases/update-integration-messages.test.js +0 -142
  150. package/integrations/tests/use-cases/update-integration-status.test.js +0 -103
  151. package/integrations/tests/use-cases/update-integration.test.js +0 -141
  152. package/integrations/use-cases/create-process.test.js +0 -178
  153. package/integrations/use-cases/get-process.test.js +0 -190
  154. package/integrations/use-cases/load-integration-context-full.test.js +0 -329
  155. package/integrations/use-cases/load-integration-context.test.js +0 -114
  156. package/integrations/use-cases/update-process-metrics.test.js +0 -308
  157. package/integrations/use-cases/update-process-state.test.js +0 -256
  158. package/lambda/TimeoutCatcher.test.js +0 -68
  159. package/logs/logger.test.js +0 -76
  160. package/modules/module-hydration.test.js +0 -205
  161. package/modules/requester/requester.test.js +0 -28
  162. package/user/tests/use-cases/create-individual-user.test.js +0 -24
  163. package/user/tests/use-cases/create-organization-user.test.js +0 -28
  164. package/user/tests/use-cases/create-token-for-user-id.test.js +0 -19
  165. package/user/tests/use-cases/get-user-from-bearer-token.test.js +0 -64
  166. package/user/tests/use-cases/login-user.test.js +0 -140
@@ -0,0 +1,149 @@
1
+ const Boom = require('@hapi/boom');
2
+
3
+ /**
4
+ * STUB: Use case for retrieving a user from adopter-provided JWT token.
5
+ *
6
+ * This is a stub implementation for future JWT authentication support.
7
+ * When implemented, this will allow adopters to use their own JWT tokens
8
+ * for authentication instead of Frigg's native token system.
9
+ *
10
+ * FUTURE IMPLEMENTATION REQUIREMENTS:
11
+ * - Validate JWT signature using jwtConfig.secret from app definition
12
+ * - Support configurable signing algorithms (HS256, HS384, HS512, RS256, RS384, RS512)
13
+ * - Extract user identifiers from JWT claims based on jwtConfig.userIdClaim and jwtConfig.orgIdClaim
14
+ * - Find or create user based on extracted claim values
15
+ * - Handle token expiration and validation errors
16
+ * - Support refresh tokens (optional)
17
+ * - Validate user ID conflicts if both individual and org IDs present in JWT
18
+ *
19
+ * RECOMMENDED IMPLEMENTATION:
20
+ * - Use 'jsonwebtoken' package for JWT parsing and validation
21
+ * - Cache JWT public keys for RS* algorithms
22
+ * - Add comprehensive error handling for invalid tokens
23
+ * - Log authentication attempts for security auditing
24
+ *
25
+ * @todo Implement JWT validation with jsonwebtoken package
26
+ * @todo Add unit tests for JWT parsing and claim extraction
27
+ * @todo Document adopter JWT integration guide in Frigg docs
28
+ * @todo Add support for JWT refresh tokens
29
+ * @todo Implement JWT public key caching for RS* algorithms
30
+ *
31
+ * @class GetUserFromAdopterJwt
32
+ */
33
+ class GetUserFromAdopterJwt {
34
+ /**
35
+ * Creates a new GetUserFromAdopterJwt instance.
36
+ * @param {Object} params - Configuration parameters.
37
+ * @param {import('../repositories/user-repository-interface').UserRepositoryInterface} params.userRepository - Repository for user data operations.
38
+ * @param {Object} params.userConfig - The user config in the app definition.
39
+ */
40
+ constructor({ userRepository, userConfig }) {
41
+ this.userRepository = userRepository;
42
+ this.userConfig = userConfig;
43
+ }
44
+
45
+ /**
46
+ * Executes the use case.
47
+ * @async
48
+ * @param {string} jwtToken - The JWT token from the Authorization header.
49
+ * @returns {Promise<import('../user').User>} The authenticated user object.
50
+ * @throws {Boom} 501 Not Implemented - This feature is not yet available.
51
+ */
52
+ async execute(jwtToken) {
53
+ throw Boom.notImplemented(
54
+ 'Adopter JWT authentication is not yet implemented. ' +
55
+ 'This feature is planned for a future Frigg release. ' +
56
+ 'Please use one of the supported authentication modes instead: ' +
57
+ 'friggToken (native bearer token) or xFriggHeaders (backend-to-backend with x-frigg-appUserId/appOrgId headers).'
58
+ );
59
+
60
+ /* FUTURE IMPLEMENTATION PSEUDOCODE:
61
+
62
+ const jwt = require('jsonwebtoken');
63
+
64
+ // Validate JWT configuration exists
65
+ if (!this.userConfig.jwtConfig || !this.userConfig.jwtConfig.secret) {
66
+ throw Boom.badImplementation('JWT configuration is required when adopterJwt auth mode is enabled');
67
+ }
68
+
69
+ try {
70
+ // Verify and decode JWT
71
+ const decoded = jwt.verify(jwtToken, this.userConfig.jwtConfig.secret, {
72
+ algorithms: [this.userConfig.jwtConfig.algorithm || 'HS256']
73
+ });
74
+
75
+ // Extract user identifiers from claims
76
+ const appUserId = decoded[this.userConfig.jwtConfig.userIdClaim || 'sub'];
77
+ const appOrgId = decoded[this.userConfig.jwtConfig.orgIdClaim || 'org_id'];
78
+
79
+ // At least one identifier required
80
+ if (!appUserId && !appOrgId) {
81
+ throw Boom.badRequest('JWT must contain user or organization identifier claims');
82
+ }
83
+
84
+ // Find existing users
85
+ let individualUserData = null;
86
+ let organizationUserData = null;
87
+
88
+ if (appUserId) {
89
+ individualUserData = await this.userRepository.findIndividualUserByAppUserId(appUserId);
90
+ }
91
+
92
+ if (appOrgId) {
93
+ organizationUserData = await this.userRepository.findOrganizationUserByAppOrgId(appOrgId);
94
+ }
95
+
96
+ // Validate no conflicts if both IDs present
97
+ if (appUserId && appOrgId && individualUserData && organizationUserData) {
98
+ const individualOrgId = individualUserData.organizationUser?.toString();
99
+ const expectedOrgId = organizationUserData.id?.toString();
100
+
101
+ if (individualOrgId !== expectedOrgId) {
102
+ throw Boom.badRequest(
103
+ 'User ID mismatch: JWT claims refer to different users. ' +
104
+ 'Individual and organization IDs must belong to the same user.'
105
+ );
106
+ }
107
+ }
108
+
109
+ // Auto-create if not found
110
+ if (!individualUserData && !organizationUserData) {
111
+ if (appUserId) {
112
+ individualUserData = await this.userRepository.createIndividualUser({
113
+ appUserId,
114
+ username: `jwt-user-${appUserId}`,
115
+ email: decoded.email || `${appUserId}@jwt.local`,
116
+ });
117
+ } else {
118
+ organizationUserData = await this.userRepository.createOrganizationUser({
119
+ appOrgId,
120
+ });
121
+ }
122
+ }
123
+
124
+ return new User(
125
+ individualUserData,
126
+ organizationUserData,
127
+ this.userConfig.usePassword,
128
+ this.userConfig.primary,
129
+ this.userConfig.individualUserRequired,
130
+ this.userConfig.organizationUserRequired
131
+ );
132
+
133
+ } catch (error) {
134
+ if (error.name === 'TokenExpiredError') {
135
+ throw Boom.unauthorized('JWT token has expired');
136
+ } else if (error.name === 'JsonWebTokenError') {
137
+ throw Boom.unauthorized('Invalid JWT token');
138
+ } else if (error.isBoom) {
139
+ throw error;
140
+ }
141
+ throw Boom.unauthorized('JWT authentication failed');
142
+ }
143
+ */
144
+ }
145
+ }
146
+
147
+ module.exports = { GetUserFromAdopterJwt };
148
+
149
+
@@ -0,0 +1,106 @@
1
+ const Boom = require('@hapi/boom');
2
+ const { User } = require('../user');
3
+
4
+ /**
5
+ * Use case for retrieving or creating a user from x-frigg header identifiers.
6
+ * Supports backend-to-backend API communication using application user IDs.
7
+ *
8
+ * @class GetUserFromXFriggHeaders
9
+ */
10
+ class GetUserFromXFriggHeaders {
11
+ /**
12
+ * Creates a new GetUserFromXFriggHeaders instance.
13
+ * @param {Object} params - Configuration parameters.
14
+ * @param {import('../repositories/user-repository-interface').UserRepositoryInterface} params.userRepository - Repository for user data operations.
15
+ * @param {Object} params.userConfig - The user config in the app definition.
16
+ */
17
+ constructor({ userRepository, userConfig }) {
18
+ this.userRepository = userRepository;
19
+ this.userConfig = userConfig;
20
+ }
21
+
22
+ /**
23
+ * Executes the use case.
24
+ * @async
25
+ * @param {string} [appUserId] - The app user ID from x-frigg-appUserId header.
26
+ * @param {string} [appOrgId] - The app organization ID from x-frigg-appOrgId header.
27
+ * @returns {Promise<import('../user').User>} The authenticated user object.
28
+ * @throws {Boom} 400 Bad Request if neither ID is provided or if both IDs are provided but belong to different users.
29
+ */
30
+ async execute(appUserId, appOrgId) {
31
+ // At least one header must be provided
32
+ if (!appUserId && !appOrgId) {
33
+ throw Boom.badRequest(
34
+ 'At least one of x-frigg-appUserId or x-frigg-appOrgId headers is required for backend-to-backend authentication'
35
+ );
36
+ }
37
+
38
+ // Find users by both IDs if both are provided
39
+ let individualUserData = null;
40
+ let organizationUserData = null;
41
+
42
+ if (appUserId && this.userConfig.individualUserRequired !== false) {
43
+ individualUserData =
44
+ await this.userRepository.findIndividualUserByAppUserId(
45
+ appUserId
46
+ );
47
+ }
48
+
49
+ if (appOrgId && this.userConfig.organizationUserRequired) {
50
+ organizationUserData =
51
+ await this.userRepository.findOrganizationUserByAppOrgId(
52
+ appOrgId
53
+ );
54
+ }
55
+
56
+ // VALIDATION: If both IDs provided and both users exist, verify they match
57
+ if (
58
+ appUserId &&
59
+ appOrgId &&
60
+ individualUserData &&
61
+ organizationUserData
62
+ ) {
63
+ // Check if individual user is linked to the org user
64
+ const individualOrgId =
65
+ individualUserData.organizationUser?.toString();
66
+ const expectedOrgId = organizationUserData.id?.toString();
67
+
68
+ if (individualOrgId !== expectedOrgId) {
69
+ throw Boom.badRequest(
70
+ 'User ID mismatch: x-frigg-appUserId and x-frigg-appOrgId refer to different users. ' +
71
+ 'Provide only one identifier or ensure they belong to the same user.'
72
+ );
73
+ }
74
+ }
75
+
76
+ // Auto-create user if not found
77
+ if (!individualUserData && !organizationUserData) {
78
+ if (appUserId) {
79
+ individualUserData =
80
+ await this.userRepository.createIndividualUser({
81
+ appUserId,
82
+ username: `app-user-${appUserId}`,
83
+ email: `${appUserId}@app.local`,
84
+ });
85
+ } else {
86
+ organizationUserData =
87
+ await this.userRepository.createOrganizationUser({
88
+ appOrgId,
89
+ });
90
+ }
91
+ }
92
+
93
+ return new User(
94
+ individualUserData,
95
+ organizationUserData,
96
+ this.userConfig.usePassword,
97
+ this.userConfig.primary,
98
+ this.userConfig.individualUserRequired,
99
+ this.userConfig.organizationUserRequired
100
+ );
101
+ }
102
+ }
103
+
104
+ module.exports = { GetUserFromXFriggHeaders };
105
+
106
+
@@ -65,7 +65,7 @@ class LoginUser {
65
65
  this.userConfig.organizationUserRequired
66
66
  );
67
67
 
68
- if (!individualUser.isPasswordValid(password)) {
68
+ if (!(await individualUser.isPasswordValid(password))) {
69
69
  throw Boom.unauthorized('Incorrect username or password');
70
70
  }
71
71
 
package/user/user.js CHANGED
@@ -41,12 +41,12 @@ class User {
41
41
  return this.usePassword;
42
42
  }
43
43
 
44
- isPasswordValid(password) {
44
+ async isPasswordValid(password) {
45
45
  if (!this.isPasswordRequired()) {
46
46
  return true;
47
47
  }
48
48
 
49
- return bcrypt.compareSync(password, this.getPrimaryUser().hashword);
49
+ return await bcrypt.compare(password, this.getPrimaryUser().hashword);
50
50
  }
51
51
 
52
52
  setIndividualUser(individualUser) {
@@ -72,6 +72,22 @@ class User {
72
72
  getOrganizationUser() {
73
73
  return this.organizationUser;
74
74
  }
75
+
76
+ /**
77
+ * Gets the appUserId from the individual user if present.
78
+ * @returns {string|null} The app user ID or null
79
+ */
80
+ getAppUserId() {
81
+ return this.individualUser?.appUserId || null;
82
+ }
83
+
84
+ /**
85
+ * Gets the appOrgId from the organization user if present.
86
+ * @returns {string|null} The app organization ID or null
87
+ */
88
+ getAppOrgId() {
89
+ return this.organizationUser?.appOrgId || null;
90
+ }
75
91
  }
76
92
 
77
93
  module.exports = { User };
@@ -1,5 +1,8 @@
1
1
  const { prisma } = require('../../database/prisma');
2
- const AWS = require('aws-sdk');
2
+ const {
3
+ ApiGatewayManagementApiClient,
4
+ PostToConnectionCommand,
5
+ } = require('@aws-sdk/client-apigatewaymanagementapi');
3
6
  const {
4
7
  WebsocketConnectionRepositoryInterface,
5
8
  } = require('./websocket-connection-repository-interface');
@@ -74,20 +77,18 @@ class WebsocketConnectionRepositoryMongo extends WebsocketConnectionRepositoryIn
74
77
  return connections.map((conn) => ({
75
78
  connectionId: conn.connectionId,
76
79
  send: async (data) => {
77
- const apigwManagementApi = new AWS.ApiGatewayManagementApi({
78
- apiVersion: '2018-11-29',
80
+ const apigwManagementApi = new ApiGatewayManagementApiClient({
79
81
  endpoint: process.env.WEBSOCKET_API_ENDPOINT,
80
82
  });
81
83
 
82
84
  try {
83
- await apigwManagementApi
84
- .postToConnection({
85
- ConnectionId: conn.connectionId,
86
- Data: JSON.stringify(data),
87
- })
88
- .promise();
85
+ const command = new PostToConnectionCommand({
86
+ ConnectionId: conn.connectionId,
87
+ Data: JSON.stringify(data),
88
+ });
89
+ await apigwManagementApi.send(command);
89
90
  } catch (error) {
90
- if (error.statusCode === 410) {
91
+ if (error.statusCode === 410 || error.$metadata?.httpStatusCode === 410) {
91
92
  console.log(
92
93
  `Stale connection ${conn.connectionId}`
93
94
  );
@@ -1,5 +1,8 @@
1
1
  const { prisma } = require('../../database/prisma');
2
- const AWS = require('aws-sdk');
2
+ const {
3
+ ApiGatewayManagementApiClient,
4
+ PostToConnectionCommand,
5
+ } = require('@aws-sdk/client-apigatewaymanagementapi');
3
6
  const {
4
7
  WebsocketConnectionRepositoryInterface,
5
8
  } = require('./websocket-connection-repository-interface');
@@ -108,20 +111,18 @@ class WebsocketConnectionRepositoryPostgres extends WebsocketConnectionRepositor
108
111
  return connections.map((conn) => ({
109
112
  connectionId: conn.connectionId,
110
113
  send: async (data) => {
111
- const apigwManagementApi = new AWS.ApiGatewayManagementApi({
112
- apiVersion: '2018-11-29',
114
+ const apigwManagementApi = new ApiGatewayManagementApiClient({
113
115
  endpoint: process.env.WEBSOCKET_API_ENDPOINT,
114
116
  });
115
117
 
116
118
  try {
117
- await apigwManagementApi
118
- .postToConnection({
119
- ConnectionId: conn.connectionId,
120
- Data: JSON.stringify(data),
121
- })
122
- .promise();
119
+ const command = new PostToConnectionCommand({
120
+ ConnectionId: conn.connectionId,
121
+ Data: JSON.stringify(data),
122
+ });
123
+ await apigwManagementApi.send(command);
123
124
  } catch (error) {
124
- if (error.statusCode === 410) {
125
+ if (error.statusCode === 410 || error.$metadata?.httpStatusCode === 410) {
125
126
  console.log(
126
127
  `Stale connection ${conn.connectionId}`
127
128
  );
@@ -1,5 +1,8 @@
1
1
  const { prisma } = require('../../database/prisma');
2
- const AWS = require('aws-sdk');
2
+ const {
3
+ ApiGatewayManagementApiClient,
4
+ PostToConnectionCommand,
5
+ } = require('@aws-sdk/client-apigatewaymanagementapi');
3
6
  const {
4
7
  WebsocketConnectionRepositoryInterface,
5
8
  } = require('./websocket-connection-repository-interface');
@@ -79,20 +82,18 @@ class WebsocketConnectionRepository extends WebsocketConnectionRepositoryInterfa
79
82
  return connections.map((conn) => ({
80
83
  connectionId: conn.connectionId,
81
84
  send: async (data) => {
82
- const apigwManagementApi = new AWS.ApiGatewayManagementApi({
83
- apiVersion: '2018-11-29',
85
+ const apigwManagementApi = new ApiGatewayManagementApiClient({
84
86
  endpoint: process.env.WEBSOCKET_API_ENDPOINT,
85
87
  });
86
88
 
87
89
  try {
88
- await apigwManagementApi
89
- .postToConnection({
90
- ConnectionId: conn.connectionId,
91
- Data: JSON.stringify(data),
92
- })
93
- .promise();
90
+ const command = new PostToConnectionCommand({
91
+ ConnectionId: conn.connectionId,
92
+ Data: JSON.stringify(data),
93
+ });
94
+ await apigwManagementApi.send(command);
94
95
  } catch (error) {
95
- if (error.statusCode === 410) {
96
+ if (error.statusCode === 410 || error.$metadata?.httpStatusCode === 410) {
96
97
  console.log(
97
98
  `Stale connection ${conn.connectionId}`
98
99
  );
@@ -1,123 +0,0 @@
1
- jest.mock('../../database/config', () => ({
2
- DB_TYPE: 'mongodb',
3
- getDatabaseType: jest.fn(() => 'mongodb'),
4
- PRISMA_LOG_LEVEL: 'error,warn',
5
- PRISMA_QUERY_LOGGING: false,
6
- }));
7
-
8
- const mockFindExecute = jest.fn();
9
-
10
- jest.mock('../../integrations/use-cases/find-integration-context-by-external-entity-id', () => {
11
- return {
12
- FindIntegrationContextByExternalEntityIdUseCase: jest
13
- .fn()
14
- .mockImplementation(() => ({
15
- execute: mockFindExecute,
16
- })),
17
- };
18
- });
19
-
20
- const {
21
- createIntegrationCommands,
22
- findIntegrationContextByExternalEntityId,
23
- } = require('./integration-commands');
24
- const {
25
- FindIntegrationContextByExternalEntityIdUseCase,
26
- } = require('../../integrations/use-cases/find-integration-context-by-external-entity-id');
27
- const { DummyIntegration } = require('../../integrations/tests/doubles/dummy-integration-class');
28
-
29
- describe('integration commands', () => {
30
- beforeEach(() => {
31
- jest.clearAllMocks();
32
- mockFindExecute.mockReset();
33
- });
34
-
35
- it('requires an integrationClass when creating commands', () => {
36
- expect(() => createIntegrationCommands()).toThrow(
37
- 'integrationClass is required',
38
- );
39
- });
40
-
41
- it('creates use cases with default repositories', () => {
42
- createIntegrationCommands({
43
- integrationClass: DummyIntegration,
44
- });
45
-
46
- // Verify that the use case is created with default repositories instantiated internally
47
- expect(
48
- FindIntegrationContextByExternalEntityIdUseCase,
49
- ).toHaveBeenCalledWith({
50
- integrationRepository: expect.any(Object),
51
- moduleRepository: expect.any(Object),
52
- loadIntegrationContextUseCase: expect.any(Object),
53
- });
54
- });
55
-
56
- it('returns context when findIntegrationContextByExternalEntityId succeeds', async () => {
57
- const expectedContext = { record: { id: 'integration-1' } };
58
- mockFindExecute.mockResolvedValue({ context: expectedContext });
59
- const commands = createIntegrationCommands({
60
- integrationClass: DummyIntegration,
61
- });
62
-
63
- const result = await commands.findIntegrationContextByExternalEntityId(
64
- 'ext-1',
65
- );
66
-
67
- expect(mockFindExecute).toHaveBeenCalledWith({
68
- externalEntityId: 'ext-1',
69
- });
70
- expect(result).toEqual({ context: expectedContext });
71
- });
72
-
73
- it('maps known errors to status codes', async () => {
74
- const error = Object.assign(new Error('Entity missing'), {
75
- code: 'ENTITY_NOT_FOUND',
76
- });
77
- mockFindExecute.mockRejectedValue(error);
78
- const commands = createIntegrationCommands({
79
- integrationClass: DummyIntegration,
80
- });
81
-
82
- const result = await commands.findIntegrationContextByExternalEntityId(
83
- 'ext-1',
84
- );
85
-
86
- expect(result).toEqual({
87
- error: 401,
88
- reason: 'Entity missing',
89
- code: 'ENTITY_NOT_FOUND',
90
- });
91
- });
92
-
93
- it('delegates loadIntegrationContextById to the loader use case', async () => {
94
- // This test verifies that the command properly delegates to the use case
95
- // We can't easily mock the internal use case, so we'll test the integration
96
- const commands = createIntegrationCommands({
97
- integrationClass: DummyIntegration,
98
- });
99
-
100
- // The actual use case will be called - this is more of an integration test
101
- // For unit testing, we'd need to refactor to allow DI of the use case
102
- // But since we've decided to always use default use cases, this is acceptable
103
- const result = await commands.loadIntegrationContextById('integration-1');
104
-
105
- // Result will have error since we don't have a real database
106
- expect(result).toHaveProperty('error');
107
- });
108
-
109
- it('exposes a one-off helper for finding integration context by external entity id', async () => {
110
- const expectedContext = { record: { id: 'integration-1' } };
111
- mockFindExecute.mockResolvedValue({ context: expectedContext });
112
-
113
- const result = await findIntegrationContextByExternalEntityId({
114
- integrationClass: DummyIntegration,
115
- externalEntityId: 'ext-2',
116
- });
117
-
118
- expect(mockFindExecute).toHaveBeenCalledWith({
119
- externalEntityId: 'ext-2',
120
- });
121
- expect(result).toEqual({ context: expectedContext });
122
- });
123
- });