- 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>
283 lines
8.3 KiB
Dart
283 lines
8.3 KiB
Dart
import 'dart:developer';
|
|
|
|
import 'package:injectable/injectable.dart';
|
|
import 'package:data_channel/data_channel.dart';
|
|
|
|
import '../../../common/api/api_client.dart';
|
|
import '../../../common/api/api_failure.dart';
|
|
import '../../../common/extension/extension.dart';
|
|
import '../../../common/url/api_path.dart';
|
|
import '../../../domain/auth/auth.dart';
|
|
import '../auth_dtos.dart';
|
|
|
|
@injectable
|
|
class AuthRemoteDataProvider {
|
|
final ApiClient _apiClient;
|
|
final String _logName = "AuthRemoteDataProvider";
|
|
|
|
AuthRemoteDataProvider(this._apiClient);
|
|
|
|
static const _defaultErrorMessage = 'Terjadi kesalahan coba lagi nanti';
|
|
|
|
Map? _firstError(dynamic body) {
|
|
final errors = body is Map ? body['errors'] : null;
|
|
if (errors is! List || errors.isEmpty || errors.first is! Map) return null;
|
|
return errors.first as Map;
|
|
}
|
|
|
|
/// Kode bisa dikirim sebagai string ("304") maupun int (304).
|
|
String? _errorCode(dynamic body) => _firstError(body)?['code']?.toString();
|
|
|
|
/// Pesan untuk error validasi (`303`/`310`) dan aturan bisnis (`304`).
|
|
String _errorMessage(dynamic body) {
|
|
final error = _firstError(body);
|
|
final code = error?['code']?.toString();
|
|
final cause = error?['cause']?.toString() ?? '';
|
|
final lower = cause.toLowerCase();
|
|
|
|
if (lower.contains('password') &&
|
|
(lower.contains('min') ||
|
|
lower.contains('least') ||
|
|
lower.contains('short'))) {
|
|
final length = RegExp(r'\d+').firstMatch(cause)?.group(0);
|
|
return length == null
|
|
? 'Kata sandi terlalu pendek'
|
|
: 'Kata sandi minimal $length karakter';
|
|
}
|
|
|
|
if ((code == '303' || code == '304' || code == '310') &&
|
|
cause.isNotEmpty) {
|
|
return cause;
|
|
}
|
|
|
|
return _defaultErrorMessage;
|
|
}
|
|
|
|
Future<DC<AuthFailure, CheckPhoneDto>> checkPhone({
|
|
required String phoneNumber,
|
|
}) async {
|
|
try {
|
|
final response = await _apiClient.post(
|
|
ApiPath.checkPhone,
|
|
data: {'phone_number': phoneNumber},
|
|
);
|
|
|
|
if (response.data['code'] == 401) {
|
|
return DC.error(
|
|
AuthFailure.serverError(
|
|
ApiFailure.unauthorized('Incorrect email or password'),
|
|
),
|
|
);
|
|
}
|
|
|
|
if (response.data['success'] == false) {
|
|
if ((response.data['errors'] as List).isNotEmpty) {
|
|
if (response.data['errors'][0]['code'] == 303) {
|
|
return DC.error(
|
|
AuthFailure.dynamicErrorMessage('No. Telepon Tidak Boleh Kosong'),
|
|
);
|
|
}
|
|
if (response.data['errors'][0]['code'] == 304) {
|
|
return DC.error(
|
|
AuthFailure.dynamicErrorMessage(
|
|
response.data['errors'][0]['cause'],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
final dto = CheckPhoneDto.fromJson(response.data['data']);
|
|
return DC.data(dto);
|
|
} on ApiFailure catch (e, s) {
|
|
log('checkPhone', name: _logName, error: e, stackTrace: s);
|
|
return DC.error(AuthFailure.serverError(e));
|
|
}
|
|
}
|
|
|
|
Future<DC<AuthFailure, RegisterDto>> register({
|
|
required String phoneNumber,
|
|
required String name,
|
|
required DateTime birthDate,
|
|
}) async {
|
|
try {
|
|
final response = await _apiClient.post(
|
|
ApiPath.register,
|
|
data: {
|
|
'phone_number': phoneNumber,
|
|
'name': name,
|
|
'birth_date': birthDate.toServerDate,
|
|
},
|
|
);
|
|
|
|
if (response.data['success'] == false) {
|
|
if ((response.data['errors'] as List).isNotEmpty) {
|
|
if (response.data['errors'][0]['code'] == "900") {
|
|
return DC.error(
|
|
AuthFailure.dynamicErrorMessage('No. Telepon Sudah Terdaftar'),
|
|
);
|
|
} else {
|
|
return DC.error(
|
|
AuthFailure.dynamicErrorMessage(
|
|
'Terjadi kesalahan coba lagi nanti',
|
|
),
|
|
);
|
|
}
|
|
} else {
|
|
return DC.error(
|
|
AuthFailure.dynamicErrorMessage(
|
|
'Terjadi kesalahan coba lagi nanti',
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
final dto = RegisterDto.fromJson(response.data['data']);
|
|
return DC.data(dto);
|
|
} on ApiFailure catch (e, s) {
|
|
log('register', name: _logName, error: e, stackTrace: s);
|
|
return DC.error(AuthFailure.serverError(e));
|
|
}
|
|
}
|
|
|
|
Future<DC<AuthFailure, VerifyDto>> verify({
|
|
required String registrationToken,
|
|
required String otpCode,
|
|
}) async {
|
|
try {
|
|
final response = await _apiClient.post(
|
|
ApiPath.verify,
|
|
data: {'registration_token': registrationToken, 'otp_code': otpCode},
|
|
);
|
|
|
|
if (response.data['success'] == false) {
|
|
if ((response.data['errors'] as List).isNotEmpty) {
|
|
if (response.data['errors'][0]['code'] == "900") {
|
|
return DC.error(
|
|
AuthFailure.dynamicErrorMessage('Kode OTP Tidak Sesuai'),
|
|
);
|
|
} else {
|
|
return DC.error(
|
|
AuthFailure.dynamicErrorMessage(
|
|
'Terjadi kesalahan coba lagi nanti',
|
|
),
|
|
);
|
|
}
|
|
} else {
|
|
return DC.error(
|
|
AuthFailure.dynamicErrorMessage(
|
|
'Terjadi kesalahan coba lagi nanti',
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
final dto = VerifyDto.fromJson(response.data['data']);
|
|
return DC.data(dto);
|
|
} on ApiFailure catch (e, s) {
|
|
log('verify', name: _logName, error: e, stackTrace: s);
|
|
return DC.error(AuthFailure.serverError(e));
|
|
}
|
|
}
|
|
|
|
Future<DC<AuthFailure, LoginDto>> setPassword({
|
|
required String registrationToken,
|
|
required String password,
|
|
required String confirmPassword,
|
|
}) async {
|
|
try {
|
|
final response = await _apiClient.post(
|
|
ApiPath.setPassword,
|
|
data: {
|
|
'registration_token': registrationToken,
|
|
'password': password,
|
|
'confirm_password': confirmPassword,
|
|
},
|
|
);
|
|
|
|
if (response.data['success'] == false) {
|
|
final code = _errorCode(response.data);
|
|
return DC.error(
|
|
AuthFailure.dynamicErrorMessage(
|
|
code == '900'
|
|
? 'Invalid Registration, Lakukan kembali dari awal'
|
|
: _errorMessage(response.data),
|
|
),
|
|
);
|
|
}
|
|
|
|
final dto = LoginDto.fromJson(response.data['data']);
|
|
return DC.data(dto);
|
|
} on ApiFailure catch (e, s) {
|
|
log('setPassword', name: _logName, error: e, stackTrace: s);
|
|
return DC.error(AuthFailure.serverError(e));
|
|
}
|
|
}
|
|
|
|
Future<DC<AuthFailure, LoginDto>> login({
|
|
required String phoneNumber,
|
|
required String password,
|
|
}) async {
|
|
try {
|
|
final response = await _apiClient.post(
|
|
ApiPath.login,
|
|
data: {'phone_number': phoneNumber, 'password': password},
|
|
);
|
|
|
|
if (response.data['success'] == false) {
|
|
if ((response.data['errors'] as List).isNotEmpty) {
|
|
if (response.data['errors'][0]['code'] == "900") {
|
|
return DC.error(
|
|
AuthFailure.dynamicErrorMessage(
|
|
response.data['errors'][0]['cause'],
|
|
),
|
|
);
|
|
} else {
|
|
return DC.error(
|
|
AuthFailure.dynamicErrorMessage(
|
|
'Terjadi kesalahan coba lagi nanti',
|
|
),
|
|
);
|
|
}
|
|
} else {
|
|
return DC.error(
|
|
AuthFailure.dynamicErrorMessage(
|
|
'Terjadi kesalahan coba lagi nanti',
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
final dto = LoginDto.fromJson(response.data['data']);
|
|
return DC.data(dto);
|
|
} on ApiFailure catch (e, s) {
|
|
log('login', name: _logName, error: e, stackTrace: s);
|
|
return DC.error(AuthFailure.serverError(e));
|
|
}
|
|
}
|
|
|
|
Future<DC<AuthFailure, ResendDto>> resend({
|
|
required String phoneNumber,
|
|
required String purpose, //login or registration
|
|
}) async {
|
|
try {
|
|
final response = await _apiClient.post(
|
|
ApiPath.resend,
|
|
data: {'phone_number': phoneNumber, 'purpose': purpose},
|
|
);
|
|
|
|
if (response.data['success'] == false) {
|
|
return DC.error(
|
|
AuthFailure.dynamicErrorMessage('Terjadi kesalahan coba lagi nanti'),
|
|
);
|
|
}
|
|
|
|
final dto = ResendDto.fromJson(response.data['data']);
|
|
return DC.data(dto);
|
|
} on ApiFailure catch (e, s) {
|
|
log('resend', name: _logName, error: e, stackTrace: s);
|
|
return DC.error(AuthFailure.serverError(e));
|
|
}
|
|
}
|
|
}
|