Skip to content

Commit

Permalink
chore: utils refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Pritish Budhiraja committed Jan 23, 2024
1 parent 1c2330f commit b5ea9ae
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 193 deletions.
11 changes: 3 additions & 8 deletions src/hooks/ListHooks.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
open APIUtils
let useListCount = (~entityName) => {
open ConnectorUtils
let fetchDetails = useGetMethod()
let (count, setCount) = React.useState(() => 0)

Expand All @@ -11,15 +12,9 @@ let useListCount = (~entityName) => {
let count = switch entityName {
| ROUTING => response->getDictFromJsonObject->getArrayFromDict("records", [])->Array.length
| CONNECTOR =>
response
->getObjectArrayFromJson
->HSwitchUtils.filterList(~removeFromList=FRMPlayer)
->Array.length
response->getObjectArrayFromJson->filterList(~removeFromList=FRMPlayer)->Array.length
| FRAUD_RISK_MANAGEMENT =>
response
->getObjectArrayFromJson
->HSwitchUtils.filterList(~removeFromList=Connector)
->Array.length
response->getObjectArrayFromJson->filterList(~removeFromList=Connector)->Array.length
| _ => response->getArrayFromJson([])->Array.length
}
setCount(_ => count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@ open HomeUtils
module ConnectorOverview = {
@react.component
let make = () => {
open ConnectorUtils
let (screenState, setScreenState) = React.useState(_ => PageLoaderWrapper.Loading)
let (configuredConnectors, setConfiguredConnectors) = React.useState(_ => [])
let fetchConnectorListResponse = ConnectorUtils.useFetchConnectorList()
let fetchConnectorListResponse = useFetchConnectorList()

let getConnectorList = async () => {
open LogicUtils
try {
let response = await fetchConnectorListResponse()
let connectorsList =
response->HSwitchUtils.getProcessorsListFromJson(
~removeFromList=HSwitchUtils.FRMPlayer,
(),
)
response->getProcessorsListFromJson(~removeFromList=ConnectorTypes.FRMPlayer, ())

let arr =
connectorsList->Array.map(paymentMethod =>
paymentMethod
->getString("connector_name", "")
->ConnectorUtils.getConnectorNameTypeFromString
paymentMethod->getString("connector_name", "")->getConnectorNameTypeFromString
)
setConfiguredConnectors(_ => arr)
setScreenState(_ => Success)
Expand All @@ -42,7 +38,7 @@ module ConnectorOverview = {
->Array.mapWithIndex((connector, index) => {
let iconStyle = `${index === 0 ? "" : "-ml-4"} z-${(30 - index * 10)->Js.Int.toString}`
<GatewayIcon
gateway={connector->ConnectorUtils.getConnectorNameString->String.toUpperCase}
gateway={connector->getConnectorNameString->String.toUpperCase}
className={`w-12 h-12 rounded-full border-3 border-white ${iconStyle} bg-white`}
/>
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,13 @@ let make = (~setCurrentStep, ~setInitialValues, ~initialValues, ~isUpdateFlow, ~
| _ => "Loading..."
}

let (suggestedAction, suggestedActionExists) = ConnectorUtils.getSuggestedAction(
~verifyErrorMessage,
~connector,
)
let (suggestedAction, suggestedActionExists) = getSuggestedAction(~verifyErrorMessage, ~connector)

<PageLoaderWrapper screenState>
<Form
initialValues={updatedInitialVal}
onSubmit={(values, _) =>
ConnectorUtils.onSubmit(
onSubmit(
~values,
~onSubmitVerify,
~onSubmitMain,
Expand Down
27 changes: 13 additions & 14 deletions src/screens/HyperSwitch/Connectors/ConnectorList.res
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ module NewProcessorCards = {
~showIcons: bool,
~isPayoutFlow: bool,
) => {
open ConnectorUtils
let featureFlagDetails = HyperswitchAtom.featureFlagAtom->Recoil.useRecoilValueFromAtom

let connectorsAvailableForIntegration = featureFlagDetails.isLiveMode
? ConnectorUtils.connectorListForLive
? connectorListForLive
: isPayoutFlow
? ConnectorUtils.payoutConnectorList
: ConnectorUtils.connectorList
? payoutConnectorList
: connectorList

let unConfiguredConnectors =
connectorsAvailableForIntegration->Array.filter(total =>
Expand Down Expand Up @@ -91,8 +92,8 @@ module NewProcessorCards = {
className="grid gap-x-5 gap-y-6 2xl:grid-cols-4 lg:grid-cols-3 md:grid-cols-2 grid-cols-1 mb-5">
{connectorList
->Array.mapWithIndex((connector, i) => {
let connectorName = connector->ConnectorUtils.getConnectorNameString
let connectorInfo = connector->ConnectorUtils.getConnectorInfo
let connectorName = connector->getConnectorNameString
let connectorInfo = connector->getConnectorInfo
let size = "w-14 h-14 rounded-sm"

<div
Expand Down Expand Up @@ -158,7 +159,7 @@ module NewProcessorCards = {
<div className="bg-white rounded-md flex gap-2 flex-wrap p-4 border">
{connectorList
->Array.mapWithIndex((connector, i) => {
let connectorName = connector->ConnectorUtils.getConnectorNameString
let connectorName = connector->getConnectorNameString
let size = "w-14 h-14 rounded-sm"
<ToolTip
key={i->string_of_int}
Expand All @@ -181,9 +182,7 @@ module NewProcessorCards = {
let connectorListFiltered = {
if searchedConnector->String.length > 0 {
connectorsAvailableForIntegration->Array.filter(item =>
item
->ConnectorUtils.getConnectorNameString
->String.includes(searchedConnector->String.toLowerCase)
item->getConnectorNameString->String.includes(searchedConnector->String.toLowerCase)
)
} else {
connectorsAvailableForIntegration
Expand All @@ -196,15 +195,15 @@ module NewProcessorCards = {
{connectorListFiltered->iconsConnectors("Connect a new connector", true, ())}
{<UIUtils.RenderIf condition={featureFlagDetails.testProcessors && !isPayoutFlow}>
{featureFlagDetails.testProcessors
->ConnectorUtils.dummyConnectorList
->dummyConnectorList
->iconsConnectors("Connect a test connector", false, ~showSearch=false, ())}
</UIUtils.RenderIf>}
</>
} else {
<>
<UIUtils.RenderIf condition={featureFlagDetails.testProcessors && !isPayoutFlow}>
{featureFlagDetails.testProcessors
->ConnectorUtils.dummyConnectorList
->dummyConnectorList
->descriptedConnectors("Connect a test connector", false, ~showSearch=false, ())}
</UIUtils.RenderIf>
{connectorListFiltered->descriptedConnectors("Connect a new connector", true, ())}
Expand Down Expand Up @@ -236,14 +235,14 @@ let make = (~isPayoutFlow=false) => {
let detailedCardCount = 5
let showConnectorIcons = configuredConnectors->len > detailedCardCount
let (searchText, setSearchText) = React.useState(_ => "")
let fetchConnectorListResponse = ConnectorUtils.useFetchConnectorList()
let fetchConnectorListResponse = useFetchConnectorList()

let getConnectorListAndUpdateState = async () => {
open LogicUtils
try {
let response = await fetchConnectorListResponse()
let removeFromList = isPayoutFlow ? HSwitchUtils.PayoutConnector : HSwitchUtils.FRMPlayer
let connectorsList = response->HSwitchUtils.getProcessorsListFromJson(~removeFromList, ())
let removeFromList = isPayoutFlow ? ConnectorTypes.PayoutConnector : ConnectorTypes.FRMPlayer
let connectorsList = response->getProcessorsListFromJson(~removeFromList, ())
let previousData = connectorsList->Array.map(ConnectorTableUtils.getProcessorPayloadType)

setFilteredConnectorData(_ => previousData->Array.map(Js.Nullable.return))
Expand Down
4 changes: 2 additions & 2 deletions src/screens/HyperSwitch/Connectors/ConnectorPaymentMethod.res
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ let make = (
React.useEffect1(() => {
setScreenState(_ => Loading)
initialValues
->ConnectorUtils.getConnectorPaymentMethodDetails(
->getConnectorPaymentMethodDetails(
setPaymentMethods,
setMetaData,
setScreenState,
Expand All @@ -52,7 +52,7 @@ let make = (
let body =
constructConnectorRequestBody(obj, initialValues)->ignoreFields(
connectorID->Option.getWithDefault(""),
ConnectorUtils.connectorIgnoredField,
connectorIgnoredField,
)
let connectorUrl = getURL(~entityName=CONNECTOR, ~methodType=Post, ~id=connectorID, ())
let response = await updateAPIHook(connectorUrl, body, Post)
Expand Down
4 changes: 1 addition & 3 deletions src/screens/HyperSwitch/Connectors/ConnectorTableUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,9 @@ let getCell = (connector: connectorPayload, colType): Table.cell => {
}
}

let connectorsTableDefaultColumns = Recoil.atom(. "connectorsTableDefaultColumns", defaultColumns)

let getArrayDataFromJson = (json, itemToObjMapper: Js.Dict.t<Js.Json.t> => connectorPayload) => {
json
->HSwitchUtils.getProcessorsListFromJson()
->ConnectorUtils.getProcessorsListFromJson()
->Array.map(itemToObjMapper)
->Array.filter(item => !(item.connector_name->String.includes("apple")))
}
Expand Down
2 changes: 2 additions & 0 deletions src/screens/HyperSwitch/Connectors/ConnectorTypes.res
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,5 @@ type connectorPayload = {
frm_configs?: array<frm_config>,
status: string,
}

type processors = FRMPlayer | Connector | PayoutConnector
52 changes: 36 additions & 16 deletions src/screens/HyperSwitch/Connectors/ConnectorUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -845,8 +845,8 @@ let validateConnectorRequiredFields = (
let walletType = fieldName->getPaymentMethodTypeFromString
if walletType !== GooglePay && walletType !== ApplePay {
let key = `metadata.${fieldName}`
let errorKey = connectorMetaDataFields->LogicUtils.getString(fieldName, "")
let value = valuesFlattenJson->LogicUtils.getString(`metadata.${fieldName}`, "")
let errorKey = connectorMetaDataFields->getString(fieldName, "")
let value = valuesFlattenJson->getString(`metadata.${fieldName}`, "")
if value->String.length === 0 && connector->getMetaDataRequiredFields(fieldName) {
Dict.set(newDict, key, `Please enter ${errorKey}`->Js.Json.string)
}
Expand All @@ -857,18 +857,17 @@ let validateConnectorRequiredFields = (
->Dict.keysToArray
->Array.forEach(fieldName => {
let key = `connector_webhook_details.${fieldName}`
let errorKey = connectorWebHookDetails->LogicUtils.getString(fieldName, "")
let value =
valuesFlattenJson->LogicUtils.getString(`connector_webhook_details.${fieldName}`, "")
let errorKey = connectorWebHookDetails->getString(fieldName, "")
let value = valuesFlattenJson->getString(`connector_webhook_details.${fieldName}`, "")
if value->String.length === 0 && connector->getWebHookRequiredFields(fieldName) {
Dict.set(newDict, key, `Please enter ${errorKey}`->Js.Json.string)
}
})
connectorLabelDetailField
->Dict.keysToArray
->Array.forEach(fieldName => {
let errorKey = connectorLabelDetailField->LogicUtils.getString(fieldName, "")
let value = valuesFlattenJson->LogicUtils.getString(fieldName, "")
let errorKey = connectorLabelDetailField->getString(fieldName, "")
let value = valuesFlattenJson->getString(fieldName, "")
if value->String.length === 0 {
Dict.set(newDict, fieldName, `Please enter ${errorKey}`->Js.Json.string)
}
Expand All @@ -895,19 +894,16 @@ let getConnectorDetailsValue = (connectorInfo: ConnectorTypes.connectorPayload,
}

let getConnectorFields = connectorDetails => {
open LogicUtils
let connectorAccountDict =
connectorDetails->LogicUtils.getDictFromJsonObject->LogicUtils.getDictfromDict("connector_auth")
connectorDetails->getDictFromJsonObject->getDictfromDict("connector_auth")
let bodyType =
connectorAccountDict->Dict.keysToArray->Belt.Array.get(0)->Option.getWithDefault("")
let connectorAccountFields = connectorAccountDict->LogicUtils.getDictfromDict(bodyType)
let connectorMetaDataFields =
connectorDetails->LogicUtils.getDictFromJsonObject->LogicUtils.getDictfromDict("metadata")
let isVerifyConnector =
connectorDetails->LogicUtils.getDictFromJsonObject->LogicUtils.getBool("is_verifiable", false)
let connectorAccountFields = connectorAccountDict->getDictfromDict(bodyType)
let connectorMetaDataFields = connectorDetails->getDictFromJsonObject->getDictfromDict("metadata")
let isVerifyConnector = connectorDetails->getDictFromJsonObject->getBool("is_verifiable", false)
let connectorWebHookDetails =
connectorDetails
->LogicUtils.getDictFromJsonObject
->LogicUtils.getDictfromDict("connector_webhook_details")
connectorDetails->getDictFromJsonObject->getDictfromDict("connector_webhook_details")
let connectorLabelDetailField = Dict.fromArray([
("connector_label", "Connector label"->Js.Json.string),
])
Expand Down Expand Up @@ -1167,3 +1163,27 @@ let getConnectorPaymentMethodDetails = async (
}
}
}

let filterList = (items, ~removeFromList: ConnectorTypes.processors) => {
open LogicUtils
items->Array.filter(dict => {
let connectorType = dict->getString("connector_type", "")
let isPayoutConnector = connectorType == "payout_processor"
let isConnector = connectorType !== "payment_vas" && !isPayoutConnector

switch removeFromList {
| Connector => !isConnector
| FRMPlayer => isConnector
| PayoutConnector => isPayoutConnector
}
})
}

let getProcessorsListFromJson = (
json,
~removeFromList: ConnectorTypes.processors=FRMPlayer,
(),
) => {
open LogicUtils
json->getArrayFromJson([])->Array.map(getDictFromJsonObject)->filterList(~removeFromList)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ module SelectProcessor = {
~setConnectorConfigureState,
~connectorArray,
) => {
open ConnectorUtils
let url = RescriptReactRouter.useUrl()
let mixpanelEvent = MixpanelHook.useSendEvent()
let connectorName = selectedConnector->ConnectorUtils.getConnectorNameString
let connectorName = selectedConnector->getConnectorNameString
let {setQuickStartPageState} = React.useContext(GlobalProvider.defaultContext)

<QuickStartUIUtils.BaseComponent
Expand Down Expand Up @@ -41,8 +42,8 @@ module SelectProcessor = {
<QuickStartUIUtils.SelectConnectorGrid
selectedConnector
setSelectedConnector
connectorList={ConnectorUtils.connectorList->Array.filter(value =>
!(connectorArray->Array.includes(value->ConnectorUtils.getConnectorNameString))
connectorList={connectorList->Array.filter(value =>
!(connectorArray->Array.includes(value->getConnectorNameString))
)}
/>
</QuickStartUIUtils.BaseComponent>
Expand All @@ -61,7 +62,7 @@ module ConfigureProcessor = {
open ConnectorUtils
let mixpanelEvent = MixpanelHook.useSendEvent()
let featureFlagDetails = HyperswitchAtom.featureFlagAtom->Recoil.useRecoilValueFromAtom
let connectorName = selectedConnector->ConnectorUtils.getConnectorNameString
let connectorName = selectedConnector->getConnectorNameString

let connectorDetails = React.useMemo1(() => {
try {
Expand All @@ -85,7 +86,7 @@ module ConfigureProcessor = {
) = getConnectorFields(connectorDetails)

let onSubmit = async (values, _) => {
let body = ConnectorUtils.generateInitialValuesDict(
let body = generateInitialValuesDict(
~values,
~connector=connectorName,
~bodyType,
Expand Down Expand Up @@ -167,15 +168,16 @@ module SelectPaymentMethods = {
~setButtonState: (Button.buttonState => Button.buttonState) => unit,
~buttonState,
) => {
open ConnectorUtils
let {quickStartPageState} = React.useContext(GlobalProvider.defaultContext)
let updateAPIHook = APIUtils.useUpdateMethod()
let showToast = ToastState.useShowToast()
let mixpanelEvent = MixpanelHook.useSendEvent()
let usePostEnumDetails = EnumVariantHook.usePostEnumDetails()
let connectorName = selectedConnector->ConnectorUtils.getConnectorNameString
let connectorName = selectedConnector->getConnectorNameString

let (paymentMethodsEnabled, setPaymentMethods) = React.useState(_ =>
Dict.make()->Js.Json.object_->ConnectorUtils.getPaymentMethodEnabled
Dict.make()->Js.Json.object_->getPaymentMethodEnabled
)
let (metaData, setMetaData) = React.useState(_ => Dict.make()->Js.Json.object_)

Expand Down Expand Up @@ -205,7 +207,7 @@ module SelectPaymentMethods = {
payment_methods_enabled: paymentMethodsEnabled,
metadata: metaData,
}
let body = ConnectorUtils.constructConnectorRequestBody(obj, initialValues)
let body = constructConnectorRequestBody(obj, initialValues)
let connectorUrl = APIUtils.getURL(~entityName=CONNECTOR, ~methodType=Post, ~id=None, ())

let response = await updateAPIHook(connectorUrl, body, Post)
Expand All @@ -229,7 +231,7 @@ module SelectPaymentMethods = {

React.useEffect1(() => {
initialValues
->ConnectorUtils.getConnectorPaymentMethodDetails(
->getConnectorPaymentMethodDetails(
setPaymentMethods,
setMetaData,
_ => (),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ module SelectConnectorGrid = {
HyperswitchAtom.connectorListAtom
->Recoil.useRecoilValueFromAtom
->LogicUtils.safeParse
->HSwitchUtils.getProcessorsListFromJson()
->ConnectorUtils.getProcessorsListFromJson()
->Array.map(connectorDict =>
connectorDict
->LogicUtils.getString("connector_name", "")
Expand Down
6 changes: 0 additions & 6 deletions src/screens/HyperSwitch/Home/QuickStart/QuickStartUtils.res
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ let connectorChoiceVariantToString = variantValue =>
| _ => ""
}

let defaultChoiceStateValue: landingChoiceType = {
displayText: "Not Selected",
description: "Not Selected",
variantType: #NotSelected,
}

let connectorChoiceArray: array<landingChoiceType> = [
{
displayText: "Single Payment Processor",
Expand Down
Loading

0 comments on commit b5ea9ae

Please sign in to comment.