MIF_E31221222/sigap-mobile/lib/src/features/personalization/models/officers_model.dart

160 lines
4.3 KiB
Dart

import 'package:sigap/src/features/personalization/models/roles_model.dart';
class OfficerModel {
final String id;
final String unitId;
final String roleId;
final String? patrolUnitId;
final String nrp;
final String name;
final String? rank;
final String? position;
final String? phone;
final String? email;
final String? avatar;
final DateTime? validUntil;
final String? qrCode;
final DateTime? createdAt;
final DateTime? updatedAt;
final RoleModel? role;
OfficerModel({
required this.id,
required this.unitId,
required this.roleId,
this.patrolUnitId,
required this.nrp,
required this.name,
this.rank,
this.position,
this.phone,
this.email,
this.avatar,
this.validUntil,
this.qrCode,
this.createdAt,
this.updatedAt,
this.role,
});
// Create an OfficerModel instance from a JSON object
factory OfficerModel.fromJson(Map<String, dynamic> json) {
return OfficerModel(
id: json['id'] as String,
unitId: json['unit_id'] as String,
roleId: json['role_id'] as String,
patrolUnitId: json['patrol_unit_id'] as String?,
nrp: json['nrp'] as String,
name: json['name'] as String,
rank: json['rank'] as String?,
position: json['position'] as String?,
phone: json['phone'] as String?,
email: json['email'] as String?,
avatar: json['avatar'] as String?,
validUntil:
json['valid_until'] != null
? DateTime.parse(json['valid_until'] as String)
: null,
qrCode: json['qr_code'] as String?,
createdAt:
json['created_at'] != null
? DateTime.parse(json['created_at'] as String)
: null,
updatedAt:
json['updated_at'] != null
? DateTime.parse(json['updated_at'] as String)
: null,
role:
json['roles'] != null
? RoleModel.fromJson(json['roles'] as Map<String, dynamic>)
: null,
);
}
// Convert an OfficerModel instance to a JSON object
Map<String, dynamic> toJson() {
return {
'id': id,
'unit_id': unitId,
'role_id': roleId,
'patrol_unit_id': patrolUnitId,
'nrp': nrp,
'name': name,
'rank': rank,
'position': position,
'phone': phone,
'email': email,
'avatar': avatar,
'valid_until': validUntil?.toIso8601String(),
'qr_code': qrCode,
'created_at': createdAt?.toIso8601String(),
'updated_at': updatedAt?.toIso8601String(),
if (role != null) 'roles': role!.toJson(),
};
}
// Create a copy of the OfficerModel with updated fields
OfficerModel copyWith({
String? id,
String? unitId,
String? roleId,
String? patrolUnitId,
String? nrp,
String? name,
String? rank,
String? position,
String? phone,
String? email,
String? avatar,
DateTime? validUntil,
String? qrCode,
DateTime? createdAt,
DateTime? updatedAt,
RoleModel? role,
}) {
return OfficerModel(
id: id ?? this.id,
unitId: unitId ?? this.unitId,
roleId: roleId ?? this.roleId,
patrolUnitId: patrolUnitId ?? this.patrolUnitId,
nrp: nrp ?? this.nrp,
name: name ?? this.name,
rank: rank ?? this.rank,
position: position ?? this.position,
phone: phone ?? this.phone,
email: email ?? this.email,
avatar: avatar ?? this.avatar,
validUntil: validUntil ?? this.validUntil,
qrCode: qrCode ?? this.qrCode,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
role: role ?? this.role,
);
}
// Create an OfficerModel from a User metadata
factory OfficerModel.fromUserMetadata(
String userId,
Map<String, dynamic> metadata,
) {
final officerData = metadata['officer_data'] ?? {};
return OfficerModel(
id: userId,
unitId: officerData['unit_id'] ?? '',
roleId: '', // This would need to be fetched or defined elsewhere
nrp: officerData['nrp'] ?? '',
name: officerData['name'] ?? '',
rank: officerData['rank'],
position: officerData['position'],
phone: officerData['phone'],
email: metadata['email'],
);
}
@override
String toString() {
return 'OfficerModel(id: $id, name: $name, nrp: $nrp)';
}
}