Skip to content

Commit 483c0f9

Browse files
committed
refactor: collapseSmallPayments logic
1 parent 2d8e8a9 commit 483c0f9

3 files changed

Lines changed: 61 additions & 54 deletions

File tree

pages/api/payments/download/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ export default async (req: any, res: any): Promise<void> => {
4343
networkIdArray = [networkId]
4444
};
4545
const transactions = await fetchAllPaymentsByUserId(userId, networkIdArray)
46-
47-
await downloadTxsFile(res, quoteSlug, timezone, transactions, networkTicker)
46+
await downloadTxsFile(res, quoteSlug, timezone, transactions)
4847
} catch (error: any) {
4948
switch (error.message) {
5049
case RESPONSE_MESSAGES.METHOD_NOT_ALLOWED.message:

services/transactionService.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,10 @@ export async function fetchAllPaymentsByUserId (
763763
gt: 0
764764
}
765765
},
766-
include: includePaybuttonsAndPrices
766+
include: includePaybuttonsAndPrices,
767+
orderBy: {
768+
timestamp: 'asc'
769+
}
767770
})
768771

769772
return transactions

utils/files.ts

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -123,78 +123,83 @@ export const collapseSmallPayments = (
123123
collapseThreshold: number): TransactionFileData[] => {
124124
const treatedPayments: TransactionFileData[] = []
125125
let tempGroup: TransactionsWithPaybuttonsAndPrices[] = []
126+
let totalPaymentsTreated = 0
127+
const pushTempGroup = (): void => {
128+
const totalAmount = tempGroup.reduce((sum, p) => sum + Number(p.amount), 0)
129+
const totalValue = tempGroup.reduce((sum, p) => sum + Number(getTransactionValue(p)[currency]), 0)
130+
const rate = totalValue / totalAmount
131+
const buttonName = tempGroup[0].address.paybuttons[0].paybutton.name
132+
const notes = `${buttonName} - ${tempGroup.length.toString()} transactions`
133+
134+
totalPaymentsTreated += tempGroup.length
135+
136+
treatedPayments.push({
137+
amount: totalAmount,
138+
value: totalValue,
139+
date: moment.tz(tempGroup[0].timestamp * 1000, timezone),
140+
transactionId: DEFAULT_MULTI_VALUES_LINE_LABEL,
141+
rate,
142+
currency,
143+
address: DEFAULT_MULTI_VALUES_LINE_LABEL,
144+
notes
145+
} as TransactionFileData)
126146

127-
payments.forEach((tx: TransactionsWithPaybuttonsAndPrices, index: number) => {
147+
tempGroup = []
148+
}
149+
const pushTx = (tx: TransactionsWithPaybuttonsAndPrices): void => {
128150
const { timestamp, hash, address, amount } = tx
129151
const values = getTransactionValue(tx)
130152
const value = Number(values[currency])
131153
const rate = value / Number(amount)
132-
const dateKey = moment.tz(timestamp * 1000, timezone).format('YYYY-MM-DD')
133154

155+
const notes = ''
156+
157+
treatedPayments.push({
158+
amount,
159+
value,
160+
date: moment.tz(timestamp * 1000, timezone),
161+
transactionId: hash,
162+
rate,
163+
currency,
164+
address: address.address,
165+
notes
166+
} as TransactionFileData)
167+
totalPaymentsTreated += 1
168+
}
169+
170+
payments.forEach((tx: TransactionsWithPaybuttonsAndPrices, index: number) => {
171+
const { timestamp } = tx
172+
const values = getTransactionValue(tx)
173+
const value = Number(values[currency])
174+
const dateKey = moment.tz(timestamp * 1000, timezone).format('YYYY-MM-DD')
134175
const nextPayment = payments[index + 1]
135176
const nextDateKey = (nextPayment !== undefined) ? moment.tz(nextPayment.timestamp * 1000, timezone).format('YYYY-MM-DD') : null
136177

137-
if (value < collapseThreshold) {
178+
if ((value < collapseThreshold)) {
138179
tempGroup.push(tx)
139180
} else {
140-
if (tempGroup.length > 0) {
141-
const totalAmount = tempGroup.reduce((sum, p) => sum + Number(p.amount), 0)
142-
const totalValue = tempGroup.reduce((sum, p) => sum + Number(getTransactionValue(p)[currency]), 0)
143-
const rate = totalValue / totalAmount
144-
const buttonName = tempGroup[0].address.paybuttons[0].paybutton.name
145-
const notes = `${buttonName} - ${tempGroup.length.toString()} transactions`
146-
147-
treatedPayments.push({
148-
amount: totalAmount,
149-
value: totalValue,
150-
date: moment.tz(tempGroup[0].timestamp * 1000, timezone),
151-
transactionId: DEFAULT_MULTI_VALUES_LINE_LABEL,
152-
rate,
153-
currency,
154-
address: DEFAULT_MULTI_VALUES_LINE_LABEL,
155-
notes
156-
} as TransactionFileData)
157-
181+
if (tempGroup.length > 1) {
182+
pushTempGroup()
183+
} else if (tempGroup.length === 1) {
184+
pushTx(tempGroup[0])
158185
tempGroup = []
159186
}
160-
161-
const notes = ''
162-
163-
treatedPayments.push({
164-
amount,
165-
value,
166-
date: moment.tz(timestamp * 1000, timezone),
167-
transactionId: hash,
168-
rate,
169-
currency,
170-
address: address.address,
171-
notes
172-
} as TransactionFileData)
187+
pushTx(tx)
173188
}
174189

175190
// If it's the last small payment in sequence or the next payment is from another day, collapse it
176191
if (tempGroup.length > 1 && ((nextPayment === undefined) || nextDateKey !== dateKey)) {
177-
const totalAmount = tempGroup.reduce((sum, p) => sum + Number(p.amount), 0)
178-
const totalValue = tempGroup.reduce((sum, p) => sum + Number(getTransactionValue(p)[currency]), 0)
179-
const rate = totalValue / totalAmount
180-
const buttonName = tempGroup[0].address.paybuttons[0].paybutton.name
181-
const notes = `${buttonName} - ${tempGroup.length.toString()} transactions`
182-
183-
treatedPayments.push({
184-
amount: totalAmount,
185-
value: totalValue,
186-
date: moment.tz(tempGroup[0].timestamp * 1000, timezone),
187-
transactionId: DEFAULT_MULTI_VALUES_LINE_LABEL,
188-
rate,
189-
currency,
190-
address: DEFAULT_MULTI_VALUES_LINE_LABEL,
191-
notes
192-
} as TransactionFileData)
193-
192+
pushTempGroup()
193+
} else if ((tempGroup.length === 1) && nextDateKey !== dateKey) {
194+
pushTx(tempGroup[0])
194195
tempGroup = []
195196
}
196197
})
197198

199+
if (totalPaymentsTreated !== payments.length) {
200+
throw new Error('Error to collapse payments')
201+
}
202+
198203
return treatedPayments
199204
}
200205

0 commit comments

Comments
 (0)