providers-in-store (#4)

Reviewed-on: http://catelliub.zapto.org:3000/brontomark/flux/pulls/4
Co-authored-by: mark-cachy <marco@catelli.it>
Co-committed-by: mark-cachy <marco@catelli.it>
This commit is contained in:
2026-04-17 15:37:14 +02:00
committed by brontomark
parent c1b6c9e7ac
commit 667bbf6404
14 changed files with 688 additions and 335 deletions

View File

@@ -40,29 +40,22 @@ 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('''
*,
providers_in_stores(count)
associated_stores:providers_in_stores (
store (
*
)
)
''')
.eq('company_id', companyId)
.order('nome');
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();
return (response as List).map((m) => ProviderModel.fromMap(m)).toList();
} catch (e) {
throw Exception('Errore fetch all providers: $e');
throw 'Errore fetch providers: $e';
}
}