feat: add validation check for break outside availability

This commit is contained in:
Freek van de Ven 2024-07-26 14:12:07 +02:00 committed by FlutterJoey
parent c004417b36
commit c7ce06624b
4 changed files with 20 additions and 1 deletions

View file

@ -13,6 +13,11 @@ enum AvailabilityError {
"and end time",
),
/// Error identifier for when a break is outside the availability time
breakOutsideAvailabilityTime(
"The break is outside the availability time",
),
/// Error identifier for when the end of the day is before the start
endBeforeStart("The end of the day is before the start"),

View file

@ -99,6 +99,8 @@ class _AvailabilitiesModificationScreenState
error = AvailabilityError.breakEndBeforeStart;
} on BreakSubmittedDurationTooLongException {
error = AvailabilityError.breakSubmittedDurationTooLong;
} on BreakOutsideAvailabilityTimeException {
error = AvailabilityError.breakOutsideAvailabilityTime;
}
if (error != null && context.mounted) {

View file

@ -8,6 +8,9 @@ class BreakEndBeforeStartException implements Exception {}
/// between the start and end time
class BreakSubmittedDurationTooLongException implements Exception {}
/// Exception thrown when a break is outside the availability time
class BreakOutsideAvailabilityTimeException implements Exception {}
/// Exception thrown when the end is before the start
class AvailabilityEndBeforeStartException implements Exception {}
@ -100,6 +103,12 @@ class AvailabilityModel {
for (var breakData in breaks) {
breakData.validate();
var breakStart = breakData.startTime.time;
var breakEnd = breakData.endTime.time;
if (breakStart.isBefore(startDate.time) ||
breakEnd.isAfter(endDate.time)) {
throw BreakOutsideAvailabilityTimeException();
}
}
}
}
@ -201,7 +210,7 @@ class AvailabilityBreakModel {
/// Verify the validity of this break
void validate() {
if (endTime.compareTo(startTime) < 0) {
if (!startTime.isBefore(endTime)) {
throw BreakEndBeforeStartException();
}

View file

@ -3,6 +3,9 @@ extension DateUtils on DateTime {
/// Gets the date without the time
DateTime get date => DateTime(year, month, day);
/// Gets the time without the date
DateTime get time => DateTime(0, 0, 0, hour, minute);
/// Returns true if the time of the date matches the time of the other date
bool timeMatches(DateTime other) =>
other.hour == hour && other.minute == minute;