Files
prosappweb/lib/repositories/firebase_chat_repository.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

62 lines
2.2 KiB
Dart

import 'dart:async';
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: kept filename to avoid breaking imports
class FirebaseChatRepository {
final _api = ApiService.instance;
Stream<ChatEntity?> getChatById(String serviceId, String professionalId) async* {
String? chatId;
while (true) {
try {
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;
}
await Future.delayed(const Duration(seconds: 3));
}
}
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('/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();
}
}