Skip to content

Commit

Permalink
fix: schedule date timezones
Browse files Browse the repository at this point in the history
  • Loading branch information
okradze committed Nov 30, 2023
1 parent 904a073 commit 59edfeb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
6 changes: 3 additions & 3 deletions apps/server/models/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions apps/ui/src/pages/Schedule/Schedule.utils.ts
Original file line number Diff line number Diff line change
@@ -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 }
7 changes: 4 additions & 3 deletions apps/ui/src/pages/Schedule/useCreateSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>
Expand Down Expand Up @@ -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: '',
Expand All @@ -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,
Expand Down
11 changes: 5 additions & 6 deletions apps/ui/src/pages/Schedule/useEditSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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],
}
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion apps/ui/src/services/schedule/useCreateScheduleService.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -11,7 +12,7 @@ export interface ScheduleInput {
cron_expression?: string
schedule_type?: string
start_date: string
end_date: string
end_date: Nullable<string>
interval?: string
}
configs: {
Expand Down

0 comments on commit 59edfeb

Please sign in to comment.