Skip to content

Commit

Permalink
feat: update sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanjoshi914 committed Jan 10, 2025
1 parent a8478b8 commit a54914e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
12 changes: 9 additions & 3 deletions src/common/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,19 @@ export async function poll<T>({
return new Promise(executePoll);
}

function hasSettleDate(
transaction: ConnectorTransaction
): transaction is ConnectorTransaction & { settleDate: number } {
return transaction.settleDate !== null;
}

export function mergeTransactions(
invoices: ConnectorTransaction[],
payments: ConnectorTransaction[]
): ConnectorTransaction[] {
const mergedTransactions = [...invoices, ...payments].sort((a, b) => {
return (b.settleDate ?? 0) - (a.settleDate ?? 0);
});
const mergedTransactions = [...invoices, ...payments]
.filter(hasSettleDate)
.sort((a, b) => b.settleDate - a.settleDate);

return mergedTransactions;
}
20 changes: 11 additions & 9 deletions src/extension/background-script/connectors/lawallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,14 @@ export default class LaWallet implements Connector {
}
);

const transactions = _transactions.map((event) => {
return {
...event,
kind: event.kind as EventKind,
};
}) as Event[];
const transactions: Event[] = _transactions
.map((event) => {
return {
...event,
kind: event.kind as EventKind,
};
})
.sort((a, b) => b.created_at - a.created_at);

const parsedTransactions: ConnectorTransaction[] = await Promise.all(
transactions.map(
Expand All @@ -127,9 +129,9 @@ export default class LaWallet implements Connector {

return {
data: {
transactions: parsedTransactions
.sort((a, b) => (b.settleDate ?? 0) - (a.settleDate ?? 0))
.filter((transaction) => transaction.settled),
transactions: parsedTransactions.filter(
(transaction) => transaction.settled
),
},
};
}
Expand Down
34 changes: 16 additions & 18 deletions src/extension/background-script/connectors/lndhub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,22 @@ export default class LndHub implements Connector {
}[]
>("GET", "/getuserinvoices", undefined);

const invoices: ConnectorTransaction[] = data
.map(
(invoice, index): ConnectorTransaction => ({
custom_records: invoice.custom_records,
id: `${invoice.payment_request}-${index}`,
memo: invoice.description,
preimage: "", // lndhub doesn't support preimage (yet)
payment_hash: invoice.payment_hash,
settled: invoice.ispaid,
settleDate: invoice.timestamp * 1000,
creationDate: invoice.timestamp * 1000,
totalAmount: invoice.amt,
type: "received",
})
)
.sort((a, b) => {
return (b.settleDate ?? 0) - (a.settleDate ?? 0);
});
data.sort((a, b) => b.timestamp - a.timestamp);

const invoices: ConnectorTransaction[] = data.map(
(invoice, index): ConnectorTransaction => ({
custom_records: invoice.custom_records,
id: `${invoice.payment_request}-${index}`,
memo: invoice.description,
preimage: "", // lndhub doesn't support preimage (yet)
payment_hash: invoice.payment_hash,
settled: invoice.ispaid,
settleDate: invoice.timestamp * 1000,
creationDate: invoice.timestamp * 1000,
totalAmount: invoice.amt,
type: "received",
})
);

return invoices;
}
Expand Down

0 comments on commit a54914e

Please sign in to comment.