Files
flux/lib/features/master_data/providers/models/provider_model.dart
Mark M2 Macbook 6bb65e8296 refactor
Co-authored-by: Copilot <copilot@github.com>
2026-05-03 12:05:47 +02:00

135 lines
3.5 KiB
Dart

import 'package:equatable/equatable.dart';
import 'package:flux/features/master_data/store/models/store_model.dart';
class ProviderModel extends Equatable {
final String? id;
final String name;
final bool landline;
final bool mobile;
final bool energy;
final bool insurance;
final bool entertainment;
final bool financing;
final bool telepass;
final bool other;
final bool isActive;
final String companyId;
final List<StoreModel> associatedStores;
const ProviderModel({
this.id,
required this.name,
required this.landline,
required this.mobile,
required this.energy,
required this.insurance,
required this.entertainment,
required this.financing,
required this.telepass,
required this.other,
required this.isActive,
required this.companyId,
this.associatedStores = const [],
});
factory ProviderModel.fromMap(Map<String, dynamic> map) {
// Estraiamo la lista dalla pivot e poi prendiamo l'oggetto 'store' annidato
final pivotList = map['associated_stores'] as List?;
List<StoreModel> stores = [];
if (pivotList != null) {
stores = pivotList
.where((item) => item['store'] != null) // Sicurezza
.map(
(item) => StoreModel.fromMap(item['store'] as Map<String, dynamic>),
)
.toList();
}
return ProviderModel(
id: map['id'],
name: map['name'],
landline: map['landline'] ?? false,
mobile: map['mobile'] ?? false,
energy: map['energy'] ?? false,
insurance: map['insurance'] ?? false,
entertainment: map['entertainment'] ?? false,
financing: map['financing'] ?? false,
telepass: map['telepass'] ?? false,
other: map['other'] ?? false,
isActive: map['is_active'] ?? true,
companyId: map['company_id'],
associatedStores: stores,
);
}
Map<String, dynamic> toMap() {
final map = {
'name': name,
'landline': landline,
'mobile': mobile,
'energy': energy,
'insurance': insurance,
'entertainment': entertainment,
'financing': financing,
'telepass': telepass,
'other': other,
'is_active': isActive,
'company_id': companyId,
};
// AGGIUNGIAMO L'ID SOLO SE NON È NULLO
// Senza questo, l'upsert non sa dove andare a parare
if (id != null) {
map['id'] = id!;
}
return map;
}
@override
List<Object?> get props => [
id,
name,
landline,
mobile,
energy,
insurance,
entertainment,
financing,
telepass,
other,
isActive,
companyId,
associatedStores,
];
ProviderModel copyWith({
String? id,
String? name,
bool? landline,
bool? mobile,
bool? energy,
bool? insurance,
bool? entertainment,
bool? financing,
bool? telepass,
bool? other,
bool? isActive,
String? companyId,
List<StoreModel>? associatedStores,
}) {
return ProviderModel(
id: id ?? this.id,
name: name ?? this.name,
landline: landline ?? this.landline,
mobile: mobile ?? this.mobile,
energy: energy ?? this.energy,
insurance: insurance ?? this.insurance,
entertainment: entertainment ?? this.entertainment,
financing: financing ?? this.financing,
telepass: telepass ?? this.telepass,
other: other ?? this.other,
isActive: isActive ?? this.isActive,
companyId: companyId ?? this.companyId,
associatedStores: associatedStores ?? this.associatedStores,
);
}
}