2026-05-20 11:03:33 +02:00
|
|
|
import 'package:flux/core/enums_and_consts/consts.dart';
|
2026-04-14 11:29:49 +02:00
|
|
|
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
|
|
|
|
|
import 'package:flux/features/master_data/store/models/store_model.dart';
|
|
|
|
|
import 'package:get_it/get_it.dart';
|
|
|
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
|
|
|
|
|
|
|
|
class StaffRepository {
|
|
|
|
|
final SupabaseClient _supabase = GetIt.I.get<SupabaseClient>();
|
|
|
|
|
|
|
|
|
|
// --- ANAGRAFICA PURA ---
|
|
|
|
|
|
|
|
|
|
// Prende tutto lo staff della Company (per l'Hub Anagrafiche)
|
|
|
|
|
Future<List<StaffMemberModel>> getStaffMembers(String companyId) async {
|
|
|
|
|
final response = await _supabase
|
2026-05-20 11:03:33 +02:00
|
|
|
.from(Tables.staffMembers)
|
2026-05-26 19:31:25 +02:00
|
|
|
.select('''
|
|
|
|
|
*,
|
|
|
|
|
stores (*)
|
|
|
|
|
''')
|
2026-04-14 11:29:49 +02:00
|
|
|
.eq('company_id', companyId)
|
|
|
|
|
.order('name', ascending: true);
|
|
|
|
|
|
2026-04-17 15:37:14 +02:00
|
|
|
return (response as List).map((s) => StaffMemberModel.fromMap(s)).toList();
|
2026-04-14 11:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-21 14:43:47 +02:00
|
|
|
Future<StaffMemberModel?> getStaffMemberById(String staffId) async {
|
|
|
|
|
try {
|
|
|
|
|
final response = await _supabase
|
|
|
|
|
.from(Tables.staffMembers)
|
|
|
|
|
.select()
|
|
|
|
|
.eq('id', staffId)
|
|
|
|
|
.single();
|
|
|
|
|
return StaffMemberModel.fromMap(response);
|
|
|
|
|
} on Exception catch (e) {
|
|
|
|
|
throw ('Errore nel recupero del membro staff con ID $staffId: $e');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 11:29:49 +02:00
|
|
|
Future<StaffMemberModel> saveStaffMember(StaffMemberModel member) async {
|
|
|
|
|
final response = await _supabase
|
2026-05-20 11:03:33 +02:00
|
|
|
.from(Tables.staffMembers)
|
2026-04-17 15:37:14 +02:00
|
|
|
.upsert(member.toMap())
|
2026-04-14 11:29:49 +02:00
|
|
|
.select()
|
|
|
|
|
.single();
|
|
|
|
|
|
2026-04-17 15:37:14 +02:00
|
|
|
return StaffMemberModel.fromMap(response);
|
2026-04-14 11:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-26 16:29:31 +02:00
|
|
|
// --- LOGICA DI INVITO (Tramite Edge Function) ---
|
|
|
|
|
|
|
|
|
|
Future<String> inviteStaffMember(StaffMemberModel newMember) async {
|
|
|
|
|
if (newMember.email == null || newMember.email!.isEmpty) {
|
|
|
|
|
throw Exception(
|
|
|
|
|
"L'indirizzo email è obbligatorio per invitare un collega.",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
final response = await _supabase.functions.invoke(
|
|
|
|
|
'invite_staff',
|
|
|
|
|
body: newMember.toMap(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (response.status != 200) {
|
|
|
|
|
throw Exception("Errore dal server: ${response.data}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// La funzione ci restituisce l'ID fresco di database!
|
|
|
|
|
final responseData = response.data as Map<String, dynamic>;
|
|
|
|
|
return responseData['user_id'] as String;
|
|
|
|
|
} on FunctionException catch (e) {
|
|
|
|
|
throw Exception(
|
|
|
|
|
"Errore di comunicazione con il server: ${e.reasonPhrase}",
|
|
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw Exception("Impossibile invitare il collega: $e");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-29 09:20:17 +02:00
|
|
|
Future<void> resetPasswordOrResendInviteLink(String email) async {
|
|
|
|
|
try {
|
|
|
|
|
await _supabase.auth.resetPasswordForEmail(
|
|
|
|
|
email,
|
2026-05-20 11:03:33 +02:00
|
|
|
redirectTo: resetPasswordUrl,
|
2026-04-29 09:20:17 +02:00
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw Exception("Errore nell'invio del link: $e");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 11:29:49 +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
|
2026-05-20 11:03:33 +02:00
|
|
|
.from(Tables.staffInStores)
|
2026-04-14 11:29:49 +02:00
|
|
|
.select(
|
2026-05-20 11:03:33 +02:00
|
|
|
'${Tables.staffMembers} (*)',
|
2026-04-14 11:29:49 +02:00
|
|
|
) // Prende tutti i campi della tabella staff_member collegata
|
|
|
|
|
.eq('store_id', storeId);
|
|
|
|
|
|
|
|
|
|
return (response as List)
|
2026-05-20 11:03:33 +02:00
|
|
|
.map((item) => StaffMemberModel.fromMap(item[Tables.staffMembers]))
|
2026-04-14 11:29:49 +02:00
|
|
|
.toList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Recupera i negozi assegnati a uno specifico membro
|
|
|
|
|
// Qui facciamo una JOIN per avere i dati del membro partendo dalla tabella di giunzione
|
|
|
|
|
Future<List<StoreModel>> getStaffMemberStore(String staffId) async {
|
|
|
|
|
final response = await _supabase
|
2026-05-20 11:03:33 +02:00
|
|
|
.from(Tables.staffInStores)
|
2026-04-14 11:29:49 +02:00
|
|
|
.select(
|
2026-05-20 11:03:33 +02:00
|
|
|
'${Tables.stores} (*)',
|
2026-04-14 11:29:49 +02:00
|
|
|
) // Prende tutti i campi della tabella store collegata
|
|
|
|
|
.eq('staff_member_id', staffId);
|
|
|
|
|
|
|
|
|
|
return (response as List)
|
2026-05-20 11:03:33 +02:00
|
|
|
.map((item) => StoreModel.fromMap(item[Tables.stores]))
|
2026-04-14 11:29:49 +02:00
|
|
|
.toList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Assegna un membro a un negozio
|
2026-04-26 16:29:31 +02:00
|
|
|
Future<void> assignStaffToStore(String staffId, String storeId) async {
|
2026-05-20 11:03:33 +02:00
|
|
|
await _supabase.from(Tables.staffInStores).insert({
|
2026-04-14 11:29:49 +02:00
|
|
|
'staff_member_id': staffId,
|
|
|
|
|
'store_id': storeId,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rimuove l'assegnazione
|
2026-04-26 16:29:31 +02:00
|
|
|
Future<void> removeStaffFromStore(String staffId, String storeId) async {
|
2026-04-14 11:29:49 +02:00
|
|
|
await _supabase
|
2026-05-20 11:03:33 +02:00
|
|
|
.from(Tables.staffInStores)
|
2026-04-14 11:29:49 +02:00
|
|
|
.delete()
|
|
|
|
|
.eq('staff_member_id', staffId)
|
|
|
|
|
.eq('store_id', storeId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Nel StaffRepository
|
|
|
|
|
|
|
|
|
|
// Utility per pulire le assegnazioni esistenti prima di riscriverle
|
|
|
|
|
Future<void> clearStoreAssignments(String staffId) async {
|
|
|
|
|
await _supabase
|
2026-05-20 11:03:33 +02:00
|
|
|
.from(Tables.staffInStores)
|
2026-04-14 11:29:49 +02:00
|
|
|
.delete()
|
|
|
|
|
.eq('staff_member_id', staffId);
|
|
|
|
|
}
|
|
|
|
|
}
|