Reviewed-on: http://catelliub.zapto.org:3000/brontomark/flux/pulls/2 Co-authored-by: Mark M2 Macbook <marco@catelli.it> Co-committed-by: Mark M2 Macbook <marco@catelli.it>
94 lines
2.8 KiB
Dart
94 lines
2.8 KiB
Dart
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
import '../models/provider_model.dart';
|
|
|
|
class ProviderRepository {
|
|
final _supabase = Supabase.instance.client;
|
|
|
|
// --- ASSOCIAZIONE PROVIDER <-> STORE ---
|
|
|
|
// Aggiunge un provider a un negozio (Attiva mandato)
|
|
Future<void> associateProviderToStore({
|
|
required String providerId,
|
|
required String storeId,
|
|
}) async {
|
|
try {
|
|
await _supabase.from('providers_in_stores').insert({
|
|
'provider_id': providerId,
|
|
'store_id': storeId,
|
|
});
|
|
} catch (e) {
|
|
throw Exception('Errore durante l\'associazione provider: $e');
|
|
}
|
|
}
|
|
|
|
// Rimuove un provider da un negozio (Disattiva mandato)
|
|
Future<void> disassociateProviderFromStore({
|
|
required String providerId,
|
|
required String storeId,
|
|
}) async {
|
|
try {
|
|
await _supabase
|
|
.from('providers_in_stores')
|
|
.delete()
|
|
.eq('provider_id', providerId)
|
|
.eq('store_id', storeId);
|
|
} catch (e) {
|
|
throw Exception('Errore durante la disassociazione provider: $e');
|
|
}
|
|
}
|
|
|
|
// Recupera tutti i provider di una company (per la lista generale)
|
|
Future<List<ProviderModel>> fetchAllCompanyProviders(String companyId) async {
|
|
try {
|
|
final response = await _supabase
|
|
.from('provider')
|
|
.select()
|
|
.eq('company_id', companyId);
|
|
|
|
return (response as List).map((m) => ProviderModel.fromMap(m)).toList();
|
|
} catch (e) {
|
|
throw Exception('Errore fetch provider: $e');
|
|
}
|
|
}
|
|
|
|
// Recupera gli ID dei provider associati a uno store (utile per le checkbox)
|
|
Future<List<String>> fetchAssociatedProviderIds(String storeId) async {
|
|
try {
|
|
final response = await _supabase
|
|
.from('providers_in_stores')
|
|
.select('provider_id')
|
|
.eq('store_id', storeId);
|
|
|
|
return (response as List)
|
|
.map((item) => item['provider_id'].toString())
|
|
.toList();
|
|
} catch (e) {
|
|
throw Exception('Errore recupero ID associati: $e');
|
|
}
|
|
}
|
|
|
|
// --- FUNZIONI STANDARD ---
|
|
|
|
// Questa la userai nel Form Servizi: carica solo i provider abilitati per lo store
|
|
Future<List<ProviderModel>> fetchActiveProvidersForStore(
|
|
String storeId,
|
|
) async {
|
|
try {
|
|
final response = await _supabase
|
|
.from('provider')
|
|
.select('*, providers_in_stores!inner(store_id)')
|
|
.eq('providers_in_stores.store_id', storeId)
|
|
.eq('is_active', true);
|
|
|
|
return (response as List).map((m) => ProviderModel.fromMap(m)).toList();
|
|
} catch (e) {
|
|
throw Exception('Errore fetch provider attivi: $e');
|
|
}
|
|
}
|
|
|
|
// Salva o aggiorna l'anagrafica del Provider
|
|
Future<void> saveProvider(ProviderModel provider) async {
|
|
await _supabase.from('provider').upsert(provider.toMap());
|
|
}
|
|
}
|