30 lines
936 B
Dart
30 lines
936 B
Dart
import '../../domain/entities/customer.dart';
|
|
import '../../domain/repositories/customer_repository.dart';
|
|
import '../datasources/remote/odoo_session_api_client.dart';
|
|
|
|
class OdooCustomerRepository implements CustomerRepository {
|
|
const OdooCustomerRepository({required this.apiClient});
|
|
|
|
final OdooSessionApiClient apiClient;
|
|
|
|
@override
|
|
Future<List<Customer>> search(String query) async {
|
|
try {
|
|
final payload = await apiClient.postForm(
|
|
'/pdv_pos/clientes',
|
|
body: {'query': query.trim()},
|
|
);
|
|
final records = payload['customers'];
|
|
if (records is! List) {
|
|
throw const CustomerSearchException('Resposta de clientes inválida.');
|
|
}
|
|
return records
|
|
.whereType<Map<String, dynamic>>()
|
|
.map(Customer.fromJson)
|
|
.toList(growable: false);
|
|
} on OdooApiException catch (error) {
|
|
throw CustomerSearchException(error.message);
|
|
}
|
|
}
|
|
}
|