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>
138 lines
4.0 KiB
Dart
138 lines
4.0 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('''
|
|
*,
|
|
associated_stores:providers_in_stores (
|
|
store (
|
|
*
|
|
)
|
|
)
|
|
''')
|
|
.eq('company_id', companyId)
|
|
.order('nome');
|
|
|
|
return (response as List).map((m) => ProviderModel.fromMap(m)).toList();
|
|
} catch (e) {
|
|
throw 'Errore fetch providers: $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<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');
|
|
}
|
|
}
|
|
}
|