2
0

🚸 (variables) Allow null values in variable list

This commit is contained in:
Baptiste Arnaud
2023-03-21 15:42:03 +01:00
parent c52a284013
commit 0c39ae41b6
18 changed files with 69 additions and 33 deletions

View File

@@ -19,7 +19,7 @@ export const injectVariableValuesInButtonsInputBlock =
if (!variable || typeof variable.value === 'string') return block
return {
...block,
items: variable.value.map((item, idx) => ({
items: variable.value.filter(isDefined).map((item, idx) => ({
id: idx.toString(),
type: ItemType.BUTTON,
blockId: block.id,

View File

@@ -9,10 +9,11 @@ import {
SendEmailOptions,
SessionState,
SmtpCredentials,
Variable,
} from '@typebot.io/schemas'
import { createTransport } from 'nodemailer'
import Mail from 'nodemailer/lib/mailer'
import { byId, isEmpty, isNotDefined, omit } from '@typebot.io/lib'
import { byId, isDefined, isEmpty, isNotDefined, omit } from '@typebot.io/lib'
import { parseAnswers } from '@typebot.io/lib/results'
import { decrypt } from '@typebot.io/lib/api'
import { defaultFrom, defaultTransportOptions } from './constants'
@@ -51,8 +52,7 @@ export const executeSendEmailBlock = async (
replyTo: options.replyTo
? parseVariables(variables)(options.replyTo)
: undefined,
fileUrls:
variables.find(byId(options.attachmentsVariableId))?.value ?? undefined,
fileUrls: getFileUrls(variables)(options.attachmentsVariableId),
isCustomBody: options.isCustomBody,
isBodyCode: options.isBodyCode,
})
@@ -238,3 +238,12 @@ const parseEmailRecipient = (
email: recipient,
}
}
const getFileUrls =
(variables: Variable[]) =>
(variableId: string | undefined): string | string[] | undefined => {
const fileUrls = variables.find(byId(variableId))?.value
if (!fileUrls) return
if (typeof fileUrls === 'string') return fileUrls
return fileUrls.filter(isDefined)
}

View File

@@ -83,7 +83,7 @@ const parseResultSample = (
headerCells: ResultHeaderCell[],
variables: Variable[]
) =>
headerCells.reduce<Record<string, string | string[] | undefined>>(
headerCells.reduce<Record<string, string | (string | null)[] | undefined>>(
(resultSample, cell) => {
const inputBlock = inputBlocks.find((inputBlock) =>
cell.blocks?.some((block) => block.id === inputBlock.id)

View File

@@ -41,8 +41,8 @@ const executeComparison =
if (isNotDefined(value)) return false
switch (comparison.comparisonOperator) {
case ComparisonOperators.CONTAINS: {
const contains = (a: string, b: string) => {
if (b === '') return false
const contains = (a: string | null, b: string | null) => {
if (b === '' || !b || !a) return false
return a.toLowerCase().trim().includes(b.toLowerCase().trim())
}
return compare(contains, inputValue, value)
@@ -55,14 +55,20 @@ const executeComparison =
}
case ComparisonOperators.GREATER: {
return compare(
(a, b) => parseFloat(a) > parseFloat(b),
(a, b) =>
isDefined(a) && isDefined(b)
? parseFloat(a) > parseFloat(b)
: false,
inputValue,
value
)
}
case ComparisonOperators.LESS: {
return compare(
(a, b) => parseFloat(a) < parseFloat(b),
(a, b) =>
isDefined(a) && isDefined(b)
? parseFloat(a) < parseFloat(b)
: false,
inputValue,
value
)
@@ -74,10 +80,11 @@ const executeComparison =
}
const compare = (
func: (a: string, b: string) => boolean,
a: string | string[],
b: string | string[]
func: (a: string | null, b: string | null) => boolean,
a: Variable['value'],
b: Variable['value']
): boolean => {
if (!a || !b) return false
if (typeof a === 'string') {
if (typeof b === 'string') return func(a, b)
return b.some((b) => func(a, b))

View File

@@ -2,7 +2,7 @@ import { Variable } from '@typebot.io/schemas'
export const findUniqueVariableValue =
(variables: Variable[]) =>
(value: string | undefined): string | string[] | null => {
(value: string | undefined): Variable['value'] => {
if (!value || !value.startsWith('{{') || !value.endsWith('}}')) return null
const variableName = value.slice(2, -2)
const variable = variables.find(

View File

@@ -2,7 +2,7 @@ import { Variable } from '@typebot.io/schemas'
export const parseGuessedValueType = (
value: Variable['value']
): string | string[] | boolean | number | null | undefined => {
): string | (string | null)[] | boolean | number | null | undefined => {
if (value === null) return null
if (value === undefined) return undefined
if (typeof value !== 'string') return value

View File

@@ -30,7 +30,7 @@ const updateResultVariables =
const serializedNewVariables = newVariables.map((variable) => ({
...variable,
value: Array.isArray(variable.value)
? variable.value.map(safeStringify).filter(isDefined)
? variable.value.map(safeStringify)
: safeStringify(variable.value),
}))
@@ -62,7 +62,7 @@ const updateTypebotVariables =
const serializedNewVariables = newVariables.map((variable) => ({
...variable,
value: Array.isArray(variable.value)
? variable.value.map(safeStringify).filter(isDefined)
? variable.value.map(safeStringify)
: safeStringify(variable.value),
}))