From 3c457e2010c8a48d87a9d10b9a8d27b9481fe09f Mon Sep 17 00:00:00 2001 From: Aman Chaturvedi Date: Sat, 21 Sep 2024 13:57:38 +0530 Subject: [PATCH] TP-80461 | auto app updater fallback added --- src/action/appDownloadAction.ts | 9 ++--- src/common/BlockerScreen.tsx | 59 +++++++++++++++++++++---------- src/components/utlis/apiHelper.ts | 2 ++ 3 files changed, 47 insertions(+), 23 deletions(-) diff --git a/src/action/appDownloadAction.ts b/src/action/appDownloadAction.ts index 0d098831..5203abbe 100644 --- a/src/action/appDownloadAction.ts +++ b/src/action/appDownloadAction.ts @@ -1,8 +1,9 @@ import { BuildFlavours } from '@common/Constants'; +import { ApiKeys, getApiUrl } from '@components/utlis/apiHelper'; import { logError } from '@components/utlis/errorUtils'; import axios from 'axios'; import { Linking, NativeModules } from 'react-native'; -import RNFetchBlob from 'rn-fetch-blob'; +import RNFetchBlob from 'react-native-blob-util'; const { ApkInstaller } = NativeModules; @@ -40,12 +41,12 @@ export const downloadApkFromS3 = async (s3Url: string, fileName: string) => { export const downloadLatestAppS3Url = async (buildFlavour: BuildFlavours) => { try { - const response = await axios.get( - `https://longhorn.navi.com/api/app/download?appType=${buildFlavour}` - ); + const url = getApiUrl(ApiKeys.DOWNLOAD_LATEST_APP, {}, { appType: buildFlavour }); + const response = await axios.get(url); return response.data.downloadPreSignedUrl; } catch (err) { logError(err as Error, 'Error while downloading the latest app'); + return ''; } }; diff --git a/src/common/BlockerScreen.tsx b/src/common/BlockerScreen.tsx index 60c4d8ce..853e880e 100644 --- a/src/common/BlockerScreen.tsx +++ b/src/common/BlockerScreen.tsx @@ -46,7 +46,10 @@ const BlockerScreen = (props: IBlockerScreen) => { (roles?.length === 1 && roles.includes(IUserRole.ROLE_FIELD_AGENT)) || roles.includes(IUserRole.ROLE_OMA); - const [shouldUpdate, setShouldUpdate] = useState(''); + const [shouldUpdate, setShouldUpdate] = useState({ + newApkCachedUrl: '', + switchToFallback: false, + }); const [showActionBtnLoader, setShowActionBtnLoader] = useState(false); const dispatch = useAppDispatch(); @@ -57,32 +60,36 @@ const BlockerScreen = (props: IBlockerScreen) => { const downloadLatestApp = async () => { let apkFileUrl; if (GLOBAL.BUILD_FLAVOUR.includes(BuildFlavours.FIELD_AGENTS)) { - // Field agent + // Download app for Field agent apkFileUrl = await downloadLatestApkAndGetFilePath(BuildFlavours.FIELD_AGENTS); } else { - // Calling agent + // Download app for Calling agent apkFileUrl = await downloadLatestApkAndGetFilePath(BuildFlavours.CALLING_AGENTS); } if (apkFileUrl) { - setShouldUpdate(apkFileUrl); + setShouldUpdate({ newApkCachedUrl: apkFileUrl, switchToFallback: false }); + } else { + setShouldUpdate({ newApkCachedUrl: '', switchToFallback: true }); } }; const handleAppUpdate = () => { - if (shouldUpdate) { - installApk(shouldUpdate, (error) => { - if (!error) { - return; - } - let fallbackLonghornUrl; - if (GLOBAL.BUILD_FLAVOUR.includes('fieldAgents')) { - fallbackLonghornUrl = appState?.fieldAgents?.currentProdAPK; - } else { - fallbackLonghornUrl = appState?.telecallingAgents?.currentProdAPK; - } - openFallbackLonghornLink(fallbackLonghornUrl); - }); + let fallbackLonghornUrl; + if (GLOBAL.BUILD_FLAVOUR.includes('fieldAgents')) { + fallbackLonghornUrl = appState?.fieldAgents?.currentProdAPK; + } else { + fallbackLonghornUrl = appState?.telecallingAgents?.currentProdAPK; } + if (!shouldUpdate.newApkCachedUrl) { + openFallbackLonghornLink(fallbackLonghornUrl); + return; + } + installApk(shouldUpdate.newApkCachedUrl, (error) => { + if (!error) { + return; + } + openFallbackLonghornLink(fallbackLonghornUrl); + }); }; React.useEffect(() => { @@ -106,7 +113,10 @@ const BlockerScreen = (props: IBlockerScreen) => { ) { downloadLatestApp(); } else { - setShouldUpdate(''); + setShouldUpdate({ + newApkCachedUrl: '', + switchToFallback: false, + }); deleteCachedApkFiles(); } }, [appState]); @@ -145,10 +155,21 @@ const BlockerScreen = (props: IBlockerScreen) => { } }; - if (shouldUpdate) { + if (shouldUpdate.newApkCachedUrl) { return ; } + if (shouldUpdate.switchToFallback) { + const { heading, instructions } = BLOCKER_SCREEN_DATA.UNINSTALL_APP; + return ( + + ); + } + if (!isTimeSynced) { const { heading, instructions } = BLOCKER_SCREEN_DATA.TIME_UNSYNC; return ( diff --git a/src/components/utlis/apiHelper.ts b/src/components/utlis/apiHelper.ts index 0a78bad9..6d212bb3 100644 --- a/src/components/utlis/apiHelper.ts +++ b/src/components/utlis/apiHelper.ts @@ -94,6 +94,7 @@ export enum ApiKeys { SEND_COMMUNICATION_NAVI_ACCOUNT = 'SEND_COMMUNICATION_NAVI_ACCOUNT', SYNC_CALL_FEEDBACK_NUDGE_DETAILS = 'SYNC_CALL_FEEDBACK_NUDGE_DETAILS', GENERATE_DYNAMIC_DOCUMENT = 'GENERATE_DYNAMIC_DOCUMENT', + DOWNLOAD_LATEST_APP = 'DOWNLOAD_LATEST_APP' } export const API_URLS: Record = {} as Record; @@ -180,6 +181,7 @@ API_URLS[ApiKeys.FETCH_AGENT_DOCUMENTS] = '/documents/agent'; API_URLS[ApiKeys.FETCH_DOCUMENT_SPECIFIC_LANGUAGE] = '/documents/language/{loanAccountNumber}'; API_URLS[ApiKeys.SEND_COMMUNICATION_NAVI_ACCOUNT] = '/navi-communications/{loanAccountNumber}'; API_URLS[ApiKeys.GENERATE_DYNAMIC_DOCUMENT] = '/documents/generate/{loanAccountNumber}'; +API_URLS[ApiKeys.DOWNLOAD_LATEST_APP] = 'https://longhorn.navi.com/api/app/download'; export const API_STATUS_CODE = { OK: 200,