Files
prosappco/packages/professional_repository/lib/src/entities/schedules.dart
T
Lizandro GuarnizoandClaude Opus 5 6cb2755858 fix(booking): read the live agenda and re-check the slot before confirming
/services/public-calendar also returns the professional's current opening
hours; the client threw them away and drew the day from the snapshot the
search list carried, which can be days old. It now uses the fresh ones.

Between picking an hour and pressing "Pedir cita" another patient can
take the slot. The confirm step asks the agenda again and, if the hour is
gone, says so and clears the selection instead of creating a service the
professional would have to deny. A failed re-check lets the booking
through: a network hiccup should not block a patient.

Also:
- tapping a busy slot with no id explains itself instead of doing nothing
- the profile refuses to save an empty rate with the rate switch on,
  which was reaching the backend as rate: ''

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-25 13:21:08 -05:00

113 lines
3.5 KiB
Dart

import 'package:equatable/equatable.dart';
import 'package:professional_repository/src/entities/schedule_entity.dart';
class Schedules extends Equatable {
final ScheduleEntity monday;
final ScheduleEntity tuesday;
final ScheduleEntity wednesday;
final ScheduleEntity thursday;
final ScheduleEntity friday;
final ScheduleEntity saturday;
final ScheduleEntity sunday;
const Schedules({
required this.monday,
required this.tuesday,
required this.wednesday,
required this.thursday,
required this.friday,
required this.saturday,
required this.sunday,
});
static const empty = Schedules(
monday: ScheduleEntity.empty,
tuesday: ScheduleEntity.empty,
wednesday: ScheduleEntity.empty,
thursday: ScheduleEntity.empty,
friday: ScheduleEntity.empty,
saturday: ScheduleEntity.empty,
sunday: ScheduleEntity.empty,
);
// Copy function
Schedules copyWith({
ScheduleEntity? monday,
ScheduleEntity? tuesday,
ScheduleEntity? wednesday,
ScheduleEntity? thursday,
ScheduleEntity? friday,
ScheduleEntity? saturday,
ScheduleEntity? sunday,
}) {
return Schedules(
monday: monday ?? this.monday,
tuesday: tuesday ?? this.tuesday,
wednesday: wednesday ?? this.wednesday,
thursday: thursday ?? this.thursday,
friday: friday ?? this.friday,
saturday: saturday ?? this.saturday,
sunday: sunday ?? this.sunday,
);
}
/// Parses the array the backend returns, keyed by `day_of_week`
/// (0 = Monday … 6 = Sunday). Anything else yields [Schedules.empty]
/// rather than throwing, so one malformed row cannot blank a screen.
static Schedules fromApiArray(dynamic raw) {
if (raw is! List || raw.isEmpty) return Schedules.empty;
final byDay = <int, Map<String, dynamic>>{};
for (final s in raw) {
if (s is! Map) continue;
final day = (s['day_of_week'] as num?)?.toInt();
if (day != null) byDay[day] = s.cast<String, dynamic>();
}
ScheduleEntity entityFor(int day) {
final s = byDay[day];
if (s == null) return ScheduleEntity.empty;
return ScheduleEntity(
enabled: s['enabled'] as bool? ?? false,
continuousDay: s['continuous_day'] as bool? ?? false,
range1Hour1: ScheduleEntity.parseTime(s['range1_hour1']?.toString()),
range1Hour2: ScheduleEntity.parseTime(s['range1_hour2']?.toString()),
range2Hour1: ScheduleEntity.parseTime(s['range2_hour1']?.toString()),
range2Hour2: ScheduleEntity.parseTime(s['range2_hour2']?.toString()),
);
}
return Schedules(
monday: entityFor(0),
tuesday: entityFor(1),
wednesday: entityFor(2),
thursday: entityFor(3),
friday: entityFor(4),
saturday: entityFor(5),
sunday: entityFor(6),
);
}
/// The array PATCH /professionals/me/schedules expects, ordered
/// 0 = Monday … 6 = Sunday.
///
/// The map form below is NOT accepted by the backend: sending it to
/// /professionals/me returns 200 and silently discards the schedules, which
/// is why editing opening hours appeared to work and never persisted.
List<Map<String, dynamic>> toSchedulesArray() {
return [
monday.toScheduleDto(0),
tuesday.toScheduleDto(1),
wednesday.toScheduleDto(2),
thursday.toScheduleDto(3),
friday.toScheduleDto(4),
saturday.toScheduleDto(5),
sunday.toScheduleDto(6),
];
}
@override
List<Object?> get props =>
[monday, tuesday, wednesday, thursday, friday, saturday, sunday];
}