🐛 (typebotLink) Fix linked typebot fetching error

Closes #429
This commit is contained in:
Baptiste Arnaud
2023-03-30 10:30:26 +02:00
parent 70416c0d14
commit 684e6338e2
25 changed files with 2716 additions and 605 deletions

View File

@@ -0,0 +1,34 @@
import { canReadTypebots } from '@/helpers/api/dbRules'
import prisma from '@/lib/prisma'
import { User } from '@typebot.io/prisma'
import { PublicTypebot, Typebot } from '@typebot.io/schemas'
type Props = {
isPreview?: boolean
typebotIds: string[]
user?: User
}
export const fetchLinkedTypebots = async ({
user,
isPreview,
typebotIds,
}: Props) => {
const linkedTypebots = (
isPreview
? await prisma.typebot.findMany({
where: user
? {
AND: [
{ id: { in: typebotIds } },
canReadTypebots(typebotIds, user as User),
],
}
: { id: { in: typebotIds } },
})
: await prisma.publicTypebot.findMany({
where: { id: { in: typebotIds } },
})
) as (Typebot | PublicTypebot)[]
return linkedTypebots
}

View File

@@ -1,20 +0,0 @@
import prisma from '@/lib/prisma'
import { PublicTypebot, Typebot } from '@typebot.io/schemas'
type Props = {
isPreview: boolean
typebotIds: string[]
}
export const getLinkedTypebots = async ({ isPreview, typebotIds }: Props) => {
const linkedTypebots = (
isPreview
? await prisma.typebot.findMany({
where: { id: { in: typebotIds } },
})
: await prisma.publicTypebot.findMany({
where: { id: { in: typebotIds } },
})
) as (Typebot | PublicTypebot)[]
return linkedTypebots
}

View File

@@ -1,5 +1,3 @@
import prisma from '@/lib/prisma'
import { canReadTypebots } from '@/helpers/api/dbRules'
import { User } from '@typebot.io/prisma'
import {
LogicBlockType,
@@ -8,6 +6,7 @@ import {
TypebotLinkBlock,
} from '@typebot.io/schemas'
import { isDefined } from '@typebot.io/lib'
import { fetchLinkedTypebots } from './fetchLinkedTypebots'
type Props = {
typebots: Pick<PublicTypebot, 'groups'>[]
@@ -15,7 +14,7 @@ type Props = {
isPreview?: boolean
}
export const getLinkedTypebotsChildren =
export const getPreviouslyLinkedTypebots =
({ typebots, user, isPreview }: Props) =>
async (
capturedLinkedBots: (Typebot | PublicTypebot)[]
@@ -39,23 +38,12 @@ export const getLinkedTypebotsChildren =
)
.filter(isDefined)
if (linkedTypebotIds.length === 0) return capturedLinkedBots
const linkedTypebots = (
isPreview
? await prisma.typebot.findMany({
where: user
? {
AND: [
{ id: { in: linkedTypebotIds } },
canReadTypebots(linkedTypebotIds, user as User),
],
}
: { id: { in: linkedTypebotIds } },
})
: await prisma.publicTypebot.findMany({
where: { id: { in: linkedTypebotIds } },
})
) as (Typebot | PublicTypebot)[]
return getLinkedTypebotsChildren({
const linkedTypebots = (await fetchLinkedTypebots({
user,
typebotIds: linkedTypebotIds,
isPreview,
})) as (Typebot | PublicTypebot)[]
return getPreviouslyLinkedTypebots({
typebots: linkedTypebots,
user,
isPreview,

View File

@@ -21,8 +21,8 @@ import Cors from 'cors'
import prisma from '@/lib/prisma'
import { parseVariables } from '@/features/variables/parseVariables'
import { parseSampleResult } from '@/features/blocks/integrations/webhook/parseSampleResult'
import { getLinkedTypebots } from '@/features/blocks/logic/typebotLink/getLinkedTypebots'
import { getLinkedTypebotsChildren } from '@/features/blocks/logic/typebotLink/getLinkedTypebotsChildren'
import { fetchLinkedTypebots } from '@/features/blocks/logic/typebotLink/fetchLinkedTypebots'
import { getPreviouslyLinkedTypebots } from '@/features/blocks/logic/typebotLink/getPreviouslyLinkedTypebots'
import { saveErrorLog } from '@/features/logs/saveErrorLog'
import { saveSuccessLog } from '@/features/logs/saveSuccessLog'
@@ -127,11 +127,11 @@ export const executeWebhook =
convertKeyValueTableToObject(webhook.queryParams, variables)
)
const contentType = headers ? headers['Content-Type'] : undefined
const linkedTypebotsParents = await getLinkedTypebots({
const linkedTypebotsParents = await fetchLinkedTypebots({
isPreview: !('typebotId' in typebot),
typebotIds: parentTypebotIds,
})
const linkedTypebotsChildren = await getLinkedTypebotsChildren({
const linkedTypebotsChildren = await getPreviouslyLinkedTypebots({
isPreview: !('typebotId' in typebot),
typebots: [typebot],
})([])

View File

@@ -3,7 +3,7 @@ import prisma from '@/lib/prisma'
import { Typebot } from '@typebot.io/schemas'
import { NextApiRequest, NextApiResponse } from 'next'
import { methodNotAllowed } from '@typebot.io/lib/api'
import { getLinkedTypebotsChildren } from '@/features/blocks/logic/typebotLink/getLinkedTypebotsChildren'
import { getPreviouslyLinkedTypebots } from '@/features/blocks/logic/typebotLink/getPreviouslyLinkedTypebots'
import { parseSampleResult } from '@/features/blocks/integrations/webhook/parseSampleResult'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
@@ -23,7 +23,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
.flatMap((g) => g.blocks)
.find((s) => s.id === blockId)
if (!block) return res.status(404).send({ message: 'Group not found' })
const linkedTypebots = await getLinkedTypebotsChildren({
const linkedTypebots = await getPreviouslyLinkedTypebots({
isPreview: true,
typebots: [typebot],
user,

View File

@@ -3,7 +3,7 @@ import { Typebot } from '@typebot.io/schemas'
import { NextApiRequest, NextApiResponse } from 'next'
import { methodNotAllowed } from '@typebot.io/lib/api'
import { parseSampleResult } from '@/features/blocks/integrations/webhook/parseSampleResult'
import { getLinkedTypebotsChildren } from '@/features/blocks/logic/typebotLink/getLinkedTypebotsChildren'
import { getPreviouslyLinkedTypebots } from '@/features/blocks/logic/typebotLink/getPreviouslyLinkedTypebots'
import { authenticateUser } from '@/helpers/authenticateUser'
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
@@ -19,7 +19,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
},
})) as unknown as Typebot | undefined
if (!typebot) return res.status(400).send({ message: 'Typebot not found' })
const linkedTypebots = await getLinkedTypebotsChildren({
const linkedTypebots = await getPreviouslyLinkedTypebots({
isPreview: true,
typebots: [typebot],
user,

View File

@@ -15,7 +15,7 @@ import Mail from 'nodemailer/lib/mailer'
import { DefaultBotNotificationEmail } from '@typebot.io/emails'
import { render } from '@faire/mjml-react/utils/render'
import prisma from '@/lib/prisma'
import { getLinkedTypebotsChildren } from '@/features/blocks/logic/typebotLink/getLinkedTypebotsChildren'
import { getPreviouslyLinkedTypebots } from '@/features/blocks/logic/typebotLink/getPreviouslyLinkedTypebots'
import { saveErrorLog } from '@/features/logs/saveErrorLog'
import { saveSuccessLog } from '@/features/logs/saveSuccessLog'
@@ -194,7 +194,7 @@ const getEmailBody = async ({
where: { typebotId },
})) as unknown as PublicTypebot
if (!typebot) return
const linkedTypebots = await getLinkedTypebotsChildren({
const linkedTypebots = await getPreviouslyLinkedTypebots({
typebots: [typebot],
})([])
const answers = parseAnswers(typebot, linkedTypebots)(resultValues)