import { defineLogicFunction, type RoutePayload } from "twenty-sdk/define"; import { Response } from "twenty-sdk/logic-function"; import { SETUP_WEBHOOK_ROUTE_UNIVERSAL_IDENTIFIER } from "src/constants/universal-identifiers"; import { jsonResponse } from "src/logic-functions/utils/json-response"; import { SajnApiError } from "src/logic-functions/utils/sajn-api"; import { getSajnConnectionForRequest } from "src/logic-functions/utils/sajn-connection"; import { configureSajnWebhook } from "src/logic-functions/utils/sajn-webhook-registration"; const handler = async ( event: RoutePayload, ): Promise => { try { const connection = await getSajnConnectionForRequest(event); const result = await configureSajnWebhook(connection); return jsonResponse({ success: true, action: result.action, webhookId: result.registration.webhookId, message: result.action === "updated" ? "The sajn webhook was updated." : "The sajn webhook was created.", }); } catch (error) { const status = error instanceof SajnApiError ? error.status : 400; const message = error instanceof Error ? error.message : "Could not configure the webhook."; return jsonResponse({ success: false, message }, status); } }; export default defineLogicFunction({ universalIdentifier: SETUP_WEBHOOK_ROUTE_UNIVERSAL_IDENTIFIER, name: "setup-sajn-webhook", description: "Creates or updates the sajn document-status webhook for this Twenty server.", timeoutSeconds: 20, handler, httpRouteTriggerSettings: { path: "/sajn/setup-webhook", httpMethod: "POST", isAuthRequired: true, }, });