- 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:
@@ -29,6 +29,7 @@ class CategoryApiPaths {
|
||||
|
||||
class ReceiptImportApiPaths {
|
||||
static const refreshCategories = '/receipt-import/refresh-categories';
|
||||
static const unitMappings = '/receipt-import/unit-mappings';
|
||||
}
|
||||
|
||||
class ReceiptAliasApiPaths {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -464,8 +464,10 @@ class _ReceiptImportTabState extends ConsumerState<ReceiptImportTab> {
|
||||
int pantryAdded = 0;
|
||||
int pantrySkipped = 0;
|
||||
int aliasesLearned = 0;
|
||||
int unitMappingsLearned = 0;
|
||||
try {
|
||||
final token = await ref.read(authStateProvider.future);
|
||||
final repo = ref.read(importRepositoryProvider);
|
||||
final invRepo = ref.read(inventoryRepositoryProvider);
|
||||
final pantryRepo = ref.read(pantryRepositoryProvider);
|
||||
final adminRepo = ref.read(adminRepositoryProvider);
|
||||
@@ -497,6 +499,8 @@ class _ReceiptImportTabState extends ConsumerState<ReceiptImportTab> {
|
||||
: (edit.quantity ?? inferred.totalQuantity ?? item.quantity ?? 1.0);
|
||||
final unit = packUnit;
|
||||
final existing = _inventoryByProduct[pid];
|
||||
final originalUnit = (item.unit ?? '').trim();
|
||||
final preferredUnitForLearning = (existing?.unit ?? unit).trim();
|
||||
final qtyInExistingUnit = existing == null
|
||||
? null
|
||||
: convertQuantity(qty, unit, existing.unit);
|
||||
@@ -516,6 +520,23 @@ class _ReceiptImportTabState extends ConsumerState<ReceiptImportTab> {
|
||||
}, token: token);
|
||||
created++;
|
||||
}
|
||||
|
||||
if (originalUnit.isNotEmpty && preferredUnitForLearning.isNotEmpty) {
|
||||
try {
|
||||
await repo.upsertUnitMapping(
|
||||
productId: pid,
|
||||
originalUnit: originalUnit,
|
||||
preferredUnit: preferredUnitForLearning,
|
||||
token: token,
|
||||
);
|
||||
if (originalUnit.toLowerCase().trim() != preferredUnitForLearning.toLowerCase().trim()) {
|
||||
unitMappingsLearned++;
|
||||
}
|
||||
} catch (e, st) {
|
||||
debugPrint('ReceiptImportTab unit mapping upsert failed: $e');
|
||||
debugPrintStack(stackTrace: st);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final normalizedReceiptName = item.rawName.trim().toLowerCase();
|
||||
@@ -544,6 +565,7 @@ class _ReceiptImportTabState extends ConsumerState<ReceiptImportTab> {
|
||||
if (pantryAdded > 0) '$pantryAdded tillagd${pantryAdded == 1 ? '' : 'a'} i baslager',
|
||||
if (pantrySkipped > 0) '$pantrySkipped fanns redan i baslager',
|
||||
if (aliasesLearned > 0) '$aliasesLearned alias inlärda',
|
||||
if (unitMappingsLearned > 0) '$unitMappingsLearned enhetsmappningar inlärda',
|
||||
];
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(parts.join(', ') + '.')),
|
||||
|
||||
Reference in New Issue
Block a user