import 'package:professional_repository/professional_repository.dart'; import 'package:prosappco/utils/time_of_day_extension.dart'; const _dayNames = [ 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado', 'Domingo', ]; /// Human-readable problems with a week of opening hours, empty when it is fine. /// /// Nothing validated these before: an inverted or half-filled day saved /// happily and then produced zero bookable slots, with no clue as to why. List validateSchedules(Schedules schedules) { final days = [ schedules.monday, schedules.tuesday, schedules.wednesday, schedules.thursday, schedules.friday, schedules.saturday, schedules.sunday, ]; final problems = []; for (var i = 0; i < days.length; i++) { final problem = _validateDay(days[i]); if (problem != null) problems.add('${_dayNames[i]}: $problem'); } return problems; } String? _validateDay(ScheduleEntity day) { if (!day.enabled) return null; if (day.continuousDay) { if (day.range1Hour1 == null || day.range2Hour2 == null) { return 'falta la hora de apertura o de cierre'; } if (!day.range1Hour1!.isBefore(day.range2Hour2!)) { return 'la hora de cierre debe ser posterior a la de apertura'; } return null; } final r = [day.range1Hour1, day.range1Hour2, day.range2Hour1, day.range2Hour2]; if (r.any((t) => t == null)) { return 'faltan horas en la jornada partida'; } if (!r[0]!.isBefore(r[1]!)) { return 'la primera jornada termina antes de empezar'; } if (!r[2]!.isBefore(r[3]!)) { return 'la segunda jornada termina antes de empezar'; } if (r[2]!.isBefore(r[1]!)) { return 'las dos jornadas se cruzan'; } return null; }