2026-05-30 12:12:14 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
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)
|
2026-05-30 12:12:14 +02:00
|
|
|
Future<List<StaffMemberModel>> getStaffMembers(
|
|
|
|
|
String companyId, {
|
|
|
|
|
String? storeId,
|
|
|
|
|
}) async {
|
|
|
|
|
try {
|
|
|
|
|
var filterBuilder = _supabase
|
|
|
|
|
.from(Tables.staffMembers)
|
|
|
|
|
.select('''
|
|
|
|
|
*,
|
|
|
|
|
store_assignments:${Tables.staffInStores} (
|
|
|
|
|
${Tables.stores}(*)
|
|
|
|
|
)
|
|
|
|
|
''')
|
|
|
|
|
.eq('company_id', companyId);
|
|
|
|
|
|
|
|
|
|
if (storeId != null) {
|
|
|
|
|
filterBuilder = filterBuilder.or(
|
|
|
|
|
'store_id.eq.$storeId,store_id.is.null',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var transformBuilder = filterBuilder.order('name', ascending: true);
|
2026-04-14 11:29:49 +02:00
|
|
|
|
2026-05-30 12:12:14 +02:00
|
|
|
final response = await transformBuilder;
|
|
|
|
|
|
|
|
|
|
return (response as List)
|
|
|
|
|
.map((s) => StaffMemberModel.fromMap(s))
|
|
|
|
|
.toList();
|
|
|
|
|
} on Exception catch (e) {
|
|
|
|
|
debugPrint('Errore nel recupero della lista di staff: $e');
|
|
|
|
|
throw Exception('Errore nel recupero della lista di staff: $e');
|
|
|
|
|
}
|
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)
|
2026-05-27 16:00:50 +02:00
|
|
|
.select('''
|
|
|
|
|
*,
|
|
|
|
|
store_assignments:${Tables.staffInStores} (
|
|
|
|
|
${Tables.stores}(*)
|
|
|
|
|
)
|
|
|
|
|
''')
|
2026-05-21 14:43:47 +02:00
|
|
|
.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-06-02 09:32:30 +02:00
|
|
|
Future<void> resetPassword(String email) async {
|
2026-04-29 09:20:17 +02:00
|
|
|
try {
|
2026-06-02 10:55:26 +02:00
|
|
|
final response = await Supabase.instance.client.functions.invoke(
|
|
|
|
|
'reset_password',
|
|
|
|
|
body: {'email': email.trim()},
|
2026-04-29 09:20:17 +02:00
|
|
|
);
|
2026-06-02 10:55:26 +02:00
|
|
|
|
|
|
|
|
if (response.status != 200) {
|
|
|
|
|
throw Exception(response.data['error'] ?? "Errore sconosciuto");
|
|
|
|
|
}
|
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-06-02 11:52:31 +02:00
|
|
|
await _supabase.from(Tables.staffInStores).upsert({
|
2026-04-14 11:29:49 +02:00
|
|
|
'staff_member_id': staffId,
|
|
|
|
|
'store_id': storeId,
|
2026-06-02 11:52:31 +02:00
|
|
|
}, onConflict: 'staff_member_id,store_id'); // Evita duplicati
|
2026-04-14 11:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
}
|