feat: add unit mapping functionality
Test Suite / test (24.15.0) (push) Has been cancelled

- Added new API path for unit mappings in `api_paths.dart`.
- Implemented `upsertUnitMapping` method in `ImportRepository` to handle unit mapping creation.
- Updated `ReceiptImportTab` to learn and save unit mappings during receipt import.
- Created DTO for unit mapping with validation in `create-unit-mapping.dto.ts`.
- Added SQL migration for `UnitMapping` table creation with necessary constraints.
This commit is contained in:
Nils-Johan Gynther
2026-05-07 10:00:42 +02:00
parent 26823fbf35
commit a68a0ca86f
35 changed files with 558 additions and 24 deletions
@@ -5,6 +5,7 @@ import 'dart:typed_data';
import 'package:http/http.dart' as http;
import 'dart:developer' as developer;
import '../../../core/api/api_paths.dart';
import '../../../core/api/api_exception.dart';
import '../domain/quick_import_result.dart';
@@ -215,6 +216,45 @@ class ImportRepository {
}
}
Future<void> upsertUnitMapping({
required int productId,
required String originalUnit,
required String preferredUnit,
String? token,
}) async {
final normalizedOriginalUnit = originalUnit.trim().toLowerCase();
final normalizedPreferredUnit = preferredUnit.trim().toLowerCase();
if (normalizedOriginalUnit.isEmpty || normalizedPreferredUnit.isEmpty) {
return;
}
if (normalizedOriginalUnit == normalizedPreferredUnit) {
return;
}
final uri = Uri.parse('$_baseUrl${ReceiptImportApiPaths.unitMappings}');
final response = await _client.post(
uri,
headers: {
'Content-Type': 'application/json',
if (token != null) 'Authorization': 'Bearer $token',
},
body: jsonEncode({
'productId': productId,
'originalUnit': normalizedOriginalUnit,
'preferredUnit': normalizedPreferredUnit,
}),
);
if (response.statusCode < 200 || response.statusCode >= 300) {
throw ApiException(
type: _mapStatusCodeToErrorType(response.statusCode),
message: 'Kunde inte spara enhetsmappning: ${response.body}',
statusCode: response.statusCode,
);
}
}
/// Helper method to map HTTP status codes to [ApiErrorType].
ApiErrorType _mapStatusCodeToErrorType(int statusCode) {
if (statusCode == 401) return ApiErrorType.unauthorized;