🐛 (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,