Refactor Staff and Store models to use fromMap method; enhance StoreCubit with provider management functionality

This commit is contained in:
2026-04-17 12:40:58 +02:00
parent 22a4f1dac4
commit 08a521c21c
6 changed files with 176 additions and 38 deletions

View File

@@ -16,17 +16,17 @@ class StaffRepository {
.eq('company_id', companyId)
.order('name', ascending: true);
return (response as List).map((s) => StaffMemberModel.fromJson(s)).toList();
return (response as List).map((s) => StaffMemberModel.fromMap(s)).toList();
}
Future<StaffMemberModel> saveStaffMember(StaffMemberModel member) async {
final response = await _supabase
.from('staff_member')
.upsert(member.toJson())
.upsert(member.toMap())
.select()
.single();
return StaffMemberModel.fromJson(response);
return StaffMemberModel.fromMap(response);
}
// --- LOGICA DI GIUNZIONE (Staff <-> Store) ---
@@ -42,7 +42,7 @@ class StaffRepository {
.eq('store_id', storeId);
return (response as List)
.map((item) => StaffMemberModel.fromJson(item['staff_member']))
.map((item) => StaffMemberModel.fromMap(item['staff_member']))
.toList();
}

View File

@@ -18,20 +18,20 @@ class StaffMemberModel extends Equatable {
required this.companyId,
});
factory StaffMemberModel.fromJson(Map<String, dynamic> json) {
factory StaffMemberModel.fromMap(Map<String, dynamic> map) {
return StaffMemberModel(
id: json['id'],
id: map['id'],
// Applichiamo il tuo myFormat per visualizzare i nomi correttamente
name: (json['name'] as String).myFormat(),
name: (map['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'],
email: (map['email'] as String? ?? '').toLowerCase().trim(),
phone: (map['phone'] as String? ?? '').trim(),
isActive: map['is_active'] ?? true,
companyId: map['company_id'],
);
}
Map<String, dynamic> toJson() {
Map<String, dynamic> toMap() {
return {
if (id != null) 'id': id,
'name': name.toLowerCase().trim(), // Salviamo pulito per le query