Files
doormile_milderapp/lib/providers/pickup/pickup_provider.dart
2026-08-11 13:16:33 +05:30

60 lines
2.6 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:miler/data/api_config.dart';
import 'package:miler/data/miler_api.dart';
/// Reads the rider's bookings.
///
/// ── What this used to be ──
///
/// Three near-identical methods — `getPickupQueues`, `getCurrentPickups`,
/// `getPickupQueuesPicked` — each with its own legacy URL, its own
/// `fromdate`/`todate`/`userid` query and its own hand-rolled envelope
/// unwrapping, and each opening with `if (ApiConfig.useNewApi) return
/// _getBookingsNew(...)`. All three legacy paths hit different endpoints on a
/// backend that is no longer deployed, and all three new paths were the same
/// call. What is left is that one call.
///
/// The three names survive because three call sites read differently — Home
/// wants the queue, Bookings wants the accepted work — even though the request
/// is identical and the filtering happens client-side. Collapsing them into one
/// method is a separate change to the screens, not to this file.
class PickupProvider {
/// `GET /miler/bookings` → the list, mapped into the legacy stop shape the
/// cards read.
///
/// The mapping lives in [ApiConfig.pickupFromBooking] rather than here: it is
/// the one place that knows how a v1 booking becomes a stop, and the same
/// translation is needed by anything else that receives a booking.
Future<List<dynamic>> _bookings({String? status}) async {
final res = await MilerApi.bookings(status: status);
if (!res.ok) {
throw Exception('Failed (${res.status})${res.message.isEmpty ? '' : ': ${res.message}'}');
}
final mapped = ApiConfig.pickupsFromBookings(res.list);
debugPrint('[BOOKINGS] ${res.list.length} rows → ${mapped.length} stops');
if (mapped.isEmpty && res.list.isNotEmpty) {
// The rows arrived and none of them survived translation, which is a
// field-name mismatch rather than an empty day. Worth saying out loud —
// it is the difference between "no work" and "the adapter is wrong".
ApiConfig.logGap(
'pickupFromBooking',
'${res.list.length} bookings returned but none mapped — check the '
'field names against a real payload.',
);
}
return mapped;
}
/// Everything the hub has put in front of this rider today.
Future<List<dynamic>> getPickupQueues({String? orderstatus}) =>
_bookings(status: orderstatus);
/// The stops he is working now.
Future<List<dynamic>> getCurrentPickups() => _bookings();
/// The stops he has accepted.
Future<List<dynamic>> getPickupQueuesPicked() => _bookings();
}