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

@@ -1,14 +1,16 @@
import 'package:flux/features/master_data/providers/models/provider_model.dart';
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
import 'package:get_it/get_it.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import '../models/store_model.dart';
class StoreRepository {
final SupabaseClient _client = GetIt.I.get<SupabaseClient>();
final SupabaseClient _supabase = GetIt.I.get<SupabaseClient>();
/// Crea un nuovo negozio associato alla compagnia dell'utente
Future<void> createStore(StoreModel store) async {
try {
await _client.from('store').insert(store.toJson());
await _supabase.from('store').insert(store.toMap());
} on PostgrestException catch (e) {
// Intercettiamo errori specifici del database
throw e.message;
@@ -17,20 +19,119 @@ class StoreRepository {
}
}
/// Recupera tutti i negozi di una determinata compagnia
Future<List<StoreModel>> getStoresByCompany(String companyId) async {
Future<StoreModel> saveStore(StoreModel store) async {
try {
final response = await _client
final response = await _supabase
.from('store')
.upsert(store.toMap())
.select()
.eq('company_id', companyId)
.order('created_at');
return (response as List)
.map((json) => StoreModel.fromJson(json))
.toList();
.single();
return StoreModel.fromMap(response);
} on PostgrestException catch (e) {
throw e.message;
} catch (e) {
throw 'Errore nel recupero dei negozi';
throw 'Errore imprevisto durante il salvataggio del negozio: $e';
}
}
Future<void> syncStoreProviders(
String storeId,
List<ProviderModel> providers,
) async {
try {
// 1. Eliminiamo tutte le associazioni correnti per questo negozio
await _supabase
.from('providers_in_stores')
.delete()
.eq('store_id', storeId);
// 2. Se ci sono nuovi provider da associare, li inseriamo
if (providers.isNotEmpty) {
final inserts = providers
.map((p) => {'store_id': storeId, 'provider_id': p.id})
.toList();
await _supabase.from('providers_in_stores').insert(inserts);
}
} catch (e) {
throw 'Errore durante la sincronizzazione provider: $e';
}
}
Future<void> syncStoreStaff(
String storeId,
List<StaffMemberModel> staffMembers,
) async {
try {
// 1. Eliminiamo tutte le associazioni correnti per questo negozio
await _supabase.from('staff_in_stores').delete().eq('store_id', storeId);
// 2. Se ci sono nuovi dipendenti da associare, li inseriamo
if (staffMembers.isNotEmpty) {
final inserts = staffMembers
.map((s) => {'store_id': storeId, 'staff_id': s.id})
.toList();
await _supabase.from('staff_in_stores').insert(inserts);
}
} catch (e) {
throw 'Errore durante la sincronizzazione staff: $e';
}
}
/// Recupera tutti i negozi di una determinata compagnia
Future<List<StoreModel>> fetchAllCompanyStores(String companyId) async {
try {
final response = await _supabase
.from('store')
.select('''
*,
associated_providers:providers_in_stores (
provider (
*
)
)
associated_staff:staff_in_stores (
staff_member (
*
)
)
''')
.eq('company_id', companyId)
.order('nome');
return (response as List).map((m) => StoreModel.fromMap(m)).toList();
} catch (e) {
throw 'Errore nel recupero dei negozi: $e';
}
}
Future<void> associateStoreToProvider({
required String storeId,
required String providerId,
}) async {
try {
await _supabase.from('providers_in_stores').insert({
'store_id': storeId,
'provider_id': providerId,
});
} catch (e) {
throw 'Errore durante l\'associazione del negozio al provider: $e';
}
}
Future<void> removeStoreFromProvider({
required String storeId,
required String providerId,
}) async {
try {
await _supabase
.from('providers_in_stores')
.delete()
.eq('store_id', storeId)
.eq('provider_id', providerId);
} catch (e) {
throw 'Errore durante la rimozione del negozio dal provider: $e';
}
}
}