calculate deposit wire transfer

This commit is contained in:
Sebastian 2023-03-31 14:51:58 -03:00
parent b08bb05a40
commit 0f3b38745b
No known key found for this signature in database
GPG Key ID: 173909D1A5F66069

View File

@ -1571,6 +1571,38 @@ function RefundDetails({ amount }: { amount: AmountWithFee }): VNode {
);
}
type AmountAmountByWireTransferByWire = {
id: string;
amount: string;
}[];
function calculateAmountByWireTransfer(
state: TransactionDeposit["trackingState"],
): AmountAmountByWireTransferByWire {
const allTracking = Object.values(state ?? {});
//group tracking by wtid, sum amounts
const trackByWtid = allTracking.reduce((prev, cur) => {
const fee = Amounts.parseOrThrow(cur.wireFee);
const raw = Amounts.parseOrThrow(cur.amountRaw);
const total = !prev[cur.wireTransferId]
? raw
: Amounts.add(prev[cur.wireTransferId].total, raw).amount;
prev[cur.wireTransferId] = {
total,
fee,
};
return prev;
}, {} as Record<string, { total: AmountJson; fee: AmountJson }>);
//remove wire fee from total amount
return Object.entries(trackByWtid).map(([id, info]) => ({
id,
amount: Amounts.stringify(Amounts.sub(info.total, info.fee).amount),
}));
}
function TrackingDepositDetails({
trackingState,
}: {
@ -1578,18 +1610,7 @@ function TrackingDepositDetails({
}): VNode {
const { i18n } = useTranslationContext();
const trackByWtid = Object.values(trackingState ?? {}).reduce((prev, cur) => {
const am = Amounts.parseOrThrow(cur.amountEffective);
const sum = !prev[cur.wireTransferId]
? am
: Amounts.add(prev[cur.wireTransferId], am).amount;
prev[cur.wireTransferId] = sum;
return prev;
}, {} as Record<string, AmountJson>);
const wireTransfers = Object.entries(trackByWtid).map(([id, amountJson]) => ({
id,
amount: Amounts.stringify(amountJson),
}));
const wireTransfers = calculateAmountByWireTransfer(trackingState);
return (
<PurchaseDetailsTable>