Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-05-03 12:05:47 +02:00
parent 4559db620d
commit 6bb65e8296
13 changed files with 832 additions and 815 deletions

View File

@@ -99,38 +99,35 @@ class OperationsRepository {
}
// --- SALVATAGGIO COMPLETO (PRIMA PADRE, POI FIGLI) ---
Future<OperationModel> saveFullOperation(OperationModel operation) async {
Future<OperationModel> saveFullOperation({
required OperationModel operation,
}) async {
try {
// 1. Upsert del record principale
final operationData = await _supabase
// 1. Salvataggio classico dell'operazione corrente
final response = await _supabase
.from('operation')
.upsert(operation.toMap())
.select()
.select(
'*, provider(*), model(*), store(*), staff_member(*), customer(*), attachment(*)',
)
.single();
final String newId = operationData['id'];
final savedOperation = OperationModel.fromMap(response);
// 5. GRAN FINALE: RECUPERO DEL MODELLO COMPLETO E AGGIORNATO
// Interroghiamo Supabase per farci restituire la pratica con TUTTI gli ID generati
// (inclusi quelli della tabella operation_file appena inseriti)
final updatedOperationData = await _supabase
.from('operation')
.select('''
*,
staff_member(name),
store(name),
provider(name),
model(name_with_brand),
customer(name),
attachment(*)
''')
.eq('id', newId)
.single();
// 2. ALLINEAMENTO BATCH SEMPRE ATTIVO!
if (operation.batchUuid.isNotEmpty) {
await _supabase
.from('operation')
.update({'note': operation.note}) // Spalmiamo la nota attuale
.eq(
'batch_uuid',
operation.batchUuid,
); // Su tutte le pratiche di questo scontrino
}
return OperationModel.fromMap(updatedOperationData);
return savedOperation;
} catch (e) {
// Qui potresti aggiungere una logica di "rollback manuale" se necessario
throw Exception('$e');
throw Exception("Errore nel salvataggio dell'operazione: $e");
}
}