diff --git a/apps/builder/services/typebots/typebots.ts b/apps/builder/services/typebots/typebots.ts index 200da95f6..49b6c9f00 100644 --- a/apps/builder/services/typebots/typebots.ts +++ b/apps/builder/services/typebots/typebots.ts @@ -50,6 +50,7 @@ import { stringify } from 'qs' import { isChoiceInput, isConditionStep, sendRequest, omit } from 'utils' import cuid from 'cuid' import { diff } from 'deep-object-diff' +import { duplicateWebhook } from 'services/webhook' export type TypebotInDashboard = Pick< Typebot, @@ -132,7 +133,7 @@ export const duplicateTypebot = async (typebotId: string) => { return sendRequest({ url: `/api/typebots`, method: 'POST', - body: cleanUpTypebot(duplicatedTypebot), + body: await cleanAndDuplicateWebhooks(duplicatedTypebot), }) } @@ -148,6 +149,26 @@ const cleanUpTypebot = ( })), }) +const cleanAndDuplicateWebhooks = async ( + typebot: Omit +) => ({ + ...typebot, + blocks: await Promise.all( + typebot.blocks.map(async (b) => ({ + ...b, + steps: await Promise.all( + b.steps.map(async (s) => { + if (isWebhookStep(s)) { + const newWebhook = await duplicateWebhook(s.webhookId) + return { ...s, webhookId: newWebhook ? newWebhook.id : cuid() } + } + return s + }) + ), + })) + ), +}) + const getTypebot = (typebotId: string) => sendRequest<{ typebot: Typebot }>({ url: `/api/typebots/${typebotId}`, diff --git a/apps/builder/services/webhook.ts b/apps/builder/services/webhook.ts index 443a3a7f0..485b1f8d9 100644 --- a/apps/builder/services/webhook.ts +++ b/apps/builder/services/webhook.ts @@ -1,3 +1,4 @@ +import cuid from 'cuid' import { Webhook } from 'models' import { sendRequest } from 'utils' @@ -7,3 +8,15 @@ export const saveWebhook = (webhookId: string, webhook: Partial) => url: `/api/webhooks/${webhookId}`, body: webhook, }) + +export const duplicateWebhook = async ( + webhookId: string +): Promise => { + const { data } = await sendRequest<{ webhook: Webhook }>( + `/api/webhooks/${webhookId}` + ) + if (!data) return + const newWebhook = { ...data.webhook, id: cuid() } + await saveWebhook(newWebhook.id, newWebhook) + return newWebhook +}