2026-04-13 16:47:23 +02:00
|
|
|
import 'package:flux/features/staff/models/staff_member_model.dart';
|
2026-04-09 11:30:57 +02:00
|
|
|
import 'package:get_it/get_it.dart';
|
|
|
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
|
|
|
import '../models/store_model.dart';
|
|
|
|
|
|
|
|
|
|
class StoreRepository {
|
2026-04-13 16:47:23 +02:00
|
|
|
final SupabaseClient _supabase = GetIt.I.get<SupabaseClient>();
|
2026-04-09 11:30:57 +02:00
|
|
|
|
|
|
|
|
/// Crea un nuovo negozio associato alla compagnia dell'utente
|
2026-04-13 16:47:23 +02:00
|
|
|
Future<StoreModel> createStore(StoreModel store) async {
|
2026-04-09 11:30:57 +02:00
|
|
|
try {
|
2026-04-13 16:47:23 +02:00
|
|
|
final response = await _supabase
|
|
|
|
|
.from('store')
|
|
|
|
|
.upsert(store.toJson())
|
|
|
|
|
.select()
|
|
|
|
|
.single();
|
|
|
|
|
|
|
|
|
|
return StoreModel.fromJson(response);
|
2026-04-09 11:30:57 +02:00
|
|
|
} on PostgrestException catch (e) {
|
|
|
|
|
// Intercettiamo errori specifici del database
|
|
|
|
|
throw e.message;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw 'Errore imprevisto durante la creazione del negozio: $e';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Recupera tutti i negozi di una determinata compagnia
|
|
|
|
|
Future<List<StoreModel>> getStoresByCompany(String companyId) async {
|
|
|
|
|
try {
|
2026-04-13 16:47:23 +02:00
|
|
|
final response = await _supabase
|
2026-04-09 11:30:57 +02:00
|
|
|
.from('store')
|
|
|
|
|
.select()
|
|
|
|
|
.eq('company_id', companyId)
|
|
|
|
|
.order('created_at');
|
|
|
|
|
|
|
|
|
|
return (response as List)
|
|
|
|
|
.map((json) => StoreModel.fromJson(json))
|
|
|
|
|
.toList();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw 'Errore nel recupero dei negozi';
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-13 16:47:23 +02:00
|
|
|
|
2026-04-13 18:26:17 +02:00
|
|
|
Future<void> updateStoreStatus(String id, bool isActive) async {
|
|
|
|
|
await _supabase.from('store').update({'is_active': isActive}).eq('id', id);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 16:47:23 +02:00
|
|
|
// --- LOGICA DI GIUNZIONE (Staff <-> Store) ---
|
|
|
|
|
|
|
|
|
|
// Recupera i membri assegnati a uno specifico negozio
|
|
|
|
|
// Qui facciamo una JOIN per avere i dati del membro partendo dalla tabella di giunzione
|
|
|
|
|
Future<List<StaffMemberModel>> getStaffMembersInStore(String storeId) async {
|
|
|
|
|
final response = await _supabase
|
|
|
|
|
.from('staff_in_stores')
|
|
|
|
|
.select(
|
|
|
|
|
'staff_member (*)',
|
|
|
|
|
) // Prende tutti i campi della tabella staff_member collegata
|
|
|
|
|
.eq('store_id', storeId);
|
|
|
|
|
|
|
|
|
|
return (response as List)
|
|
|
|
|
.map((item) => StaffMemberModel.fromJson(item['staff_member']))
|
|
|
|
|
.toList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Recupera i negozi associati ad un specifico membro
|
|
|
|
|
// Qui facciamo una JOIN per avere i dati del membro partendo dalla tabella di giunzione
|
|
|
|
|
Future<List<StoreModel>> getStoresWithStaffMember(
|
|
|
|
|
String staffMemberId,
|
|
|
|
|
) async {
|
|
|
|
|
final response = await _supabase
|
|
|
|
|
.from('staff_in_stores')
|
|
|
|
|
.select(
|
|
|
|
|
'store (*)',
|
|
|
|
|
) // Prende tutti i campi della tabella store collegata
|
|
|
|
|
.eq('staff_member_id', staffMemberId);
|
|
|
|
|
|
|
|
|
|
return (response as List)
|
|
|
|
|
.map((item) => StoreModel.fromJson(item['store']))
|
|
|
|
|
.toList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Assegna un negozio a un membro
|
|
|
|
|
Future<void> assignToMember(String staffId, String storeId) async {
|
|
|
|
|
await _supabase.from('staff_in_stores').insert({
|
|
|
|
|
'staff_member_id': staffId,
|
|
|
|
|
'store_id': storeId,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rimuove l'assegnazione
|
|
|
|
|
Future<void> removeFromStore(String staffId, String storeId) async {
|
|
|
|
|
await _supabase
|
|
|
|
|
.from('staff_in_stores')
|
|
|
|
|
.delete()
|
|
|
|
|
.eq('staff_member_id', staffId)
|
|
|
|
|
.eq('store_id', storeId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Utility per pulire le assegnazioni esistenti prima di riscriverle
|
|
|
|
|
Future<void> clearStoreAssignments(String staffId) async {
|
|
|
|
|
await _supabase
|
|
|
|
|
.from('staff_in_stores')
|
|
|
|
|
.delete()
|
|
|
|
|
.eq('staff_member_id', staffId);
|
|
|
|
|
}
|
2026-04-09 11:30:57 +02:00
|
|
|
}
|