Skip to content

Commit

Permalink
Fix incorrect endtime in schedule validation message (Fixes #778) (#808)
Browse files Browse the repository at this point in the history
* Return the corrected schedule time as UTC

* Update the start_date with todays date on schedule save.

* Fix tests to use timezones
  • Loading branch information
MelissaAutumn authored Dec 20, 2024
1 parent 88eec97 commit 4cb0c7d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions backend/src/appointment/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ class Schedule(Base):
details: str = Column(encrypted_type(String))
start_date: datetime.date = Column(encrypted_type(Date), index=True)
end_date: datetime.date = Column(encrypted_type(Date), index=True)
start_time: datetime.time = Column(encrypted_type(Time), index=True)
end_time: datetime.time = Column(encrypted_type(Time), index=True)
start_time: datetime.time = Column(encrypted_type(Time), index=True) # in utc
end_time: datetime.time = Column(encrypted_type(Time), index=True) # in utc
earliest_booking: int = Column(Integer, default=1440) # in minutes, defaults to 24 hours
farthest_booking: int = Column(Integer, default=20160) # in minutes, defaults to 2 weeks
weekdays: str | dict = Column(JSON, default='[1,2,3,4,5]') # list of ISO weekdays, Mo-Su => 1-7
Expand Down
8 changes: 4 additions & 4 deletions backend/src/appointment/database/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,16 @@ def start_time_should_be_before_end_time(self) -> Self:
start_time = datetime.combine(self.start_date, self.start_time, tzinfo=timezone.utc).astimezone(zoneinfo.ZoneInfo(tz))
end_time = datetime.combine(self.start_date, self.end_time, tzinfo=timezone.utc).astimezone(zoneinfo.ZoneInfo(tz))

start_time = (start_time + timedelta(minutes=self.slot_duration)).time()
end_time = end_time.time()
start_time = (start_time + timedelta(minutes=self.slot_duration))
end_time = end_time
# Compare time objects!
if start_time > end_time:
if start_time.time() > end_time.time():
msg = l10n('error-minimum-value')

# These can't be field or value because that will auto-format the msg? Weird feature but okay.
raise PydanticCustomError(defines.END_TIME_BEFORE_START_TIME_ERR, msg, {
'err_field': 'end_time',
'err_value': start_time
'err_value': start_time.astimezone(timezone.utc).time()
})

return self
Expand Down
9 changes: 6 additions & 3 deletions backend/test/integration/test_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def schedule_input(self):
'farthest_booking': 20160,
'weekdays': [1, 2, 3, 4, 5],
'slot_duration': 30,
'timezone': 'America/Vancouver'
}

def test_create_schedule_on_connected_calendar(self, with_client, make_caldav_calendar, schedule_input):
Expand Down Expand Up @@ -68,8 +69,10 @@ def test_create_schedule_with_end_time_before_start_time(
generated_calendar = make_caldav_calendar(connected=True)

schedule_data = schedule_input
schedule_data['start_time'] = '09:00'
schedule_data['end_time'] = '06:00'
# start_time and end_time are always in UTC
# 9am and 8am PT
schedule_data['start_time'] = '17:00'
schedule_data['end_time'] = '16:00'

response = with_client.post(
'/schedule',
Expand All @@ -86,7 +89,7 @@ def test_create_schedule_with_end_time_before_start_time(
assert data.get('detail')[0]['type'] == defines.END_TIME_BEFORE_START_TIME_ERR
assert data.get('detail')[0]['msg'] == '{field} should be at least {value}.'
assert data.get('detail')[0]['ctx']['err_field'] == 'end_time'
assert data.get('detail')[0]['ctx']['err_value'] == '09:30:00'
assert data.get('detail')[0]['ctx']['err_value'] == '17:30:00'



Expand Down
2 changes: 2 additions & 0 deletions frontend/src/components/ScheduleCreation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ const saveSchedule = async (withConfirmation = true) => {
.tz(user.data.timezone ?? dj.tz.guess(), true)
.utc()
.format('HH:mm');
// Update the start_date with the current date
obj.start_date = dj().format(dateFormat);
// remove unwanted properties
delete obj.availabilities;
delete obj.time_created;
Expand Down

0 comments on commit 4cb0c7d

Please sign in to comment.