2023-09-20 15:26:52 +02:00
|
|
|
import { ParsedReply } from '../../../types'
|
2023-08-29 10:01:28 +02:00
|
|
|
import { DateInputBlock } from '@typebot.io/schemas'
|
|
|
|
import { parse as chronoParse } from 'chrono-node'
|
2023-09-05 10:34:56 +02:00
|
|
|
import { format } from 'date-fns'
|
2023-08-29 10:01:28 +02:00
|
|
|
|
|
|
|
export const parseDateReply = (
|
|
|
|
reply: string,
|
|
|
|
block: DateInputBlock
|
|
|
|
): ParsedReply => {
|
|
|
|
const parsedDate = chronoParse(reply)
|
|
|
|
if (parsedDate.length === 0) return { status: 'fail' }
|
2023-09-05 10:34:56 +02:00
|
|
|
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' }
|
|
|
|
|
2023-09-08 11:38:23 +02:00
|
|
|
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' }
|
|
|
|
|
2023-08-29 10:01:28 +02:00
|
|
|
return {
|
|
|
|
status: 'success',
|
|
|
|
reply: block.options.isRange ? `${startDate} to ${endDate}` : startDate,
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 10:34:56 +02:00
|
|
|
|
|
|
|
const parseDateWithNeutralTimezone = (date: Date) =>
|
|
|
|
new Date(date.valueOf() + date.getTimezoneOffset() * 60 * 1000)
|