Untitled

Anonymous
plain_text
04/21/2026 12:25 PM
13.3 KB
10
Indexable
import { call, put, takeLatest } from 'redux-saga/effects';
// import { nbGaEvent } from '../../../utils/Common/eventUtils.js';
import {
	CHECK_IF_PHONE_EXIST,
	SEND_VERIFICATION_OTP_REQUEST,
	VERIFY_OTP_REQUEST,
	NEW_VERIFY_OTP_REQUEST,
	VERIFY_PASSWORD_REQUEST,
	CREATE_NEW_ACCOUNT_REQUEST,
	OTP_REQUEST_EMAIL,
	SEND_FORGET_PASSWORD_REQUEST,
	UPDATE_SECONDARY_NUMBER_REQUEST,
	SEND_MOBILE_NUMBER_UPDATE_REQUEST,
	CREATE_NEW_OTP_FLOW_ACCOUNT_REQUEST,
} from './constants';

import {
	checkIfPhoneExists,
	sendVerificationOtp,
	verifyOtp,
	verifyPassword,
	createNewAccount,
	sendEmailOTP,
	forgetPassword,
	updateSecondaryNumber,
	createNewOtpFlowAccount,
	updateMobileNumber,
} from './api';

import {
	checkIfPhoneExistsSuccess,
	checkIfPhoneExistsFailed,
	sendVerificationOtpSuccess,
	sendVerificationOtpFailed,
	verifyOtpFailed,
	verifyOtpSuccess,
	verifyPasswordSuccess,
	verifyPasswordFailed,
	createNewAccountSuccess,
	createNewAccountFailed,
	sendForgetPasswordSuccess,
	sendForgetPasswordFailed,
	verifyEmailOtpSuccess,
	newVerifyOtpSuccess,
	updateNewNUmberOtpFlowSuccess,
	updateNewNUmberOtpFlowFalire,
} from './actions';

import { getUserInfoRequest } from '../../../containers/App/Common/actions';
import { adobeMediaOptimiser } from '../../../utils/Common/eventUtils';
import { readCookie } from '../../../utils/Common/Common';
import facebookEvent from '../../../utils/Common/fbEvents';

/**
 * home data saga
 */
export function* checkIfPhoneExistsRequest(action) {
	const { dialCode, phoneNumber, flow } = action;
	try {
		// Call our request helper (see 'utils/request')
		const response = yield call(checkIfPhoneExists, dialCode, phoneNumber, flow);
		if (response.status === 200) {
			yield put(checkIfPhoneExistsSuccess(response.data));
			// if (response.data.statusCode === 10002) {
			//   nbGaEvent('PackersMovers', 'newPhone-packersMovers');
			// }
			// if (response.data.statusCode === 10003) {
			//   nbGaEvent('PackersMovers', 'SignIn_Initiated-packersMovers');
			// }
		} else {
			yield put(checkIfPhoneExistsFailed());
		}
	} catch (err) {
		yield put(checkIfPhoneExistsFailed());
	}
}
export function* sendForgetPassword(action) {
	const { email } = action;
	try {
		// Call our request helper (see 'utils/request')
		const response = yield call(forgetPassword, email);
		if (response.status === 200) {
			yield put(sendForgetPasswordSuccess());
		} else {
			yield put(sendForgetPasswordFailed());
		}
	} catch (err) {
		yield put(sendForgetPasswordFailed());
	}
}

export function* sendVerificationOtpRequest(action) {
	const { phoneNumber, dialCode, isUpdateOtp, isOtpMandateFlowEnabled, iso2, newFlow } = action;
	try {
		// Call our request helper (see 'utils/request')
		const response = yield call(
			sendVerificationOtp,
			phoneNumber,
			dialCode,
			isUpdateOtp,
			isOtpMandateFlowEnabled,
			iso2,
			newFlow
		);
		if (response.status === 200) {
			if (newFlow) {
				const resStatusCode = response.statusCode;
				if (resStatusCode === 10001 || resStatusCode === 11011) {
					yield put(sendVerificationOtpFailed());
				} else {
					yield put(sendVerificationOtpSuccess(response.data));
				}
			} else {
				yield put(sendVerificationOtpSuccess(response.data));
			}
		} else {
			yield put(sendVerificationOtpFailed());
		}
	} catch (err) {
		yield put(sendVerificationOtpFailed());
	}
}

export function* sendEmailOTPRequest(action) {
	const { email } = action;
	try {
		const response = yield call(sendEmailOTP, email);
		if (response.status === 200 && response.data.statusCode === 200) {
			yield put(getUserInfoRequest(verifyOtpSuccess));
		} else {
			yield put(verifyOtpFailed());
		}
	} catch (e) {
		yield put(verifyOtpFailed());
	}
}

export function* verifyNewOtpRequestSaga(action) {
	const { otpLoginObject } = action;
	try {
		const response = yield call(verifyOtp, otpLoginObject);
		if (
			response.status === 200 &&
			(response?.data?.data?.statusCode === 10002 ||
				response?.data?.statusCode === 10002 ||
				response?.data?.data?.statusCode === 10003 ||
				response?.data?.statusCode === 10003)
			// (response.data.statusCode !== 11004 || response.data?.data?.statusCode !== 11004) ||
			// (response.data.statusCode !== 11007 || response.data?.data?.statusCode !== 11007) ||
			// (response.data.statusCode !== 11005 || response.data?.data?.statusCode !== 11005) ||
			// (response.data.statusCode !== 11002 || response.data?.data?.statusCode !== 11002) ||
			// (response.data.statusCode !== 11003 || response?.data?.data?.statusCode !== 10003)
		) {
			yield put(getUserInfoRequest(newVerifyOtpSuccess, response.data));
		} else {
			yield put(verifyOtpFailed());
		}
	} catch (err) {
		yield put(verifyOtpFailed());
	}
}

export function* verifyOtpRequestSaga(action) {
	const { otpLoginObject } = action;
	try {
		// Call our request helper (see 'utils/request')
		const response = yield call(verifyOtp, otpLoginObject);
		if (response.status === 200 && (response.data.statusCode === 11003 || response.data.statusCode === 200)) {
			if (otpLoginObject.email) {
				yield put(verifyEmailOtpSuccess(response.data));
			} else {
				yield put(getUserInfoRequest(verifyOtpSuccess));
				window.triggerAlert('Signed in Successfully', 'success');
			}
		} else {
			yield put(verifyOtpFailed());
		}
	} catch (err) {
		yield put(verifyOtpFailed());
	}
}

export function* verifyPasswordRequest(action) {
	const { verifyPasswordRequestObject } = action;
	try {
		// Call our request helper (see 'utils/request')
		const response = yield call(verifyPassword, verifyPasswordRequestObject);
		if (response?.status === 200 && response?.data?.statusCode === 200) {
			yield put(getUserInfoRequest(verifyPasswordSuccess));
			window.triggerAlert('Signed in Successfully', 'success');
		} else {
			yield put(verifyPasswordFailed());
		}
	} catch (err) {
		yield put(verifyPasswordFailed());
	}
}

export function* newMobileNumberUpdateRequest(action) {
	const { mobileUpdateObject } = action;
	try {
		const response = yield call(updateMobileNumber, mobileUpdateObject);
		if (
			response?.status === 200 &&
			response?.data?.statusCode === 200 &&
			response?.data?.statusCode !== 11011 &&
			response?.data?.statusCode !== 10009 &&
			response?.data?.statusCode !== 10001
		) {
			yield put(getUserInfoRequest(verifyPasswordSuccess));
			yield put(updateNewNUmberOtpFlowSuccess());
			window.triggerAlert('Phone Number Updated', 'success');
		} else {
			yield put(updateNewNUmberOtpFlowFalire());
			window.triggerAlert('Phone Number Updation Failed');
		}
	} catch (e) {
		yield put(updateNewNUmberOtpFlowFalire());
		window.triggerAlert('Phone Number Updation Failed');
	}
}

export function* createNewOtpFlowAccountRequest(action) {
	const { signupObject } = action;
	try {
		const response = yield call(createNewOtpFlowAccount, signupObject);
		if (response.status === 200 && response.data.statusCode === 200) {
			yield put(createNewAccountSuccess(response?.data));
			yield put(getUserInfoRequest(createNewAccountSuccess));
			adobeMediaOptimiser.logEvent(`ev_Newuser-${readCookie('nbpt')}`);
			facebookEvent.log('CompleteRegistration', '', {
				content_name: 'Normal Registration',
			});
			if (!signupObject?.isOtpMandateFlowEnabled) {
				window.triggerAlert('Signed up Successfully', 'success');
			}
			if (typeof signupObject?.callbackMethod === 'function') {
				signupObject.callbackMethod();
			}
		} else {
			if (response?.data?.statusCode === 10001) {
				window.triggerAlert('Please enter a valid phone number.');
			}
			yield put(createNewAccountFailed(response.data));
		}
	} catch (err) {
		yield put(createNewAccountFailed());
	}
}

export function* createNewAccountRequest(action) {
	const { signupObject } = action;
	try {
		// Call our request helper (see 'utils/request')
		const response = yield call(createNewAccount, signupObject);
		if (response.status === 200 && response.data.statusCode === 200) {
			if (signupObject?.isOtpMandateFlowEnabled) {
				yield put(createNewAccountSuccess(response?.data));
			} else {
				yield put(getUserInfoRequest(createNewAccountSuccess));
			}
			adobeMediaOptimiser.logEvent(`ev_Newuser-${readCookie('nbpt')}`);
			facebookEvent.log('CompleteRegistration', '', {
				content_name: 'Normal Registration',
			});
			if (!signupObject?.isOtpMandateFlowEnabled) {
				window.triggerAlert('Signed up Successfully', 'success');
			}
			if (typeof signupObject?.callbackMethod === 'function') {
				signupObject.callbackMethod();
			}
		} else {
			if (response?.data?.statusCode === 10001) {
				window.triggerAlert('Please enter a valid phone number.');
			}
			yield put(createNewAccountFailed(response.data));
		}
	} catch (err) {
		yield put(createNewAccountFailed());
	}
}

/* Method to update the Secondary Number */
export function* updateSecondaryNumberRequest(action) {
	const { updatePhoneObject } = action;
	try {
		yield call(updateSecondaryNumber, updatePhoneObject);
		yield put(getUserInfoRequest());
	} catch (err) {
		// log error
	}
}

/**
 * Root saga manages watcher lifecycle
 */
export default function* rootSagaManger() {
	// Watches for LOAD_REPOS actions and calls getRepos when one comes in.
	// By using `takeLatest` only the result of the latest API call is applied.
	// It returns task descriptor (just like fork) so we can continue execution
	// It will be cancelled automatically on component unmount
	yield takeLatest(CHECK_IF_PHONE_EXIST, checkIfPhoneExistsRequest);
	yield takeLatest(SEND_VERIFICATION_OTP_REQUEST, sendVerificationOtpRequest);
	yield takeLatest(SEND_MOBILE_NUMBER_UPDATE_REQUEST, newMobileNumberUpdateRequest);
	yield takeLatest(VERIFY_OTP_REQUEST, verifyOtpRequestSaga);
	yield takeLatest(NEW_VERIFY_OTP_REQUEST, verifyNewOtpRequestSaga);
	yield takeLatest(VERIFY_PASSWORD_REQUEST, verifyPasswordRequest);
	yield takeLatest(CREATE_NEW_ACCOUNT_REQUEST, createNewAccountRequest);
	yield takeLatest(CREATE_NEW_OTP_FLOW_ACCOUNT_REQUEST, createNewOtpFlowAccountRequest);
	yield takeLatest(OTP_REQUEST_EMAIL, sendEmailOTPRequest);
	yield takeLatest(SEND_FORGET_PASSWORD_REQUEST, sendForgetPassword);
	yield takeLatest(UPDATE_SECONDARY_NUMBER_REQUEST, updateSecondaryNumberRequest);
	// yield takeLatest(VERIFY_WHATSAPP_OPT_IN);
}
Editor is loading...
Leave a Comment