Replace all Firebase SDK (Auth, Firestore, Storage) with HTTP calls to backend.prosapp.co/api/v1: - New ApiService singleton (JWT token, GET/POST/PATCH/DELETE/upload) - auth_provider: Firebase Auth → /auth/login, /auth/register, /auth/phone/* - services_provider + calendar_services_provider → /services endpoints - professional_provider + professionals_provider → /professional-info, /users/professionals - profile_form_provider + professional_form_provider → /users/me, /storage/upload - cities/professions/settings providers → /cities, /professions, /settings - firebase_chat_repository → polling via /chats endpoints (3s interval) - firebase_score_repository → polling via /comments endpoints (10s interval) - professional_detail_provider → /users/:id + /professional-info/:id - dashboard_view: Firestore.add → POST /services - chat_view + rating_view: FirebaseAuth.uid → AuthProvider.user.id - Models: Timestamp → String for createdAt fields - google_fonts upgraded to ^8.1.0 (Dart 3.12 compat) - Remove firebase_core, firebase_auth, cloud_firestore, firebase_storage from pubspec Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
133 lines
4.6 KiB
Dart
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': enumToIntService(newStatus)});
|
|
notifyListeners();
|
|
} catch (e) {
|
|
print('Error al actualizar el estado: $e');
|
|
}
|
|
}
|
|
|
|
Future<void> changeUserScored(String serviceId) async {
|
|
try {
|
|
await _api.patch('/services/$serviceId', {'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', {'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?user_id=$userId&status=0,1,3', forUser: true);
|
|
|
|
getServicesRequestsForProfessional(String userId) async => _loadServices('/services?professional_id=$userId&status=0', forUser: false);
|
|
|
|
getServicesForProfessional(String userId) async => _loadServices('/services?professional_id=$userId&status=1,3', forUser: false);
|
|
|
|
getServicesHistoryForUser(String userId) async => _loadServices('/services?user_id=$userId&status=2,4,5', forUser: true);
|
|
|
|
getServicesHistoryForProfessional(String userId) async => _loadServices('/services?professional_id=$userId&status=2,4,5', 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();
|
|
}
|
|
}
|
|
}
|