99 lines
2.6 KiB
Dart
99 lines
2.6 KiB
Dart
DateTime? _parseDateTime(dynamic value) {
|
|
if (value == null) return null;
|
|
final s = value.toString().trim();
|
|
if (s.isEmpty) return null;
|
|
try {
|
|
return DateTime.parse(s);
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
class User {
|
|
final String id;
|
|
final String mobileNumber;
|
|
final String fullName;
|
|
final String role; // 'manager' or 'member'
|
|
final String? createdBy; // Manager who created this account
|
|
final bool isActive;
|
|
final String? email;
|
|
final String? address;
|
|
final String? emergencyContact;
|
|
final DateTime createdAt;
|
|
final DateTime updatedAt;
|
|
|
|
User({
|
|
required this.id,
|
|
required this.mobileNumber,
|
|
required this.fullName,
|
|
required this.role,
|
|
this.createdBy,
|
|
required this.isActive,
|
|
this.email,
|
|
this.address,
|
|
this.emergencyContact,
|
|
required this.createdAt,
|
|
required this.updatedAt,
|
|
});
|
|
|
|
factory User.fromJson(Map<String, dynamic> json) {
|
|
return User(
|
|
id: json['id'] ?? '',
|
|
mobileNumber: json['mobile_number'] ?? '',
|
|
fullName: json['full_name'] ?? '',
|
|
role: json['role'] ?? 'member',
|
|
createdBy: json['created_by'],
|
|
isActive: json['is_active'] ?? true,
|
|
email: json['email'],
|
|
address: json['address'],
|
|
emergencyContact: json['emergency_contact'],
|
|
createdAt: _parseDateTime(json['created_at']) ?? DateTime.now(),
|
|
updatedAt: _parseDateTime(json['updated_at']) ?? DateTime.now(),
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
'id': id,
|
|
'mobile_number': mobileNumber,
|
|
'full_name': fullName,
|
|
'role': role,
|
|
'created_by': createdBy,
|
|
'is_active': isActive,
|
|
if (email != null) 'email': email,
|
|
if (address != null) 'address': address,
|
|
if (emergencyContact != null) 'emergency_contact': emergencyContact,
|
|
'created_at': createdAt.toIso8601String(),
|
|
'updated_at': updatedAt.toIso8601String(),
|
|
};
|
|
}
|
|
|
|
User copyWith({
|
|
String? id,
|
|
String? mobileNumber,
|
|
String? fullName,
|
|
String? role,
|
|
String? createdBy,
|
|
bool? isActive,
|
|
String? email,
|
|
String? address,
|
|
String? emergencyContact,
|
|
DateTime? createdAt,
|
|
DateTime? updatedAt,
|
|
}) {
|
|
return User(
|
|
id: id ?? this.id,
|
|
mobileNumber: mobileNumber ?? this.mobileNumber,
|
|
fullName: fullName ?? this.fullName,
|
|
role: role ?? this.role,
|
|
createdBy: createdBy ?? this.createdBy,
|
|
isActive: isActive ?? this.isActive,
|
|
email: email ?? this.email,
|
|
address: address ?? this.address,
|
|
emergencyContact: emergencyContact ?? this.emergencyContact,
|
|
createdAt: createdAt ?? this.createdAt,
|
|
updatedAt: updatedAt ?? this.updatedAt,
|
|
);
|
|
}
|
|
}
|