Files
bot/packages/bot-engine/src/utils/inputs.ts

63 lines
1.7 KiB
TypeScript
Raw Normal View History

import { validateEmail } from '@/features/blocks/inputs/email'
import { validatePhoneNumber } from '@/features/blocks/inputs/phone'
import { validateUrl } from '@/features/blocks/inputs/url'
import { parseVariables } from '@/features/variables'
2022-02-10 10:25:38 +01:00
import {
2022-06-11 07:27:38 +02:00
BubbleBlock,
BubbleBlockType,
2022-02-10 10:25:38 +01:00
Edge,
2022-06-11 07:27:38 +02:00
EmailInputBlock,
InputBlockType,
PhoneNumberInputBlock,
Block,
UrlInputBlock,
2022-02-10 10:25:38 +01:00
Variable,
} from 'models'
2022-06-11 07:27:38 +02:00
import { isInputBlock } from 'utils'
2022-02-10 10:25:38 +01:00
export const isInputValid = (
inputValue: string,
2022-06-11 07:27:38 +02:00
type: InputBlockType
2022-02-10 10:25:38 +01:00
): boolean => {
switch (type) {
2022-06-11 07:27:38 +02:00
case InputBlockType.EMAIL:
return validateEmail(inputValue)
2022-06-11 07:27:38 +02:00
case InputBlockType.PHONE:
return validatePhoneNumber(inputValue)
2022-06-11 07:27:38 +02:00
case InputBlockType.URL:
return validateUrl(inputValue)
2022-02-10 10:25:38 +01:00
}
return true
}
2022-06-11 07:27:38 +02:00
export const blockCanBeRetried = (
block: Block
): block is EmailInputBlock | UrlInputBlock | PhoneNumberInputBlock =>
isInputBlock(block) && 'retryMessageContent' in block.options
2022-02-10 10:25:38 +01:00
2022-06-11 07:27:38 +02:00
export const parseRetryBlock = (
block: EmailInputBlock | UrlInputBlock | PhoneNumberInputBlock,
2022-02-10 10:25:38 +01:00
variables: Variable[],
createEdge: (edge: Edge) => void
2022-06-11 07:27:38 +02:00
): BubbleBlock => {
const content = parseVariables(variables)(block.options.retryMessageContent)
const newBlockId = block.id + Math.random() * 1000
2022-02-10 10:25:38 +01:00
const newEdge: Edge = {
id: (Math.random() * 1000).toString(),
2022-06-11 07:27:38 +02:00
from: { blockId: newBlockId, groupId: block.groupId },
to: { groupId: block.groupId, blockId: block.id },
2022-02-10 10:25:38 +01:00
}
createEdge(newEdge)
return {
2022-06-11 07:27:38 +02:00
groupId: block.groupId,
id: newBlockId,
type: BubbleBlockType.TEXT,
2022-02-10 10:25:38 +01:00
content: {
html: `<div>${content}</div>`,
richText: [],
plainText: content,
},
outgoingEdgeId: newEdge.id,
}
}