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

@@ -4,7 +4,7 @@ import { authenticatedProcedure } from '@/helpers/server/trpc'
import { TRPCError } from '@trpc/server'
import { Typebot, Webhook } from '@typebot.io/schemas'
import { z } from 'zod'
import { getLinkedTypebots } from '@/features/blocks/logic/typebotLink/helpers/getLinkedTypebots'
import { fetchLinkedTypebots } from '@/features/blocks/logic/typebotLink/helpers/fetchLinkedTypebots'
import { parseResultExample } from '../helpers/parseResultExample'
export const getResultExample = authenticatedProcedure
@@ -63,7 +63,7 @@ export const getResultExample = authenticatedProcedure
if (!block)
throw new TRPCError({ code: 'NOT_FOUND', message: 'Block not found' })
const linkedTypebots = await getLinkedTypebots(typebot, user)
const linkedTypebots = await fetchLinkedTypebots(typebot, user)
return {
resultExample: await parseResultExample(

View File

@@ -0,0 +1,66 @@
import prisma from '@/lib/prisma'
import { authenticatedProcedure } from '@/helpers/server/trpc'
import { TRPCError } from '@trpc/server'
import { Typebot, typebotSchema } from '@typebot.io/schemas'
import { z } from 'zod'
import { getUserRoleInWorkspace } from '@/features/workspace/helpers/getUserRoleInWorkspace'
export const getLinkedTypebots = authenticatedProcedure
.meta({
openapi: {
method: 'GET',
path: '/linkedTypebots',
protect: true,
summary: 'Get linked typebots',
tags: ['Typebot'],
},
})
.input(
z.object({
workspaceId: z.string(),
typebotIds: z.string().describe('Comma separated list of typebot ids'),
})
)
.output(
z.object({
typebots: z.array(
typebotSchema.pick({
id: true,
groups: true,
variables: true,
name: true,
})
),
})
)
.query(async ({ input: { workspaceId, typebotIds }, ctx: { user } }) => {
const typebotIdsArray = typebotIds.split(',')
const workspace = await prisma.workspace.findUnique({
where: { id: workspaceId },
select: { members: true },
})
const userRole = getUserRoleInWorkspace(user.id, workspace?.members)
if (userRole === undefined)
throw new TRPCError({ code: 'NOT_FOUND', message: 'Workspace not found' })
const typebots = (await prisma.typebot.findMany({
where: {
isArchived: { not: true },
id: { in: typebotIdsArray },
workspaceId,
},
orderBy: { createdAt: 'desc' },
select: {
id: true,
groups: true,
variables: true,
name: true,
},
})) as Pick<Typebot, 'id' | 'groups' | 'variables' | 'name'>[]
if (!typebots)
throw new TRPCError({ code: 'NOT_FOUND', message: 'No typebots found' })
return {
typebots,
}
})

View File

@@ -1,27 +1,22 @@
import prisma from '@/lib/prisma'
import { canReadTypebots } from '@/helpers/databaseRules'
import { User } from '@typebot.io/prisma'
import {
LogicBlockType,
PublicTypebot,
Typebot,
TypebotLinkBlock,
} from '@typebot.io/schemas'
import { isDefined } from '@typebot.io/lib'
import { LogicBlockType, PublicTypebot, Typebot } from '@typebot.io/schemas'
export const getLinkedTypebots = async (
export const fetchLinkedTypebots = async (
typebot: Pick<PublicTypebot, 'groups'>,
user?: User
): Promise<(Typebot | PublicTypebot)[]> => {
const linkedTypebotIds = (
typebot.groups
.flatMap((g) => g.blocks)
.filter(
(s) =>
s.type === LogicBlockType.TYPEBOT_LINK &&
isDefined(s.options.typebotId)
) as TypebotLinkBlock[]
).map((s) => s.options.typebotId as string)
const linkedTypebotIds = typebot.groups
.flatMap((group) => group.blocks)
.reduce<string[]>((typebotIds, block) => {
if (block.type !== LogicBlockType.TYPEBOT_LINK) return typebotIds
const typebotId = block.options.typebotId
if (!typebotId) return typebotIds
return typebotIds.includes(typebotId)
? typebotIds
: [...typebotIds, typebotId]
}, [])
if (linkedTypebotIds.length === 0) return []
const typebots = (await ('typebotId' in typebot
? prisma.publicTypebot.findMany({
@@ -36,6 +31,6 @@ export const getLinkedTypebots = async (
],
}
: { id: { in: linkedTypebotIds } },
}))) as unknown as (Typebot | PublicTypebot)[]
}))) as (Typebot | PublicTypebot)[]
return typebots
}

View File

@@ -57,7 +57,7 @@ test('should be configurable', async ({ page }) => {
await page.waitForTimeout(1000)
await page.getByTestId('selected-item-label').first().click({ force: true })
await page.click('button >> text=Current typebot')
await page.getByPlaceholder('Select a block').click()
await page.getByRole('textbox').nth(1).click()
await page.click('button >> text=Hello')
await page.click('text=Preview')