Food delivery driver module + masked calls + TURN
This commit is contained in:
@@ -22,30 +22,66 @@ class FoodDeliveryService {
|
||||
return FoodDeliveryApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
/// حالة السائق عند فتح التبويب — وضع التوصيل مخزَّن في Redis على الخادم،
|
||||
/// فلا يجوز افتراض "مغلق" محلياً وإلا ظهر التبديل مطفأً والعروض تصل فعلاً.
|
||||
static Future<FoodDeliveryApiResult<FoodCourierStatus>> getStatus() async {
|
||||
final res = await CRUD().post(link: '$_base/courier/status.php');
|
||||
final msg = _payload(res);
|
||||
if (msg != null) {
|
||||
return FoodDeliveryApiResult(true, FoodCourierStatus.fromJson(msg), 'ok');
|
||||
}
|
||||
return FoodDeliveryApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
static Future<FoodDeliveryApiResult<List<FoodDeliveryTask>>> getActiveTasks() async {
|
||||
final res = await CRUD().post(link: '$_base/courier/active.php');
|
||||
if (res is Map && res['status'] == 'success') {
|
||||
final msg = res['message'];
|
||||
final list = (msg is Map && msg['orders'] is List)
|
||||
? (msg['orders'] as List)
|
||||
.map((o) => FoodDeliveryTask.fromJson(Map<String, dynamic>.from(o)))
|
||||
.toList()
|
||||
: <FoodDeliveryTask>[];
|
||||
return FoodDeliveryApiResult(true, list, 'ok');
|
||||
final msg = _payload(res);
|
||||
if (msg != null) {
|
||||
return FoodDeliveryApiResult(true, _list(msg['orders'], FoodDeliveryTask.fromJson), 'ok');
|
||||
}
|
||||
return FoodDeliveryApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
static Future<FoodDeliveryApiResult<List<FoodDeliveryOffer>>> getPendingOffers() async {
|
||||
final res = await CRUD().post(link: '$_base/courier/pending_offers.php');
|
||||
if (res is Map && res['status'] == 'success') {
|
||||
final msg = res['message'];
|
||||
final list = (msg is Map && msg['offers'] is List)
|
||||
? (msg['offers'] as List)
|
||||
.map((o) => FoodDeliveryOffer.fromJson(Map<String, dynamic>.from(o)))
|
||||
.toList()
|
||||
: <FoodDeliveryOffer>[];
|
||||
return FoodDeliveryApiResult(true, list, 'ok');
|
||||
final msg = _payload(res);
|
||||
if (msg != null) {
|
||||
return FoodDeliveryApiResult(true, _list(msg['offers'], FoodDeliveryOffer.fromJson), 'ok');
|
||||
}
|
||||
return FoodDeliveryApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
static Future<FoodDeliveryApiResult<FoodOrderDetails>> getOrderDetails(int orderId) async {
|
||||
final res = await CRUD().post(
|
||||
link: '$_base/courier/order_details.php',
|
||||
payload: {'order_id': orderId.toString()},
|
||||
);
|
||||
final msg = _payload(res);
|
||||
if (msg != null && msg['order'] is Map) {
|
||||
return FoodDeliveryApiResult(
|
||||
true, FoodOrderDetails.fromJson(Map<String, dynamic>.from(msg['order'])), 'ok');
|
||||
}
|
||||
return FoodDeliveryApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
static Future<FoodDeliveryApiResult<FoodEarnings>> getEarnings() async {
|
||||
final res = await CRUD().post(link: '$_base/courier/earnings.php');
|
||||
final msg = _payload(res);
|
||||
if (msg != null) return FoodDeliveryApiResult(true, FoodEarnings.fromJson(msg), 'ok');
|
||||
return FoodDeliveryApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
static Future<FoodDeliveryApiResult<List<FoodHistoryEntry>>> getHistory({
|
||||
int limit = 20,
|
||||
int offset = 0,
|
||||
}) async {
|
||||
final res = await CRUD().post(
|
||||
link: '$_base/courier/history.php',
|
||||
payload: {'limit': limit.toString(), 'offset': offset.toString()},
|
||||
);
|
||||
final msg = _payload(res);
|
||||
if (msg != null) {
|
||||
return FoodDeliveryApiResult(true, _list(msg['orders'], FoodHistoryEntry.fromJson), 'ok');
|
||||
}
|
||||
return FoodDeliveryApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
@@ -55,9 +91,9 @@ class FoodDeliveryService {
|
||||
link: '$_base/courier/offer_respond.php',
|
||||
payload: {'order_id': orderId.toString(), 'response': accept ? 'accept' : 'reject'},
|
||||
);
|
||||
if (res is Map && res['status'] == 'success') {
|
||||
final msg = res['message'];
|
||||
return FoodDeliveryApiResult(true, (msg is Map ? msg['response']?.toString() : null) ?? '', 'ok');
|
||||
final msg = _payload(res);
|
||||
if (msg != null) {
|
||||
return FoodDeliveryApiResult(true, msg['response']?.toString() ?? '', 'ok');
|
||||
}
|
||||
return FoodDeliveryApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
@@ -80,6 +116,19 @@ class FoodDeliveryService {
|
||||
return FoodDeliveryApiResult(false, null, _errMsg(res));
|
||||
}
|
||||
|
||||
// كل ردود backend/food تأتي بغلاف {status, message:{...}} — نستخرج المحتوى مرة واحدة
|
||||
static Map<String, dynamic>? _payload(dynamic res) {
|
||||
if (res is Map && res['status'] == 'success' && res['message'] is Map) {
|
||||
return Map<String, dynamic>.from(res['message']);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static List<T> _list<T>(dynamic raw, T Function(Map<String, dynamic>) fromJson) {
|
||||
if (raw is! List) return <T>[];
|
||||
return raw.map((e) => fromJson(Map<String, dynamic>.from(e))).toList();
|
||||
}
|
||||
|
||||
static String _errMsg(dynamic res) {
|
||||
if (res == 'no_internet') return 'تحقق من اتصالك بالإنترنت';
|
||||
if (res == 'token_expired') return 'انتهت الجلسة، حاول مجدداً';
|
||||
|
||||
Reference in New Issue
Block a user