Skip to content

Commit

Permalink
feat(webhook): ⚡️ Show linked typebots results in webhook sample
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Apr 21, 2022
1 parent 937621e commit 12f43cd
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 88 deletions.
12 changes: 9 additions & 3 deletions apps/builder/services/api/dbRules.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { CollaborationType, Prisma, User } from 'db'

const parseWhereFilter = (
typebotId: string,
typebotIds: string[] | string,
user: User,
type: 'read' | 'write'
): Prisma.TypebotWhereInput => ({
OR: [
{
id: typebotId,
id: typeof typebotIds === 'string' ? typebotIds : { in: typebotIds },
ownerId:
(type === 'read' && user.email === process.env.ADMIN_EMAIL) ||
process.env.NEXT_PUBLIC_E2E_TEST
? undefined
: user.id,
},
{
id: typebotId,
id: typeof typebotIds === 'string' ? typebotIds : { in: typebotIds },
collaborators: {
some: {
userId: user.id,
Expand All @@ -31,3 +31,9 @@ export const canReadTypebot = (typebotId: string, user: User) =>

export const canWriteTypebot = (typebotId: string, user: User) =>
parseWhereFilter(typebotId, user, 'write')

export const canReadTypebots = (typebotIds: string[], user: User) =>
parseWhereFilter(typebotIds, user, 'read')

export const canWriteTypebots = (typebotIds: string[], user: User) =>
parseWhereFilter(typebotIds, user, 'write')
13 changes: 0 additions & 13 deletions apps/viewer/pages/api/ping.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ import { stringify } from 'qs'
import { withSentry } from '@sentry/nextjs'
import Cors from 'cors'
import { parseSampleResult } from 'services/api/webhooks'
import { saveErrorLog, saveSuccessLog } from 'services/api/utils'
import {
getLinkedTypebots,
saveErrorLog,
saveSuccessLog,
} from 'services/api/utils'

const cors = initMiddleware(Cors())
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
Expand Down Expand Up @@ -117,9 +121,13 @@ export const executeWebhook =
convertKeyValueTableToObject(webhook.queryParams, variables)
)
const contentType = headers ? headers['Content-Type'] : undefined
const linkedTypebots = await getLinkedTypebots(typebot)
const body =
webhook.method !== HttpMethod.GET
? getBodyContent(typebot)({
? await getBodyContent(
typebot,
linkedTypebots
)({
body: webhook.body,
resultValues,
blockId,
Expand Down Expand Up @@ -178,22 +186,34 @@ export const executeWebhook =
}

const getBodyContent =
(typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables' | 'edges'>) =>
({
(
typebot: Pick<Typebot | PublicTypebot, 'blocks' | 'variables' | 'edges'>,
linkedTypebots: (Typebot | PublicTypebot)[]
) =>
async ({
body,
resultValues,
blockId,
}: {
body?: string | null
resultValues?: ResultValues
blockId: string
}): string | undefined => {
}): Promise<string | undefined> => {
if (!body) return
return body === '{{state}}'
? JSON.stringify(
resultValues
? parseAnswers(typebot)(resultValues)
: parseSampleResult(typebot)(blockId)
? parseAnswers({
blocks: [
...typebot.blocks,
...linkedTypebots.flatMap((t) => t.blocks),
],
variables: [
...typebot.variables,
...linkedTypebots.flatMap((t) => t.variables),
],
})(resultValues)
: await parseSampleResult(typebot, linkedTypebots)(blockId)
)
: body
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import prisma from 'libs/prisma'
import { Typebot } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { authenticateUser } from 'services/api/utils'
import { authenticateUser, getLinkedTypebots } from 'services/api/utils'
import { parseSampleResult } from 'services/api/webhooks'
import { methodNotAllowed } from 'utils'

Expand All @@ -19,7 +19,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
.flatMap((b) => b.steps)
.find((s) => s.id === stepId)
if (!step) return res.status(404).send({ message: 'Block not found' })
return res.send(parseSampleResult(typebot)(step.blockId))
const linkedTypebots = await getLinkedTypebots(typebot, user)
return res.send(
await parseSampleResult(typebot, linkedTypebots)(step.blockId)
)
}
methodNotAllowed(res)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import prisma from 'libs/prisma'
import { Typebot } from 'models'
import { NextApiRequest, NextApiResponse } from 'next'
import { authenticateUser } from 'services/api/utils'
import { authenticateUser, getLinkedTypebots } from 'services/api/utils'
import { parseSampleResult } from 'services/api/webhooks'
import { methodNotAllowed } from 'utils'

Expand All @@ -15,7 +15,8 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
where: { id_ownerId: { id: typebotId, ownerId: user.id } },
})) as unknown as Typebot | undefined
if (!typebot) return res.status(400).send({ message: 'Typebot not found' })
return res.send(parseSampleResult(typebot)(blockId))
const linkedTypebots = await getLinkedTypebots(typebot, user)
return res.send(await parseSampleResult(typebot, linkedTypebots)(blockId))
}
methodNotAllowed(res)
}
Expand Down
39 changes: 39 additions & 0 deletions apps/viewer/services/api/dbRules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { CollaborationType, Prisma, User } from 'db'

const parseWhereFilter = (
typebotIds: string[] | string,
user: User,
type: 'read' | 'write'
): Prisma.TypebotWhereInput => ({
OR: [
{
id: typeof typebotIds === 'string' ? typebotIds : { in: typebotIds },
ownerId:
(type === 'read' && user.email === process.env.ADMIN_EMAIL) ||
process.env.NEXT_PUBLIC_E2E_TEST
? undefined
: user.id,
},
{
id: typeof typebotIds === 'string' ? typebotIds : { in: typebotIds },
collaborators: {
some: {
userId: user.id,
type: type === 'write' ? CollaborationType.WRITE : undefined,
},
},
},
],
})

export const canReadTypebot = (typebotId: string, user: User) =>
parseWhereFilter(typebotId, user, 'read')

export const canWriteTypebot = (typebotId: string, user: User) =>
parseWhereFilter(typebotId, user, 'write')

export const canReadTypebots = (typebotIds: string[], user: User) =>
parseWhereFilter(typebotIds, user, 'read')

export const canWriteTypebots = (typebotIds: string[], user: User) =>
parseWhereFilter(typebotIds, user, 'write')
34 changes: 34 additions & 0 deletions apps/viewer/services/api/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { User } from 'db'
import prisma from 'libs/prisma'
import { LogicStepType, Typebot, TypebotLinkStep, PublicTypebot } from 'models'
import { NextApiRequest } from 'next'
import { isDefined } from 'utils'
import { canReadTypebots } from './dbRules'

export const authenticateUser = async (
req: NextApiRequest
Expand Down Expand Up @@ -52,3 +55,34 @@ const formatDetails = (details: any) => {
return details
}
}

export const getLinkedTypebots = async (
typebot: Typebot | PublicTypebot,
user?: User
): Promise<(Typebot | PublicTypebot)[]> => {
const linkedTypebotIds = (
typebot.blocks
.flatMap((b) => b.steps)
.filter(
(s) =>
s.type === LogicStepType.TYPEBOT_LINK &&
isDefined(s.options.typebotId)
) as TypebotLinkStep[]
).map((s) => s.options.typebotId as string)
if (linkedTypebotIds.length === 0) return []
const typebots = (await ('typebotId' in typebot
? prisma.publicTypebot.findMany({
where: { id: { in: linkedTypebotIds } },
})
: prisma.typebot.findMany({
where: user
? {
AND: [
{ id: { in: linkedTypebotIds } },
canReadTypebots(linkedTypebotIds, user as User),
],
}
: { id: { in: linkedTypebotIds } },
}))) as unknown as (Typebot | PublicTypebot)[]
return typebots
}
Loading

4 comments on commit 12f43cd

@vercel
Copy link

@vercel vercel bot commented on 12f43cd Apr 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 12f43cd Apr 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 12f43cd Apr 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-typebot-io.vercel.app
builder-v2-git-main-typebot-io.vercel.app
app.typebot.io

Please sign in to comment.