Files
prosappweb/lib/providers/services_provider.dart
T
Lizandro GuarnizoandClaude Sonnet 4.6 0ecca498ad fix: align Flutter endpoints and model parsing with actual NestJS backend
- Auth: phone login → POST /auth/phone (direct, no OTP); link phone → POST /auth/verify-phone; add email → POST /auth/link-email
- Services: replace query-param paths with dedicated role endpoints (/services/me, /services/professional/requests, /services/professional, etc.)
- Services: PATCH /services/:id → PATCH /services/:id/status
- Calendar: → GET /services/professional/calendar
- Professionals: /users/professionals → /professionals (handles {data:[...]} response)
- Professional info: /professional-info/:id → /professionals/:id; PATCH → /professionals/me
- Comments: /comments?... → /comments/user/:id and /comments/professional/:id
- Chat: /chats → /chat; start chat → POST /chat/start/:professionalId; poll GET /chat/:chatId/messages; send → POST /chat/:chatId/message
- Cities: /cities → GET /locations/countries (parse nested countries→regions→cities)
- Profesional.fromDocument: null-safe fields; convert schedules array→Schedules, specializations array→name/picture lists, payment_methods object/array
- Usuario.fromDocument: null-safe professional_state and id
- UsuarioProfesional.fromDocument: handle backend format (users nested, professional at top level)
- ScheduleEntity.parseTime: handle ISO8601 time strings from backend
- MessageEntity.fromDocument: accept sender_id (backend) or owner_id (legacy)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-18 16:07:44 -05:00

133 lines
4.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:prosapp_web_app/models/service.dart';
import 'package:prosapp_web_app/models/service_status.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 ServicesProvider extends ChangeNotifier {
List<ServicioProfesional> services = [];
ServicioProfesional? service;
bool isLoading = true;
final _api = ApiService.instance;
void logout() {
services = [];
service = null;
isLoading = false;
notifyListeners();
}
Future<void> changeServiceStatus(String serviceId, ServiceStatus newStatus) async {
try {
await _api.patch('/services/$serviceId/status', {'status': enumToIntService(newStatus)});
notifyListeners();
} catch (e) {
print('Error al actualizar el estado: $e');
}
}
Future<void> changeUserScored(String serviceId) async {
try {
await _api.patch('/services/$serviceId/status', {'user_scored': true});
notifyListeners();
} catch (e) {
print('Error al actualizar user_scored: $e');
}
}
Future<void> changeProfessionalScored(String serviceId) async {
try {
await _api.patch('/services/$serviceId/status', {'professional_scored': true});
notifyListeners();
} catch (e) {
print('Error al actualizar professional_scored: $e');
}
}
void clearServices() {
services.clear();
notifyListeners();
}
getServiceForUser(String serviceId) async {
try {
isLoading = true;
final data = await _api.get('/services/$serviceId');
final map = data as Map<String, dynamic>;
final servicio = Service.fromJson(map, map['id'] as String);
final userData = await _api.get('/users/${servicio.professionalId}');
final user = Usuario.fromDocument(userData as Map<String, dynamic>);
service = ServicioProfesional(user: user, service: servicio);
} catch (e) {
service = null;
} finally {
isLoading = false;
notifyListeners();
}
}
getServiceForProfessional(String serviceId) async {
try {
isLoading = true;
final data = await _api.get('/services/$serviceId');
final map = data as Map<String, dynamic>;
final servicio = Service.fromJson(map, map['id'] as String);
final userData = await _api.get('/users/${servicio.userId}');
final user = Usuario.fromDocument(userData as Map<String, dynamic>);
service = ServicioProfesional(user: user, service: servicio);
} catch (e) {
service = null;
} finally {
isLoading = false;
notifyListeners();
}
}
getServicesForUser(String userId) async => _loadServices('/services/me', forUser: true);
getServicesRequestsForProfessional(String userId) async => _loadServices('/services/professional/requests', forUser: false);
getServicesForProfessional(String userId) async => _loadServices('/services/professional', forUser: false);
getServicesHistoryForUser(String userId) async => _loadServices('/services/me/history', forUser: true);
getServicesHistoryForProfessional(String userId) async => _loadServices('/services/professional/history', forUser: false);
Future<void> _loadServices(String path, {required bool forUser}) async {
try {
isLoading = true;
final data = await _api.get(path) as List;
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) => forUser ? s.professionalId : 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) {
final userId = forUser ? s.professionalId : s.userId;
return ServicioProfesional(user: usersMap[userId]!, 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();
}
}
}