feat: update profit shareing
Build & Deploy iOS to TestFlight / build-and-deploy (push) Canceled after 0s
Build & Deploy iOS to TestFlight / build-and-deploy (push) Canceled after 0s
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:data_channel/data_channel.dart';
|
||||
import 'package:dio/dio.dart' show Response;
|
||||
import 'package:injectable/injectable.dart';
|
||||
|
||||
import '../../../common/api/api_client.dart';
|
||||
@@ -291,7 +292,12 @@ class AnalyticRemoteDataProvider {
|
||||
|
||||
return DC.data(dto);
|
||||
} on ApiFailure catch (e, s) {
|
||||
log('fetchExclusiveSummaryError', name: _logName, error: e, stackTrace: s);
|
||||
log(
|
||||
'fetchExclusiveSummaryError',
|
||||
name: _logName,
|
||||
error: e,
|
||||
stackTrace: s,
|
||||
);
|
||||
return DC.error(AnalyticFailure.serverError(e));
|
||||
}
|
||||
}
|
||||
@@ -310,17 +316,18 @@ class AnalyticRemoteDataProvider {
|
||||
};
|
||||
if (outletId != null) params['outlet_id'] = outletId;
|
||||
|
||||
final response = await _apiClient.get(
|
||||
ApiPath.parentCategoryAnalytic,
|
||||
params: params,
|
||||
headers: getAuthorizationHeader(),
|
||||
);
|
||||
final response = await _getProfitSharing('', params);
|
||||
|
||||
if (response.data['data'] == null) {
|
||||
final failure = _profitSharingFailure(response);
|
||||
if (failure != null) return DC.error(failure);
|
||||
|
||||
final body = response.data;
|
||||
final data = body is Map ? body['data'] : null;
|
||||
if (data == null) {
|
||||
return DC.error(AnalyticFailure.empty());
|
||||
}
|
||||
|
||||
final dto = ProfitSharingDto.fromJson(response.data['data']);
|
||||
final dto = ProfitSharingDto.fromJson(data);
|
||||
|
||||
return DC.data(dto);
|
||||
} on ApiFailure catch (e, s) {
|
||||
@@ -342,17 +349,18 @@ class AnalyticRemoteDataProvider {
|
||||
};
|
||||
if (outletId != null) params['outlet_id'] = outletId;
|
||||
|
||||
final response = await _apiClient.get(
|
||||
'${ApiPath.parentCategoryAnalytic}/$parentCategoryId',
|
||||
params: params,
|
||||
headers: getAuthorizationHeader(),
|
||||
);
|
||||
final response = await _getProfitSharing('/$parentCategoryId', params);
|
||||
|
||||
if (response.data['data'] == null) {
|
||||
final failure = _profitSharingFailure(response);
|
||||
if (failure != null) return DC.error(failure);
|
||||
|
||||
final body = response.data;
|
||||
final data = body is Map ? body['data'] : null;
|
||||
if (data == null) {
|
||||
return DC.error(AnalyticFailure.empty());
|
||||
}
|
||||
|
||||
final dto = ProfitSharingDetailDto.fromJson(response.data['data']);
|
||||
final dto = ProfitSharingDetailDto.fromJson(data);
|
||||
|
||||
return DC.data(dto);
|
||||
} on ApiFailure catch (e, s) {
|
||||
@@ -365,4 +373,43 @@ class AnalyticRemoteDataProvider {
|
||||
return DC.error(AnalyticFailure.serverError(e));
|
||||
}
|
||||
}
|
||||
|
||||
/// GET ke endpoint profit sharing. Selama backend lama masih jalan path baru
|
||||
/// mengembalikan 404, jadi dicoba sekali lagi ke path lama.
|
||||
Future<Response> _getProfitSharing(
|
||||
String subPath,
|
||||
Map<String, dynamic> params,
|
||||
) async {
|
||||
Future<Response> request(String basePath) => _apiClient.get(
|
||||
'$basePath$subPath',
|
||||
params: params,
|
||||
headers: getAuthorizationHeader(),
|
||||
// Status 5xx tidak dilempar supaya `cause` di body masih bisa dibaca.
|
||||
validateStatus: (status) => status != null && status < 600,
|
||||
);
|
||||
|
||||
final response = await request(ApiPath.profitSharingAnalytic);
|
||||
if (response.statusCode != 404) return response;
|
||||
return request(ApiPath.profitSharingAnalyticLegacy);
|
||||
}
|
||||
|
||||
/// Failure dari status error endpoint profit sharing, null kalau berhasil.
|
||||
/// Detail kategori non-team ditolak 500 dengan `cause` "... is not a team".
|
||||
AnalyticFailure? _profitSharingFailure(Response response) {
|
||||
final status = response.statusCode ?? 0;
|
||||
if (status == 404) {
|
||||
return const AnalyticFailure.serverError(ApiFailure.notFound(null));
|
||||
}
|
||||
if (status < 500) return null;
|
||||
|
||||
final body = response.data;
|
||||
final errors = body is Map ? body['errors'] : null;
|
||||
final isNotTeam =
|
||||
errors is List &&
|
||||
errors.any(
|
||||
(e) => e is Map && '${e['cause']}'.contains('is not a team'),
|
||||
);
|
||||
if (isNotTeam) return const AnalyticFailure.notTeam();
|
||||
return const AnalyticFailure.serverError(ApiFailure.internalServerError());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user