providers (#3)
Reviewed-on: http://catelliub.zapto.org:3000/brontomark/flux/pulls/3 Co-authored-by: Mark M2 Macbook <marco@catelli.it> Co-committed-by: Mark M2 Macbook <marco@catelli.it>
This commit is contained in:
@@ -40,14 +40,29 @@ class ProviderRepository {
|
||||
// Recupera tutti i provider di una company (per la lista generale)
|
||||
Future<List<ProviderModel>> fetchAllCompanyProviders(String companyId) async {
|
||||
try {
|
||||
// La magia è qui: selezioniamo tutto e chiediamo il conteggio (count)
|
||||
// della tabella pivot providers_in_stores
|
||||
final response = await _supabase
|
||||
.from('provider')
|
||||
.select()
|
||||
.eq('company_id', companyId);
|
||||
.select('''
|
||||
*,
|
||||
providers_in_stores(count)
|
||||
''')
|
||||
.eq('company_id', companyId)
|
||||
.order('nome');
|
||||
|
||||
return (response as List).map((m) => ProviderModel.fromMap(m)).toList();
|
||||
return (response as List).map((m) {
|
||||
// Estraiamo il conteggio dalla struttura restituita da Supabase
|
||||
// La risposta per ogni riga sarà tipo: { "id": "...", "providers_in_stores": [{"count": 5}] }
|
||||
final storesList = m['providers_in_stores'] as List?;
|
||||
final count = (storesList != null && storesList.isNotEmpty)
|
||||
? storesList[0]['count'] as int
|
||||
: 0;
|
||||
|
||||
return ProviderModel.fromMap(m).copyWith(storesCount: count);
|
||||
}).toList();
|
||||
} catch (e) {
|
||||
throw Exception('Errore fetch provider: $e');
|
||||
throw Exception('Errore fetch all providers: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,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');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user