2
0

(setVariable) Add timezone option for dates

Closes #1251
This commit is contained in:
Baptiste Arnaud
2024-02-16 10:07:29 +01:00
parent 07240e6d4b
commit 3e0d3e7724
9 changed files with 250 additions and 33 deletions

View File

@@ -1,11 +1,12 @@
import { SessionState, SetVariableBlock, Variable } from '@typebot.io/schemas'
import { byId } from '@typebot.io/lib'
import { byId, isEmpty } from '@typebot.io/lib'
import { ExecuteLogicResponse } from '../../../types'
import { parseScriptToExecuteClientSideAction } from '../script/executeScript'
import { parseGuessedValueType } from '@typebot.io/variables/parseGuessedValueType'
import { parseVariables } from '@typebot.io/variables/parseVariables'
import { updateVariablesInSession } from '@typebot.io/variables/updateVariablesInSession'
import { createId } from '@paralleldrive/cuid2'
import { utcToZonedTime, format as tzFormat } from 'date-fns-tz'
export const executeSetVariable = (
state: SessionState,
@@ -87,13 +88,19 @@ const getExpressionToEvaluate =
return phoneNumber ? `"${state.whatsApp?.contact.phoneNumber}"` : null
}
case 'Now':
if (isEmpty(options.timeZone)) return 'new Date().toISOString()'
return toISOWithTz(new Date(), options.timeZone)
case 'Today':
return 'new Date().toISOString()'
case 'Tomorrow': {
return 'new Date(Date.now() + 86400000).toISOString()'
if (isEmpty(options.timeZone))
return 'new Date(Date.now() + 86400000).toISOString()'
return toISOWithTz(new Date(Date.now() + 86400000), options.timeZone)
}
case 'Yesterday': {
return 'new Date(Date.now() - 86400000).toISOString()'
if (isEmpty(options.timeZone))
return 'new Date(Date.now() - 86400000).toISOString()'
return toISOWithTz(new Date(Date.now() - 86400000), options.timeZone)
}
case 'Random ID': {
return `"${createId()}"`
@@ -130,3 +137,8 @@ const getExpressionToEvaluate =
}
}
}
const toISOWithTz = (date: Date, timeZone: string) => {
const zonedDate = utcToZonedTime(date, timeZone)
return tzFormat(zonedDate, "yyyy-MM-dd'T'HH:mm:ssXXX", { timeZone })
}

View File

@@ -477,7 +477,11 @@ const parseReply =
? { status: 'fail' }
: { status: 'skip' }
const urls = reply.split(', ')
const status = urls.some((url) => isURL(url)) ? 'success' : 'fail'
const status = urls.some((url) =>
isURL(url, { require_tld: env.S3_ENDPOINT !== 'localhost' })
)
? 'success'
: 'fail'
return { status, reply: reply }
}
case InputBlockType.PAYMENT: {

View File

@@ -19,8 +19,9 @@
"@typebot.io/variables": "workspace:*",
"@udecode/plate-common": "21.1.5",
"ai": "2.2.33",
"chrono-node": "2.7.0",
"chrono-node": "2.7.5",
"date-fns": "2.30.0",
"date-fns-tz": "2.0.0",
"google-auth-library": "8.9.0",
"google-spreadsheet": "4.1.1",
"got": "12.6.0",

View File

@@ -10,7 +10,7 @@ const extractBaseUrl = (val: string | undefined) => {
export const auth = {
type: 'encryptedCredentials',
name: 'Dify.AI account',
name: 'Dify.AI assistant',
schema: option.object({
apiEndpoint: option.string
.layout({

View File

@@ -13,9 +13,6 @@ const baseOptions = z.object({
const basicSetVariableOptionsSchema = baseOptions.extend({
type: z.enum([
'Today',
'Now',
'Yesterday',
'Tomorrow',
'Moment of the day',
'Empty',
'Environment name',
@@ -26,6 +23,11 @@ const basicSetVariableOptionsSchema = baseOptions.extend({
]),
})
const dateSetVariableOptionsSchema = baseOptions.extend({
type: z.enum(['Now', 'Yesterday', 'Tomorrow']),
timeZone: z.string().optional(),
})
const initialSetVariableOptionsSchema = baseOptions.extend({
type: z.undefined().openapi({ type: 'string' }),
expressionToEvaluate: z.string().optional(),
@@ -56,6 +58,7 @@ const appendItemToListOptionsSchema = baseOptions.extend({
export const setVariableOptionsSchema = z.discriminatedUnion('type', [
initialSetVariableOptionsSchema,
dateSetVariableOptionsSchema,
basicSetVariableOptionsSchema,
customSetVariableOptionsSchema,
mapListItemsOptionsSchema,