wallet-core: refunds transactions should be sorted after payments

This commit is contained in:
Florian Dold 2022-06-01 10:47:46 +02:00
parent 59a2119dcb
commit f9192d986f
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B

View File

@ -96,6 +96,19 @@ function shouldSkipSearch(
return true; return true;
} }
/**
* Fallback order of transactions that have the same timestamp.
*/
const txOrder: { [t in TransactionType]: number } = {
[TransactionType.Withdrawal]: 1,
[TransactionType.Tip]: 2,
[TransactionType.Payment]: 3,
[TransactionType.Refund]: 4,
[TransactionType.Deposit]: 5,
[TransactionType.Refresh]: 6,
[TransactionType.Tip]: 7,
};
/** /**
* Retrieve the full event history for this wallet. * Retrieve the full event history for this wallet.
*/ */
@ -306,8 +319,10 @@ export async function getTransactions(
} }
let totalRefundRaw = Amounts.getZero(contractData.amount.currency); let totalRefundRaw = Amounts.getZero(contractData.amount.currency);
let totalRefundEffective = Amounts.getZero(contractData.amount.currency); let totalRefundEffective = Amounts.getZero(
const refunds: RefundInfoShort[] = [] contractData.amount.currency,
);
const refunds: RefundInfoShort[] = [];
for (const groupKey of refundGroupKeys.values()) { for (const groupKey of refundGroupKeys.values()) {
const refundTombstoneId = makeEventId( const refundTombstoneId = makeEventId(
@ -353,7 +368,7 @@ export async function getTransactions(
timestamp: r0.obtainedTime, timestamp: r0.obtainedTime,
amountEffective: Amounts.stringify(amountEffective), amountEffective: Amounts.stringify(amountEffective),
amountRaw: Amounts.stringify(amountRaw), amountRaw: Amounts.stringify(amountRaw),
}) });
} }
} }
if (!r0) { if (!r0) {
@ -361,7 +376,10 @@ export async function getTransactions(
} }
totalRefundRaw = Amounts.add(totalRefundRaw, amountRaw).amount; totalRefundRaw = Amounts.add(totalRefundRaw, amountRaw).amount;
totalRefundEffective = Amounts.add(totalRefundEffective, amountEffective).amount; totalRefundEffective = Amounts.add(
totalRefundEffective,
amountEffective,
).amount;
transactions.push({ transactions.push({
type: TransactionType.Refund, type: TransactionType.Refund,
info, info,
@ -370,7 +388,10 @@ export async function getTransactions(
timestamp: r0.obtainedTime, timestamp: r0.obtainedTime,
amountEffective: Amounts.stringify(amountEffective), amountEffective: Amounts.stringify(amountEffective),
amountRaw: Amounts.stringify(amountRaw), amountRaw: Amounts.stringify(amountRaw),
refundPending: pr.refundAwaiting === undefined ? undefined : Amounts.stringify(pr.refundAwaiting), refundPending:
pr.refundAwaiting === undefined
? undefined
: Amounts.stringify(pr.refundAwaiting),
pending: false, pending: false,
frozen: false, frozen: false,
}); });
@ -383,7 +404,10 @@ export async function getTransactions(
amountEffective: Amounts.stringify(pr.totalPayCost), amountEffective: Amounts.stringify(pr.totalPayCost),
totalRefundRaw: Amounts.stringify(totalRefundRaw), totalRefundRaw: Amounts.stringify(totalRefundRaw),
totalRefundEffective: Amounts.stringify(totalRefundEffective), totalRefundEffective: Amounts.stringify(totalRefundEffective),
refundPending: pr.refundAwaiting === undefined ? undefined : Amounts.stringify(pr.refundAwaiting), refundPending:
pr.refundAwaiting === undefined
? undefined
: Amounts.stringify(pr.refundAwaiting),
status: pr.timestampFirstSuccessfulPay status: pr.timestampFirstSuccessfulPay
? PaymentStatus.Paid ? PaymentStatus.Paid
: PaymentStatus.Accepted, : PaymentStatus.Accepted,
@ -398,7 +422,6 @@ export async function getTransactions(
frozen: pr.payFrozen ?? false, frozen: pr.payFrozen ?? false,
...(err ? { error: err } : {}), ...(err ? { error: err } : {}),
}); });
}); });
tx.tips.iter().forEachAsync(async (tipRecord) => { tx.tips.iter().forEachAsync(async (tipRecord) => {
@ -434,18 +457,19 @@ export async function getTransactions(
const txPending = transactions.filter((x) => x.pending); const txPending = transactions.filter((x) => x.pending);
const txNotPending = transactions.filter((x) => !x.pending); const txNotPending = transactions.filter((x) => !x.pending);
txPending.sort((h1, h2) => const txCmp = (h1: Transaction, h2: Transaction) => {
AbsoluteTime.cmp( const tsCmp = AbsoluteTime.cmp(
AbsoluteTime.fromTimestamp(h1.timestamp), AbsoluteTime.fromTimestamp(h1.timestamp),
AbsoluteTime.fromTimestamp(h2.timestamp), AbsoluteTime.fromTimestamp(h2.timestamp),
), );
); if (tsCmp === 0) {
txNotPending.sort((h1, h2) => return Math.sign(txOrder[h1.type] - txOrder[h2.type]);
AbsoluteTime.cmp( }
AbsoluteTime.fromTimestamp(h1.timestamp), return tsCmp;
AbsoluteTime.fromTimestamp(h2.timestamp), };
),
); txPending.sort(txCmp);
txNotPending.sort(txCmp);
return { transactions: [...txNotPending, ...txPending] }; return { transactions: [...txNotPending, ...txPending] };
} }