2
0

fix: 🚑️ Webhook and instant updates

This commit is contained in:
Baptiste Arnaud
2022-02-22 10:16:35 +01:00
parent 642a42779b
commit d49461cde6
36 changed files with 317 additions and 204 deletions

View File

@@ -1,10 +1,6 @@
import { Answer, ResultWithAnswers, VariableWithValue } from 'models'
import { Answer, ResultValues, VariableWithValue } from 'models'
import React, { createContext, ReactNode, useContext, useState } from 'react'
export type ResultValues = Pick<
ResultWithAnswers,
'answers' | 'createdAt' | 'prefilledVariables'
>
const answersContext = createContext<{
resultValues: ResultValues
addAnswer: (answer: Answer) => void

View File

@@ -1,4 +1,3 @@
import { ResultValues } from 'contexts/AnswersContext'
import {
IntegrationStep,
IntegrationStepType,
@@ -14,9 +13,10 @@ import {
SendEmailStep,
PublicBlock,
ZapierStep,
ResultValues,
} from 'models'
import { stringify } from 'qs'
import { parseAnswers, sendRequest } from 'utils'
import { sendRequest } from 'utils'
import { sendGaEvent } from '../../lib/gtag'
import { sendErrorMessage, sendInfoMessage } from './postMessage'
import { parseVariables, parseVariablesInObject } from './variable'
@@ -166,6 +166,8 @@ const executeWebhook = async (
updateVariableValue,
typebotId,
apiHost,
resultValues,
isPreview,
}: IntegrationContext
) => {
if (!step.webhook) return step.outgoingEdgeId
@@ -174,9 +176,11 @@ const executeWebhook = async (
method: 'POST',
body: {
variables,
resultValues,
},
})
console.error(error)
if (isPreview && error) sendErrorMessage(`Webhook failed: ${error.message}`)
step.options.responseVariableMapping.forEach((varMapping) => {
if (!varMapping?.bodyPath || !varMapping.variableId) return
const value = safeEval(`(${JSON.stringify(data)}).${varMapping?.bodyPath}`)
@@ -186,7 +190,7 @@ const executeWebhook = async (
const sendEmail = async (
step: SendEmailStep,
{ variables, apiHost, isPreview, resultValues, blocks }: IntegrationContext
{ variables, apiHost, isPreview }: IntegrationContext
) => {
if (isPreview) sendInfoMessage('Emails are not sent in preview mode')
if (isPreview) return step.outgoingEdgeId
@@ -198,15 +202,11 @@ const sendEmail = async (
credentialsId: options.credentialsId,
recipients: options.recipients.map(parseVariables(variables)),
subject: parseVariables(variables)(options.subject ?? ''),
body:
options.body === '{{state}}'
? parseAnswers({ variables, blocks })(resultValues)
: parseVariables(variables)(options.body ?? ''),
body: parseVariables(variables)(options.body ?? ''),
cc: (options.cc ?? []).map(parseVariables(variables)),
bcc: (options.bcc ?? []).map(parseVariables(variables)),
},
})
console.error(error)
if (isPreview && error) sendErrorMessage(`Webhook failed: ${error.message}`)
return step.outgoingEdgeId
}

View File

@@ -0,0 +1,9 @@
-- AlterTable
ALTER TABLE "DashboardFolder" ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
-- AlterTable
ALTER TABLE "PublicTypebot" ADD COLUMN "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;
-- AlterTable
ALTER TABLE "Result" ADD COLUMN "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP;

View File

@@ -90,6 +90,7 @@ model VerificationToken {
model DashboardFolder {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
name String
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
ownerId String
@@ -104,7 +105,7 @@ model DashboardFolder {
model Typebot {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
name String
ownerId String
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
@@ -125,22 +126,25 @@ model Typebot {
}
model PublicTypebot {
id String @id @default(cuid())
typebotId String @unique
typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
typebotId String @unique
typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
name String
blocks Json[]
variables Json[]
edges Json[]
theme Json
settings Json
publicId String? @unique
customDomain String? @unique
publicId String? @unique
customDomain String? @unique
}
model Result {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
typebotId String
typebot Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
answers Answer[]

View File

@@ -3,7 +3,13 @@ import { PublicTypebot as PublicTypebotFromPrisma } from 'db'
export type PublicTypebot = Omit<
PublicTypebotFromPrisma,
'blocks' | 'theme' | 'settings' | 'variables' | 'edges'
| 'blocks'
| 'theme'
| 'settings'
| 'variables'
| 'edges'
| 'createdAt'
| 'updatedAt'
> & {
blocks: PublicBlock[]
variables: Variable[]

View File

@@ -7,3 +7,8 @@ export type Result = Omit<
> & { createdAt: string; prefilledVariables: VariableWithValue[] }
export type ResultWithAnswers = Result & { answers: Answer[] }
export type ResultValues = Pick<
ResultWithAnswers,
'answers' | 'createdAt' | 'prefilledVariables'
>

View File

@@ -6,7 +6,7 @@ import { Variable } from './variable'
export type Typebot = Omit<
TypebotFromPrisma,
'blocks' | 'theme' | 'settings' | 'variables' | 'edges'
'blocks' | 'theme' | 'settings' | 'variables' | 'edges' | 'createdAt'
> & {
blocks: Block[]
variables: Variable[]

View File

@@ -34,6 +34,12 @@ export const sendRequest = async <ResponseData>(
const response = await fetch(url, {
method: typeof params === 'string' ? 'GET' : params.method,
mode: 'cors',
headers:
typeof params !== 'string' && isDefined(params.body)
? {
'Content-Type': 'application/json',
}
: undefined,
body:
typeof params !== 'string' && isDefined(params.body)
? JSON.stringify(params.body)