⚡ Regroup database queries of /sendMessage in one place
Potentially reduces the total queries to database and it will help to migrate to Edge runtime
This commit is contained in:
@@ -24,8 +24,6 @@ import { parseAnswers } from '@typebot.io/lib/results'
|
||||
import got, { Method, HTTPError, OptionsInit } from 'got'
|
||||
import { parseSampleResult } from './parseSampleResult'
|
||||
import { ExecuteIntegrationResponse } from '@/features/chat/types'
|
||||
import { saveErrorLog } from '@/features/logs/saveErrorLog'
|
||||
import { saveSuccessLog } from '@/features/logs/saveSuccessLog'
|
||||
import { parseVariables } from '@/features/variables/parseVariables'
|
||||
import { resumeWebhookExecution } from './resumeWebhookExecution'
|
||||
|
||||
@@ -39,21 +37,16 @@ export const executeWebhookBlock = async (
|
||||
block: WebhookBlock | ZapierBlock | MakeComBlock | PabblyConnectBlock
|
||||
): Promise<ExecuteIntegrationResponse> => {
|
||||
const { typebot, result } = state
|
||||
let log: ReplyLog | undefined
|
||||
const logs: ReplyLog[] = []
|
||||
const webhook = (await prisma.webhook.findUnique({
|
||||
where: { id: block.webhookId },
|
||||
})) as Webhook | null
|
||||
if (!webhook) {
|
||||
log = {
|
||||
logs.push({
|
||||
status: 'error',
|
||||
description: `Couldn't find webhook with id ${block.webhookId}`,
|
||||
}
|
||||
result &&
|
||||
(await saveErrorLog({
|
||||
resultId: result.id,
|
||||
message: log.description,
|
||||
}))
|
||||
return { outgoingEdgeId: block.outgoingEdgeId, logs: [log] }
|
||||
})
|
||||
return { outgoingEdgeId: block.outgoingEdgeId, logs }
|
||||
}
|
||||
const preparedWebhook = prepareWebhookAttributes(webhook, block.options)
|
||||
const parsedWebhook = await parseWebhookAttributes(
|
||||
@@ -62,16 +55,11 @@ export const executeWebhookBlock = async (
|
||||
result
|
||||
)(preparedWebhook)
|
||||
if (!parsedWebhook) {
|
||||
log = {
|
||||
logs.push({
|
||||
status: 'error',
|
||||
description: `Couldn't parse webhook attributes`,
|
||||
}
|
||||
result &&
|
||||
(await saveErrorLog({
|
||||
resultId: result.id,
|
||||
message: log.description,
|
||||
}))
|
||||
return { outgoingEdgeId: block.outgoingEdgeId, logs: [log] }
|
||||
})
|
||||
return { outgoingEdgeId: block.outgoingEdgeId, logs }
|
||||
}
|
||||
if (block.options.isExecutedOnClient)
|
||||
return {
|
||||
@@ -82,8 +70,14 @@ export const executeWebhookBlock = async (
|
||||
},
|
||||
],
|
||||
}
|
||||
const webhookResponse = await executeWebhook(parsedWebhook, result)
|
||||
return resumeWebhookExecution(state, block)(webhookResponse)
|
||||
const { response: webhookResponse, logs: executeWebhookLogs } =
|
||||
await executeWebhook(parsedWebhook)
|
||||
return resumeWebhookExecution({
|
||||
state,
|
||||
block,
|
||||
logs: executeWebhookLogs,
|
||||
response: webhookResponse,
|
||||
})
|
||||
}
|
||||
|
||||
const prepareWebhookAttributes = (
|
||||
@@ -162,9 +156,9 @@ const parseWebhookAttributes =
|
||||
}
|
||||
|
||||
export const executeWebhook = async (
|
||||
webhook: ParsedWebhook,
|
||||
result: ResultInSession
|
||||
): Promise<WebhookResponse> => {
|
||||
webhook: ParsedWebhook
|
||||
): Promise<{ response: WebhookResponse; logs?: ReplyLog[] }> => {
|
||||
const logs: ReplyLog[] = []
|
||||
const { headers, url, method, basicAuth, body, isJson } = webhook
|
||||
const contentType = headers ? headers['Content-Type'] : undefined
|
||||
|
||||
@@ -183,9 +177,9 @@ export const executeWebhook = async (
|
||||
} satisfies OptionsInit
|
||||
try {
|
||||
const response = await got(request.url, omit(request, 'url'))
|
||||
await saveSuccessLog({
|
||||
resultId: result.id,
|
||||
message: 'Webhook successfuly executed.',
|
||||
logs.push({
|
||||
status: 'success',
|
||||
description: `Webhook successfuly executed.`,
|
||||
details: {
|
||||
statusCode: response.statusCode,
|
||||
request,
|
||||
@@ -193,8 +187,11 @@ export const executeWebhook = async (
|
||||
},
|
||||
})
|
||||
return {
|
||||
statusCode: response.statusCode,
|
||||
data: safeJsonParse(response.body).data,
|
||||
response: {
|
||||
statusCode: response.statusCode,
|
||||
data: safeJsonParse(response.body).data,
|
||||
},
|
||||
logs,
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof HTTPError) {
|
||||
@@ -202,30 +199,31 @@ export const executeWebhook = async (
|
||||
statusCode: error.response.statusCode,
|
||||
data: safeJsonParse(error.response.body as string).data,
|
||||
}
|
||||
await saveErrorLog({
|
||||
resultId: result.id,
|
||||
message: 'Webhook returned an error',
|
||||
logs.push({
|
||||
status: 'error',
|
||||
description: `Webhook returned an error.`,
|
||||
details: {
|
||||
statusCode: error.response.statusCode,
|
||||
request,
|
||||
response,
|
||||
},
|
||||
})
|
||||
return response
|
||||
return { response, logs }
|
||||
}
|
||||
const response = {
|
||||
statusCode: 500,
|
||||
data: { message: `Error from Typebot server: ${error}` },
|
||||
}
|
||||
console.error(error)
|
||||
await saveErrorLog({
|
||||
resultId: result.id,
|
||||
message: 'Webhook failed to execute',
|
||||
logs.push({
|
||||
status: 'error',
|
||||
description: `Webhook failed to execute.`,
|
||||
details: {
|
||||
request,
|
||||
response,
|
||||
},
|
||||
})
|
||||
return response
|
||||
return { response, logs }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user