2
0

feat(dashboard): Duplicate webhooks on typebot duplication

This commit is contained in:
Baptiste Arnaud
2022-03-21 19:20:44 +01:00
parent 71816f76ad
commit b2bf6f09f6
2 changed files with 35 additions and 1 deletions

View File

@ -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<Typebot>({
url: `/api/typebots`,
method: 'POST',
body: cleanUpTypebot(duplicatedTypebot),
body: await cleanAndDuplicateWebhooks(duplicatedTypebot),
})
}
@ -148,6 +149,26 @@ const cleanUpTypebot = (
})),
})
const cleanAndDuplicateWebhooks = async (
typebot: Omit<Typebot, 'id' | 'updatedAt' | 'createdAt'>
) => ({
...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}`,

View File

@ -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<Webhook>) =>
url: `/api/webhooks/${webhookId}`,
body: webhook,
})
export const duplicateWebhook = async (
webhookId: string
): Promise<Webhook | undefined> => {
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
}