🔥 Remove disable response saving option

Doesn't work properly when it comes to keep tracking storage usage
This commit is contained in:
Baptiste Arnaud
2023-03-07 14:41:57 +01:00
parent 0c19ea20f8
commit b77e2c8d2c
26 changed files with 194 additions and 182 deletions

View File

@@ -11,17 +11,18 @@ import Stripe from 'stripe'
import { decrypt } from 'utils/api/encryption'
export const computePaymentInputRuntimeOptions =
(state: Pick<SessionState, 'isPreview' | 'typebot'>) =>
(state: Pick<SessionState, 'result' | 'typebot'>) =>
(options: PaymentInputOptions) =>
createStripePaymentIntent(state)(options)
const createStripePaymentIntent =
(state: Pick<SessionState, 'isPreview' | 'typebot'>) =>
(state: Pick<SessionState, 'result' | 'typebot'>) =>
async (options: PaymentInputOptions): Promise<PaymentInputRuntimeOptions> => {
const {
isPreview,
result,
typebot: { variables },
} = state
const isPreview = !result.id
if (!options.credentialsId)
throw new TRPCError({
code: 'BAD_REQUEST',

View File

@@ -50,10 +50,11 @@ if (window.$chatwoot) {
}`
export const executeChatwootBlock = (
{ typebot: { variables }, isPreview }: SessionState,
{ typebot: { variables }, result }: SessionState,
block: ChatwootBlock,
lastBubbleBlockId?: string
): ExecuteIntegrationResponse => {
const isPreview = !result.id
const chatwootCode = parseChatwootOpenCode(block.options)
return {
outgoingEdgeId: block.outgoingEdgeId,

View File

@@ -6,7 +6,7 @@ import { render } from '@faire/mjml-react/utils/render'
import { DefaultBotNotificationEmail } from 'emails'
import {
PublicTypebot,
ResultValues,
ResultInSession,
SendEmailBlock,
SendEmailOptions,
SessionState,
@@ -20,11 +20,12 @@ import { decrypt } from 'utils/api'
import { defaultFrom, defaultTransportOptions } from '../constants'
export const executeSendEmailBlock = async (
{ result, typebot, isPreview }: SessionState,
{ result, typebot }: SessionState,
block: SendEmailBlock
): Promise<ExecuteIntegrationResponse> => {
const { options } = block
const { variables } = typebot
const isPreview = !result.id
if (isPreview)
return {
outgoingEdgeId: block.outgoingEdgeId,
@@ -37,7 +38,7 @@ export const executeSendEmailBlock = async (
}
await sendEmail({
typebotId: typebot.id,
resultId: result?.id,
result,
credentialsId: options.credentialsId,
recipients: options.recipients.map(parseVariables(variables)),
subject: parseVariables(variables)(options.subject ?? ''),
@@ -57,7 +58,7 @@ export const executeSendEmailBlock = async (
const sendEmail = async ({
typebotId,
resultId,
result,
credentialsId,
recipients,
body,
@@ -70,7 +71,7 @@ const sendEmail = async ({
fileUrls,
}: SendEmailOptions & {
typebotId: string
resultId?: string
result: ResultInSession
fileUrls?: string | string[]
}) => {
const { name: replyToName } = parseEmailRecipient(replyTo)
@@ -94,12 +95,12 @@ const sendEmail = async ({
isCustomBody,
isBodyCode,
typebotId,
resultId,
result,
})
if (!emailBody) {
await saveErrorLog({
resultId,
resultId: result.id,
message: 'Email not sent',
details: {
error: 'No email body found',
@@ -132,7 +133,7 @@ const sendEmail = async ({
try {
await transporter.sendMail(email)
await saveSuccessLog({
resultId,
resultId: result.id,
message: 'Email successfully sent',
details: {
transportConfig: {
@@ -144,7 +145,7 @@ const sendEmail = async ({
})
} catch (err) {
await saveErrorLog({
resultId,
resultId: result.id,
message: 'Email not sent',
details: {
error: err,
@@ -182,10 +183,10 @@ const getEmailBody = async ({
isCustomBody,
isBodyCode,
typebotId,
resultId,
result,
}: {
typebotId: string
resultId?: string
result: ResultInSession
} & Pick<SendEmailOptions, 'isCustomBody' | 'isBodyCode' | 'body'>): Promise<
{ html?: string; text?: string } | undefined
> => {
@@ -198,12 +199,7 @@ const getEmailBody = async ({
where: { typebotId },
})) as unknown as PublicTypebot
if (!typebot) return
const resultValues = (await prisma.result.findUnique({
where: { id: resultId },
include: { answers: true },
})) as ResultValues | null
if (!resultValues) return
const answers = parseAnswers(typebot, [])(resultValues)
const answers = parseAnswers(typebot, [])(result)
return {
html: render(
<DefaultBotNotificationEmail

View File

@@ -16,16 +16,15 @@ import {
WebhookOptions,
defaultWebhookAttributes,
HttpMethod,
ResultValues,
PublicTypebot,
KeyValue,
ReplyLog,
ResultInSession,
} from 'models'
import { stringify } from 'qs'
import { byId, omit } from 'utils'
import { parseAnswers } from 'utils/results'
import got, { Method, Headers, HTTPError } from 'got'
import { getResultValues } from '@/features/results/api'
import { parseSampleResult } from './parseSampleResult'
export const executeWebhookBlock = async (
@@ -50,14 +49,11 @@ export const executeWebhookBlock = async (
return { outgoingEdgeId: block.outgoingEdgeId, logs: [log] }
}
const preparedWebhook = prepareWebhookAttributes(webhook, block.options)
const resultValues =
(result && (await getResultValues(result.id))) ?? undefined
const webhookResponse = await executeWebhook({ typebot })(
preparedWebhook,
typebot.variables,
block.groupId,
resultValues,
result?.id
result
)
const status = webhookResponse.statusCode.toString()
const isError = status.startsWith('4') || status.startsWith('5')
@@ -139,8 +135,7 @@ export const executeWebhook =
webhook: Webhook,
variables: Variable[],
groupId: string,
resultValues?: ResultValues,
resultId?: string
result: ResultInSession
): Promise<WebhookResponse> => {
if (!webhook.url || !webhook.method)
return {
@@ -176,7 +171,7 @@ export const executeWebhook =
[]
)({
body: webhook.body,
resultValues,
result,
groupId,
variables,
})
@@ -206,7 +201,7 @@ export const executeWebhook =
try {
const response = await got(request.url, omit(request, 'url'))
await saveSuccessLog({
resultId,
resultId: result.id,
message: 'Webhook successfuly executed.',
details: {
statusCode: response.statusCode,
@@ -225,7 +220,7 @@ export const executeWebhook =
data: safeJsonParse(error.response.body as string).data,
}
await saveErrorLog({
resultId,
resultId: result.id,
message: 'Webhook returned an error',
details: {
request,
@@ -240,7 +235,7 @@ export const executeWebhook =
}
console.error(error)
await saveErrorLog({
resultId,
resultId: result.id,
message: 'Webhook failed to execute',
details: {
request,
@@ -258,20 +253,20 @@ const getBodyContent =
) =>
async ({
body,
resultValues,
result,
groupId,
variables,
}: {
body?: string | null
resultValues?: ResultValues
result?: ResultInSession
groupId: string
variables: Variable[]
}): Promise<string | undefined> => {
if (!body) return
return body === '{{state}}'
? JSON.stringify(
resultValues
? parseAnswers(typebot, linkedTypebots)(resultValues)
result
? parseAnswers(typebot, linkedTypebots)(result)
: await parseSampleResult(typebot, linkedTypebots)(
groupId,
variables

View File

@@ -29,7 +29,7 @@ export const parseSampleResult =
return {
message: 'This is a sample result, it has been generated ⬇️',
'Submitted at': new Date().toISOString(),
submittedAt: new Date().toISOString(),
...parseResultSample(linkedInputBlocks, header, variables),
}
}

View File

@@ -115,7 +115,8 @@ const getLinkedTypebot = async (
state: SessionState,
typebotId: string
): Promise<TypebotInSession | null> => {
const { typebot, isPreview } = state
const { typebot, result } = state
const isPreview = !result.id
if (typebotId === 'current') return typebot
const availableTypebots =
'linkedTypebots' in state
@@ -123,12 +124,12 @@ const getLinkedTypebot = async (
: [typebot]
const linkedTypebot =
availableTypebots.find(byId(typebotId)) ??
(await fetchTypebot({ isPreview }, typebotId))
(await fetchTypebot(isPreview, typebotId))
return linkedTypebot
}
const fetchTypebot = async (
{ isPreview }: Pick<SessionState, 'isPreview'>,
isPreview: boolean,
typebotId: string
): Promise<TypebotInSession | null> => {
if (isPreview) {