Files
enaklo-flutter/lib/infrastructure/wallet/datasources/wallet_remote_data_provider.dart
T
efrilmandClaude Opus 5.5 f5e3075205 EnakPoint & EnakCoin wallet, PIN, outlet, order, community, reward
- Wallet EnakPoint & EnakCoin: beranda, riwayat, saldo kedaluwarsa, kode
  bayar, tukar EnakCoin, transfer
- PIN (buat, ganti, lupa), outlet, riwayat order, game spin
- Halaman community, webview, coming soon, dan ikon menu beranda baru
- Redesign Reward Saya: kartu tiket, filter chip, detail kode + QR
- Hapus customer point loader, menu, dan checkout lama
- Base URL ke enaklo-pos-api.altru.id

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 01:43:22 +07:00

179 lines
5.5 KiB
Dart

import 'dart:developer';
import 'package:data_channel/data_channel.dart';
import 'package:dio/dio.dart';
import 'package:injectable/injectable.dart';
import '../../../common/api/api_client.dart';
import '../../../common/api/api_failure.dart';
import '../../../common/api/interceptors/sensitive_aware_log_interceptor.dart';
import '../../../common/extension/extension.dart';
import '../../../common/function/app_function.dart';
import '../../../common/url/api_path.dart';
import '../../../domain/wallet/wallet.dart';
import '../wallet_dtos.dart';
import '../wallet_response.dart';
@injectable
class WalletRemoteDataProvider {
final ApiClient _apiClient;
final String _logName = 'WalletRemoteDataProvider';
WalletRemoteDataProvider(this._apiClient);
Future<DC<WalletFailure, WalletSummaryDto>> fetchWallet() => _request(
'fetchWallet',
() => _apiClient.get(ApiPath.wallet, headers: getAuthorizationHeader()),
(response) => WalletSummaryDto.fromJson(walletDataOf(response)),
);
Future<DC<WalletFailure, WalletTransactionPageDto>> fetchTransactions({
required int page,
required int limit,
WalletCurrency? currency,
List<WalletTransactionType> types = const [],
DateTime? from,
DateTime? to,
}) => _request(
'fetchTransactions',
() => _apiClient.get(
ApiPath.walletTransactions,
headers: getAuthorizationHeader(),
params: {
'page': page,
'limit': limit,
if (currency != null) 'currency': currency.apiValue,
if (types.isNotEmpty) 'type': types.map((e) => e.apiValue).join(','),
if (from != null) 'from': from.toServerDate,
if (to != null) 'to': to.toServerDate,
},
),
_parseTransactionPage,
);
Future<DC<WalletFailure, WalletExpiringDto>> fetchExpiring() => _request(
'fetchExpiring',
() => _apiClient.get(
ApiPath.walletExpiring,
headers: getAuthorizationHeader(),
),
(response) => WalletExpiringDto.fromJson(walletDataOf(response)),
);
Future<DC<WalletFailure, PaymentCodeDto>> createPaymentCode({
required String pin,
}) => _request(
'createPaymentCode',
() => _apiClient.post(
ApiPath.walletPaymentCode,
headers: getAuthorizationHeader(),
data: {'pin': pin},
extra: {kSensitiveRequest: true},
),
(response) => PaymentCodeDto.fromJson(walletDataOf(response)),
);
Future<DC<WalletFailure, ExchangePreviewDto>> previewExchange({
required int coins,
}) => _request(
'previewExchange',
() => _apiClient.get(
ApiPath.walletExchangePreview,
headers: getAuthorizationHeader(),
params: {'coins': coins},
),
(response) => ExchangePreviewDto.fromJson(walletDataOf(response)),
);
Future<DC<WalletFailure, ExchangeResultDto>> exchange({
required int coins,
required String pin,
required String idempotencyKey,
}) => _request(
'exchange',
() => _apiClient.post(
ApiPath.walletExchange,
headers: {...getAuthorizationHeader(), 'Idempotency-Key': idempotencyKey},
data: {'coins': coins, 'pin': pin},
extra: {kSensitiveRequest: true},
),
(response) => ExchangeResultDto.fromJson(walletDataOf(response)),
);
Future<DC<WalletFailure, TransferRecipientDto>> checkRecipient({
required String phone,
}) => _request(
'checkRecipient',
() => _apiClient.get(
ApiPath.walletTransferRecipient,
headers: getAuthorizationHeader(),
params: {'phone': phone},
),
(response) => TransferRecipientDto.fromJson(walletDataOf(response)),
);
Future<DC<WalletFailure, TransferResultDto>> transfer({
required WalletCurrency currency,
required int amount,
required String recipientPhone,
required String pin,
required String idempotencyKey,
}) => _request(
'transfer',
() => _apiClient.post(
ApiPath.walletTransfer,
headers: {...getAuthorizationHeader(), 'Idempotency-Key': idempotencyKey},
data: {
'currency': currency.apiValue,
'amount': amount,
'recipient_phone': recipientPhone,
'pin': pin,
},
extra: {kSensitiveRequest: true},
),
(response) => TransferResultDto.fromJson(walletDataOf(response)),
);
/// `data` bisa berupa list langsung dengan `pagination` di envelope, atau
/// objek `{ data, pagination }`.
WalletTransactionPageDto _parseTransactionPage(Response response) {
final body = response.data as Map;
final payload = body['data'];
final rawItems = payload is Map ? payload['data'] : payload;
final rawPagination = payload is Map
? payload['pagination']
: body['pagination'];
return WalletTransactionPageDto(
items: (rawItems as List? ?? [])
.map(
(e) => WalletTransactionDto.fromJson(
Map<String, dynamic>.from(e as Map),
),
)
.toList(),
pagination: rawPagination is Map
? WalletPaginationDto.fromJson(
Map<String, dynamic>.from(rawPagination),
)
: const WalletPaginationDto(),
);
}
Future<DC<WalletFailure, T>> _request<T>(
String name,
Future<Response> Function() call,
T Function(Response response) parse,
) async {
try {
final response = await call();
final failure = parseWalletFailure(response);
if (failure != null) return DC.error(failure);
return DC.data(parse(response));
} on ApiFailure catch (e, s) {
log(name, name: _logName, error: e, stackTrace: s);
return DC.error(WalletFailure.serverError(e));
}
}
}