chore(import): improve error handling and add flyer integration
Test Suite / backend-pr-quick (push) Has been skipped
Test Suite / quick-import-pr-quick (push) Has been skipped
Test Suite / backend-full (push) Failing after 3m41s
Test Suite / flutter-quality (push) Successful in 2m3s

- Replace BadRequestException with UnauthorizedException for authentication failures in flyer-import and flyer-selection controllers
- Add bulk selection endpoint in FlyerSelectionController for creating multiple selections in one request
- Update FlyerSelectionModule to include new FlyerSelectionMatcherService and FlyerSelectionSyncController
- Extend FlyerSelectionService with createMany method for bulk operations
- Add new DTOs for bulk selection and receipt matching functionality
- Update ReceiptImportService to accept FlyerSelectionService dependency and track successful rows
- Extend SaveReceiptResponse with flyerAutoSync field for receipt-to-flyer matching results
- Add new API paths for flyer import and selection endpoints
- Update Flutter UI to include Flyer import tab and adjust tab controller length
- Add new domain models and repository methods for flyer import functionality
- Update test files to include new FlyerSelectionService dependency
- Modify .kilo plan documentation to reflect current system architecture
This commit is contained in:
Nils-Johan Gynther
2026-05-18 22:51:27 +02:00
parent 24a96c3da1
commit d5f903db98
26 changed files with 1359 additions and 247 deletions
@@ -0,0 +1,196 @@
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../auth/data/auth_providers.dart';
import '../data/import_providers.dart';
import '../domain/flyer_import_item.dart';
import '../domain/flyer_import_result.dart';
import 'error_dialog.dart';
class FlyerImportTab extends ConsumerStatefulWidget {
const FlyerImportTab({super.key});
@override
ConsumerState<FlyerImportTab> createState() => _FlyerImportTabState();
}
class _FlyerImportTabState extends ConsumerState<FlyerImportTab> {
bool _isLoading = false;
bool _isSaving = false;
PlatformFile? _pickedFile;
FlyerImportResult? _result;
final Map<int, bool> _selected = {};
Future<void> _pickFile() async {
final result = await FilePicker.pickFiles(
type: FileType.custom,
allowedExtensions: ['pdf', 'txt'],
withData: true,
);
if (result == null || result.files.isEmpty) return;
setState(() => _pickedFile = result.files.first);
}
Future<void> _parseFlyer() async {
final file = _pickedFile;
if (file?.bytes == null) return;
setState(() => _isLoading = true);
try {
final token = await ref.read(authStateProvider.future);
final repo = ref.read(importRepositoryProvider);
final parsed = await repo.importFlyerFile(
bytes: file!.bytes!,
filename: file.name,
token: token,
);
if (!mounted) return;
final selected = <int, bool>{};
for (var i = 0; i < parsed.items.length; i++) {
selected[i] = parsed.items[i].matchedProductId != null;
}
setState(() {
_result = parsed;
_selected
..clear()
..addAll(selected);
});
} catch (e) {
if (mounted) showErrorDialog(context, 'Flyerimport misslyckades: $e');
} finally {
if (mounted) setState(() => _isLoading = false);
}
}
Future<void> _planSelected() async {
final result = _result;
if (result?.sessionId == null) return;
final itemsToSave = <Map<String, dynamic>>[];
for (var i = 0; i < result!.items.length; i++) {
final item = result.items[i];
final isSelected = _selected[i] == true;
if (!isSelected || item.flyerItemId == null) continue;
itemsToSave.add({
'itemId': item.flyerItemId,
'plannedQuantity': 1,
'plannedUnit': item.priceUnit,
'priority': 'normal',
});
}
if (itemsToSave.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Markera minst en rad att planera.')),
);
return;
}
setState(() => _isSaving = true);
try {
final token = await ref.read(authStateProvider.future);
final repo = ref.read(importRepositoryProvider);
final saved = await repo.createFlyerSelectionsBulk(
sessionId: result.sessionId!,
items: itemsToSave,
token: token,
);
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('${saved.length} varor planerade.')),
);
} catch (e) {
if (mounted) showErrorDialog(context, 'Kunde inte planera varor: $e');
} finally {
if (mounted) setState(() => _isSaving = false);
}
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final items = _result?.items ?? const <FlyerImportItem>[];
final selectedCount = _selected.values.where((value) => value).length;
return SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Ladda upp flyer (PDF/txt), granska rader och planera inköp med ett klick.',
style: theme.textTheme.bodyMedium,
),
const SizedBox(height: 16),
OutlinedButton.icon(
onPressed: _isLoading ? null : _pickFile,
icon: const Icon(Icons.attach_file),
label: Text(_pickedFile?.name ?? 'Välj flyerfil'),
),
const SizedBox(height: 12),
FilledButton.icon(
onPressed: (!_isLoading && _pickedFile?.bytes != null) ? _parseFlyer : null,
icon: const Icon(Icons.auto_awesome),
label: const Text('Importera flyer'),
),
if (_isLoading) ...[
const SizedBox(height: 12),
const LinearProgressIndicator(),
],
if (items.isNotEmpty) ...[
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('${items.length} rader hittades', style: theme.textTheme.titleSmall),
TextButton(
onPressed: () {
final target = selectedCount < items.length;
setState(() {
for (var i = 0; i < items.length; i++) {
_selected[i] = target;
}
});
},
child: Text(selectedCount < items.length ? 'Välj alla' : 'Avmarkera alla'),
),
],
),
const SizedBox(height: 8),
...items.asMap().entries.map((entry) {
final index = entry.key;
final item = entry.value;
return CheckboxListTile(
value: _selected[index] ?? false,
onChanged: (value) => setState(() => _selected[index] = value ?? false),
title: Text(item.rawName),
subtitle: Text([
if (item.offerText != null && item.offerText!.isNotEmpty) item.offerText!,
if (item.matchedProductName != null) 'Match: ${item.matchedProductName}',
].join(' · ')),
controlAffinity: ListTileControlAffinity.leading,
);
}),
const SizedBox(height: 8),
SizedBox(
width: double.infinity,
child: FilledButton.icon(
onPressed: (_isSaving || selectedCount == 0) ? null : _planSelected,
icon: _isSaving
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white),
)
: const Icon(Icons.playlist_add_check),
label: Text('Planera $selectedCount markerade'),
),
),
],
],
),
);
}
}
@@ -1,9 +1,10 @@
import 'package:flutter/material.dart';
import 'recipe_import_tab.dart';
import 'receipt_import_tab.dart';
import 'flyer_import_tab.dart';
import 'recipe_import_tab.dart';
import 'receipt_import_tab.dart';
/// Main import screen with tabs: Recept | Kvitto.
/// Main import screen with tabs: Recept | Kvitto | Flyer.
///
/// Fas 6a: Recept-fliken är implementerad.
/// Fas 6b: Kvitto-fliken läggs till i ett senare steg.
@@ -18,10 +19,11 @@ class _ImportScreenState extends State<ImportScreen> {
@override
Widget build(BuildContext context) {
return const TabBarView(
children: [
RecipeImportTab(),
ReceiptImportTab(),
],
);
}
}
children: [
RecipeImportTab(),
ReceiptImportTab(),
FlyerImportTab(),
],
);
}
}
@@ -708,8 +708,9 @@ class _ReceiptImportTabState extends ConsumerState<ReceiptImportTab> {
final pantryAdded = response['pantryAdded'] as int? ?? 0;
final pantrySkipped = response['pantrySkipped'] as int? ?? 0;
final aliasesLearned = response['aliasesLearned'] as int? ?? 0;
final unitMappingsLearned = response['unitMappingsLearned'] as int? ?? 0;
final errors = response['errors'] as List? ?? [];
final unitMappingsLearned = response['unitMappingsLearned'] as int? ?? 0;
final flyerAutoSync = response['flyerAutoSync'] as Map<String, dynamic>?;
final errors = response['errors'] as List? ?? [];
final parts = <String>[
if (created > 0) '$created ny${created == 1 ? '' : 'a'} i inventarie',
@@ -717,8 +718,12 @@ 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',
];
if (unitMappingsLearned > 0) '$unitMappingsLearned enhetsmappningar inlärda',
if ((flyerAutoSync?['bought'] as int? ?? 0) > 0)
'${flyerAutoSync?['bought']} planerade flyer-varor markerade som köpta',
if ((flyerAutoSync?['ambiguous'] as int? ?? 0) > 0)
'${flyerAutoSync?['ambiguous']} flyer-matchningar kräver kontroll',
];
if (errors.isNotEmpty) {
final errorParts = <String>[];