2
0

♻️ Add a test for internal waitUntil

This commit is contained in:
Baptiste Arnaud
2024-04-16 12:56:47 +02:00
parent 8d62898d15
commit 87f5d8515a
89 changed files with 1029 additions and 1154 deletions

View File

@@ -1,53 +0,0 @@
import {
WhatsAppWebhookRequestBody,
whatsAppWebhookRequestBodySchema,
} from '@typebot.io/schemas/features/whatsapp'
import { resumeWhatsAppFlow } from '@typebot.io/bot-engine/whatsapp/resumeWhatsAppFlow'
import { isNotDefined } from '@typebot.io/lib'
import type { RequestContext } from '@vercel/edge'
type Props = {
entry: WhatsAppWebhookRequestBody['entry']
workspaceId: string
credentialsId: string
}
const processWhatsAppReply = async ({
entry,
workspaceId,
credentialsId,
}: Props) => {
const receivedMessage = entry.at(0)?.changes.at(0)?.value.messages?.at(0)
if (isNotDefined(receivedMessage)) return { message: 'No message found' }
const contactName =
entry.at(0)?.changes.at(0)?.value?.contacts?.at(0)?.profile?.name ?? ''
const contactPhoneNumber =
entry.at(0)?.changes.at(0)?.value?.messages?.at(0)?.from ?? ''
const phoneNumberId = entry.at(0)?.changes.at(0)?.value
.metadata.phone_number_id
if (!phoneNumberId) return { message: 'No phone number id found' }
return resumeWhatsAppFlow({
receivedMessage,
sessionId: `wa-${phoneNumberId}-${receivedMessage.from}`,
phoneNumberId,
credentialsId,
workspaceId,
contact: {
name: contactName,
phoneNumber: contactPhoneNumber,
},
})
}
export async function POST(request: Request, context: RequestContext) {
const workspaceId = request.url.match(/\/workspaces\/([^/]+)\//)?.[1]
const credentialsId = request.url.match(/\/whatsapp\/([^/]+)\//)?.[1]
if (!workspaceId || !credentialsId) {
console.error('No workspace or credentials id found')
return { message: 'No workspace or credentials id found' }
}
const body = await request.json()
const { entry } = whatsAppWebhookRequestBodySchema.parse(body)
context.waitUntil(processWhatsAppReply({ entry, workspaceId, credentialsId }))
return new Response('Message is being processed.', { status: 200 })
}

View File

@@ -41,11 +41,7 @@ const landingPagePaths = [
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
transpilePackages: [
'@typebot.io/lib',
'@typebot.io/schemas',
'@typebot.io/emails',
],
transpilePackages: ['@typebot.io/lib', '@typebot.io/schemas'],
output: 'standalone',
experimental: {
outputFileTracingRoot: join(__dirname, '../../'),

View File

@@ -21,13 +21,14 @@
"@typebot.io/js": "workspace:*",
"@typebot.io/nextjs": "workspace:*",
"@typebot.io/prisma": "workspace:*",
"@udecode/plate-core": "31.3.2",
"ai": "3.0.12",
"bot-engine": "workspace:*",
"cors": "2.8.5",
"google-spreadsheet": "4.1.1",
"got": "12.6.0",
"ky": "1.2.3",
"next": "14.1.0",
"next": "14.2.1",
"nextjs-cors": "2.1.2",
"nodemailer": "6.9.8",
"openai": "4.28.4",
@@ -40,7 +41,7 @@
"@faire/mjml-react": "3.3.0",
"@paralleldrive/cuid2": "2.2.1",
"@playwright/test": "1.36.0",
"@typebot.io/emails": "workspace:*",
"@typebot.io/email-legacy": "workspace:*",
"@typebot.io/env": "workspace:*",
"@typebot.io/forge": "workspace:*",
"@typebot.io/forge-repository": "workspace:*",
@@ -56,7 +57,6 @@
"@types/papaparse": "5.3.7",
"@types/qs": "6.9.7",
"@types/react": "18.2.15",
"@vercel/edge": "1.1.1",
"dotenv": "16.4.5",
"dotenv-cli": "7.2.1",
"eslint": "8.44.0",

View File

@@ -13,8 +13,7 @@ import { decrypt } from '@typebot.io/lib/api/encryption/decrypt'
import Cors from 'cors'
import Mail from 'nodemailer/lib/mailer'
import { DefaultBotNotificationEmail } from '@typebot.io/emails'
import { render } from '@faire/mjml-react/utils/render'
import { DefaultBotNotificationEmail, render } from '@typebot.io/email-legacy'
import prisma from '@typebot.io/lib/prisma'
import { env } from '@typebot.io/env'
import { saveErrorLog } from '@typebot.io/bot-engine/logs/saveErrorLog'

View File

@@ -0,0 +1,20 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { createPlateEditor } from '@udecode/plate-core'
export const config = {
supportsResponseStreaming: true,
}
export default async function handler(_: NextApiRequest, res: NextApiResponse) {
console.log(createPlateEditor())
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const ctx = globalThis[Symbol.for('@vercel/request-context')]
ctx.get().waitUntil(wait)
return res.status(200).send('Message is being processed.')
}
const wait = async () => {
await new Promise((resolve) => setTimeout(resolve, 5000))
return
}