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

@@ -1,4 +1,6 @@
import 'package:equatable/equatable.dart';
import 'package:flux/features/master_data/providers/models/provider_model.dart';
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
class StoreModel extends Equatable {
final String? id;
@@ -11,9 +13,9 @@ class StoreModel extends Equatable {
final String cap;
final String comune;
final String provincia;
final int providersCount; // Numero di provider associati, utile per la lista
final int
staffMembersCount; // Numero di membri dello staff associati, utile per la lista
final List<ProviderModel> associatedProviders; // Provider associati
final List<StaffMemberModel>
associatedStaffMembers; // Membri dello staff associati
const StoreModel({
this.id,
@@ -26,8 +28,8 @@ class StoreModel extends Equatable {
required this.cap,
required this.comune,
required this.provincia,
this.providersCount = 0, // Default a 0 se non specificato
this.staffMembersCount = 0, // Default a 0 se non specificato
this.associatedProviders = const [],
this.associatedStaffMembers = const [],
});
// Fondamentale per Equatable: definisce quali proprietà determinano l'uguaglianza
@@ -43,8 +45,8 @@ class StoreModel extends Equatable {
cap,
comune,
provincia,
providersCount,
staffMembersCount,
associatedProviders,
associatedStaffMembers,
];
// Il mitico copyWith per creare nuove istanze modificando solo ciò che serve
@@ -59,8 +61,8 @@ class StoreModel extends Equatable {
String? cap,
String? comune,
String? provincia,
int? providersCount,
int? staffMembersCount,
List<ProviderModel>? associatedProviders,
List<StaffMemberModel>? associatedStaffMembers,
}) {
return StoreModel(
id: id ?? this.id,
@@ -73,12 +75,36 @@ class StoreModel extends Equatable {
cap: cap ?? this.cap,
comune: comune ?? this.comune,
provincia: provincia ?? this.provincia,
providersCount: providersCount ?? this.providersCount,
staffMembersCount: staffMembersCount ?? this.staffMembersCount,
associatedProviders: associatedProviders ?? this.associatedProviders,
associatedStaffMembers:
associatedStaffMembers ?? this.associatedStaffMembers,
);
}
factory StoreModel.fromMap(Map<String, dynamic> map) {
final providersPivotList = map['associated_providers'] as List?;
List<ProviderModel> providers = [];
if (providersPivotList != null) {
providers = providersPivotList
.where((item) => item['provider'] != null) // Sicurezza
.map(
(item) =>
ProviderModel.fromMap(item['provider'] as Map<String, dynamic>),
)
.toList();
}
final staffPivotList = map['associated_staff'] as List?;
List<StaffMemberModel> staffMembers = [];
if (staffPivotList != null) {
staffMembers = staffPivotList
.where((item) => item['staff_member'] != null) // Sicurezza
.map(
(item) => StaffMemberModel.fromMap(
item['staff_member'] as Map<String, dynamic>,
),
)
.toList();
}
return StoreModel(
id: map['id'] as String,
nome: map['nome'],
@@ -92,15 +118,8 @@ class StoreModel extends Equatable {
cap: map['cap'],
comune: map['comune'],
provincia: map['provincia'],
providersCount:
map['providers_count'] != null && map['providers_count'].isNotEmpty
? map['providers_count'][0]['count'] as int
: 0,
staffMembersCount:
map['staff_members_count'] != null &&
map['staff_members_count'].isNotEmpty
? map['staff_members_count'][0]['count'] as int
: 0,
associatedProviders: providers,
associatedStaffMembers: staffMembers,
);
}