From b2bf6f09f616d1d16487f21183f5444b48f227a6 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Mon, 21 Mar 2022 19:20:44 +0100 Subject: [PATCH] =?UTF-8?q?feat(dashboard):=20=E2=9C=A8=20Duplicate=20webh?= =?UTF-8?q?ooks=20on=20typebot=20duplication?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/builder/services/typebots/typebots.ts | 23 +++++++++++++++++++++- apps/builder/services/webhook.ts | 13 ++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) 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 +}