Enhance Provider Management: Update saveProvider method to handle store associations and add temporary store selection in ProviderFormSheet

This commit is contained in:
2026-04-17 10:12:39 +02:00
parent aa18b7dd1f
commit 11248b5bf3
4 changed files with 125 additions and 14 deletions

View File

@@ -102,7 +102,43 @@ class ProviderRepository {
}
// Salva o aggiorna l'anagrafica del Provider
Future<void> saveProvider(ProviderModel provider) async {
await _supabase.from('provider').upsert(provider.toMap());
Future<ProviderModel> saveProvider(ProviderModel provider) async {
try {
// .select().single() è fondamentale per farsi restituire
// l'oggetto appena creato/aggiornato con l'ID
final response = await _supabase
.from('provider')
.upsert(provider.toMap())
.select()
.single();
return ProviderModel.fromMap(response); // <--- DEVE ESSERCI IL RETURN
} catch (e) {
rethrow; // <--- Rilancia l'errore al Cubit, non ritornare null!
}
}
Future<void> syncProviderStores(
String providerId,
List<String> storeIds,
) async {
try {
// 1. Eliminiamo tutte le associazioni correnti per questo provider
await _supabase
.from('providers_in_stores')
.delete()
.eq('provider_id', providerId);
// 2. Se ci sono nuovi store da associare, li inseriamo
if (storeIds.isNotEmpty) {
final inserts = storeIds
.map((sId) => {'provider_id': providerId, 'store_id': sId})
.toList();
await _supabase.from('providers_in_stores').insert(inserts);
}
} catch (e) {
throw Exception('Errore durante la sincronizzazione store: $e');
}
}
}