fix: 🐛 Webhook duplication
This commit is contained in:
@ -101,32 +101,56 @@ export const createTypebot = async ({
|
||||
}
|
||||
|
||||
export const importTypebot = async (typebot: Typebot, userPlan: Plan) => {
|
||||
return sendRequest<Typebot>({
|
||||
const { typebot: newTypebot, webhookIdsMapping } = duplicateTypebot(
|
||||
typebot,
|
||||
userPlan
|
||||
)
|
||||
const { data, error } = await sendRequest<Typebot>({
|
||||
url: `/api/typebots`,
|
||||
method: 'POST',
|
||||
body: await duplicateTypebot(typebot, userPlan),
|
||||
body: newTypebot,
|
||||
})
|
||||
if (!data) return { data, error }
|
||||
const webhookSteps = typebot.blocks
|
||||
.flatMap((b) => b.steps)
|
||||
.filter(isWebhookStep)
|
||||
await Promise.all(
|
||||
webhookSteps.map((s) =>
|
||||
duplicateWebhook(
|
||||
newTypebot.id,
|
||||
s.webhookId,
|
||||
webhookIdsMapping.get(s.webhookId) as string
|
||||
)
|
||||
)
|
||||
)
|
||||
return { data, error }
|
||||
}
|
||||
|
||||
const duplicateTypebot = async (
|
||||
const duplicateTypebot = (
|
||||
typebot: Typebot,
|
||||
userPlan: Plan
|
||||
): Promise<Typebot> => {
|
||||
): { typebot: Typebot; webhookIdsMapping: Map<string, string> } => {
|
||||
const blockIdsMapping = generateOldNewIdsMapping(typebot.blocks)
|
||||
const edgeIdsMapping = generateOldNewIdsMapping(typebot.edges)
|
||||
const webhookIdsMapping = generateOldNewIdsMapping(
|
||||
typebot.blocks
|
||||
.flatMap((b) => b.steps)
|
||||
.filter(isWebhookStep)
|
||||
.map((s) => ({ id: s.webhookId }))
|
||||
)
|
||||
const id = cuid()
|
||||
return {
|
||||
typebot: {
|
||||
...typebot,
|
||||
id: cuid(),
|
||||
id,
|
||||
name: `${typebot.name} copy`,
|
||||
publishedTypebotId: null,
|
||||
publicId: null,
|
||||
customDomain: null,
|
||||
blocks: await Promise.all(
|
||||
typebot.blocks.map(async (b) => ({
|
||||
blocks: typebot.blocks.map((b) => ({
|
||||
...b,
|
||||
id: blockIdsMapping.get(b.id) as string,
|
||||
steps: await Promise.all(
|
||||
b.steps.map(async (s) => {
|
||||
steps: b.steps.map((s) => {
|
||||
const newIds = {
|
||||
blockId: blockIdsMapping.get(s.blockId) as string,
|
||||
outgoingEdgeId: s.outgoingEdgeId
|
||||
@ -157,10 +181,9 @@ const duplicateTypebot = async (
|
||||
...newIds,
|
||||
} as ChoiceInputStep | ConditionStep
|
||||
if (isWebhookStep(s)) {
|
||||
const newWebhook = await duplicateWebhook(s.webhookId)
|
||||
return {
|
||||
...s,
|
||||
webhookId: newWebhook ? newWebhook.id : cuid(),
|
||||
webhookId: webhookIdsMapping.get(s.webhookId) as string,
|
||||
...newIds,
|
||||
}
|
||||
}
|
||||
@ -168,10 +191,8 @@ const duplicateTypebot = async (
|
||||
...s,
|
||||
...newIds,
|
||||
}
|
||||
})
|
||||
),
|
||||
}))
|
||||
),
|
||||
}),
|
||||
})),
|
||||
edges: typebot.edges.map((e) => ({
|
||||
...e,
|
||||
id: edgeIdsMapping.get(e.id) as string,
|
||||
@ -191,6 +212,8 @@ const duplicateTypebot = async (
|
||||
: typebot.settings,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
webhookIdsMapping,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,3 @@
|
||||
import cuid from 'cuid'
|
||||
import { Webhook } from 'models'
|
||||
import { sendRequest } from 'utils'
|
||||
|
||||
@ -10,13 +9,15 @@ export const saveWebhook = (webhookId: string, webhook: Partial<Webhook>) =>
|
||||
})
|
||||
|
||||
export const duplicateWebhook = async (
|
||||
webhookId: string
|
||||
typebotId: string,
|
||||
existingWebhookId: string,
|
||||
newWebhookId: string
|
||||
): Promise<Webhook | undefined> => {
|
||||
const { data } = await sendRequest<{ webhook: Webhook }>(
|
||||
`/api/webhooks/${webhookId}`
|
||||
`/api/webhooks/${existingWebhookId}`
|
||||
)
|
||||
if (!data) return
|
||||
const newWebhook = { ...data.webhook, id: cuid() }
|
||||
const newWebhook = { ...data.webhook, id: newWebhookId, typebotId }
|
||||
await saveWebhook(newWebhook.id, newWebhook)
|
||||
return newWebhook
|
||||
}
|
||||
|
Reference in New Issue
Block a user