123 lines
3.2 KiB
Dart
123 lines
3.2 KiB
Dart
import 'package:equatable/equatable.dart';
|
|
import 'package:flux/core/utils/extensions.dart';
|
|
import 'package:flux/features/attachments/models/attachment_model.dart';
|
|
|
|
class CustomerModel extends Equatable {
|
|
final String? id; // Bigint in SQL
|
|
final DateTime? createdAt;
|
|
final String name;
|
|
final String phoneNumber;
|
|
final String email;
|
|
final String note;
|
|
final DateTime? lastContactDate;
|
|
final bool doNotDisturb;
|
|
final String companyId; // UUID
|
|
final bool isActive;
|
|
final List<AttachmentModel> attachments;
|
|
|
|
const CustomerModel({
|
|
this.id,
|
|
this.createdAt,
|
|
required this.name,
|
|
required this.phoneNumber,
|
|
required this.email,
|
|
required this.note,
|
|
this.lastContactDate,
|
|
this.doNotDisturb = false,
|
|
required this.companyId,
|
|
this.isActive = true,
|
|
this.attachments = const [],
|
|
});
|
|
|
|
@override
|
|
List<Object?> get props => [
|
|
id,
|
|
createdAt,
|
|
name,
|
|
phoneNumber,
|
|
email,
|
|
note,
|
|
lastContactDate,
|
|
doNotDisturb,
|
|
companyId,
|
|
isActive,
|
|
attachments,
|
|
];
|
|
|
|
factory CustomerModel.empty() => CustomerModel(
|
|
name: '',
|
|
phoneNumber: '',
|
|
email: '',
|
|
note: '',
|
|
companyId:
|
|
'', // Dovrebbe essere sempre fornito, ma lasciamo vuoto per sicurezza
|
|
);
|
|
|
|
CustomerModel copyWith({
|
|
String? id,
|
|
DateTime? createdAt,
|
|
String? name,
|
|
String? phoneNumber,
|
|
String? email,
|
|
String? note,
|
|
DateTime? lastContactDate,
|
|
bool? doNotDisturb,
|
|
String? companyId,
|
|
bool? isActive,
|
|
List<AttachmentModel>? attachments,
|
|
}) {
|
|
return CustomerModel(
|
|
id: id ?? this.id,
|
|
createdAt: createdAt ?? this.createdAt,
|
|
name: name ?? this.name,
|
|
phoneNumber: phoneNumber ?? this.phoneNumber,
|
|
email: email ?? this.email,
|
|
note: note ?? this.note,
|
|
lastContactDate: lastContactDate ?? this.lastContactDate,
|
|
doNotDisturb: doNotDisturb ?? this.doNotDisturb,
|
|
companyId: companyId ?? this.companyId,
|
|
isActive: isActive ?? this.isActive,
|
|
attachments: attachments ?? this.attachments,
|
|
);
|
|
}
|
|
|
|
factory CustomerModel.fromMap(Map<String, dynamic> map) {
|
|
return CustomerModel(
|
|
id: map['id'] as String,
|
|
createdAt: map['created_at'] != null
|
|
? DateTime.parse(map['created_at'])
|
|
: null,
|
|
name: (map['name'] as String).myFormat(),
|
|
phoneNumber: map['phone_number'],
|
|
email: map['email'],
|
|
note: map['note'] ?? '',
|
|
lastContactDate: map['last_contact_date'] != null
|
|
? DateTime.parse(map['last_contact_date'])
|
|
: null,
|
|
doNotDisturb: map['do_not_disturb'] ?? false,
|
|
companyId: map['company_id'] as String,
|
|
isActive: map['is_active'] ?? true,
|
|
attachments:
|
|
(map['attachment'] as List?)
|
|
?.map((x) => AttachmentModel.fromMap(x))
|
|
.toList() ??
|
|
const [],
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
if (id != null) 'id': id,
|
|
'name': name.toLowerCase().trim(),
|
|
'phone_number': phoneNumber,
|
|
'email': email.toLowerCase().trim(),
|
|
'note': note,
|
|
if (lastContactDate != null)
|
|
'last_contact_date': lastContactDate!.toIso8601String(),
|
|
'do_not_disturb': doNotDisturb,
|
|
'company_id': companyId,
|
|
'is_active': isActive,
|
|
};
|
|
}
|
|
}
|