@friggframework/core 2.0.0-next.5 → 2.0.0-next.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +693 -0
- package/README.md +959 -50
- package/application/commands/README.md +421 -0
- package/application/commands/credential-commands.js +224 -0
- package/application/commands/entity-commands.js +315 -0
- package/application/commands/integration-commands.js +179 -0
- package/application/commands/user-commands.js +213 -0
- package/application/index.js +69 -0
- package/core/CLAUDE.md +690 -0
- package/core/Worker.js +8 -21
- package/core/create-handler.js +2 -7
- package/credential/repositories/credential-repository-factory.js +47 -0
- package/credential/repositories/credential-repository-interface.js +98 -0
- package/credential/repositories/credential-repository-mongo.js +307 -0
- package/credential/repositories/credential-repository-postgres.js +313 -0
- package/credential/repositories/credential-repository.js +302 -0
- package/credential/use-cases/get-credential-for-user.js +21 -0
- package/credential/use-cases/update-authentication-status.js +15 -0
- package/database/MONGODB_TRANSACTION_FIX.md +198 -0
- package/database/adapters/lambda-invoker.js +97 -0
- package/database/config.js +154 -0
- package/database/encryption/README.md +684 -0
- package/database/encryption/encryption-schema-registry.js +141 -0
- package/database/encryption/field-encryption-service.js +226 -0
- package/database/encryption/logger.js +79 -0
- package/database/encryption/prisma-encryption-extension.js +222 -0
- package/database/index.js +25 -12
- package/database/models/WebsocketConnection.js +16 -10
- package/database/models/readme.md +1 -0
- package/database/prisma.js +222 -0
- package/database/repositories/health-check-repository-factory.js +43 -0
- package/database/repositories/health-check-repository-interface.js +87 -0
- package/database/repositories/health-check-repository-mongodb.js +91 -0
- package/database/repositories/health-check-repository-postgres.js +82 -0
- package/database/repositories/health-check-repository.js +108 -0
- package/database/repositories/migration-status-repository-s3.js +137 -0
- package/database/use-cases/check-database-health-use-case.js +29 -0
- package/database/use-cases/check-database-state-use-case.js +81 -0
- package/database/use-cases/check-encryption-health-use-case.js +83 -0
- package/database/use-cases/get-database-state-via-worker-use-case.js +61 -0
- package/database/use-cases/get-migration-status-use-case.js +93 -0
- package/database/use-cases/run-database-migration-use-case.js +137 -0
- package/database/use-cases/test-encryption-use-case.js +253 -0
- package/database/use-cases/trigger-database-migration-use-case.js +157 -0
- package/database/utils/mongodb-collection-utils.js +91 -0
- package/database/utils/mongodb-schema-init.js +106 -0
- package/database/utils/prisma-runner.js +400 -0
- package/database/utils/prisma-schema-parser.js +182 -0
- package/docs/PROCESS_MANAGEMENT_QUEUE_SPEC.md +517 -0
- package/encrypt/Cryptor.js +34 -168
- package/encrypt/index.js +1 -2
- package/encrypt/test-encrypt.js +0 -2
- package/generated/prisma-mongodb/client.d.ts +1 -0
- package/generated/prisma-mongodb/client.js +4 -0
- package/generated/prisma-mongodb/default.d.ts +1 -0
- package/generated/prisma-mongodb/default.js +4 -0
- package/generated/prisma-mongodb/edge.d.ts +1 -0
- package/generated/prisma-mongodb/edge.js +334 -0
- package/generated/prisma-mongodb/index-browser.js +316 -0
- package/generated/prisma-mongodb/index.d.ts +22898 -0
- package/generated/prisma-mongodb/index.js +359 -0
- package/generated/prisma-mongodb/package.json +183 -0
- package/generated/prisma-mongodb/query-engine-debian-openssl-3.0.x +0 -0
- package/generated/prisma-mongodb/query-engine-rhel-openssl-3.0.x +0 -0
- package/generated/prisma-mongodb/runtime/binary.d.ts +1 -0
- package/generated/prisma-mongodb/runtime/binary.js +289 -0
- package/generated/prisma-mongodb/runtime/edge-esm.js +34 -0
- package/generated/prisma-mongodb/runtime/edge.js +34 -0
- package/generated/prisma-mongodb/runtime/index-browser.d.ts +370 -0
- package/generated/prisma-mongodb/runtime/index-browser.js +16 -0
- package/generated/prisma-mongodb/runtime/library.d.ts +3982 -0
- package/generated/prisma-mongodb/runtime/react-native.js +83 -0
- package/generated/prisma-mongodb/runtime/wasm-compiler-edge.js +84 -0
- package/generated/prisma-mongodb/runtime/wasm-engine-edge.js +36 -0
- package/generated/prisma-mongodb/schema.prisma +362 -0
- package/generated/prisma-mongodb/wasm-edge-light-loader.mjs +4 -0
- package/generated/prisma-mongodb/wasm-worker-loader.mjs +4 -0
- package/generated/prisma-mongodb/wasm.d.ts +1 -0
- package/generated/prisma-mongodb/wasm.js +341 -0
- package/generated/prisma-postgresql/client.d.ts +1 -0
- package/generated/prisma-postgresql/client.js +4 -0
- package/generated/prisma-postgresql/default.d.ts +1 -0
- package/generated/prisma-postgresql/default.js +4 -0
- package/generated/prisma-postgresql/edge.d.ts +1 -0
- package/generated/prisma-postgresql/edge.js +356 -0
- package/generated/prisma-postgresql/index-browser.js +338 -0
- package/generated/prisma-postgresql/index.d.ts +25072 -0
- package/generated/prisma-postgresql/index.js +381 -0
- package/generated/prisma-postgresql/package.json +183 -0
- package/generated/prisma-postgresql/query-engine-debian-openssl-3.0.x +0 -0
- package/generated/prisma-postgresql/query-engine-rhel-openssl-3.0.x +0 -0
- package/generated/prisma-postgresql/query_engine_bg.js +2 -0
- package/generated/prisma-postgresql/query_engine_bg.wasm +0 -0
- package/generated/prisma-postgresql/runtime/binary.d.ts +1 -0
- package/generated/prisma-postgresql/runtime/binary.js +289 -0
- package/generated/prisma-postgresql/runtime/edge-esm.js +34 -0
- package/generated/prisma-postgresql/runtime/edge.js +34 -0
- package/generated/prisma-postgresql/runtime/index-browser.d.ts +370 -0
- package/generated/prisma-postgresql/runtime/index-browser.js +16 -0
- package/generated/prisma-postgresql/runtime/library.d.ts +3982 -0
- package/generated/prisma-postgresql/runtime/react-native.js +83 -0
- package/generated/prisma-postgresql/runtime/wasm-compiler-edge.js +84 -0
- package/generated/prisma-postgresql/runtime/wasm-engine-edge.js +36 -0
- package/generated/prisma-postgresql/schema.prisma +345 -0
- package/generated/prisma-postgresql/wasm-edge-light-loader.mjs +4 -0
- package/generated/prisma-postgresql/wasm-worker-loader.mjs +4 -0
- package/generated/prisma-postgresql/wasm.d.ts +1 -0
- package/generated/prisma-postgresql/wasm.js +363 -0
- package/handlers/WEBHOOKS.md +653 -0
- package/handlers/app-definition-loader.js +38 -0
- package/handlers/app-handler-helpers.js +56 -0
- package/handlers/backend-utils.js +180 -0
- package/handlers/database-migration-handler.js +227 -0
- package/handlers/integration-event-dispatcher.js +54 -0
- package/handlers/routers/HEALTHCHECK.md +342 -0
- package/handlers/routers/auth.js +15 -0
- package/handlers/routers/db-migration.handler.js +29 -0
- package/handlers/routers/db-migration.js +256 -0
- package/handlers/routers/health.js +519 -0
- package/handlers/routers/integration-defined-routers.js +45 -0
- package/handlers/routers/integration-webhook-routers.js +67 -0
- package/handlers/routers/user.js +63 -0
- package/handlers/routers/websocket.js +57 -0
- package/handlers/use-cases/check-external-apis-health-use-case.js +81 -0
- package/handlers/use-cases/check-integrations-health-use-case.js +44 -0
- package/handlers/workers/db-migration.js +352 -0
- package/handlers/workers/integration-defined-workers.js +27 -0
- package/index.js +77 -22
- package/integrations/WEBHOOK-QUICKSTART.md +151 -0
- package/integrations/index.js +12 -10
- package/integrations/integration-base.js +296 -54
- package/integrations/integration-router.js +381 -182
- package/integrations/options.js +1 -1
- package/integrations/repositories/integration-mapping-repository-factory.js +50 -0
- package/integrations/repositories/integration-mapping-repository-interface.js +106 -0
- package/integrations/repositories/integration-mapping-repository-mongo.js +161 -0
- package/integrations/repositories/integration-mapping-repository-postgres.js +227 -0
- package/integrations/repositories/integration-mapping-repository.js +156 -0
- package/integrations/repositories/integration-repository-factory.js +44 -0
- package/integrations/repositories/integration-repository-interface.js +127 -0
- package/integrations/repositories/integration-repository-mongo.js +303 -0
- package/integrations/repositories/integration-repository-postgres.js +352 -0
- package/integrations/repositories/process-repository-factory.js +46 -0
- package/integrations/repositories/process-repository-interface.js +90 -0
- package/integrations/repositories/process-repository-mongo.js +190 -0
- package/integrations/repositories/process-repository-postgres.js +217 -0
- package/integrations/tests/doubles/dummy-integration-class.js +83 -0
- package/integrations/tests/doubles/test-integration-repository.js +99 -0
- package/integrations/use-cases/create-integration.js +83 -0
- package/integrations/use-cases/create-process.js +128 -0
- package/integrations/use-cases/delete-integration-for-user.js +101 -0
- package/integrations/use-cases/find-integration-context-by-external-entity-id.js +72 -0
- package/integrations/use-cases/get-integration-for-user.js +78 -0
- package/integrations/use-cases/get-integration-instance-by-definition.js +67 -0
- package/integrations/use-cases/get-integration-instance.js +83 -0
- package/integrations/use-cases/get-integrations-for-user.js +87 -0
- package/integrations/use-cases/get-possible-integrations.js +27 -0
- package/integrations/use-cases/get-process.js +87 -0
- package/integrations/use-cases/index.js +19 -0
- package/integrations/use-cases/load-integration-context.js +71 -0
- package/integrations/use-cases/update-integration-messages.js +44 -0
- package/integrations/use-cases/update-integration-status.js +32 -0
- package/integrations/use-cases/update-integration.js +93 -0
- package/integrations/use-cases/update-process-metrics.js +201 -0
- package/integrations/use-cases/update-process-state.js +119 -0
- package/integrations/utils/map-integration-dto.js +36 -0
- package/jest-global-setup-noop.js +3 -0
- package/jest-global-teardown-noop.js +3 -0
- package/logs/logger.js +0 -4
- package/{module-plugin → modules}/entity.js +1 -1
- package/{module-plugin → modules}/index.js +0 -8
- package/modules/module-factory.js +56 -0
- package/modules/module.js +221 -0
- package/modules/repositories/module-repository-factory.js +33 -0
- package/modules/repositories/module-repository-interface.js +129 -0
- package/modules/repositories/module-repository-mongo.js +377 -0
- package/modules/repositories/module-repository-postgres.js +426 -0
- package/modules/repositories/module-repository.js +316 -0
- package/{module-plugin → modules}/requester/requester.js +1 -0
- package/{module-plugin → modules}/test/mock-api/api.js +8 -3
- package/{module-plugin → modules}/test/mock-api/definition.js +12 -8
- package/modules/tests/doubles/test-module-factory.js +16 -0
- package/modules/tests/doubles/test-module-repository.js +39 -0
- package/modules/use-cases/get-entities-for-user.js +32 -0
- package/modules/use-cases/get-entity-options-by-id.js +59 -0
- package/modules/use-cases/get-entity-options-by-type.js +34 -0
- package/modules/use-cases/get-module-instance-from-type.js +31 -0
- package/modules/use-cases/get-module.js +55 -0
- package/modules/use-cases/process-authorization-callback.js +122 -0
- package/modules/use-cases/refresh-entity-options.js +59 -0
- package/modules/use-cases/test-module-auth.js +55 -0
- package/modules/utils/map-module-dto.js +18 -0
- package/package.json +82 -50
- package/prisma-mongodb/schema.prisma +362 -0
- package/prisma-postgresql/migrations/20250930193005_init/migration.sql +315 -0
- package/prisma-postgresql/migrations/20251006135218_init/migration.sql +9 -0
- package/prisma-postgresql/migrations/20251010000000_remove_unused_entity_reference_map/migration.sql +3 -0
- package/prisma-postgresql/migrations/migration_lock.toml +3 -0
- package/prisma-postgresql/schema.prisma +345 -0
- package/queues/queuer-util.js +28 -15
- package/syncs/manager.js +468 -443
- package/syncs/repositories/sync-repository-factory.js +38 -0
- package/syncs/repositories/sync-repository-interface.js +109 -0
- package/syncs/repositories/sync-repository-mongo.js +239 -0
- package/syncs/repositories/sync-repository-postgres.js +319 -0
- package/syncs/sync.js +0 -1
- package/token/repositories/token-repository-factory.js +33 -0
- package/token/repositories/token-repository-interface.js +131 -0
- package/token/repositories/token-repository-mongo.js +212 -0
- package/token/repositories/token-repository-postgres.js +257 -0
- package/token/repositories/token-repository.js +219 -0
- package/types/core/index.d.ts +2 -2
- package/types/integrations/index.d.ts +2 -6
- package/types/module-plugin/index.d.ts +5 -59
- package/types/syncs/index.d.ts +0 -2
- package/user/repositories/user-repository-factory.js +46 -0
- package/user/repositories/user-repository-interface.js +198 -0
- package/user/repositories/user-repository-mongo.js +291 -0
- package/user/repositories/user-repository-postgres.js +350 -0
- package/user/tests/doubles/test-user-repository.js +72 -0
- package/user/use-cases/authenticate-user.js +127 -0
- package/user/use-cases/authenticate-with-shared-secret.js +48 -0
- package/user/use-cases/create-individual-user.js +61 -0
- package/user/use-cases/create-organization-user.js +47 -0
- package/user/use-cases/create-token-for-user-id.js +30 -0
- package/user/use-cases/get-user-from-adopter-jwt.js +149 -0
- package/user/use-cases/get-user-from-bearer-token.js +77 -0
- package/user/use-cases/get-user-from-x-frigg-headers.js +106 -0
- package/user/use-cases/login-user.js +122 -0
- package/user/user.js +93 -0
- package/utils/backend-path.js +38 -0
- package/utils/index.js +6 -0
- package/websocket/repositories/websocket-connection-repository-factory.js +37 -0
- package/websocket/repositories/websocket-connection-repository-interface.js +106 -0
- package/websocket/repositories/websocket-connection-repository-mongo.js +156 -0
- package/websocket/repositories/websocket-connection-repository-postgres.js +196 -0
- package/websocket/repositories/websocket-connection-repository.js +161 -0
- package/database/models/State.js +0 -9
- package/database/models/Token.js +0 -70
- package/database/mongo.js +0 -45
- package/encrypt/Cryptor.test.js +0 -32
- package/encrypt/encrypt.js +0 -132
- package/encrypt/encrypt.test.js +0 -1069
- package/errors/base-error.test.js +0 -32
- package/errors/fetch-error.test.js +0 -79
- package/errors/halt-error.test.js +0 -11
- package/errors/validation-errors.test.js +0 -120
- package/integrations/create-frigg-backend.js +0 -31
- package/integrations/integration-factory.js +0 -251
- package/integrations/integration-mapping.js +0 -43
- package/integrations/integration-model.js +0 -46
- package/integrations/integration-user.js +0 -144
- package/integrations/test/integration-base.test.js +0 -144
- package/lambda/TimeoutCatcher.test.js +0 -68
- package/logs/logger.test.js +0 -76
- package/module-plugin/auther.js +0 -393
- package/module-plugin/credential.js +0 -22
- package/module-plugin/entity-manager.js +0 -70
- package/module-plugin/manager.js +0 -169
- package/module-plugin/module-factory.js +0 -61
- package/module-plugin/requester/requester.test.js +0 -28
- package/module-plugin/test/auther.test.js +0 -97
- /package/{module-plugin → modules}/ModuleConstants.js +0 -0
- /package/{module-plugin → modules}/requester/api-key.js +0 -0
- /package/{module-plugin → modules}/requester/basic.js +0 -0
- /package/{module-plugin → modules}/requester/oauth-2.js +0 -0
- /package/{module-plugin → modules}/test/mock-api/mocks/hubspot.js +0 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const { createHandler, flushDebugLog } = require('@friggframework/core');
|
|
2
|
+
const express = require('express');
|
|
3
|
+
const bodyParser = require('body-parser');
|
|
4
|
+
const cors = require('cors');
|
|
5
|
+
const Boom = require('@hapi/boom');
|
|
6
|
+
const serverlessHttp = require('serverless-http');
|
|
7
|
+
|
|
8
|
+
const createApp = (applyMiddleware) => {
|
|
9
|
+
const app = express();
|
|
10
|
+
|
|
11
|
+
app.use(bodyParser.json({ limit: '10mb' }));
|
|
12
|
+
app.use(bodyParser.urlencoded({ extended: true }));
|
|
13
|
+
app.use(
|
|
14
|
+
cors({
|
|
15
|
+
origin: '*',
|
|
16
|
+
allowedHeaders: '*',
|
|
17
|
+
methods: '*',
|
|
18
|
+
credentials: true,
|
|
19
|
+
})
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
if (applyMiddleware) applyMiddleware(app);
|
|
23
|
+
|
|
24
|
+
// Handle sending error response and logging server errors to console
|
|
25
|
+
app.use((err, req, res, next) => {
|
|
26
|
+
const boomError = err.isBoom ? err : Boom.boomify(err);
|
|
27
|
+
const {
|
|
28
|
+
output: { statusCode = 500 },
|
|
29
|
+
} = boomError;
|
|
30
|
+
|
|
31
|
+
if (statusCode >= 500) {
|
|
32
|
+
flushDebugLog(boomError);
|
|
33
|
+
res.status(statusCode).json({ error: 'Internal Server Error' });
|
|
34
|
+
} else {
|
|
35
|
+
res.status(statusCode).json({ error: err.message });
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
return app;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
function createAppHandler(eventName, router, shouldUseDatabase = true) {
|
|
43
|
+
const app = createApp((app) => {
|
|
44
|
+
app.use(router);
|
|
45
|
+
});
|
|
46
|
+
return createHandler({
|
|
47
|
+
eventName,
|
|
48
|
+
method: serverlessHttp(app),
|
|
49
|
+
shouldUseDatabase,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
module.exports = {
|
|
54
|
+
createApp,
|
|
55
|
+
createAppHandler,
|
|
56
|
+
};
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
const { Router } = require('express');
|
|
2
|
+
const { Worker } = require('@friggframework/core');
|
|
3
|
+
const {
|
|
4
|
+
IntegrationEventDispatcher,
|
|
5
|
+
} = require('./integration-event-dispatcher');
|
|
6
|
+
const {
|
|
7
|
+
GetIntegrationInstance,
|
|
8
|
+
} = require('../integrations/use-cases/get-integration-instance');
|
|
9
|
+
const { ModuleFactory } = require('../modules/module-factory');
|
|
10
|
+
const {
|
|
11
|
+
createProcessRepository,
|
|
12
|
+
} = require('../integrations/repositories/process-repository-factory');
|
|
13
|
+
const {
|
|
14
|
+
createIntegrationRepository,
|
|
15
|
+
} = require('../integrations/repositories/integration-repository-factory');
|
|
16
|
+
const {
|
|
17
|
+
createModuleRepository,
|
|
18
|
+
} = require('../modules/repositories/module-repository-factory');
|
|
19
|
+
const {
|
|
20
|
+
getModulesDefinitionFromIntegrationClasses,
|
|
21
|
+
} = require('../integrations/utils/map-integration-dto');
|
|
22
|
+
|
|
23
|
+
const loadRouterFromObject = (IntegrationClass, routerObject) => {
|
|
24
|
+
const router = Router();
|
|
25
|
+
const { path, method, event } = routerObject;
|
|
26
|
+
|
|
27
|
+
console.log(
|
|
28
|
+
`Registering ${method} ${path} for ${IntegrationClass.Definition.name}`
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
router[method.toLowerCase()](path, async (req, res, next) => {
|
|
32
|
+
try {
|
|
33
|
+
const integrationInstance = new IntegrationClass();
|
|
34
|
+
const dispatcher = new IntegrationEventDispatcher(
|
|
35
|
+
integrationInstance
|
|
36
|
+
);
|
|
37
|
+
const result = await dispatcher.dispatchHttp({
|
|
38
|
+
event,
|
|
39
|
+
req,
|
|
40
|
+
res,
|
|
41
|
+
next,
|
|
42
|
+
});
|
|
43
|
+
res.json(result);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
next(error);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return router;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const initializeRepositories = () => {
|
|
53
|
+
const processRepository = createProcessRepository();
|
|
54
|
+
const integrationRepository = createIntegrationRepository();
|
|
55
|
+
const moduleRepository = createModuleRepository();
|
|
56
|
+
|
|
57
|
+
return { processRepository, integrationRepository, moduleRepository };
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const createModuleFactoryWithDefinitions = (
|
|
61
|
+
moduleRepository,
|
|
62
|
+
integrationClasses
|
|
63
|
+
) => {
|
|
64
|
+
const moduleDefinitions =
|
|
65
|
+
getModulesDefinitionFromIntegrationClasses(integrationClasses);
|
|
66
|
+
|
|
67
|
+
return new ModuleFactory({
|
|
68
|
+
moduleRepository,
|
|
69
|
+
moduleDefinitions,
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const loadIntegrationForWebhook = async (integrationId) => {
|
|
74
|
+
const { loadAppDefinition } = require('./app-definition-loader');
|
|
75
|
+
const { integrations: integrationClasses } = loadAppDefinition();
|
|
76
|
+
|
|
77
|
+
const { integrationRepository, moduleRepository } =
|
|
78
|
+
initializeRepositories();
|
|
79
|
+
|
|
80
|
+
const moduleFactory = createModuleFactoryWithDefinitions(
|
|
81
|
+
moduleRepository,
|
|
82
|
+
integrationClasses
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const getIntegrationInstance = new GetIntegrationInstance({
|
|
86
|
+
integrationRepository,
|
|
87
|
+
integrationClasses,
|
|
88
|
+
moduleFactory,
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
const integrationRecord = await integrationRepository.findIntegrationById(
|
|
92
|
+
integrationId
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
return await getIntegrationInstance.execute(
|
|
96
|
+
integrationId,
|
|
97
|
+
integrationRecord.userId
|
|
98
|
+
);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const loadIntegrationForProcess = async (processId, integrationClass) => {
|
|
102
|
+
const { processRepository, integrationRepository, moduleRepository } =
|
|
103
|
+
initializeRepositories();
|
|
104
|
+
|
|
105
|
+
const moduleFactory = createModuleFactoryWithDefinitions(moduleRepository, [
|
|
106
|
+
integrationClass,
|
|
107
|
+
]);
|
|
108
|
+
|
|
109
|
+
const getIntegrationInstance = new GetIntegrationInstance({
|
|
110
|
+
integrationRepository,
|
|
111
|
+
integrationClasses: [integrationClass],
|
|
112
|
+
moduleFactory,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
if (!processId) {
|
|
116
|
+
throw new Error('processId is required in queue message data');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const process = await processRepository.findById(processId);
|
|
120
|
+
|
|
121
|
+
if (!process) {
|
|
122
|
+
throw new Error(`Process not found: ${processId}`);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return await getIntegrationInstance.execute(
|
|
126
|
+
process.integrationId,
|
|
127
|
+
process.userId
|
|
128
|
+
);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const createQueueWorker = (integrationClass) => {
|
|
132
|
+
class QueueWorker extends Worker {
|
|
133
|
+
async _run(params, context) {
|
|
134
|
+
try {
|
|
135
|
+
let integrationInstance;
|
|
136
|
+
if (
|
|
137
|
+
params.event === 'ON_WEBHOOK' &&
|
|
138
|
+
params.data?.integrationId
|
|
139
|
+
) {
|
|
140
|
+
integrationInstance = await loadIntegrationForWebhook(
|
|
141
|
+
params.data.integrationId
|
|
142
|
+
);
|
|
143
|
+
} else if (params.data?.processId) {
|
|
144
|
+
integrationInstance = await loadIntegrationForProcess(
|
|
145
|
+
params.data.processId,
|
|
146
|
+
integrationClass
|
|
147
|
+
);
|
|
148
|
+
} else {
|
|
149
|
+
// Instantiates a DRY integration class without database records.
|
|
150
|
+
// There will be cases where we need to use helpers that the api modules can export.
|
|
151
|
+
// Like for HubSpot, the answer is to do a reverse lookup for the integration by the entity external ID (HubSpot Portal ID),
|
|
152
|
+
// and then you'll have the integration ID available to hydrate from.
|
|
153
|
+
integrationInstance = new integrationClass();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const dispatcher = new IntegrationEventDispatcher(
|
|
157
|
+
integrationInstance
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
return await dispatcher.dispatchJob({
|
|
161
|
+
event: params.event,
|
|
162
|
+
data: params.data,
|
|
163
|
+
context: context,
|
|
164
|
+
});
|
|
165
|
+
} catch (error) {
|
|
166
|
+
console.error(
|
|
167
|
+
`Error in ${params.event} for ${integrationClass.Definition.name}:`,
|
|
168
|
+
error
|
|
169
|
+
);
|
|
170
|
+
throw error;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return QueueWorker;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
module.exports = {
|
|
178
|
+
loadRouterFromObject,
|
|
179
|
+
createQueueWorker,
|
|
180
|
+
};
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database Migration Handler for AWS Lambda
|
|
3
|
+
*
|
|
4
|
+
* Executes Prisma migrations in a Lambda environment.
|
|
5
|
+
* Based on AWS best practices for running migrations in serverless environments.
|
|
6
|
+
*
|
|
7
|
+
* Supported Commands:
|
|
8
|
+
* - deploy: Apply pending migrations to the database (production-safe)
|
|
9
|
+
* - reset: Reset database and apply all migrations (DANGEROUS - dev only)
|
|
10
|
+
*
|
|
11
|
+
* Usage:
|
|
12
|
+
* // Via Lambda invoke
|
|
13
|
+
* {
|
|
14
|
+
* "command": "deploy" // or "reset"
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* Requirements:
|
|
18
|
+
* - Prisma CLI must be included in deployment or Lambda layer
|
|
19
|
+
* - DATABASE_URL environment variable must be set
|
|
20
|
+
* - VPC configuration for Aurora access
|
|
21
|
+
*
|
|
22
|
+
* Reference: https://www.prisma.io/docs/guides/deployment/deployment-guides/deploying-to-aws-lambda
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const { execFile } = require('child_process');
|
|
26
|
+
const path = require('path');
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Execute Prisma migration command
|
|
30
|
+
*
|
|
31
|
+
* @param {string} command - Migration command ('deploy' or 'reset')
|
|
32
|
+
* @param {string} schemaPath - Path to Prisma schema file
|
|
33
|
+
* @returns {Promise<number>} Exit code
|
|
34
|
+
*/
|
|
35
|
+
async function executePrismaMigration(command, schemaPath) {
|
|
36
|
+
console.log(`Executing Prisma migration: ${command}`);
|
|
37
|
+
console.log(`Schema path: ${schemaPath}`);
|
|
38
|
+
console.log(`Database URL: ${process.env.DATABASE_URL ? '[SET]' : '[NOT SET]'}`);
|
|
39
|
+
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
// Build command arguments
|
|
42
|
+
const args = ['migrate', command];
|
|
43
|
+
|
|
44
|
+
// Add command-specific options
|
|
45
|
+
if (command === 'reset') {
|
|
46
|
+
args.push('--force'); // Skip confirmation prompt
|
|
47
|
+
args.push('--skip-generate'); // Skip client generation (already done in layer)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Add schema path if provided
|
|
51
|
+
if (schemaPath) {
|
|
52
|
+
args.push('--schema', schemaPath);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
console.log(`Running: prisma ${args.join(' ')}`);
|
|
56
|
+
|
|
57
|
+
// Execute Prisma CLI
|
|
58
|
+
execFile(
|
|
59
|
+
path.resolve('./node_modules/prisma/build/index.js'),
|
|
60
|
+
args,
|
|
61
|
+
{
|
|
62
|
+
env: {
|
|
63
|
+
...process.env,
|
|
64
|
+
// Ensure Prisma uses the correct binary target
|
|
65
|
+
PRISMA_CLI_BINARY_TARGETS: 'rhel-openssl-3.0.x',
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
(error, stdout, stderr) => {
|
|
69
|
+
// Log all output
|
|
70
|
+
if (stdout) {
|
|
71
|
+
console.log('STDOUT:', stdout);
|
|
72
|
+
}
|
|
73
|
+
if (stderr) {
|
|
74
|
+
console.error('STDERR:', stderr);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (error) {
|
|
78
|
+
console.error(`Migration ${command} exited with error:`, error.message);
|
|
79
|
+
console.error(`Exit code: ${error.code || 1}`);
|
|
80
|
+
resolve(error.code || 1);
|
|
81
|
+
} else {
|
|
82
|
+
console.log(`Migration ${command} completed successfully`);
|
|
83
|
+
resolve(0);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Validate migration command
|
|
92
|
+
*/
|
|
93
|
+
function validateCommand(command) {
|
|
94
|
+
const validCommands = ['deploy', 'reset'];
|
|
95
|
+
|
|
96
|
+
if (!validCommands.includes(command)) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
`Invalid migration command: "${command}". ` +
|
|
99
|
+
`Valid commands are: ${validCommands.join(', ')}`
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Extra validation for dangerous commands
|
|
104
|
+
if (command === 'reset') {
|
|
105
|
+
const stage = process.env.STAGE || process.env.NODE_ENV;
|
|
106
|
+
if (stage === 'production' || stage === 'prod') {
|
|
107
|
+
throw new Error(
|
|
108
|
+
'BLOCKED: "reset" command is not allowed in production environment. ' +
|
|
109
|
+
'This command would delete all data. Use "deploy" instead.'
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
console.warn('⚠️ WARNING: "reset" will DELETE all data and reset the database!');
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Determine which Prisma schema to use based on database type
|
|
118
|
+
*/
|
|
119
|
+
function getSchemaPath() {
|
|
120
|
+
// In Lambda, schemas are in @friggframework/core/generated/
|
|
121
|
+
const baseSchemaPath = './node_modules/@friggframework/core/generated';
|
|
122
|
+
|
|
123
|
+
// Check if Postgres is enabled
|
|
124
|
+
if (process.env.DATABASE_URL?.includes('postgresql') || process.env.DATABASE_URL?.includes('postgres')) {
|
|
125
|
+
const schemaPath = `${baseSchemaPath}/prisma-postgresql/schema.prisma`;
|
|
126
|
+
console.log(`Using PostgreSQL schema: ${schemaPath}`);
|
|
127
|
+
return schemaPath;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Check if MongoDB is enabled
|
|
131
|
+
if (process.env.DATABASE_URL?.includes('mongodb')) {
|
|
132
|
+
const schemaPath = `${baseSchemaPath}/prisma-mongodb/schema.prisma`;
|
|
133
|
+
console.log(`Using MongoDB schema: ${schemaPath}`);
|
|
134
|
+
return schemaPath;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Default to PostgreSQL
|
|
138
|
+
console.log('DATABASE_URL not set or database type unknown, defaulting to PostgreSQL');
|
|
139
|
+
return `${baseSchemaPath}/prisma-postgresql/schema.prisma`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Lambda handler for database migrations
|
|
144
|
+
*
|
|
145
|
+
* @param {Object} event - Lambda event
|
|
146
|
+
* @param {string} event.command - Migration command ('deploy' or 'reset')
|
|
147
|
+
* @param {Object} context - Lambda context
|
|
148
|
+
* @returns {Promise<Object>} Migration result
|
|
149
|
+
*/
|
|
150
|
+
exports.handler = async (event, context) => {
|
|
151
|
+
const startTime = Date.now();
|
|
152
|
+
|
|
153
|
+
console.log('='.repeat(60));
|
|
154
|
+
console.log('Database Migration Handler');
|
|
155
|
+
console.log('='.repeat(60));
|
|
156
|
+
console.log('Event:', JSON.stringify(event, null, 2));
|
|
157
|
+
console.log('Context:', JSON.stringify({
|
|
158
|
+
functionName: context.functionName,
|
|
159
|
+
functionVersion: context.functionVersion,
|
|
160
|
+
memoryLimitInMB: context.memoryLimitInMB,
|
|
161
|
+
logGroupName: context.logGroupName,
|
|
162
|
+
}, null, 2));
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
// Get migration command (default to 'deploy')
|
|
166
|
+
const command = event.command || 'deploy';
|
|
167
|
+
|
|
168
|
+
// Validate command
|
|
169
|
+
validateCommand(command);
|
|
170
|
+
|
|
171
|
+
// Check required environment variables
|
|
172
|
+
if (!process.env.DATABASE_URL) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
'DATABASE_URL environment variable is not set. ' +
|
|
175
|
+
'Cannot connect to database for migrations.'
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Determine schema path
|
|
180
|
+
const schemaPath = getSchemaPath();
|
|
181
|
+
|
|
182
|
+
// Execute migration
|
|
183
|
+
const exitCode = await executePrismaMigration(command, schemaPath);
|
|
184
|
+
|
|
185
|
+
const duration = Date.now() - startTime;
|
|
186
|
+
|
|
187
|
+
if (exitCode === 0) {
|
|
188
|
+
const result = {
|
|
189
|
+
success: true,
|
|
190
|
+
command,
|
|
191
|
+
message: `Migration ${command} completed successfully`,
|
|
192
|
+
duration: `${duration}ms`,
|
|
193
|
+
timestamp: new Date().toISOString(),
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
console.log('='.repeat(60));
|
|
197
|
+
console.log('Migration completed successfully');
|
|
198
|
+
console.log(JSON.stringify(result, null, 2));
|
|
199
|
+
console.log('='.repeat(60));
|
|
200
|
+
|
|
201
|
+
return result;
|
|
202
|
+
} else {
|
|
203
|
+
throw new Error(`Migration ${command} failed with exit code ${exitCode}`);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
} catch (error) {
|
|
207
|
+
const duration = Date.now() - startTime;
|
|
208
|
+
|
|
209
|
+
console.error('='.repeat(60));
|
|
210
|
+
console.error('Migration failed');
|
|
211
|
+
console.error('Error:', error.message);
|
|
212
|
+
console.error('Stack:', error.stack);
|
|
213
|
+
console.error('='.repeat(60));
|
|
214
|
+
|
|
215
|
+
const errorResult = {
|
|
216
|
+
success: false,
|
|
217
|
+
command: event.command || 'unknown',
|
|
218
|
+
error: error.message,
|
|
219
|
+
duration: `${duration}ms`,
|
|
220
|
+
timestamp: new Date().toISOString(),
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
// Return error (don't throw) so Lambda doesn't retry
|
|
224
|
+
return errorResult;
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lightweight dispatcher that executes integration event handlers.
|
|
3
|
+
* @param {import('../integrations/integration-base')} integrationInstance Pre-instantiated integration.
|
|
4
|
+
*/
|
|
5
|
+
class IntegrationEventDispatcher {
|
|
6
|
+
constructor(integrationInstance) {
|
|
7
|
+
if (!integrationInstance) {
|
|
8
|
+
throw new Error('Integration instance is required');
|
|
9
|
+
}
|
|
10
|
+
this.integrationInstance = integrationInstance;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async dispatchHttp({ event, req, res, next }) {
|
|
14
|
+
const instance = this.integrationInstance;
|
|
15
|
+
|
|
16
|
+
const handler = this.findEventHandler(instance, event);
|
|
17
|
+
|
|
18
|
+
if (!handler) {
|
|
19
|
+
const name =
|
|
20
|
+
instance.constructor?.Definition?.name || 'integration';
|
|
21
|
+
throw new Error(`Event ${event} not registered for ${name}`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return await handler.call(instance, { req, res, next });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async dispatchJob({ event, data, context }) {
|
|
28
|
+
const instance = this.integrationInstance;
|
|
29
|
+
|
|
30
|
+
const handler = this.findEventHandler(instance, event);
|
|
31
|
+
|
|
32
|
+
if (!handler) {
|
|
33
|
+
const name =
|
|
34
|
+
instance.constructor?.Definition?.name || 'integration';
|
|
35
|
+
throw new Error(`Event ${event} not registered for ${name}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return await handler.call(instance, { data, context });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
findEventHandler(integration, event) {
|
|
42
|
+
if (integration.events && integration.events[event]) {
|
|
43
|
+
return integration.events[event].handler;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (integration.defaultEvents && integration.defaultEvents[event]) {
|
|
47
|
+
return integration.defaultEvents[event].handler;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
module.exports = { IntegrationEventDispatcher };
|