2
0

🐛 (webhook) Fix saving invalid webhook when duplicated

This commit is contained in:
Baptiste Arnaud
2023-08-25 15:52:37 +02:00
parent b74117d417
commit a23a8c4456
2 changed files with 21 additions and 38 deletions

View File

@ -1,10 +1,11 @@
import prisma from '@/lib/prisma' import prisma from '@/lib/prisma'
import { authenticatedProcedure } from '@/helpers/server/trpc' import { authenticatedProcedure } from '@/helpers/server/trpc'
import { TRPCError } from '@trpc/server' import { TRPCError } from '@trpc/server'
import { typebotSchema } from '@typebot.io/schemas' import { InputBlockType, typebotSchema } from '@typebot.io/schemas'
import { z } from 'zod' import { z } from 'zod'
import { isWriteTypebotForbidden } from '../helpers/isWriteTypebotForbidden' import { isWriteTypebotForbidden } from '../helpers/isWriteTypebotForbidden'
import { sendTelemetryEvents } from '@typebot.io/lib/telemetry/sendTelemetryEvent' import { sendTelemetryEvents } from '@typebot.io/lib/telemetry/sendTelemetryEvent'
import { Plan } from '@typebot.io/prisma'
export const publishTypebot = authenticatedProcedure export const publishTypebot = authenticatedProcedure
.meta({ .meta({
@ -34,6 +35,11 @@ export const publishTypebot = authenticatedProcedure
include: { include: {
collaborators: true, collaborators: true,
publishedTypebot: true, publishedTypebot: true,
workspace: {
select: {
plan: true,
},
},
}, },
}) })
if ( if (
@ -42,6 +48,20 @@ export const publishTypebot = authenticatedProcedure
) )
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' }) throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
if (existingTypebot.workspace.plan === Plan.FREE) {
const hasFileUploadBlocks = typebotSchema._def.schema.shape.groups
.parse(existingTypebot.groups)
.some((group) =>
group.blocks.some((block) => block.type === InputBlockType.FILE)
)
if (hasFileUploadBlocks)
throw new TRPCError({
code: 'BAD_REQUEST',
message: "File upload blocks can't be published on the free plan",
})
}
if (existingTypebot.publishedTypebot) if (existingTypebot.publishedTypebot)
await prisma.publicTypebot.updateMany({ await prisma.publicTypebot.updateMany({
where: { where: {

View File

@ -5,11 +5,7 @@ import {
InputBlockType, InputBlockType,
IntegrationBlockType, IntegrationBlockType,
Typebot, Typebot,
Webhook,
WebhookBlock,
defaultWebhookAttributes,
} from '@typebot.io/schemas' } from '@typebot.io/schemas'
import { HttpMethod } from '@typebot.io/schemas/features/blocks/integrations/webhook/enums'
export const sanitizeSettings = ( export const sanitizeSettings = (
settings: Typebot['settings'], settings: Typebot['settings'],
@ -47,8 +43,6 @@ const sanitizeBlock =
), ),
}, },
} }
case IntegrationBlockType.WEBHOOK:
return await sanitizeWebhookBlock(block)
case IntegrationBlockType.GOOGLE_SHEETS: case IntegrationBlockType.GOOGLE_SHEETS:
return { return {
...block, ...block,
@ -85,37 +79,6 @@ const sanitizeBlock =
} }
} }
const sanitizeWebhookBlock = async (
block: WebhookBlock
): Promise<WebhookBlock> => {
if (!block.webhookId) return block
const webhook = await prisma.webhook.findFirst({
where: {
id: block.webhookId,
},
})
return {
...block,
webhookId: undefined,
options: {
...block.options,
webhook: webhook
? {
id: webhook.id,
url: webhook.url ?? undefined,
method: (webhook.method as Webhook['method']) ?? HttpMethod.POST,
headers: (webhook.headers as Webhook['headers']) ?? [],
queryParams: (webhook.queryParams as Webhook['headers']) ?? [],
body: webhook.body ?? undefined,
}
: {
...defaultWebhookAttributes,
id: block.webhookId ?? '',
},
},
}
}
const sanitizeCredentialsId = const sanitizeCredentialsId =
(workspaceId: string) => (workspaceId: string) =>
async (credentialsId?: string): Promise<string | undefined> => { async (credentialsId?: string): Promise<string | undefined> => {