Files
prosappweb/lib/models/schedules_entity.dart
Lizandro GuarnizoandClaude Sonnet 4.6 498481cab0 fix(scheduling): fix schedule save endpoint, time format, and calendar load
Critical fixes for the scheduling flow:

- ScheduleEntity: add toScheduleDto() with snake_case keys, day_of_week,
  and HH:MM zero-padded format required by the backend DTO @Matches validator
- Schedules: add toSchedulesArray() converting the day-keyed object to the
  array format expected by PATCH /professionals/me/schedules
- ProfessionalFormProvider: updateProfesionalProfileScheduleInfo now calls
  the correct endpoint (/professionals/me/schedules) instead of /professionals/me
  which was stripping the schedules field via ValidationPipe whitelist
- Service.formatTimeOfDay: zero-pad hours and minutes so POST /services passes
  the @Matches(/^\d{2}:\d{2}$/) DTO validation
- ProfessionalProvider: add getProfessionalById() calling /professionals/:id
  (public endpoint) to load any professional's data, not the viewer's own
- CalendarServicesProvider: add getPublicServicesForProfessional() calling
  /services/public-calendar/:id so the user calendar shows the target
  professional's booked slots, not the viewer's own services
- CalendarView: use getProfessionalById + getPublicServicesForProfessional
  so the calendar correctly reflects the selected professional's schedule

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-12 17:52:02 -05:00

128 lines
3.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class ScheduleEntity {
final bool enabled;
final bool continuousDay;
final TimeOfDay? range1Hour1;
final TimeOfDay? range1Hour2;
final TimeOfDay? range2Hour1;
final TimeOfDay? range2Hour2;
const ScheduleEntity({
required this.enabled,
required this.continuousDay,
required this.range1Hour1,
required this.range1Hour2,
required this.range2Hour1,
required this.range2Hour2,
});
static const empty = ScheduleEntity(
enabled: false,
continuousDay: false,
range1Hour1: null,
range1Hour2: null,
range2Hour1: null,
range2Hour2: null,
);
ScheduleEntity copyWith({
bool? enabled,
bool? continuousDay,
TimeOfDay? range1Hour1,
TimeOfDay? range1Hour2,
TimeOfDay? range2Hour1,
TimeOfDay? range2Hour2,
}) {
return ScheduleEntity(
enabled: enabled ?? this.enabled,
continuousDay: continuousDay ?? this.continuousDay,
range1Hour1: range1Hour1 ?? this.range1Hour1,
range1Hour2: range1Hour2 ?? this.range1Hour2,
range2Hour1: range2Hour1 ?? this.range2Hour1,
range2Hour2: range2Hour2 ?? this.range2Hour2,
);
}
static ScheduleEntity fromDocument(Map<String, dynamic> doc) {
return ScheduleEntity(
enabled: doc['habilitado'] as bool,
continuousDay: doc['continuous_day'] as bool,
range1Hour1: parseTime(doc['range1Hour1']),
range1Hour2: parseTime(doc['range1Hour2']),
range2Hour1: parseTime(doc['range2Hour1']),
range2Hour2: parseTime(doc['range2Hour2']),
);
}
static TimeOfDay? parseTime(String? time) {
try {
if (time == null) return null;
// Handle ISO8601 from backend (e.g. "1970-01-01T08:30:00.000Z")
final t = time.contains('T') ? time.split('T')[1] : time;
final components = t.split(':');
if (components.length < 2) return null;
return TimeOfDay(hour: int.parse(components[0]), minute: int.parse(components[1]));
} catch (e) {
return null;
}
}
Map<String, dynamic> toJson() {
return {
'habilitado': enabled,
'continuous_day': continuousDay,
'range1Hour1': _formatTimePadded(range1Hour1),
'range1Hour2': _formatTimePadded(range1Hour2),
'range2Hour1': _formatTimePadded(range2Hour1),
'range2Hour2': _formatTimePadded(range2Hour2),
};
}
// Produces { day_of_week, enabled, continuous_day, range*_hour* } for PATCH /professionals/me/schedules
Map<String, dynamic> toScheduleDto(int dayOfWeek) {
return {
'day_of_week': dayOfWeek,
'enabled': enabled,
'continuous_day': continuousDay,
'range1_hour1': _formatTimePadded(range1Hour1),
'range1_hour2': _formatTimePadded(range1Hour2),
'range2_hour1': _formatTimePadded(range2Hour1),
'range2_hour2': _formatTimePadded(range2Hour2),
};
}
static String? _formatTimePadded(TimeOfDay? time) {
if (time == null) return null;
final h = time.hour.toString().padLeft(2, '0');
final m = time.minute.toString().padLeft(2, '0');
return '$h:$m';
}
String? formatTimeOfDay(TimeOfDay? time) => _formatTimePadded(time);
static String? getFormatTime(TimeOfDay? time) {
if (time == null) {
return null;
}
final now = DateTime.now();
final dateTime =
DateTime(now.year, now.month, now.day, time.hour, time.minute);
final format = DateFormat.jm();
return format.format(dateTime);
}
@override
String toString() {
return '''ScheduleEntity {
enabled: $enabled,
continuousDay: $continuousDay,
range1Hour1: $range1Hour1,
range1Hour2: $range1Hour2,
range2Hour1: $range2Hour1,
range2Hour2: $range2Hour2
}''';
}
}