update info

This commit is contained in:
Phuoc Nguyen
2025-11-20 16:32:33 +07:00
parent 1fcef52d5e
commit dc85157758
12 changed files with 1613 additions and 247 deletions

View File

@@ -128,4 +128,95 @@ class UserInfoRemoteDataSource {
// Could add cache-busting headers in the future if needed
return getUserInfo();
}
/// Update User Info
///
/// Updates the current user's profile information.
///
/// API: POST https://land.dbiz.com/api/method/building_material.building_material.api.user.update_user_info
///
/// Request body:
/// ```json
/// {
/// "full_name": "...",
/// "date_of_birth": "YYYY-MM-DD",
/// "gender": "Male/Female",
/// "company_name": "...",
/// "tax_code": "...",
/// "avatar_base64": null | base64_string,
/// "id_card_front_base64": null | base64_string,
/// "id_card_back_base64": null | base64_string,
/// "certificates_base64": [] | [base64_string, ...]
/// }
/// ```
///
/// Throws:
/// - [UnauthorizedException] if user not authenticated (401)
/// - [ServerException] if server error occurs (500+)
/// - [NetworkException] for other network errors
Future<UserInfoModel> updateUserInfo(Map<String, dynamic> data) async {
try {
debugPrint('🔵 [UserInfoDataSource] Updating user info...');
debugPrint('🔵 [UserInfoDataSource] Data: $data');
final startTime = DateTime.now();
// Make POST request with update data
final response = await _dioClient.post<Map<String, dynamic>>(
'/api/method/building_material.building_material.api.user.update_user_info',
data: data,
);
final duration = DateTime.now().difference(startTime);
debugPrint('🟢 [UserInfoDataSource] Update response received in ${duration.inMilliseconds}ms');
debugPrint('🟢 [UserInfoDataSource] Status: ${response.statusCode}');
// Check response status
if (response.statusCode == 200) {
// After successful update, fetch fresh user info
debugPrint('✅ [UserInfoDataSource] Successfully updated user info');
return await getUserInfo();
} else {
throw ServerException(
'Failed to update user info: ${response.statusCode}',
response.statusCode,
);
}
} on DioException catch (e) {
// Handle specific HTTP status codes
if (e.response?.statusCode == 401) {
throw const UnauthorizedException(
'Session expired. Please login again.',
);
} else if (e.response?.statusCode == 403) {
throw const ForbiddenException();
} else if (e.response?.statusCode == 404) {
throw NotFoundException('Update user info endpoint not found');
} else if (e.response?.statusCode != null &&
e.response!.statusCode! >= 500) {
throw ServerException(
'Server error: ${e.response?.statusMessage ?? "Unknown error"}',
e.response?.statusCode,
);
} else if (e.type == DioExceptionType.connectionTimeout ||
e.type == DioExceptionType.receiveTimeout) {
throw const TimeoutException();
} else if (e.type == DioExceptionType.connectionError) {
throw const NoInternetException();
} else {
throw NetworkException(
e.message ?? 'Failed to update user info',
statusCode: e.response?.statusCode,
);
}
} catch (e) {
// Handle unexpected errors
if (e is ServerException ||
e is UnauthorizedException ||
e is NetworkException ||
e is NotFoundException) {
rethrow;
}
throw ServerException('Unexpected error: $e');
}
}
}