From a23a8c4456d5bc63b45a8bbd5497e2d6ef0fce11 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Fri, 25 Aug 2023 15:52:37 +0200 Subject: [PATCH] :bug: (webhook) Fix saving invalid webhook when duplicated --- .../features/typebot/api/publishTypebot.ts | 22 ++++++++++- .../features/typebot/helpers/sanitizers.ts | 37 ------------------- 2 files changed, 21 insertions(+), 38 deletions(-) diff --git a/apps/builder/src/features/typebot/api/publishTypebot.ts b/apps/builder/src/features/typebot/api/publishTypebot.ts index 451c012ff..912e6375e 100644 --- a/apps/builder/src/features/typebot/api/publishTypebot.ts +++ b/apps/builder/src/features/typebot/api/publishTypebot.ts @@ -1,10 +1,11 @@ import prisma from '@/lib/prisma' import { authenticatedProcedure } from '@/helpers/server/trpc' import { TRPCError } from '@trpc/server' -import { typebotSchema } from '@typebot.io/schemas' +import { InputBlockType, typebotSchema } from '@typebot.io/schemas' import { z } from 'zod' import { isWriteTypebotForbidden } from '../helpers/isWriteTypebotForbidden' import { sendTelemetryEvents } from '@typebot.io/lib/telemetry/sendTelemetryEvent' +import { Plan } from '@typebot.io/prisma' export const publishTypebot = authenticatedProcedure .meta({ @@ -34,6 +35,11 @@ export const publishTypebot = authenticatedProcedure include: { collaborators: true, publishedTypebot: true, + workspace: { + select: { + plan: true, + }, + }, }, }) if ( @@ -42,6 +48,20 @@ export const publishTypebot = authenticatedProcedure ) 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) await prisma.publicTypebot.updateMany({ where: { diff --git a/apps/builder/src/features/typebot/helpers/sanitizers.ts b/apps/builder/src/features/typebot/helpers/sanitizers.ts index 6f05b6b62..da4613d5e 100644 --- a/apps/builder/src/features/typebot/helpers/sanitizers.ts +++ b/apps/builder/src/features/typebot/helpers/sanitizers.ts @@ -5,11 +5,7 @@ import { InputBlockType, IntegrationBlockType, Typebot, - Webhook, - WebhookBlock, - defaultWebhookAttributes, } from '@typebot.io/schemas' -import { HttpMethod } from '@typebot.io/schemas/features/blocks/integrations/webhook/enums' export const sanitizeSettings = ( settings: Typebot['settings'], @@ -47,8 +43,6 @@ const sanitizeBlock = ), }, } - case IntegrationBlockType.WEBHOOK: - return await sanitizeWebhookBlock(block) case IntegrationBlockType.GOOGLE_SHEETS: return { ...block, @@ -85,37 +79,6 @@ const sanitizeBlock = } } -const sanitizeWebhookBlock = async ( - block: WebhookBlock -): Promise => { - 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 = (workspaceId: string) => async (credentialsId?: string): Promise => {