You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudsolutions-atoms/lib/models/user.dart

109 lines
3.3 KiB
Dart

3 years ago
import 'package:test_sa/controllers/notification/firebase_notification_manger.dart';
import 'package:test_sa/models/department.dart';
import 'package:test_sa/models/enums/user_types.dart';
import 'package:test_sa/models/hospital.dart';
3 years ago
class User {
3 years ago
String id;
String userName;
String password;
String email;
Hospital hospital;
Department department;
UsersTypes type;
String phoneNumber;
String whatsApp;
String token;
bool isActive;
3 years ago
DateTime tokenLife;
3 years ago
3 years ago
User(
{this.id,
this.userName = "",
this.email = "",
this.password = "",
this.phoneNumber = "",
this.hospital,
this.department,
this.type,
this.whatsApp,
this.token,
this.tokenLife,
this.isActive = false});
3 years ago
Future<Map<String, dynamic>> toLoginJson() async {
3 years ago
if (FirebaseNotificationManger.token == null) await FirebaseNotificationManger.getToken();
3 years ago
return {
3 years ago
"username": userName,
"password": password,
3 years ago
"firebase_token": FirebaseNotificationManger?.token ?? "",
};
}
3 years ago
Map<String, dynamic> toUpdateProfileJson() {
Map<String, dynamic> jsonObject = {};
if (department?.id != null) jsonObject["department"] = department.id;
if (whatsApp != null && whatsApp.isNotEmpty) jsonObject["whatsapp"] = whatsApp;
if (phoneNumber != null && phoneNumber.isNotEmpty) jsonObject["phone"] = phoneNumber;
3 years ago
return jsonObject;
}
Future<Map<String, dynamic>> toRegisterJson() async {
3 years ago
if (FirebaseNotificationManger.token == null) await FirebaseNotificationManger.getToken();
3 years ago
return {
"username": userName,
3 years ago
"email": email,
"whatsapp": whatsApp,
"client": hospital.id,
"department": department?.id,
"phone": phoneNumber,
"pass": password,
3 years ago
"firebase_token": FirebaseNotificationManger?.token ?? "",
};
}
3 years ago
Map<String, dynamic> toJson() {
3 years ago
return {
3 years ago
"userID": id,
3 years ago
"username": userName,
3 years ago
"email": email,
"token": token,
"phoneNumber": phoneNumber,
"whatsapp": whatsApp,
"client_id": hospital?.id,
"client_name": hospital?.name,
"department_id": department?.id,
"department_name": department?.name,
3 years ago
//"password":password,
3 years ago
"tokenlife": tokenLife.toIso8601String(),
"active": isActive,
"userRoles": type == UsersTypes.engineer ? "value: R-6" : "value: R-5",
// "token":token, pass is token
3 years ago
};
}
3 years ago
factory User.fromJson(Map<String, dynamic> parsedJson) {
3 years ago
UsersTypes type;
3 years ago
if (parsedJson["userRoles"].toString().contains("value: R-4") || parsedJson["userRoles"].toString().contains("value: R-5") || parsedJson["userRoles"].toString().contains("value: R-7")) {
3 years ago
type = UsersTypes.normal_user;
} else {
type = UsersTypes.engineer;
3 years ago
}
return User(
3 years ago
id: parsedJson["userID"],
userName: parsedJson["username"],
email: parsedJson["email"],
hospital: Hospital(id: parsedJson["client_id"], name: parsedJson["client_name"]),
department: Department(
id: parsedJson["department_id"],
name: parsedJson["department_name"],
),
phoneNumber: parsedJson["phoneNumber"],
whatsApp: parsedJson["phoneNumber"],
token: parsedJson["token"],
isActive: parsedJson["isAuthenticated"],
tokenLife: DateTime.tryParse(parsedJson["tokenlife"] ?? ""),
type: type);
3 years ago
}
3 years ago
}