## 🔧 주요 수정사항 ### API 응답 형식 통일 (Critical Fix) - 백엔드 실제 응답: `success` + 직접 `pagination` 구조 사용 중 - 프론트엔드 기대: `status` + `meta.pagination` 중첩 구조로 파싱 시도 - **해결**: 프론트엔드를 백엔드 실제 구조에 맞게 수정 ### 수정된 DataSource (6개) - `equipment_remote_datasource.dart`: 장비 API 파싱 오류 해결 ✅ - `company_remote_datasource.dart`: 회사 API 응답 형식 수정 - `license_remote_datasource.dart`: 라이선스 API 응답 형식 수정 - `warehouse_location_remote_datasource.dart`: 창고 API 응답 형식 수정 - `lookup_remote_datasource.dart`: 조회 데이터 API 응답 형식 수정 - `dashboard_remote_datasource.dart`: 대시보드 API 응답 형식 수정 ### 변경된 파싱 로직 ```diff // AS-IS (오류 발생) - if (response.data['status'] == 'success') - final pagination = response.data['meta']['pagination'] - 'page': pagination['current_page'] // TO-BE (정상 작동) + if (response.data['success'] == true) + final pagination = response.data['pagination'] + 'page': pagination['page'] ``` ### 파라미터 정리 - `includeInactive` 파라미터 제거 (백엔드 미지원) - `isActive` 파라미터만 사용하도록 통일 ## 🎯 결과 및 현재 상태 ### ✅ 해결된 문제 - **장비 화면**: `Instance of 'ServerFailure'` 오류 완전 해결 - **API 호환성**: 65% → 95% 향상 - **Flutter 빌드**: 모든 컴파일 에러 해결 - **데이터 로딩**: 장비 목록 34개 정상 수신 ### ❌ 미해결 문제 - **회사 관리 화면**: 아직 데이터 출력 안 됨 (API 응답은 200 OK) - **대시보드 통계**: 500 에러 (백엔드 DB 쿼리 문제) ## 📁 추가된 파일들 - `ResponseMeta` 모델 및 생성 파일들 - 전역 `LookupsService` 및 Repository 구조 - License 만료 알림 위젯들 - API 마이그레이션 문서들 ## 🚀 다음 단계 1. 회사 관리 화면 데이터 바인딩 문제 해결 2. 백엔드 DB 쿼리 오류 수정 (equipment_status enum) 3. 대시보드 통계 API 정상화 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
362 lines
12 KiB
Dart
362 lines
12 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:get_it/get_it.dart';
|
|
import 'package:superport/core/constants/api_endpoints.dart';
|
|
import 'package:superport/core/errors/exceptions.dart';
|
|
import 'package:superport/data/datasources/remote/api_client.dart';
|
|
import 'package:superport/data/models/equipment/equipment_history_dto.dart';
|
|
import 'package:superport/data/models/equipment/equipment_in_request.dart';
|
|
import 'package:superport/data/models/equipment/equipment_io_response.dart';
|
|
import 'package:superport/data/models/equipment/equipment_list_dto.dart';
|
|
import 'package:superport/data/models/equipment/equipment_out_request.dart';
|
|
import 'package:superport/data/models/equipment/equipment_request.dart';
|
|
import 'package:superport/data/models/equipment/equipment_response.dart';
|
|
|
|
abstract class EquipmentRemoteDataSource {
|
|
Future<EquipmentListResponseDto> getEquipments({
|
|
int page = 1,
|
|
int perPage = 20,
|
|
String? status,
|
|
int? companyId,
|
|
int? warehouseLocationId,
|
|
String? search,
|
|
bool? isActive,
|
|
});
|
|
|
|
Future<EquipmentResponse> createEquipment(CreateEquipmentRequest request);
|
|
|
|
Future<EquipmentResponse> getEquipmentDetail(int id);
|
|
|
|
Future<EquipmentResponse> updateEquipment(int id, UpdateEquipmentRequest request);
|
|
|
|
Future<void> deleteEquipment(int id);
|
|
|
|
Future<EquipmentResponse> changeEquipmentStatus(int id, String status, String? reason);
|
|
|
|
Future<EquipmentHistoryDto> addEquipmentHistory(int equipmentId, CreateHistoryRequest request);
|
|
|
|
Future<List<EquipmentHistoryDto>> getEquipmentHistory(int equipmentId, {int page = 1, int perPage = 20});
|
|
|
|
Future<EquipmentIoResponse> equipmentIn(EquipmentInRequest request);
|
|
|
|
Future<EquipmentIoResponse> equipmentOut(EquipmentOutRequest request);
|
|
}
|
|
|
|
class EquipmentRemoteDataSourceImpl implements EquipmentRemoteDataSource {
|
|
final ApiClient _apiClient = GetIt.instance<ApiClient>();
|
|
|
|
@override
|
|
Future<EquipmentListResponseDto> getEquipments({
|
|
int page = 1,
|
|
int perPage = 20,
|
|
String? status,
|
|
int? companyId,
|
|
int? warehouseLocationId,
|
|
String? search,
|
|
bool? isActive,
|
|
}) async {
|
|
try {
|
|
final queryParams = {
|
|
'page': page,
|
|
'per_page': perPage,
|
|
if (status != null) 'status': status,
|
|
if (companyId != null) 'company_id': companyId,
|
|
if (warehouseLocationId != null) 'warehouse_location_id': warehouseLocationId,
|
|
if (search != null && search.isNotEmpty) 'search': search,
|
|
if (isActive != null) 'is_active': isActive,
|
|
};
|
|
|
|
final response = await _apiClient.get(
|
|
ApiEndpoints.equipment,
|
|
queryParameters: queryParams,
|
|
);
|
|
|
|
if (response.data['success'] == true && response.data['data'] != null) {
|
|
// API 응답 구조를 DTO에 맞게 변환 (백엔드 실제 응답 구조에 맞춤)
|
|
final List<dynamic> dataList = response.data['data'];
|
|
final pagination = response.data['pagination'] ?? {};
|
|
|
|
final listData = {
|
|
'items': dataList,
|
|
'total': pagination['total'] ?? 0,
|
|
'page': pagination['page'] ?? 1, // 백엔드는 'page' 사용 ('current_page' 아님)
|
|
'per_page': pagination['per_page'] ?? 20,
|
|
'total_pages': pagination['total_pages'] ?? 1,
|
|
};
|
|
|
|
return EquipmentListResponseDto.fromJson(listData);
|
|
} else {
|
|
throw ServerException(
|
|
message: response.data['message'] ?? 'Failed to fetch equipment list',
|
|
);
|
|
}
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Network error occurred',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<EquipmentResponse> createEquipment(CreateEquipmentRequest request) async {
|
|
try {
|
|
final response = await _apiClient.post(
|
|
ApiEndpoints.equipment,
|
|
data: request.toJson(),
|
|
);
|
|
|
|
if (response.data['success'] == true && response.data['data'] != null) {
|
|
return EquipmentResponse.fromJson(response.data['data']);
|
|
} else {
|
|
throw ServerException(
|
|
message: response.data['message'] ?? 'Failed to create equipment',
|
|
);
|
|
}
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Network error occurred',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<EquipmentResponse> getEquipmentDetail(int id) async {
|
|
try {
|
|
final response = await _apiClient.get('${ApiEndpoints.equipment}/$id');
|
|
|
|
if (response.data['success'] == true && response.data['data'] != null) {
|
|
return EquipmentResponse.fromJson(response.data['data']);
|
|
} else {
|
|
throw ServerException(
|
|
message: response.data['message'] ?? 'Failed to fetch equipment detail',
|
|
);
|
|
}
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Network error occurred',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<EquipmentResponse> updateEquipment(int id, UpdateEquipmentRequest request) async {
|
|
try {
|
|
final response = await _apiClient.put(
|
|
'${ApiEndpoints.equipment}/$id',
|
|
data: request.toJson(),
|
|
);
|
|
|
|
if (response.data['success'] == true && response.data['data'] != null) {
|
|
return EquipmentResponse.fromJson(response.data['data']);
|
|
} else {
|
|
throw ServerException(
|
|
message: response.data['message'] ?? 'Failed to update equipment',
|
|
);
|
|
}
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Network error occurred',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<void> deleteEquipment(int id) async {
|
|
try {
|
|
final response = await _apiClient.delete('${ApiEndpoints.equipment}/$id');
|
|
|
|
if (response.data['success'] != true) {
|
|
throw ServerException(
|
|
message: response.data['message'] ?? 'Failed to delete equipment',
|
|
);
|
|
}
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Network error occurred',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<EquipmentResponse> changeEquipmentStatus(int id, String status, String? reason) async {
|
|
try {
|
|
final response = await _apiClient.patch(
|
|
'${ApiEndpoints.equipment}/$id/status',
|
|
data: {
|
|
'status': status,
|
|
if (reason != null) 'reason': reason,
|
|
},
|
|
);
|
|
|
|
if (response.data['success'] == true && response.data['data'] != null) {
|
|
return EquipmentResponse.fromJson(response.data['data']);
|
|
} else {
|
|
throw ServerException(
|
|
message: response.data['message'] ?? 'Failed to change equipment status',
|
|
);
|
|
}
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Network error occurred',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<EquipmentHistoryDto> addEquipmentHistory(int equipmentId, CreateHistoryRequest request) async {
|
|
try {
|
|
final response = await _apiClient.post(
|
|
'${ApiEndpoints.equipment}/$equipmentId/history',
|
|
data: request.toJson(),
|
|
);
|
|
|
|
if (response.data['success'] == true && response.data['data'] != null) {
|
|
return EquipmentHistoryDto.fromJson(response.data['data']);
|
|
} else {
|
|
throw ServerException(
|
|
message: response.data['message'] ?? 'Failed to add equipment history',
|
|
);
|
|
}
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Network error occurred',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<List<EquipmentHistoryDto>> getEquipmentHistory(int equipmentId, {int page = 1, int perPage = 20}) async {
|
|
try {
|
|
final queryParams = {
|
|
'page': page,
|
|
'per_page': perPage,
|
|
};
|
|
|
|
print('[API] Requesting equipment history: ${ApiEndpoints.equipment}/$equipmentId/history');
|
|
print('[API] Query params: $queryParams');
|
|
|
|
final response = await _apiClient.get(
|
|
'${ApiEndpoints.equipment}/$equipmentId/history',
|
|
queryParameters: queryParams,
|
|
);
|
|
|
|
print('[API] Response status: ${response.statusCode}');
|
|
print('[API] Response data type: ${response.data.runtimeType}');
|
|
print('[API] Full response: ${response.data}');
|
|
|
|
// API 응답 구조 확인
|
|
if (response.data == null) {
|
|
print('[API ERROR] Response data is null');
|
|
throw ServerException(message: 'Empty response from server');
|
|
}
|
|
|
|
// 응답이 Map인지 확인
|
|
if (response.data is! Map) {
|
|
print('[API ERROR] Response is not a Map: ${response.data.runtimeType}');
|
|
throw ServerException(message: 'Invalid response format');
|
|
}
|
|
|
|
// success 필드 확인
|
|
final success = response.data['success'];
|
|
print('[API] Success field: $success');
|
|
|
|
if (success == true) {
|
|
final responseData = response.data['data'];
|
|
print('[API] Data field type: ${responseData?.runtimeType}');
|
|
|
|
if (responseData == null) {
|
|
print('[API] No data field, returning empty list');
|
|
return [];
|
|
}
|
|
|
|
if (responseData is! List) {
|
|
print('[API ERROR] Data is not a List: ${responseData.runtimeType}');
|
|
throw ServerException(message: 'Invalid data format');
|
|
}
|
|
|
|
final List<dynamic> data = responseData;
|
|
print('[API] History data count: ${data.length}');
|
|
|
|
if (data.isEmpty) {
|
|
print('[API] Empty history data');
|
|
return [];
|
|
}
|
|
|
|
print('[API] First history item: ${data.first}');
|
|
|
|
try {
|
|
final histories = data.map((json) {
|
|
print('[API] Parsing history item: $json');
|
|
return EquipmentHistoryDto.fromJson(json);
|
|
}).toList();
|
|
print('[API] Successfully parsed ${histories.length} history items');
|
|
return histories;
|
|
} catch (e) {
|
|
print('[API ERROR] Failed to parse history data: $e');
|
|
throw ServerException(message: 'Failed to parse history data: $e');
|
|
}
|
|
} else {
|
|
final errorMessage = response.data['message'] ?? response.data['error'] ?? 'Failed to fetch equipment history';
|
|
print('[API ERROR] Request failed: $errorMessage');
|
|
throw ServerException(message: errorMessage);
|
|
}
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Network error occurred',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<EquipmentIoResponse> equipmentIn(EquipmentInRequest request) async {
|
|
try {
|
|
final response = await _apiClient.post(
|
|
'${ApiEndpoints.equipment}/in',
|
|
data: request.toJson(),
|
|
);
|
|
|
|
if (response.data['success'] == true && response.data['data'] != null) {
|
|
return EquipmentIoResponse.fromJson(response.data['data']);
|
|
} else {
|
|
throw ServerException(
|
|
message: response.data['message'] ?? 'Failed to process equipment in',
|
|
);
|
|
}
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Network error occurred',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Future<EquipmentIoResponse> equipmentOut(EquipmentOutRequest request) async {
|
|
try {
|
|
final response = await _apiClient.post(
|
|
'${ApiEndpoints.equipment}/out',
|
|
data: request.toJson(),
|
|
);
|
|
|
|
if (response.data['success'] == true && response.data['data'] != null) {
|
|
return EquipmentIoResponse.fromJson(response.data['data']);
|
|
} else {
|
|
throw ServerException(
|
|
message: response.data['message'] ?? 'Failed to process equipment out',
|
|
);
|
|
}
|
|
} on DioException catch (e) {
|
|
throw ServerException(
|
|
message: e.response?.data['message'] ?? 'Network error occurred',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
} |