From c3ec7e8548da992d16eb44b454f5e50ac17c88b1 Mon Sep 17 00:00:00 2001 From: Kshitij Pramod Ghongadi Date: Thu, 23 May 2024 23:00:56 +0530 Subject: [PATCH] TP-62646 | RN Quote | API unification (POST + GET) (#10942) --- .../quote-offer-screen/QuoteOfferScreen.tsx | 2 +- .../constants/AnalyticsEventsConstant.ts | 1 + .../widgets/screenData/ScreenMetaData.ts | 3 +- App/common/screen/ScreenActionHandler.tsx | 93 ++++++++++++++++--- App/common/screen/ScreenActionTypes.ts | 1 + App/common/utilities/CtaParamsUtils.ts | 22 +++-- .../widget-actions/WidgetActionHandler.ts | 6 +- network/ApiClient.ts | 15 ++- network/NetworkConstant.ts | 1 + network/NetworkUtils.ts | 23 ++++- package.json | 4 +- 11 files changed, 137 insertions(+), 34 deletions(-) diff --git a/App/Container/Navi-Insurance/screen/quote-offer-screen/QuoteOfferScreen.tsx b/App/Container/Navi-Insurance/screen/quote-offer-screen/QuoteOfferScreen.tsx index c1a1380ed6..fb837d8c9e 100644 --- a/App/Container/Navi-Insurance/screen/quote-offer-screen/QuoteOfferScreen.tsx +++ b/App/Container/Navi-Insurance/screen/quote-offer-screen/QuoteOfferScreen.tsx @@ -173,7 +173,7 @@ const QuoteOfferScreen = ({ postApiCalled === "true" ? ScreenActionTypes.FETCH_INSURANCE_QUOTE_PAGE_FROM_BACKEND : preQuoteId - ? ScreenActionTypes.FETCH_QUOTE_V3 + ? ScreenActionTypes.FETCH_QUOTE_V4 : ScreenActionTypes.FETCH_INSURANCE_QUOTE_PAGE_FROM_BACKEND; const data: QuoteOfferRequest = { preQuoteId: preQuoteId ? preQuoteId : undefined, diff --git a/App/common/constants/AnalyticsEventsConstant.ts b/App/common/constants/AnalyticsEventsConstant.ts index d979359652..4f31beb5c3 100644 --- a/App/common/constants/AnalyticsEventsConstant.ts +++ b/App/common/constants/AnalyticsEventsConstant.ts @@ -24,6 +24,7 @@ export const AnalyticsModuleNameConstant = { export const AnalyticsMethodNameConstant = { FETCH_INSURANCE_QUOTE_PAGE_FROM_BACKEND: "fetchInsuranceQuotePageFromBackend", FETCH_QUOTE_V3: "fetchQuoteV3", + FETCH_QUOTE_V4: "fetchQuoteV4", FINAL_PATCH_CALL: "finalPatchCall", COMPARE_PLAN_LIST : "comparePlanList", }; diff --git a/App/common/interface/widgets/screenData/ScreenMetaData.ts b/App/common/interface/widgets/screenData/ScreenMetaData.ts index 5fd1d4e87d..61cab95c58 100644 --- a/App/common/interface/widgets/screenData/ScreenMetaData.ts +++ b/App/common/interface/widgets/screenData/ScreenMetaData.ts @@ -1,4 +1,4 @@ -import { CtaData } from "../.."; +import { AnalyticsEvent, CtaData } from "../.."; import { Widget } from "../Widget"; export interface ScreenMetaData { @@ -6,6 +6,7 @@ export interface ScreenMetaData { redirectionCta?: CtaData; floatingWidgets?: Widget[]; screenProperties?: Array; + analyticsEvent?: AnalyticsEvent; } type LineItem = { diff --git a/App/common/screen/ScreenActionHandler.tsx b/App/common/screen/ScreenActionHandler.tsx index 9eb42727d1..4c09c16bb7 100644 --- a/App/common/screen/ScreenActionHandler.tsx +++ b/App/common/screen/ScreenActionHandler.tsx @@ -1,28 +1,32 @@ -import { ActionMetaData, BaseActionTypes } from "../actions/GenericAction"; -import { ScreenData } from "../interface/widgets/screenData/ScreenData"; -import { ScreenActionTypes } from "./ScreenActionTypes"; import { Dispatch, SetStateAction } from "react"; -import { post, get } from "../../../network/NetworkService"; -import { CtaData } from "../interface"; -import { getXTargetHeaderInfo } from "../../../network/ApiClient"; -import { GI } from "../constants/NavigationHandlerConstants"; -import { ScreenState } from "./BaseScreen"; -import { BASE_SCREEN } from "../constants/ScreenNameConstants"; -import { logToSentry } from "../hooks/useSentryLogging"; import { - sendAsAnalyticsEvent, - sendAsGlobalErrorEvent, -} from "../hooks/useAnalyticsEvent"; + getAcceptHeaderInfo, + getXTargetHeaderInfo, +} from "../../../network/ApiClient"; +import { GZIP } from "../../../network/NetworkConstant"; +import { get, post } from "../../../network/NetworkService"; +import { mergeHeaders } from "../../../network/NetworkUtils"; +import { ActionMetaData, BaseActionTypes } from "../actions/GenericAction"; +import { BASE_SCREEN, GI, QUOTE_ID } from "../constants"; import { AnalyticsFlowNameConstant, AnalyticsGlobalErrorTypeConstant, AnalyticsMethodNameConstant, AnalyticsModuleNameConstant, } from "../constants/AnalyticsEventsConstant"; +import { + sendAsAnalyticsEvent, + sendAsGlobalErrorEvent, +} from "../hooks/useAnalyticsEvent"; +import { logToSentry } from "../hooks/useSentryLogging"; +import { CtaData } from "../interface"; +import { ScreenData } from "../interface/widgets/screenData/ScreenData"; import { getStringPreference, setStringPreference, } from "../utilities/SharedPreferenceUtils"; +import { ScreenState } from "./BaseScreen"; +import { ScreenActionTypes } from "./ScreenActionTypes"; export const ScreenActionHandler = { handleScreenAction: ( @@ -140,6 +144,69 @@ export const ScreenActionHandler = { ); }); } + case ScreenActionTypes.FETCH_QUOTE_V4: { + const acceptHeader = getAcceptHeaderInfo(GZIP); + const xTargetHeader = getXTargetHeaderInfo(GI.toLocaleUpperCase()); + return post>( + "v4/quotes", + screenMetaData.data, + mergeHeaders({ configs: [acceptHeader, xTargetHeader] }), + ) + .then(response => { + if (screenData?.screenState) { + setScreenData({ + ...screenData, + screenState: ScreenState.LOADING, + }); + } + setPostApiData(); + if (response) { + const updatedScreenData: ScreenData = { + ...(response.data as ScreenData), + screenState: ScreenState.LOADED, + }; + let quoteId; + updatedScreenData?.screenMetaData?.screenProperties?.forEach( + item => { + if (item.key === QUOTE_ID) { + quoteId = item.value; + } + }, + ); + if (!!quoteId) { + storeQuoteId(quoteId); + } + // If redirectionCta is present and screenWidgets is not present then navigate to Base screen to get routed to specific screenName based on Cta + if ( + !!updatedScreenData.screenMetaData?.redirectionCta && + !updatedScreenData.screenWidgets + ) { + const cta = updatedScreenData.screenMetaData?.redirectionCta; + // By default routing is happening via Base screen hence below navigate sends the cta there to get routed to specific screenName based on Cta + if (!!cta.analyticsEventProperties) { + sendAsAnalyticsEvent(cta.analyticsEventProperties); + } + navigation.navigate(BASE_SCREEN, { ctaData: cta }); + } else { + if (updatedScreenData?.screenMetaData?.analyticsEvent) { + sendAsAnalyticsEvent( + updatedScreenData.screenMetaData.analyticsEvent, + ); + } + setScreenData(updatedScreenData); + } + } + }) + .catch(error => { + handleErrorData( + error, + setScreenData, + screenMetaData, + AnalyticsMethodNameConstant.FETCH_QUOTE_V4, + ScreenActionTypes.FETCH_QUOTE_V4, + ); + }); + } case ScreenActionTypes.SHOW_LOADER: { const updatedScreenData: ScreenData = { ...screenData, diff --git a/App/common/screen/ScreenActionTypes.ts b/App/common/screen/ScreenActionTypes.ts index 679d5855af..7cc3b69ca2 100644 --- a/App/common/screen/ScreenActionTypes.ts +++ b/App/common/screen/ScreenActionTypes.ts @@ -2,6 +2,7 @@ export const ScreenActionTypes = { FETCH_INSURANCE_QUOTE_PAGE_FROM_BACKEND: "FETCH_INSURANCE_QUOTE_PAGE_FROM_BACKEND", FETCH_QUOTE_V3: "FETCH_QUOTE_V3", + FETCH_QUOTE_V4: "FETCH_QUOTE_V4", SHOW_LOADER: "SHOW_LOADER", FETCH_COMPARE_PLAN_LIST: "FETCH_COMPARE_PLAN_LIST", }; diff --git a/App/common/utilities/CtaParamsUtils.ts b/App/common/utilities/CtaParamsUtils.ts index d71607aea6..e2b37beaf6 100644 --- a/App/common/utilities/CtaParamsUtils.ts +++ b/App/common/utilities/CtaParamsUtils.ts @@ -1,13 +1,17 @@ -import { QUOTE_ID } from "../constants/StringConstant"; +import { QUOTE_ID } from "../constants"; import { CtaData } from "../interface"; +import { ScreenMetaData } from "../interface/widgets/screenData/ScreenMetaData"; export const getQuoteIdFromCta = (ctaData?: CtaData) => { - let quoteId; - ctaData?.parameters?.forEach(item => { - if (item.key === QUOTE_ID) { - quoteId = item.value; - return; - } - }); - return quoteId; + const quoteObj = ctaData?.parameters?.find(item => item.key === QUOTE_ID); + return quoteObj?.value; +}; + +export const getQuoteIdFromScreenMetaData = ( + screenMetaData?: ScreenMetaData, +) => { + const quoteObj = screenMetaData?.screenProperties?.find( + item => item.key === QUOTE_ID, + ); + return quoteObj?.value; }; diff --git a/App/common/widgets/widget-actions/WidgetActionHandler.ts b/App/common/widgets/widget-actions/WidgetActionHandler.ts index a3ab17577d..0c0944f96b 100644 --- a/App/common/widgets/widget-actions/WidgetActionHandler.ts +++ b/App/common/widgets/widget-actions/WidgetActionHandler.ts @@ -25,7 +25,7 @@ import { ScreenData } from "../../interface/widgets/screenData/ScreenData"; import { FinalPatchCallRequestBody } from "../../interface/widgets/widgetData/FooterWithCardWidgetData"; import { NativeDeeplinkNavigatorModule } from "../../native-module/NativeModules"; import { ScreenState } from "../../screen/BaseScreen"; -import { getQuoteIdFromCta } from "../../utilities/CtaParamsUtils"; +import { getQuoteIdFromCta, getQuoteIdFromScreenMetaData } from "../../utilities/CtaParamsUtils"; import { updateValueByKeyPath } from "../../utilities/MiscUtils"; import { parseValue } from "../../utilities/SerializerUtil"; import { WidgetActionTypes } from "./WidgetActionTypes"; @@ -134,7 +134,7 @@ const WidgetActionHandler = { } case WidgetActionTypes.PATCH_QUOTE_V2: { - let quoteId = getQuoteIdFromCta(ctaData); + let quoteId = getQuoteIdFromCta(ctaData) ?? getQuoteIdFromScreenMetaData(screenData?.screenMetaData); const requestData: SumInsuredRequestData = widgetMetaData.data; if (!quoteId) { getQuoteIdFromCache().then((value) => { @@ -170,7 +170,7 @@ const WidgetActionHandler = { screenState: ScreenState.OVERLAY, }); invalidatePostApiData(); - let quoteId = getQuoteIdFromCta(ctaData); + let quoteId = getQuoteIdFromCta(ctaData) ?? getQuoteIdFromScreenMetaData(screenData?.screenMetaData); const requestData: SumInsuredRequestData = ( widgetMetaData?.data as FinalPatchCallRequestBody ).requestData; diff --git a/network/ApiClient.ts b/network/ApiClient.ts index c1f63a4856..c034984382 100644 --- a/network/ApiClient.ts +++ b/network/ApiClient.ts @@ -1,7 +1,7 @@ -import axios, { AxiosHeaders, AxiosResponse } from "axios"; -import { BASE_URL } from "./NetworkConstant"; +import axios, { AxiosRequestConfig, AxiosResponse } from "axios"; import { NetworkConnectorModule } from "../App/common/native-module/NativeModules"; -import { AxiosRequestConfig } from "axios"; +import { BASE_URL } from "./NetworkConstant"; + export const getDefaultHeaderData = async () => { try { @@ -23,6 +23,15 @@ export const getXTargetHeaderInfo = ( }; }; +export const getAcceptHeaderInfo = (acceptType: string): AxiosRequestConfig => { + return { + decompress: true, + headers: { + "Accept-Encoding": acceptType, + }, + }; +}; + export const ApiClient = axios.create({ baseURL: BASE_URL, timeout: 10000, diff --git a/network/NetworkConstant.ts b/network/NetworkConstant.ts index 01cc81b4f7..b766b22339 100644 --- a/network/NetworkConstant.ts +++ b/network/NetworkConstant.ts @@ -3,3 +3,4 @@ export const APPLICATION_ID_PATH = "/arc-warden/api/v2/application"; export const FILL_APPLICATION_PATH = "/arc-warden/api/v2/application/:applicationId/fill"; export const BUNDLE_VERSION = "x-rn-bundle-version"; +export const GZIP = "gzip"; \ No newline at end of file diff --git a/network/NetworkUtils.ts b/network/NetworkUtils.ts index aa7f9690c7..5126c86d7c 100644 --- a/network/NetworkUtils.ts +++ b/network/NetworkUtils.ts @@ -1,7 +1,11 @@ +import { + AxiosInstance, + AxiosRequestConfig, + RawAxiosRequestHeaders, +} from "axios"; import { getStringPreference } from "../App/common/utilities/SharedPreferenceUtils"; import packageJson from "../package.json"; import { BUNDLE_VERSION } from "./NetworkConstant"; -import { AxiosInstance } from "axios"; const getSessionToken = async () => { return await getStringPreference("SESSION_TOKEN", "string"); @@ -18,4 +22,19 @@ const addBundleVersionToHeader = (axiosInstance: AxiosInstance) => { }); }; -export { getSessionToken, getDeviceId, addBundleVersionToHeader }; +const mergeHeaders = ({ + configs, +}: { + configs: AxiosRequestConfig[]; +}): AxiosRequestConfig => { + const mergedHeaders = configs.reduce((acc, config) => { + const headers = config.headers as RawAxiosRequestHeaders; + return { ...acc, ...headers }; + }, {} as RawAxiosRequestHeaders); + + return { + headers: mergedHeaders, + }; +}; + +export { addBundleVersionToHeader, getDeviceId, getSessionToken, mergeHeaders }; diff --git a/package.json b/package.json index e31bd8cfce..45f3751d4e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "NaviApp", - "version": "0.1.0", - "versionCode": 1, + "version": "0.1.1", + "versionCode": 2, "private": true, "scripts": { "start": "yarn react-native start",