From fc850795c9bc552be82d9c3fc26de8540a6a278b Mon Sep 17 00:00:00 2001 From: Mark M2 Macbook Date: Mon, 8 Jun 2026 13:17:33 +0200 Subject: [PATCH] fix attachments with no parentId --- .../shared_forms/attachments_section.dart | 30 ++++++++- .../data/operations_repository.dart | 63 ------------------- 2 files changed, 28 insertions(+), 65 deletions(-) diff --git a/lib/core/widgets/shared_forms/attachments_section.dart b/lib/core/widgets/shared_forms/attachments_section.dart index 6fb9bd4..ed9aa18 100644 --- a/lib/core/widgets/shared_forms/attachments_section.dart +++ b/lib/core/widgets/shared_forms/attachments_section.dart @@ -90,6 +90,32 @@ class _SharedAttachmentsSectionState extends State { // --- SELEZIONE FILE DAL PC/TELEFONO --- Future _pickFiles() async { + final attachmentsBloc = context.read(); + String? targetId = attachmentsBloc.state.parentId; + + // 🥷 SE L'ID NON C'È (Nuova Operazione), FORZIAMO IL SALVATAGGIO PREVENTIVO! + if (targetId == null || targetId.isEmpty) { + if (widget.onGenerateIdForQr != null) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + content: Text('Salvataggio rapido scheda per allegati... ⏳'), + duration: Duration(seconds: 1), + ), + ); + + // Chiamiamo la funzione passata dal TicketForm/OperationForm + targetId = await widget.onGenerateIdForQr!(); + } + + // Se il salvataggio fallisce (es. form non valido), ci fermiamo per evitare file orfani + if (targetId == null || targetId.isEmpty) return; + + // Comunichiamo immediatamente al BLoC che l'entità padre è stata salvata e ha un nuovo ID. + // Questo eviterà che i file finiscano nei `localFiles` temporanei. + attachmentsBloc.add(ParentEntitySavedEvent(targetId)); + } + + // Ora che abbiamo la certezza matematica di avere un targetId, apriamo il picker final result = await FilePicker.pickFiles( allowMultiple: true, type: FileType.custom, @@ -98,8 +124,8 @@ class _SharedAttachmentsSectionState extends State { ); if (result != null && mounted) { - // MAGIA: Passiamo direttamente la lista di PlatformFile al tuo BLoC! - context.read().add(AddAttachmentsEvent(result.files)); + // Ora il BLoC eseguirà l'ambiente di "Upload immediato" (Bivio 2) perché ha l'ID aggiornato! + attachmentsBloc.add(AddAttachmentsEvent(result.files)); } } diff --git a/lib/features/operations/data/operations_repository.dart b/lib/features/operations/data/operations_repository.dart index 366f456..4b6368e 100644 --- a/lib/features/operations/data/operations_repository.dart +++ b/lib/features/operations/data/operations_repository.dart @@ -111,69 +111,6 @@ class OperationsRepository { } } - // --- RECUPERO PAGINATO CON FILTRI E JOIN --- - /* Future> fetchOperations({ - required String companyId, - String? storeId, - String? staffId, - String? providerId, - required int offset, - int limit = 50, - String? searchTerm, - DateTimeRange? dateRange, - }) async { - try { - var query = _supabase - .from(Tables.operations) - .select(''' - *, - ${Tables.customers}(*), - ${Tables.stores}(name), - ${Tables.providers}(name), - ${Tables.models}(name_with_brand), - ${Tables.staffMembers}(name), - ${Tables.attachments}(*) - ''') - .eq('company_id', companyId); - - // Filtro Range Date - if (dateRange != null) { - query = query - .gte('created_at', dateRange.start.toIso8601String()) - .lte('created_at', dateRange.end.toIso8601String()); - } - - if (storeId != null) { - query = query.or('store_id.eq.$storeId,store_id.is.null'); - } - - if (staffId != null) { - query = query.or('staff_id.eq.$staffId,staff_id.is.null'); - } - - if (providerId != null) { - query = query.or('provider_id.eq.$providerId,provider_id.is.null'); - } - - if (searchTerm != null && searchTerm.isNotEmpty) { - // Filtra sui campi della tabella principale O su quelli della tabella joinata - query = query.or( - 'reference.ilike.%$searchTerm%,note.ilike.%$searchTerm%,customer.name.ilike.%$searchTerm%', - ); - } - - final response = await query - .order('created_at', ascending: false) - .range(offset, offset + limit - 1); - - return (response as List) - .map((map) => OperationModel.fromMap(map)) - .toList(); - } catch (e) { - throw Exception('$e'); - } - } */ - Stream>> watchStoreOperations({ required String storeId, required int limit,