Skip to content

Commit

Permalink
feat: refunds failure reasons (#1938)
Browse files Browse the repository at this point in the history
  • Loading branch information
sagarnaikjuspay authored Dec 17, 2024
1 parent 939457d commit 971ecca
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
open NewAnalyticsTypes
open FailureReasonsRefundsTypes
open NewRefundsAnalyticsEntity
open FailureReasonsRefundsUtils
open NewAnalyticsHelper

module TableModule = {
@react.component
let make = (~data, ~className="") => {
let (offset, setOffset) = React.useState(_ => 0)

let defaultSort: Table.sortedObject = {
key: "",
order: Table.INC,
}

let visibleColumns = [
Refund_Error_Message,
Refund_Error_Message_Count,
Refund_Error_Message_Count_Ratio,
Connector,
]

let tableData = getTableData(data)

<div className>
<LoadedTable
visibleColumns
title=" "
hideTitle=true
actualData={tableData}
entity=failureReasonsTableEntity
resultsPerPage=10
totalResults={tableData->Array.length}
offset
setOffset
defaultSort
currrentFetchCount={tableData->Array.length}
tableLocalFilter=false
tableheadingClass=tableBorderClass
tableBorderClass
ignoreHeaderBg=true
tableDataBorderClass=tableBorderClass
isAnalyticsModule=true
/>
</div>
}
}

@react.component
let make = (~entity: moduleEntity) => {
open LogicUtils
open APIUtils
let getURL = useGetURL()
let updateDetails = useUpdateMethod()
let (screenState, setScreenState) = React.useState(_ => PageLoaderWrapper.Loading)
let {filterValueJson} = React.useContext(FilterContext.filterContext)
let (tableData, setTableData) = React.useState(_ => JSON.Encode.array([]))
let startTimeVal = filterValueJson->getString("startTime", "")
let endTimeVal = filterValueJson->getString("endTime", "")

let getRefundsProcessed = async () => {
setScreenState(_ => PageLoaderWrapper.Loading)
try {
let url = getURL(
~entityName=ANALYTICS_REFUNDS,
~methodType=Post,
~id=Some((entity.domain: domain :> string)),
)

let groupByNames = switch entity.requestBodyConfig.groupBy {
| Some(dimentions) =>
dimentions
->Array.map(item => (item: dimension :> string))
->Some
| _ => None
}

let body = NewAnalyticsUtils.requestBody(
~startTime=startTimeVal,
~endTime=endTimeVal,
~delta=entity.requestBodyConfig.delta,
~metrics=entity.requestBodyConfig.metrics,
~groupByNames,
)

let response = await updateDetails(url, body, Post)

let metaData = response->getDictFromJsonObject->getArrayFromDict("metaData", [])

let data =
response
->getDictFromJsonObject
->getArrayFromDict("queryData", [])
->modifyQuery(metaData)

if data->Array.length > 0 {
setTableData(_ => data->JSON.Encode.array)
setScreenState(_ => PageLoaderWrapper.Success)
} else {
setScreenState(_ => PageLoaderWrapper.Custom)
}
} catch {
| _ => setScreenState(_ => PageLoaderWrapper.Custom)
}
}

React.useEffect(() => {
if startTimeVal->isNonEmptyString && endTimeVal->isNonEmptyString {
getRefundsProcessed()->ignore
}
None
}, [startTimeVal, endTimeVal])

<div>
<ModuleHeader title={entity.title} />
<Card>
<PageLoaderWrapper
screenState customLoader={<Shimmer layoutId=entity.title />} customUI={<NoData />}>
<TableModule data={tableData} className="ml-6 mr-5 mt-6 mb-5" />
</PageLoaderWrapper>
</Card>
</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type failreResonsColsTypes =
| Refund_Error_Message
| Refund_Error_Message_Count
| Total_Refund_Error_Message_Count
| Refund_Error_Message_Count_Ratio
| Connector

type failreResonsObjectType = {
connector: string,
refund_error_message: string,
refund_error_message_count: int,
total_refund_error_message_count: int,
refund_error_message_count_ratio: float,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
open NewAnalyticsTypes
open FailureReasonsRefundsTypes
open LogicUtils

let getStringFromVariant = value => {
switch value {
| Refund_Error_Message => "refund_error_message"
| Refund_Error_Message_Count => "refund_error_message_count"
| Total_Refund_Error_Message_Count => "total_refund_error_message_count"
| Refund_Error_Message_Count_Ratio => "refund_error_message_count_ratio"
| Connector => "connector"
}
}

let tableItemToObjMapper: Dict.t<JSON.t> => failreResonsObjectType = dict => {
{
refund_error_message: dict->getString(Refund_Error_Message->getStringFromVariant, ""),
refund_error_message_count: dict->getInt(Refund_Error_Message_Count->getStringFromVariant, 0),
total_refund_error_message_count: dict->getInt(
Total_Refund_Error_Message_Count->getStringFromVariant,
0,
),
refund_error_message_count_ratio: dict->getFloat(
Refund_Error_Message_Count_Ratio->getStringFromVariant,
0.0,
),
connector: dict->getString(Connector->getStringFromVariant, ""),
}
}

let getObjects: JSON.t => array<failreResonsObjectType> = json => {
json
->LogicUtils.getArrayFromJson([])
->Array.map(item => {
tableItemToObjMapper(item->getDictFromJsonObject)
})
}

let getHeading = colType => {
switch colType {
| Refund_Error_Message =>
Table.makeHeaderInfo(
~key=Refund_Error_Message->getStringFromVariant,
~title="Error Reason",
~dataType=TextType,
)
| Refund_Error_Message_Count =>
Table.makeHeaderInfo(
~key=Refund_Error_Message_Count->getStringFromVariant,
~title="Count",
~dataType=TextType,
)
| Total_Refund_Error_Message_Count =>
Table.makeHeaderInfo(
~key=Total_Refund_Error_Message_Count->getStringFromVariant,
~title="",
~dataType=TextType,
)
| Refund_Error_Message_Count_Ratio =>
Table.makeHeaderInfo(
~key=Refund_Error_Message_Count_Ratio->getStringFromVariant,
~title="Ratio",
~dataType=TextType,
)
| Connector =>
Table.makeHeaderInfo(
~key=Connector->getStringFromVariant,
~title="Connector",
~dataType=TextType,
)
}
}

let getCell = (obj, colType): Table.cell => {
open NewAnalyticsUtils
switch colType {
| Refund_Error_Message => Text(obj.refund_error_message)
| Refund_Error_Message_Count => Text(obj.refund_error_message_count->Int.toString)
| Total_Refund_Error_Message_Count => Text(obj.total_refund_error_message_count->Int.toString)
| Refund_Error_Message_Count_Ratio =>
Text(obj.refund_error_message_count_ratio->valueFormatter(Rate))
| Connector => Text(obj.connector)
}
}

let getTableData = json => {
json->getArrayDataFromJson(tableItemToObjMapper)->Array.map(Nullable.make)
}

let modifyQuery = (queryData, metaData) => {
let totalCount = switch metaData->Array.get(0) {
| Some(val) => {
let valueDict = val->getDictFromJsonObject
let failure_reason_count =
valueDict->getInt(Total_Refund_Error_Message_Count->getStringFromVariant, 0)
failure_reason_count
}
| _ => 0
}

let modifiedQuery = if totalCount > 0 {
queryData->Array.map(query => {
let valueDict = query->getDictFromJsonObject
let failure_reason_count =
valueDict->getInt(Refund_Error_Message_Count->getStringFromVariant, 0)
let ratio = failure_reason_count->Int.toFloat /. totalCount->Int.toFloat *. 100.0

valueDict->Dict.set(
Refund_Error_Message_Count_Ratio->getStringFromVariant,
ratio->JSON.Encode.float,
)
valueDict->JSON.Encode.object
})
} else {
queryData
}

modifiedQuery->Array.sort((queryA, queryB) => {
let valueDictA = queryA->getDictFromJsonObject
let valueDictB = queryB->getDictFromJsonObject

let failure_reason_countA =
valueDictA->getInt(Refund_Error_Message_Count->getStringFromVariant, 0)
let failure_reason_countB =
valueDictB->getInt(Refund_Error_Message_Count->getStringFromVariant, 0)

compareLogic(failure_reason_countA, failure_reason_countB)
})

modifiedQuery
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ let make = () => {
<RefundsSuccessRate
entity={refundsSuccessRateEntity} chartEntity={refundsSuccessRateChartEntity}
/>
<FailedRefundsDistribution
entity={failedRefundsDistributionEntity} chartEntity={failedRefundsDistributionChartEntity}
/>
<SuccessfulRefundsDistribution
entity={successfulRefundsDistributionEntity}
chartEntity={successfulRefundsDistributionChartEntity}
/>
<FailedRefundsDistribution
entity={failedRefundsDistributionEntity} chartEntity={failedRefundsDistributionChartEntity}
/>
<RefundsReasons entity={refundsReasonsEntity} />
<FailureReasonsRefunds entity={failureReasonsEntity} />
</div>
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,31 @@ let failedRefundsDistributionTableEntity = {
)
}

// Refunds Failure Reasons
let failureReasonsEntity: moduleEntity = {
requestBodyConfig: {
delta: false,
metrics: [#sessionized_refund_error_message],
groupBy: [#refund_error_message, #connector],
},
title: "Failed Refund Error Reasons",
domain: #refunds,
}

let failureReasonsTableEntity = {
open FailureReasonsRefundsUtils
EntityType.makeEntity(
~uri=``,
~getObjects,
~dataKey="queryData",
~defaultColumns=[],
~requiredSearchFieldsList=[],
~allColumns=[],
~getCell,
~getHeading,
)
}

// Refunds Reasons
let refundsReasonsEntity: moduleEntity = {
requestBodyConfig: {
Expand Down

0 comments on commit 971ecca

Please sign in to comment.