Add Providers Management to Master Data Hub and Enhance Provider Model

This commit is contained in:
2026-04-16 19:25:42 +02:00
parent c7eedba5ac
commit aa18b7dd1f
5 changed files with 188 additions and 46 deletions

View File

@@ -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');
}
}