started staff
This commit is contained in:
58
lib/features/staff/data/staff_repository.dart
Normal file
58
lib/features/staff/data/staff_repository.dart
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
import 'package:flux/features/staff/models/staff_member_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
|
||||||
|
.from('staff_member')
|
||||||
|
.select()
|
||||||
|
.eq('company_id', companyId)
|
||||||
|
.order('name', ascending: true);
|
||||||
|
|
||||||
|
return (response as List).map((s) => StaffMemberModel.fromJson(s)).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> saveStaffMember(StaffMemberModel member) async {
|
||||||
|
await _supabase.from('staff_member').upsert(member.toJson());
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assegna un membro a un negozio
|
||||||
|
Future<void> assignToStore(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
47
lib/features/staff/models/staff_member_model.dart
Normal file
47
lib/features/staff/models/staff_member_model.dart
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
import 'package:flux/core/utils/string_extensions.dart'; // Assicurati che il percorso sia corretto
|
||||||
|
|
||||||
|
class StaffMemberModel extends Equatable {
|
||||||
|
final String? id;
|
||||||
|
final String name;
|
||||||
|
final String email;
|
||||||
|
final String phone;
|
||||||
|
final bool isActive;
|
||||||
|
final String companyId;
|
||||||
|
|
||||||
|
const StaffMemberModel({
|
||||||
|
this.id,
|
||||||
|
required this.name,
|
||||||
|
this.email = '',
|
||||||
|
this.phone = '',
|
||||||
|
this.isActive = true,
|
||||||
|
required this.companyId,
|
||||||
|
});
|
||||||
|
|
||||||
|
factory StaffMemberModel.fromJson(Map<String, dynamic> json) {
|
||||||
|
return StaffMemberModel(
|
||||||
|
id: json['id'],
|
||||||
|
// Applichiamo il tuo myFormat per visualizzare i nomi correttamente
|
||||||
|
name: (json['name'] as String).myFormat(),
|
||||||
|
// L'email la teniamo lowercase per standard tecnico
|
||||||
|
email: (json['email'] as String? ?? '').toLowerCase().trim(),
|
||||||
|
phone: (json['phone'] as String? ?? '').trim(),
|
||||||
|
isActive: json['is_active'] ?? true,
|
||||||
|
companyId: json['company_id'],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() {
|
||||||
|
return {
|
||||||
|
if (id != null) 'id': id,
|
||||||
|
'name': name.toLowerCase().trim(), // Salviamo pulito per le query
|
||||||
|
'email': email.toLowerCase().trim(),
|
||||||
|
'phone': phone.trim(),
|
||||||
|
'is_active': isActive,
|
||||||
|
'company_id': companyId,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [id, name, email, phone, isActive, companyId];
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user