import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:test_sa/controllers/notification/firebase_notification_manger.dart'; import 'package:test_sa/models/enums/user_types.dart'; class User { int? clientId; String? clientName; List? departmentId; List? departmentName; String? message; String? username; String? userID; String? email; String? password; String? token; dynamic roles; List? userRoles; String? tokenlife; bool? isAuthenticated; bool? hasError; String? profilePhotoName; String? id; String? userName; String? normalizedUserName; String? normalizedEmail; bool? emailConfirmed; dynamic passwordHash; String? securityStamp; String? concurrencyStamp; String? phoneNumber; String? extensionNo; bool? phoneNumberConfirmed; bool? twoFactorEnabled; dynamic lockoutEnd; bool? lockoutEnabled; int? accessFailedCount; List? assetGroups; bool? employeeIsHMG; bool? enableWifi; bool? enableNFC; bool? enableQR; bool? isEnabledFaceId; bool? onlySwipe; User({ this.clientId, this.clientName, this.departmentId, this.departmentName, this.message, this.username, this.userID, this.email, this.password, this.token, this.roles, this.userRoles, this.tokenlife, this.isAuthenticated, this.hasError, this.profilePhotoName, this.id, this.userName, this.normalizedUserName, this.normalizedEmail, this.emailConfirmed, this.passwordHash, this.securityStamp, this.concurrencyStamp, this.phoneNumber, this.phoneNumberConfirmed, this.twoFactorEnabled, this.lockoutEnd, this.lockoutEnabled, this.accessFailedCount, this.employeeIsHMG, this.enableNFC, this.enableQR, this.enableWifi, this.isEnabledFaceId, this.onlySwipe, }); bool get isLiveToken => tokenlife != null && (DateTime.tryParse(tokenlife!)?.isAfter(DateTime.now()) ?? false); Future> toLoginJson() async { String notificationType = (Platform.isAndroid && !(await FirebaseNotificationManger.isGoogleServicesAvailable())) ? "HMC" : "FCM"; if (FirebaseNotificationManger.token == null) { //calling this agin to check for token as sometimes token is null fresh install.. await FirebaseNotificationManger.getToken(); } return {"username": userName, "password": password, "fireBaseToken": FirebaseNotificationManger?.token ?? "", "notificationType": notificationType}; } UsersTypes? get type { switch (userRoles?.first.value) { case "R-6": return UsersTypes.engineer; case "R-7": // Nurse Role return UsersTypes.normal_user; case "R-33": // Head Nurse Role return UsersTypes.normal_user; case "R-32": // Head Nurse Role return UsersTypes.assessor; case "R-19": // Head Nurse Role return UsersTypes.assessorTl; default: return null; } } Map toUpdateProfileJson() { Map jsonObject = {}; // if (departmentId != null) jsonObject["department"] = departmentId; // if (whatsApp != null && whatsApp.isNotEmpty) jsonObject["whatsapp"] = whatsApp; // if (phoneNumber != null && phoneNumber.isNotEmpty) jsonObject["phone"] = phoneNumber; return jsonObject; } Future> toRegisterJson() async { // if (FirebaseNotificationManger.token == null) await FirebaseNotificationManger.getToken(); return { // "username": userName, // "email": email, // "whatsapp": whatsApp, // "client": hospital.id, // "department": department?.id, // "phone": phoneNumber, // "pass": password, // "firebase_token": FirebaseNotificationManger?.token ?? "", }; } Map toJson() { final map = {}; map['client_id'] = clientId; map['client_name'] = clientName; map['department_id'] = departmentId; map['department_name'] = departmentName; map['message'] = message; map['username'] = username; map['userID'] = userID; map['email'] = email; map['password'] = password; map['token'] = token; map['roles'] = roles; if (userRoles != null) { map['userRoles'] = userRoles!.map((v) => v.toJson()).toList(); } if (assetGroups != null) { map['assetGroups'] = assetGroups!.map((v) => v.toJson()).toList(); } map['tokenlife'] = tokenlife; map['isAuthenticated'] = isAuthenticated; map['hasError'] = hasError; map['profilePhotoName'] = profilePhotoName; map['id'] = id; map['userName'] = userName; map['normalizedUserName'] = normalizedUserName; map['normalizedEmail'] = normalizedEmail; map['emailConfirmed'] = emailConfirmed; map['passwordHash'] = passwordHash; map['securityStamp'] = securityStamp; map['concurrencyStamp'] = concurrencyStamp; map['phoneNumber'] = phoneNumber; map['extensionNo'] = extensionNo; map['phoneNumberConfirmed'] = phoneNumberConfirmed; map['twoFactorEnabled'] = twoFactorEnabled; map['lockoutEnd'] = lockoutEnd; map['lockoutEnabled'] = lockoutEnabled; map['accessFailedCount'] = accessFailedCount; map['employeeIsHMG'] = employeeIsHMG; map['enableWifi'] = enableWifi; map['enableNFC'] = enableNFC; map['enableQR'] = enableQR; map['onlySwipe'] = onlySwipe; map['isEnabledFaceId'] = isEnabledFaceId; return map; } static Future fromJson(Map json) { return compute(User.fromJsonCons, json); } User.fromJsonCons(dynamic json) { clientId = json['client_id']; clientName = json['client_name']; if (json['department_id'] != null) { departmentId = []; json['department_id'].forEach((v) { departmentId!.add(v); }); } if (json['department_name'] != null) { departmentName = []; json['department_name'].forEach((v) { departmentName!.add(v); }); } message = json['message']; username = json['username']; userID = json['userID']; email = json['email']; password = json['password']; token = json['token']; roles = json['roles']; if (json['userRoles'] != null) { userRoles = []; json['userRoles'].forEach((v) { userRoles!.add(UserRoles.fromJson(v)); }); } if (json['assetGroups'] != null) { assetGroups = []; json['assetGroups'].forEach((v) { assetGroups!.add(AssetGroup.fromJson(v)); }); } tokenlife = json['tokenlife']; isAuthenticated = json['isAuthenticated']; hasError = json['hasError']; profilePhotoName = json['profilePhotoName']; id = json['id']; userName = json['userName']; normalizedUserName = json['normalizedUserName']; normalizedEmail = json['normalizedEmail']; emailConfirmed = json['emailConfirmed']; passwordHash = json['passwordHash']; securityStamp = json['securityStamp']; concurrencyStamp = json['concurrencyStamp']; phoneNumber = json['phoneNumber']; extensionNo = json['extensionNo']; phoneNumberConfirmed = json['phoneNumberConfirmed']; twoFactorEnabled = json['twoFactorEnabled']; lockoutEnd = json['lockoutEnd']; lockoutEnabled = json['lockoutEnabled']; accessFailedCount = json['accessFailedCount']; employeeIsHMG = json['employeeIsHMG']; enableWifi = json['enableWifi']; enableNFC = json['enableNFC']; enableQR = json['enableQR']; onlySwipe = json['onlySwipe']; isEnabledFaceId = json['isEnabledFaceId']; } } class UserRoles { String? id; String? name; String? value; UserRoles({ this.id, this.name, this.value, }); UserRoles.fromJson(dynamic json) { id = json['id']; name = json['name']; value = json['value']; } UserRoles copyWith({ String? id, String? name, String? value, }) => UserRoles( id: id ?? this.id, name: name ?? this.name, value: value ?? this.value, ); Map toJson() { final map = {}; map['id'] = id; map['name'] = name; map['value'] = value; return map; } } class AssetGroup { int? id; String? name; String? code; bool? enabledEngineerTimer; AssetGroup({this.id, this.name, this.code, this.enabledEngineerTimer}); AssetGroup.fromJson(Map? json) { // Allow json to be null if (json != null) { // Add null check id = json['id']; name = json['name']; code = json['code']; enabledEngineerTimer = json['enabledEngineerTimer'] ?? false; } } Map toJson() { final Map data = {}; // Use for type safety data['id'] = id; data['name'] = name; data['code'] = code; data['enabledEngineerTimer'] = enabledEngineerTimer; return data; } @override bool operator ==(Object other) => identical(this, other) || other is AssetGroup && runtimeType == other.runtimeType && id == other.id; @override int get hashCode => id.hashCode; } class UserContactInfoModel { dynamic userId; String? email; String? phoneNo; String? extensionNo; UserContactInfoModel({this.email, this.phoneNo, this.extensionNo, this.userId}); Map toJson() { return { 'email': email, 'mobileNumber': phoneNo, 'extensionNo': extensionNo, }; } }