fix: repair the booking flow end to end
ci-651288 / run (push) Waiting to run
ci-946620 / run (push) Waiting to run

Verified the real contracts against the backend before changing anything.

Chat (three defects, one root cause):
- every chat endpoint is keyed by the chat id, not the service id. The app
  called POST /chat/start, threw away the id it returned and kept using the
  service id, so every later request 404'd.
- messages arrive as {data, meta}; reading the body as a bare list threw and
  surfaced as an empty conversation.
- the bloc created the chat and then never emitted ChatLoaded (the else hung
  off `if (chat == null)`), leaving a permanent spinner. Sending a message
  emitted nothing at all, so it vanished until reopening.
Messages now render optimistically and roll back if the send fails, and the
screen distinguishes "loading" from "could not open" with a retry.

Appointments:
- a null range1_hour2 parsed as 00:00, so new appointments were born
  "Caducado" and every action was hidden. It now falls back to the start time.
- service requests validate the HTTP status and tolerate an empty body: a 4xx
  was treated as success and a 204 as failure.
- creating a service with no id in the response no longer reports success and
  navigates to a service that does not exist.
- dispatching LoadService from build() looped forever on failure; the three
  detail screens now load once and offer a retry.

Ratings:
- both sides read userScored, so only one of the two could ever rate. The
  client side now reads professionalScored.
- the screen closed before the request finished, killing the provider mid
  flight while addComment swallowed every error. It now waits for confirmation.
- score/reputation parsing tolerates integers and numeric strings instead of
  emptying the review list.

Also: guarded map lookups in ScoreBloc, a nullable name in the search list,
and error states with retry where a failure used to shimmer forever.

Verified against the backend: `accepted` is the status the API expects, so the
suspected spelling bug was a false alarm and was left alone.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-08-24 20:47:54 -05:00
co-authored by Claude Opus 5
parent 8c6e2ae024
commit 389f876cfa
17 changed files with 444 additions and 153 deletions
+44 -14
View File
@@ -37,6 +37,11 @@ class _ScoreScreenState extends State<ScoreScreen> {
late Future<List<dynamic>> _userInfoFuture;
late bool isProfessional;
late String userId;
bool _isSending = false;
/// True when the person rating is the client (they rate the professional).
bool get isUser =>
widget.service.userId == (ApiUserRepository.currentUserId ?? '');
@override
void initState() {
@@ -52,7 +57,38 @@ class _ScoreScreenState extends State<ScoreScreen> {
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => Injector.appInstance.get<ScoreBloc>(),
child: Scaffold(
child: BlocListener<ScoreBloc, ScoreState>(
listener: (context, state) {
if (state is ScoreSending) {
setState(() => _isSending = true);
} else if (state is ScoreSent) {
setState(() => _isSending = false);
// Only now is the rating actually stored, so only now do we mark
// the service as scored and leave the screen.
if (isUser) {
context
.read<ServiceBloc>()
.add(UpdateProfessionalScored(widget.service.id!));
} else {
context
.read<ServiceBloc>()
.add(UpdateUserScored(widget.service.id!));
}
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('¡Gracias por tu calificación!')),
);
Navigator.pop(context);
} else if (state is ScoreSendFailure) {
setState(() => _isSending = false);
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'No se pudo enviar tu calificación. Inténtalo de nuevo.')),
);
}
},
child: Scaffold(
backgroundColor: context.bg,
appBar: AppBar(
title: const Text('Calificar servicio'),
@@ -108,6 +144,7 @@ class _ScoreScreenState extends State<ScoreScreen> {
),
],
),
),
),
);
}
@@ -292,9 +329,9 @@ class _ScoreScreenState extends State<ScoreScreen> {
return SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: () {
final isUser = widget.service.userId ==
(ApiUserRepository.currentUserId ?? '');
onPressed: _isSending
? null
: () {
final comment = CommentEntity(
serviceId: widget.service.id!,
authorId: ApiUserRepository.currentUserId ?? '',
@@ -306,18 +343,11 @@ class _ScoreScreenState extends State<ScoreScreen> {
content: _commentController.text.trim(),
createdAt: DateTime.now().toIso8601String(),
);
// No pop here: closing the screen used to kill the BlocProvider
// while the request was still in flight, so a failed rating looked
// exactly like a saved one. The listener closes it on success.
BlocProvider.of<ScoreBloc>(context)
.add(SendScoreEvent(comment: comment));
if (isUser) {
context
.read<ServiceBloc>()
.add(UpdateProfessionalScored(widget.service.id!));
} else {
context
.read<ServiceBloc>()
.add(UpdateUserScored(widget.service.id!));
}
Navigator.pop(context);
},
icon: const Icon(Icons.send_rounded, size: 18),
label: const Text('Enviar calificación',