From 59edfeb194f1ee6ed8ac710f29cfcab75a93df36 Mon Sep 17 00:00:00 2001 From: Mirian Okradze Date: Thu, 30 Nov 2023 14:12:00 +0400 Subject: [PATCH] fix: schedule date timezones --- apps/server/models/schedule.py | 6 +++--- apps/ui/src/pages/Schedule/Schedule.utils.ts | 20 +++++++++++++++++++ .../src/pages/Schedule/useCreateSchedule.ts | 7 ++++--- apps/ui/src/pages/Schedule/useEditSchedule.ts | 11 +++++----- .../schedule/useCreateScheduleService.ts | 3 ++- 5 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 apps/ui/src/pages/Schedule/Schedule.utils.ts diff --git a/apps/server/models/schedule.py b/apps/server/models/schedule.py index 006eef109..3e319d483 100644 --- a/apps/server/models/schedule.py +++ b/apps/server/models/schedule.py @@ -42,10 +42,10 @@ class ScheduleModel(BaseModel): status = Column(String, nullable=True, default=ScheduleStatus.PENDING.value) schedule_type = Column(String) # `Outbound & Inbound` cron_expression = Column(String) - start_date = Column(DateTime, nullable=True) - end_date = Column(DateTime, nullable=True) + start_date = Column(DateTime(timezone=True), nullable=True) + end_date = Column(DateTime(timezone=True), nullable=True) interval = Column(String, nullable=True) - next_run_date = Column(DateTime, nullable=True, index=True) + next_run_date = Column(DateTime(timezone=True), nullable=True, index=True) max_daily_budget = Column(Numeric(precision=5, scale=2), nullable=True) is_active = Column(Boolean, default=True, index=True) is_deleted = Column(Boolean, default=False, index=True) diff --git a/apps/ui/src/pages/Schedule/Schedule.utils.ts b/apps/ui/src/pages/Schedule/Schedule.utils.ts new file mode 100644 index 000000000..c7af43bae --- /dev/null +++ b/apps/ui/src/pages/Schedule/Schedule.utils.ts @@ -0,0 +1,20 @@ +const getDateTimeFromDate = (date: Date) => { + const formattedDate = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart( + 2, + '0', + )}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart( + 2, + '0', + )}:${String(date.getMinutes()).padStart(2, '0')}:00` + + return formattedDate +} + +const getDateTime = (dateString: string | undefined | null) => { + if (!dateString) return + + const date = new Date(dateString) + return getDateTimeFromDate(date) +} + +export { getDateTime, getDateTimeFromDate } diff --git a/apps/ui/src/pages/Schedule/useCreateSchedule.ts b/apps/ui/src/pages/Schedule/useCreateSchedule.ts index b580f4833..bac390f91 100644 --- a/apps/ui/src/pages/Schedule/useCreateSchedule.ts +++ b/apps/ui/src/pages/Schedule/useCreateSchedule.ts @@ -8,6 +8,7 @@ import { useSchedulesService } from 'services/schedule/useSchedulesService' import { useCreateScheduleService } from 'services/schedule/useCreateScheduleService' import { scheduleValidationSchema } from 'utils/validationsSchema' import { useModal } from 'hooks' +import { getDateTimeFromDate } from './Schedule.utils' type UseCreateScheduleProps = { initialValues: Record @@ -40,7 +41,7 @@ export const useCreateSchedule = ({ initialValues }: UseCreateScheduleProps) => tasks: ['Enter you task'], is_recurring: false, create_session_on_run: false, - start_date: new Date().toISOString().split('T')[0], + start_date: getDateTimeFromDate(new Date()), end_date: null, interval: '', interval_unit: '', @@ -59,8 +60,8 @@ export const useCreateSchedule = ({ initialValues }: UseCreateScheduleProps) => name: values.name, description: values.description, schedule_type: values.schedule_type, - start_date: values.start_date, - end_date: values.end_date, + start_date: new Date(values.start_date).toISOString(), + end_date: values.end_date ? new Date(values.end_date).toISOString() : null, interval: values.is_recurring ? `${values.interval} ${values.interval_unit}` : undefined, is_active: values.is_active, cron_expression: values.cron_expression, diff --git a/apps/ui/src/pages/Schedule/useEditSchedule.ts b/apps/ui/src/pages/Schedule/useEditSchedule.ts index 9c00ccb69..cadf26e32 100644 --- a/apps/ui/src/pages/Schedule/useEditSchedule.ts +++ b/apps/ui/src/pages/Schedule/useEditSchedule.ts @@ -8,6 +8,7 @@ import { useScheduleByIdService } from 'services/schedule/useScheduleByIdService import { useSchedulesService } from 'services/schedule/useSchedulesService' import { useUpdateScheduleService } from 'services/schedule/useUpdateScheduleService' import { scheduleValidationSchema } from 'utils/validationsSchema' +import { getDateTime } from './Schedule.utils' export const useEditSchedule = () => { const { setToast } = useContext(ToastContext) @@ -36,8 +37,6 @@ export const useEditSchedule = () => { if (configs.chat_id) return 'chat' } - console.log(schedule?.start_date) - const defaultValues = { name: schedule?.name, description: schedule?.description, @@ -52,8 +51,8 @@ export const useEditSchedule = () => { tasks: configs?.tasks, is_recurring: configs?.is_recurring, create_session_on_run: configs?.create_session_on_run, - start_date: schedule?.start_date?.split('T')[0], - end_date: schedule?.end_date, + start_date: getDateTime(schedule?.start_date), + end_date: getDateTime(schedule?.end_date), interval: schedule?.interval?.split(' ')[0], interval_unit: schedule?.interval?.split(' ')[1], } @@ -71,8 +70,8 @@ export const useEditSchedule = () => { name: values.name, description: values.description, schedule_type: values.schedule_type, - start_date: values.start_date, - end_date: values.end_date, + start_date: new Date(values.start_date).toISOString(), + end_date: values.end_date ? new Date(values.end_date).toISOString() : null, interval: values.is_recurring ? `${values.interval} ${values.interval_unit}` : undefined, is_active: values.is_active, cron_expression: values.cron_expression, diff --git a/apps/ui/src/services/schedule/useCreateScheduleService.ts b/apps/ui/src/services/schedule/useCreateScheduleService.ts index 3270b267b..209389f28 100644 --- a/apps/ui/src/services/schedule/useCreateScheduleService.ts +++ b/apps/ui/src/services/schedule/useCreateScheduleService.ts @@ -1,6 +1,7 @@ import { useMutation } from '@apollo/client' import CREATE_SCHEDULE_GQL from '../../gql/schedule/createSchedule.gql' +import { Nullable } from 'types' export interface ScheduleInput { schedule: { @@ -11,7 +12,7 @@ export interface ScheduleInput { cron_expression?: string schedule_type?: string start_date: string - end_date: string + end_date: Nullable interval?: string } configs: {