Files
prosappweb/lib/providers/calendar_services_provider.dart
T
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

78 lines
2.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:prosapp_web_app/models/pro_state.dart';
import 'package:prosapp_web_app/models/service.dart';
import 'package:prosapp_web_app/models/servicio_profesional.dart';
import 'package:prosapp_web_app/models/usuario.dart';
import 'package:prosapp_web_app/services/api_service.dart';
class CalendarServicesProvider extends ChangeNotifier {
List<ServicioProfesional> services = [];
ServicioProfesional? service;
bool isLoading = true;
final _api = ApiService.instance;
void logout() {
services = [];
service = null;
isLoading = false;
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 {
try {
isLoading = true;
final res = await _api.get('/services/professional/calendar');
final data = (res is Map ? res['data'] : res) as List;
// Fetch users for calendar entries (calendar endpoint returns flat list without embedded users)
final servicios = data.map((e) {
final m = e as Map<String, dynamic>;
return Service.fromJson(m, m['id'] as String);
}).toList();
final ids = servicios.map((s) => s.userId).toSet().toList();
final users = await Future.wait(ids.map((id) async {
final u = await _api.get('/users/$id');
return Usuario.fromDocument(u as Map<String, dynamic>);
}));
final usersMap = {for (var u in users) u.id: u};
services = servicios
.map((s) => ServicioProfesional(
user: usersMap[s.userId] ?? Usuario(id: s.userId, email: null, phone: null, name: '?',
nickname: null, city: null, picture: null, birthday: null, gender: null,
proState: ProState.inactive, token: null),
service: s))
.toList()
..sort((a, b) {
final dateA = DateTime.parse(a.service.day);
final dateB = DateTime.parse(b.service.day);
if (dateA != dateB) return dateA.compareTo(dateB);
final tA = a.service.range1Hour1.hour * 60 + a.service.range1Hour1.minute;
final tB = b.service.range1Hour1.hour * 60 + b.service.range1Hour1.minute;
return tA.compareTo(tB);
});
} catch (e) {
print('Error obteniendo servicios: $e');
} finally {
isLoading = false;
notifyListeners();
}
}
}