2
0

♻️ Introduce typebot v6 with events (#1013)

Closes #885
This commit is contained in:
Baptiste Arnaud
2023-11-08 15:34:16 +01:00
committed by GitHub
parent 68e4fc71fb
commit 35300eaf34
634 changed files with 58971 additions and 31449 deletions

View File

@@ -2,7 +2,6 @@ import {
SessionState,
VariableWithValue,
ChoiceInputBlock,
ItemType,
} from '@typebot.io/schemas'
import { isDefined } from '@typebot.io/lib'
import { filterChoiceItems } from './filterChoiceItems'
@@ -14,10 +13,10 @@ export const injectVariableValuesInButtonsInputBlock =
(state: SessionState) =>
(block: ChoiceInputBlock): ChoiceInputBlock => {
const { variables } = state.typebotsQueue[0].typebot
if (block.options.dynamicVariableId) {
if (block.options?.dynamicVariableId) {
const variable = variables.find(
(variable) =>
variable.id === block.options.dynamicVariableId &&
variable.id === block.options?.dynamicVariableId &&
isDefined(variable.value)
) as VariableWithValue | undefined
if (!variable) return block
@@ -26,7 +25,6 @@ export const injectVariableValuesInButtonsInputBlock =
...block,
items: value.filter(isDefined).map((item, idx) => ({
id: idx.toString(),
type: ItemType.BUTTON,
blockId: block.id,
content: item,
})),

View File

@@ -7,7 +7,7 @@ export const parseButtonsReply =
(inputValue: string, block: ChoiceInputBlock): ParsedReply => {
const displayedItems =
injectVariableValuesInButtonsInputBlock(state)(block).items
if (block.options.isMultipleChoice) {
if (block.options?.isMultipleChoice) {
const longestItemsFirst = [...displayedItems].sort(
(a, b) => (b.content?.length ?? 0) - (a.content?.length ?? 0)
)

View File

@@ -1,15 +1,11 @@
import { getPrefilledInputValue } from '../../../getPrefilledValue'
import {
DateInputBlock,
DateInputOptions,
SessionState,
Variable,
} from '@typebot.io/schemas'
import { DateInputBlock, SessionState, Variable } from '@typebot.io/schemas'
import { deepParseVariables } from '../../../variables/deepParseVariables'
import { parseVariables } from '../../../variables/parseVariables'
export const parseDateInput =
(state: SessionState) => (block: DateInputBlock) => {
if (!block.options) return block
return {
...block,
options: {
@@ -34,8 +30,10 @@ export const parseDateInput =
}
const parseDateLimit = (
limit: DateInputOptions['min'] | DateInputOptions['max'],
hasTime: DateInputOptions['hasTime'],
limit:
| NonNullable<DateInputBlock['options']>['min']
| NonNullable<DateInputBlock['options']>['max'],
hasTime: NonNullable<DateInputBlock['options']>['hasTime'],
variables: Variable[]
) => {
if (!limit) return

View File

@@ -1,7 +1,9 @@
import { isDefined } from '@typebot.io/lib'
import { ParsedReply } from '../../../types'
import { DateInputBlock } from '@typebot.io/schemas'
import { parse as chronoParse } from 'chrono-node'
import { format } from 'date-fns'
import { defaultDateInputOptions } from '@typebot.io/schemas/features/blocks/inputs/date/constants'
export const parseDateReply = (
reply: string,
@@ -10,8 +12,10 @@ export const parseDateReply = (
const parsedDate = chronoParse(reply)
if (parsedDate.length === 0) return { status: 'fail' }
const formatString =
block.options.format ??
(block.options.hasTime ? 'dd/MM/yyyy HH:mm' : 'dd/MM/yyyy')
block.options?.format ??
(block.options?.hasTime
? defaultDateInputOptions.formatWithTime
: defaultDateInputOptions.format)
const detectedStartDate = parseDateWithNeutralTimezone(
parsedDate[0].start.date()
@@ -25,25 +29,27 @@ export const parseDateReply = (
? format(detectedEndDate, formatString)
: undefined
if (block.options.isRange && !endDate) return { status: 'fail' }
if (block.options?.isRange && !endDate) return { status: 'fail' }
const max = block.options?.max
if (
block.options.max &&
(detectedStartDate > new Date(block.options.max) ||
(detectedEndDate && detectedEndDate > new Date(block.options.max)))
isDefined(max) &&
(detectedStartDate > new Date(max) ||
(detectedEndDate && detectedEndDate > new Date(max)))
)
return { status: 'fail' }
const min = block.options?.min
if (
block.options.min &&
(detectedStartDate < new Date(block.options.min) ||
(detectedEndDate && detectedEndDate < new Date(block.options.min)))
isDefined(min) &&
(detectedStartDate < new Date(min) ||
(detectedEndDate && detectedEndDate < new Date(min)))
)
return { status: 'fail' }
return {
status: 'success',
reply: block.options.isRange ? `${startDate} to ${endDate}` : startDate,
reply: block.options?.isRange ? `${startDate} to ${endDate}` : startDate,
}
}

View File

@@ -1,6 +1,6 @@
import { TRPCError } from '@trpc/server'
import {
PaymentInputOptions,
PaymentInputBlock,
PaymentInputRuntimeOptions,
SessionState,
StripeCredentials,
@@ -9,20 +9,23 @@ import Stripe from 'stripe'
import { decrypt } from '@typebot.io/lib/api/encryption/decrypt'
import { parseVariables } from '../../../variables/parseVariables'
import prisma from '@typebot.io/lib/prisma'
import { defaultPaymentInputOptions } from '@typebot.io/schemas/features/blocks/inputs/payment/constants'
export const computePaymentInputRuntimeOptions =
(state: SessionState) => (options: PaymentInputOptions) =>
(state: SessionState) => (options: PaymentInputBlock['options']) =>
createStripePaymentIntent(state)(options)
const createStripePaymentIntent =
(state: SessionState) =>
async (options: PaymentInputOptions): Promise<PaymentInputRuntimeOptions> => {
async (
options: PaymentInputBlock['options']
): Promise<PaymentInputRuntimeOptions> => {
const {
resultId,
typebot: { variables },
} = state.typebotsQueue[0]
const isPreview = !resultId
if (!options.credentialsId)
if (!options?.credentialsId)
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'Missing credentialsId',
@@ -39,9 +42,10 @@ const createStripePaymentIntent =
: stripeKeys.live.secretKey,
{ apiVersion: '2022-11-15' }
)
const currency = options?.currency ?? defaultPaymentInputOptions.currency
const amount = Math.round(
Number(parseVariables(variables)(options.amount)) *
(isZeroDecimalCurrency(options.currency) ? 1 : 100)
(isZeroDecimalCurrency(currency) ? 1 : 100)
)
if (isNaN(amount))
throw new TRPCError({
@@ -55,7 +59,7 @@ const createStripePaymentIntent =
)
const paymentIntent = await stripe.paymentIntents.create({
amount,
currency: options.currency,
currency,
receipt_email: receiptEmail === '' ? undefined : receiptEmail,
description: options.additionalInformation?.description,
automatic_payment_methods: {
@@ -84,7 +88,7 @@ const createStripePaymentIntent =
? stripeKeys.test.publicKey
: stripeKeys.live.publicKey,
amountLabel: priceFormatter.format(
amount / (isZeroDecimalCurrency(options.currency) ? 1 : 100)
amount / (isZeroDecimalCurrency(currency) ? 1 : 100)
),
}
}

View File

@@ -1,6 +1,5 @@
import {
VariableWithValue,
ItemType,
PictureChoiceBlock,
Variable,
} from '@typebot.io/schemas'
@@ -12,19 +11,19 @@ export const injectVariableValuesInPictureChoiceBlock =
(variables: Variable[]) =>
(block: PictureChoiceBlock): PictureChoiceBlock => {
if (
block.options.dynamicItems?.isEnabled &&
block.options?.dynamicItems?.isEnabled &&
block.options.dynamicItems.pictureSrcsVariableId
) {
const pictureSrcsVariable = variables.find(
(variable) =>
variable.id === block.options.dynamicItems?.pictureSrcsVariableId &&
variable.id === block.options?.dynamicItems?.pictureSrcsVariableId &&
isDefined(variable.value)
) as VariableWithValue | undefined
if (!pictureSrcsVariable) return block
const titlesVariable = block.options.dynamicItems.titlesVariableId
? (variables.find(
(variable) =>
variable.id === block.options.dynamicItems?.titlesVariableId &&
variable.id === block.options?.dynamicItems?.titlesVariableId &&
isDefined(variable.value)
) as VariableWithValue | undefined)
: undefined
@@ -37,7 +36,7 @@ export const injectVariableValuesInPictureChoiceBlock =
? (variables.find(
(variable) =>
variable.id ===
block.options.dynamicItems?.descriptionsVariableId &&
block.options?.dynamicItems?.descriptionsVariableId &&
isDefined(variable.value)
) as VariableWithValue | undefined)
: undefined
@@ -55,7 +54,6 @@ export const injectVariableValuesInPictureChoiceBlock =
...block,
items: variableValues.filter(isDefined).map((pictureSrc, idx) => ({
id: idx.toString(),
type: ItemType.PICTURE_CHOICE,
blockId: block.id,
pictureSrc,
title: titlesVariableValues?.[idx] ?? '',

View File

@@ -8,7 +8,7 @@ export const parsePictureChoicesReply =
const displayedItems = injectVariableValuesInPictureChoiceBlock(
state.typebotsQueue[0].typebot.variables
)(block).items
if (block.options.isMultipleChoice) {
if (block.options?.isMultipleChoice) {
const longestItemsFirst = [...displayedItems].sort(
(a, b) => (b.title?.length ?? 0) - (a.title?.length ?? 0)
)

View File

@@ -1,4 +1,5 @@
import { RatingInputBlock } from '@typebot.io/schemas'
import { defaultRatingInputOptions } from '@typebot.io/schemas/features/blocks/inputs/rating/constants'
export const validateRatingReply = (reply: string, block: RatingInputBlock) =>
Number(reply) <= block.options.length
Number(reply) <= (block.options?.length ?? defaultRatingInputOptions.length)