♻️ Export bot-engine code into its own package
This commit is contained in:
48
packages/bot-engine/blocks/inputs/date/parseDateInput.ts
Normal file
48
packages/bot-engine/blocks/inputs/date/parseDateInput.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { getPrefilledInputValue } from '../../../getPrefilledValue'
|
||||
import {
|
||||
DateInputBlock,
|
||||
DateInputOptions,
|
||||
SessionState,
|
||||
Variable,
|
||||
} from '@typebot.io/schemas'
|
||||
import { deepParseVariables } from '../../../variables/deepParseVariables'
|
||||
import { parseVariables } from '../../../variables/parseVariables'
|
||||
|
||||
export const parseDateInput =
|
||||
(state: SessionState) => (block: DateInputBlock) => {
|
||||
return {
|
||||
...block,
|
||||
options: {
|
||||
...deepParseVariables(state.typebotsQueue[0].typebot.variables)(
|
||||
block.options
|
||||
),
|
||||
min: parseDateLimit(
|
||||
block.options.min,
|
||||
block.options.hasTime,
|
||||
state.typebotsQueue[0].typebot.variables
|
||||
),
|
||||
max: parseDateLimit(
|
||||
block.options.max,
|
||||
block.options.hasTime,
|
||||
state.typebotsQueue[0].typebot.variables
|
||||
),
|
||||
},
|
||||
prefilledValue: getPrefilledInputValue(
|
||||
state.typebotsQueue[0].typebot.variables
|
||||
)(block),
|
||||
}
|
||||
}
|
||||
|
||||
const parseDateLimit = (
|
||||
limit: DateInputOptions['min'] | DateInputOptions['max'],
|
||||
hasTime: DateInputOptions['hasTime'],
|
||||
variables: Variable[]
|
||||
) => {
|
||||
if (!limit) return
|
||||
const parsedLimit = parseVariables(variables)(limit)
|
||||
const dateIsoNoSecondsRegex = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d/
|
||||
const matchDateTime = parsedLimit.match(dateIsoNoSecondsRegex)
|
||||
if (matchDateTime)
|
||||
return hasTime ? matchDateTime[0] : matchDateTime[0].slice(0, 10)
|
||||
return parsedLimit
|
||||
}
|
||||
51
packages/bot-engine/blocks/inputs/date/parseDateReply.ts
Normal file
51
packages/bot-engine/blocks/inputs/date/parseDateReply.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { ParsedReply } from '../../../types'
|
||||
import { DateInputBlock } from '@typebot.io/schemas'
|
||||
import { parse as chronoParse } from 'chrono-node'
|
||||
import { format } from 'date-fns'
|
||||
|
||||
export const parseDateReply = (
|
||||
reply: string,
|
||||
block: DateInputBlock
|
||||
): ParsedReply => {
|
||||
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')
|
||||
|
||||
const detectedStartDate = parseDateWithNeutralTimezone(
|
||||
parsedDate[0].start.date()
|
||||
)
|
||||
const startDate = format(detectedStartDate, formatString)
|
||||
|
||||
const detectedEndDate = parsedDate[0].end?.date()
|
||||
? parseDateWithNeutralTimezone(parsedDate[0].end?.date())
|
||||
: undefined
|
||||
const endDate = detectedEndDate
|
||||
? format(detectedEndDate, formatString)
|
||||
: undefined
|
||||
|
||||
if (block.options.isRange && !endDate) return { status: 'fail' }
|
||||
|
||||
if (
|
||||
block.options.max &&
|
||||
(detectedStartDate > new Date(block.options.max) ||
|
||||
(detectedEndDate && detectedEndDate > new Date(block.options.max)))
|
||||
)
|
||||
return { status: 'fail' }
|
||||
|
||||
if (
|
||||
block.options.min &&
|
||||
(detectedStartDate < new Date(block.options.min) ||
|
||||
(detectedEndDate && detectedEndDate < new Date(block.options.min)))
|
||||
)
|
||||
return { status: 'fail' }
|
||||
|
||||
return {
|
||||
status: 'success',
|
||||
reply: block.options.isRange ? `${startDate} to ${endDate}` : startDate,
|
||||
}
|
||||
}
|
||||
|
||||
const parseDateWithNeutralTimezone = (date: Date) =>
|
||||
new Date(date.valueOf() + date.getTimezoneOffset() * 60 * 1000)
|
||||
Reference in New Issue
Block a user