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>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
360f88777b
commit
498481cab0
@@ -73,4 +73,18 @@ class Schedules {
|
|||||||
'sunday': sunday.toJson(),
|
'sunday': sunday.toJson(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Produces the array format expected by PATCH /professionals/me/schedules
|
||||||
|
// day_of_week: 0=Monday … 6=Sunday (matches backend DB convention)
|
||||||
|
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),
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,20 +73,35 @@ class ScheduleEntity {
|
|||||||
return {
|
return {
|
||||||
'habilitado': enabled,
|
'habilitado': enabled,
|
||||||
'continuous_day': continuousDay,
|
'continuous_day': continuousDay,
|
||||||
'range1Hour1': formatTimeOfDay(range1Hour1),
|
'range1Hour1': _formatTimePadded(range1Hour1),
|
||||||
'range1Hour2': formatTimeOfDay(range1Hour2),
|
'range1Hour2': _formatTimePadded(range1Hour2),
|
||||||
'range2Hour1': formatTimeOfDay(range2Hour1),
|
'range2Hour1': _formatTimePadded(range2Hour1),
|
||||||
'range2Hour2': formatTimeOfDay(range2Hour2),
|
'range2Hour2': _formatTimePadded(range2Hour2),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
String? formatTimeOfDay(TimeOfDay? time) {
|
// Produces { day_of_week, enabled, continuous_day, range*_hour* } for PATCH /professionals/me/schedules
|
||||||
if (time != null) {
|
Map<String, dynamic> toScheduleDto(int dayOfWeek) {
|
||||||
return "${time.hour.toString()}:${time.minute.toString()}";
|
return {
|
||||||
}
|
'day_of_week': dayOfWeek,
|
||||||
return null;
|
'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) {
|
static String? getFormatTime(TimeOfDay? time) {
|
||||||
if (time == null) {
|
if (time == null) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -113,8 +113,10 @@ class Service {
|
|||||||
static TimeOfDay parseTimeOfDay(String timeString) => _parseTime(timeString);
|
static TimeOfDay parseTimeOfDay(String timeString) => _parseTime(timeString);
|
||||||
|
|
||||||
String? formatTimeOfDay(TimeOfDay? time) {
|
String? formatTimeOfDay(TimeOfDay? time) {
|
||||||
if (time != null) return '${time.hour}:${time.minute}';
|
if (time == null) return null;
|
||||||
return null;
|
final h = time.hour.toString().padLeft(2, '0');
|
||||||
|
final m = time.minute.toString().padLeft(2, '0');
|
||||||
|
return '$h:$m';
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -18,6 +18,21 @@ class CalendarServicesProvider extends ChangeNotifier {
|
|||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns services for a specific professional via the public endpoint (no auth needed).
|
||||||
|
// Used by CalendarView when a user browses a professional's availability.
|
||||||
|
Future<List<Service>> getPublicServicesForProfessional(String professionalId) async {
|
||||||
|
try {
|
||||||
|
final res = await _api.get('/services/public-calendar/$professionalId');
|
||||||
|
final rawServices = (res as Map<String, dynamic>)['services'] as List? ?? [];
|
||||||
|
return rawServices.map((e) {
|
||||||
|
final m = e as Map<String, dynamic>;
|
||||||
|
return Service.fromJson(m, m['id'] as String);
|
||||||
|
}).toList();
|
||||||
|
} catch (_) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
getServicesForProfessional(String userId) async {
|
getServicesForProfessional(String userId) async {
|
||||||
try {
|
try {
|
||||||
isLoading = true;
|
isLoading = true;
|
||||||
|
|||||||
@@ -106,12 +106,14 @@ class ProfessionalFormProvider with ChangeNotifier {
|
|||||||
|
|
||||||
Future<bool> updateProfesionalProfileScheduleInfo(String userId) async {
|
Future<bool> updateProfesionalProfileScheduleInfo(String userId) async {
|
||||||
try {
|
try {
|
||||||
await _api.patch('/professionals/me', profesional!.toDocument());
|
await _api.patch('/professionals/me/schedules', {
|
||||||
|
'schedules': profesional!.schedules.toSchedulesArray(),
|
||||||
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
NotificationsService.showSnackbar('Error al guardar: $e');
|
NotificationsService.showSnackbar('Error al guardar: $e');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
NotificationsService.showSnackbar('Información actualizada');
|
NotificationsService.showSnackbar('Horario actualizado');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,6 +44,19 @@ class ProfessionalProvider extends ChangeNotifier {
|
|||||||
return profesional!;
|
return profesional!;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fetches any professional by UUID or user_id via the public endpoint.
|
||||||
|
// Used by CalendarView to load the target professional (not the logged-in user).
|
||||||
|
Future<Profesional> getProfessionalById(String id) async {
|
||||||
|
try {
|
||||||
|
final data = await _api.get('/professionals/$id');
|
||||||
|
final prof = Profesional.fromDocument(data as Map<String, dynamic>);
|
||||||
|
notifyListeners();
|
||||||
|
return prof;
|
||||||
|
} catch (_) {
|
||||||
|
return Profesional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void logout() {
|
void logout() {
|
||||||
_isProModeActive = false;
|
_isProModeActive = false;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
|
|||||||
@@ -45,12 +45,14 @@ class _CalendarViewState extends State<CalendarView> {
|
|||||||
final proProvider =
|
final proProvider =
|
||||||
Provider.of<ProfessionalProvider>(context, listen: false);
|
Provider.of<ProfessionalProvider>(context, listen: false);
|
||||||
|
|
||||||
|
// Use public endpoint so we get THIS professional's schedule, not the viewer's own
|
||||||
final professional =
|
final professional =
|
||||||
await proProvider.getProfessional(widget.professionalId);
|
await proProvider.getProfessionalById(widget.professionalId);
|
||||||
professionalFormProvider.setProfesional(professional);
|
professionalFormProvider.setProfesional(professional);
|
||||||
|
|
||||||
|
// Use public calendar endpoint to get services for this specific professional
|
||||||
final services =
|
final services =
|
||||||
await servicesProvider.getServicesForProfessional(professional.id);
|
await servicesProvider.getPublicServicesForProfessional(professional.id);
|
||||||
setState(() {
|
setState(() {
|
||||||
_services = services;
|
_services = services;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user