family switch parent child and child 2

pull/210/head
aamir-csol 19 hours ago
parent e9ec23f506
commit be6d33e51b

@ -4,7 +4,7 @@ import 'package:hmg_patient_app_new/core/enums.dart';
class ApiConsts {
static const maxSmallScreen = 660;
static AppEnvironmentTypeEnum appEnvironmentType = AppEnvironmentTypeEnum.uat;
static AppEnvironmentTypeEnum appEnvironmentType = AppEnvironmentTypeEnum.prod;
// static String baseUrl = 'https://uat.hmgwebservices.com/'; // HIS API URL UAT

@ -176,22 +176,53 @@ class AppState {
isRatedVisible = value;
}
// Dynamic object to track user switch payloads
Map<String, dynamic> _userSwitchPayloads = {};
int _childSwitchCounter = 0;
Map<String, dynamic> get getUserSwitchPayloads => _userSwitchPayloads;
void addUserSwitchPayload(dynamic payload, {bool isSuperUser = false}) {
var patientID = payload is Map ? payload['patientid'] : null;
if (patientID == null) {
if (isSuperUser) {
_userSwitchPayloads['superUser'] = payload;
_childSwitchCounter = 0;
} else {
_childSwitchCounter++;
if (_childSwitchCounter == 1) {
_userSwitchPayloads['child'] = payload;
} else {
_userSwitchPayloads['child$_childSwitchCounter'] = payload;
}
}
return;
}
String? existingKey;
for (var entry in _userSwitchPayloads.entries) {
if (entry.value is Map && entry.value['PatientID'] == patientID) {
existingKey = entry.key;
break;
}
}
if (isSuperUser) {
if (existingKey != null && existingKey != 'superUser') {
_userSwitchPayloads.remove(existingKey);
}
_userSwitchPayloads['superUser'] = payload;
_childSwitchCounter = 0; // Reset counter when super user is set
_childSwitchCounter = 0;
} else {
_childSwitchCounter++;
if (_childSwitchCounter == 1) {
_userSwitchPayloads['child'] = payload;
if (existingKey != null) {
_userSwitchPayloads[existingKey] = payload;
} else {
_userSwitchPayloads['child$_childSwitchCounter'] = payload;
_childSwitchCounter++;
if (_childSwitchCounter == 1) {
_userSwitchPayloads['child'] = payload;
} else {
_userSwitchPayloads['child$_childSwitchCounter'] = payload;
}
}
}
}

@ -6,6 +6,7 @@ import 'package:hmg_patient_app_new/core/api_consts.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/common_models/generic_api_model.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/enums.dart';
import 'package:hmg_patient_app_new/core/exceptions/api_failure.dart';
import 'package:hmg_patient_app_new/features/authentication/models/resp_models/select_device_by_imei.dart';
import 'package:hmg_patient_app_new/services/logger_service.dart';
@ -271,7 +272,6 @@ class AuthenticationRepoImp implements AuthenticationRepo {
}
}
// Determine which API to use based on zipCode
final zipCode = _getZipCodeFromRequest(newRequest);
final isOthersCountry = zipCode == '0';
final endpoint = isFormFamilyFile
@ -282,14 +282,12 @@ class AuthenticationRepoImp implements AuthenticationRepo {
? ApiConsts.checkActivationCodeOthers
: ApiConsts.checkActivationCode;
// Store the payload in AppState
try {
GenericApiModel<dynamic>? apiResponse;
Failure? failure;
// Determine the payload to send
final requestBody = isFormFamilyFile
var requestBody = isFormFamilyFile
? familyRequest
: isRegister
? newRequest
@ -297,32 +295,24 @@ class AuthenticationRepoImp implements AuthenticationRepo {
? switchRequest
: newRequest.toJson();
if (!isSwitchUser) {
print("Initial Super User Payload");
appState.addUserSwitchPayload(requestBody, isSuperUser: true);
print(requestBody);
} else if (isSwitchUser) {
// User is switching profiles
if (appState.getSuperUserID == responseID) {
// Switching back to the super user account
print("Switching Back to Super User");
appState.addUserSwitchPayload(requestBody, isSuperUser: true);
print(requestBody);
} else {
var superUserPaylod = appState.getUserSwitchPayloadByIndex(isSuperUser: true);
if (appState.getSuperUserID == null && superUserPaylod != null) {
if (superUserPaylod != null) {
// requestBody['LoginType'] = loginType;
// requestBody['PatientIdentificationID'] = "";
// requestBody['DeviceToken'] = newRequest.deviceToken;
// requestBody.removeWhere((key, value) => ['NationalID', 'isDentalAllowedBackend', 'ProjectOutSA', 'ForRegisteration', 'PatientID'].contains(key));
}
print("Switch to Family Where Super User Is ${appState.getSuperUserID}");
appState.addUserSwitchPayload(requestBody, isSuperUser: false);
print(requestBody);
} else {
print("Switching Into Family File Member");
if (isSwitchUser) {
Map<String, dynamic> allUsersPayload;
var switchingPatientID;
var superUserPatientID;
bool isSwitchingToSuperUser = false;
allUsersPayload = appState.getUserSwitchPayloads;
switchingPatientID = switchRequest["PatientID"];
superUserPatientID = allUsersPayload['superUser']['PatientID'];
isSwitchingToSuperUser = (appState.getSuperUserID == responseID) || (allUsersPayload.containsKey('superUser') && superUserPatientID == switchingPatientID);
if (isSwitchingToSuperUser) {
var superUserPayload = appState.getUserSwitchPayloadByIndex(isSuperUser: true);
if (superUserPayload != null) {
requestBody['DeviceToken'] = newRequest.deviceToken;
requestBody['ProjectOutSA'] = 0;
requestBody['PatientIdentificationID'] = "";
requestBody.removeWhere((key, value) => ['NationalID', 'isDentalAllowedBackend', 'ForRegisteration'].contains(key));
}
}
}
@ -596,7 +586,7 @@ class AuthenticationRepoImp implements AuthenticationRepo {
}
@override
Future<Either<Failure, GenericApiModel<dynamic>>> getServicePrivilege() {
Future<Either<Failure, GenericApiModel>> getServicePrivilege() {
Map<String, dynamic> mapDevice = {};
try {
GenericApiModel<dynamic>? apiResponse;

@ -385,7 +385,7 @@ class AuthenticationViewModel extends ChangeNotifier {
mobileNumber: phoneNumber,
selectedLoginType: otpTypeEnum.toInt(),
zipCode: selectedCountrySignup.countryCode,
nationalId:nationalIdOrFileNumber,
nationalId: nationalIdOrFileNumber,
isFileNo: isForRegister ? isPatientHasFile(request: payload) : false,
patientId: isFormFamilyFile ? _appState.getAuthenticatedUser()!.patientId : 0,
isForRegister: isForRegister,
@ -615,13 +615,21 @@ class AuthenticationViewModel extends ChangeNotifier {
_appState.setSuperUserID = null;
_appState.setIsChildLoggedIn = false;
activation.list!.first.isParentUser = true;
var allUsersPayload = _appState.getUserSwitchPayloads;
if ((allUsersPayload.containsKey('superUser') && allUsersPayload['superUser'] != null)) {
_appState.addUserSwitchPayload(activation.list!.first.toJson(), isSuperUser: false);
} else {
_appState.addUserSwitchPayload(activation.list!.first.toJson(), isSuperUser: true);
}
} else {
_appState.setSuperUserID = _appState.getAuthenticatedUser()?.patientId;
_appState.setIsChildLoggedIn = true;
activation.list!.first.isParentUser = false;
_appState.addUserSwitchPayload(activation.list!.first.toJson(), isSuperUser: false);
}
} else {
activation.list!.first.isParentUser = true;
_appState.addUserSwitchPayload(activation.list!.first.toJson(), isSuperUser: true);
}
activation.list!.first.bloodGroup = activation.patientBloodType;
activation.list!.first.zipCode = selectedCountrySignup == CountryEnum.others ? '0' : selectedCountrySignup.countryCode;
@ -1152,6 +1160,7 @@ class AuthenticationViewModel extends ChangeNotifier {
_appState.setVidaPlusProjectList([]);
_appState.setHMCProjectList([]);
_appState.setProjectsDetailList([]);
_appState.clearUserSwitchPayloads();
await clearDefaultInputValues();
_navigationService.pushAndReplace(AppRoutes.landingScreen);
} catch (e) {

Loading…
Cancel
Save