Files
2026-08-11 13:16:33 +05:30

32 lines
1.2 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:miler/Models/summary/riderweeklykms.dart';
import 'package:miler/data/miler_api.dart';
import 'package:miler/views/helpers/constants/apiconstants.dart';
class RiderWeeklyKmController {
final String baseUrl = ApiConstants.summaryriderkmLive;
/// `GET /miler/earnings?period=weekly` → `data:{ total_kms, breakdown }`.
///
/// [userId] is unused: the token identifies the rider, and asking for someone
/// else's kilometres is not a thing the endpoint offers. Kept on the
/// signature because the provider above passes it and the call sites read
/// better for naming whose distance they mean.
Future<Map<String, dynamic>> getRiderWeeklyKms(int userId) async {
final res = await MilerApi.earnings(period: 'weekly');
if (!res.ok) {
throw Exception('Failed to fetch (code: ${res.status})');
}
final breakdown = res.map['breakdown'];
final details = (breakdown is List ? breakdown : const [])
.whereType<Map>()
.map((e) => RiderWeeklyKms.fromJson(Map<String, dynamic>.from(e)))
.toList();
return {
'details': details,
'total_kms': (res.map['total_kms'] ?? 0).toDouble(),
};
}
}