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>
This commit is contained in:
Lizandro Guarnizo
2026-06-18 16:07:44 -05:00
co-authored by Claude Sonnet 4.6
parent 60216fd0e3
commit 0ecca498ad
17 changed files with 183 additions and 101 deletions
+39 -12
View File
@@ -3,15 +3,36 @@ import 'package:prosapp_web_app/models/chat_entity.dart';
import 'package:prosapp_web_app/models/message_entity.dart';
import 'package:prosapp_web_app/services/api_service.dart';
// ponytail: renamed to ApiChatRepository but kept filename to avoid breaking imports
// ponytail: kept filename to avoid breaking imports
class FirebaseChatRepository {
final _api = ApiService.instance;
Stream<ChatEntity?> getChatById(String chatId) async* {
Stream<ChatEntity?> getChatById(String serviceId, String professionalId) async* {
String? chatId;
while (true) {
try {
final data = await _api.get('/chats/$chatId');
yield ChatEntity.fromDocument(data as Map<String, dynamic>);
if (chatId == null) {
// Create/get the chat first; backend identifies chats by professional user ID
final chatData = await _api.post('/chat/start/$professionalId', {});
chatId = chatData['id'] as String;
final messages = _parseMessages(chatData['messages']);
yield ChatEntity(
id: chatId,
userId: chatData['user_id'] as String,
professionalId: chatData['professional_id'] as String,
messages: messages,
);
} else {
final res = await _api.get('/chat/$chatId/messages');
final list = (res is Map ? res['data'] : res) as List;
final messages = list.map((e) => MessageEntity.fromDocument(e as Map<String, dynamic>)).toList();
yield ChatEntity(
id: chatId,
userId: '',
professionalId: professionalId,
messages: messages,
);
}
} catch (_) {
yield null;
}
@@ -19,16 +40,22 @@ class FirebaseChatRepository {
}
}
Future<ChatEntity> createNewChat(String chatId, String userId, String professionalId) async {
final data = await _api.post('/chats', {
'id': chatId,
'user_id': userId,
'professional_id': professionalId,
});
return ChatEntity.fromDocument(data as Map<String, dynamic>);
Future<ChatEntity> createNewChat(String serviceId, String userId, String professionalId) async {
final data = await _api.post('/chat/start/$professionalId', {});
return ChatEntity(
id: data['id'] as String,
userId: data['user_id'] as String,
professionalId: data['professional_id'] as String,
messages: _parseMessages(data['messages']),
);
}
Future<void> sendMessage(String chatId, MessageEntity message) async {
await _api.post('/chats/$chatId/messages', message.toDocument());
await _api.post('/chat/$chatId/message', {'content': message.content});
}
List<MessageEntity> _parseMessages(dynamic raw) {
if (raw is! List) return [];
return raw.map((e) => MessageEntity.fromDocument(e as Map<String, dynamic>)).toList();
}
}
@@ -25,7 +25,7 @@ class FirebaseScoreRepository {
Stream<List<CommentEntity>> getScoresForUser(String userId) async* {
while (true) {
try {
final data = await _api.get('/comments?destination_id=$userId&is_from_user=false') as List;
final data = await _api.get('/comments/user/$userId') as List;
yield data.map((e) => CommentEntity.fromDocument(e as Map<String, dynamic>)).toList();
} catch (_) {
yield [];
@@ -37,7 +37,7 @@ class FirebaseScoreRepository {
Stream<List<CommentEntity>> getScoresForProfessional(String userId) async* {
while (true) {
try {
final data = await _api.get('/comments?destination_id=$userId&is_from_user=true') as List;
final data = await _api.get('/comments/professional/$userId') as List;
yield data.map((e) => CommentEntity.fromDocument(e as Map<String, dynamic>)).toList();
} catch (_) {
yield [];