feat(dashboard): ✨ Duplicate webhooks on typebot duplication
This commit is contained in:
@ -50,6 +50,7 @@ import { stringify } from 'qs'
|
|||||||
import { isChoiceInput, isConditionStep, sendRequest, omit } from 'utils'
|
import { isChoiceInput, isConditionStep, sendRequest, omit } from 'utils'
|
||||||
import cuid from 'cuid'
|
import cuid from 'cuid'
|
||||||
import { diff } from 'deep-object-diff'
|
import { diff } from 'deep-object-diff'
|
||||||
|
import { duplicateWebhook } from 'services/webhook'
|
||||||
|
|
||||||
export type TypebotInDashboard = Pick<
|
export type TypebotInDashboard = Pick<
|
||||||
Typebot,
|
Typebot,
|
||||||
@ -132,7 +133,7 @@ export const duplicateTypebot = async (typebotId: string) => {
|
|||||||
return sendRequest<Typebot>({
|
return sendRequest<Typebot>({
|
||||||
url: `/api/typebots`,
|
url: `/api/typebots`,
|
||||||
method: 'POST',
|
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) =>
|
const getTypebot = (typebotId: string) =>
|
||||||
sendRequest<{ typebot: Typebot }>({
|
sendRequest<{ typebot: Typebot }>({
|
||||||
url: `/api/typebots/${typebotId}`,
|
url: `/api/typebots/${typebotId}`,
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import cuid from 'cuid'
|
||||||
import { Webhook } from 'models'
|
import { Webhook } from 'models'
|
||||||
import { sendRequest } from 'utils'
|
import { sendRequest } from 'utils'
|
||||||
|
|
||||||
@ -7,3 +8,15 @@ export const saveWebhook = (webhookId: string, webhook: Partial<Webhook>) =>
|
|||||||
url: `/api/webhooks/${webhookId}`,
|
url: `/api/webhooks/${webhookId}`,
|
||||||
body: webhook,
|
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
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user