@@ -123,11 +123,20 @@ export const collapseSmallPayments = (
123123 payments : TransactionsWithPaybuttonsAndPrices [ ] ,
124124 currency : SupportedQuotesType ,
125125 timezone : string ,
126- collapseThreshold : number ) : TransactionFileData [ ] => {
126+ collapseThreshold : number
127+ ) : TransactionFileData [ ] => {
127128 const treatedPayments : TransactionFileData [ ] = [ ]
128- let tempGroup : TransactionsWithPaybuttonsAndPrices [ ] = [ ]
129+ const tempGroups : Record < string , TransactionsWithPaybuttonsAndPrices [ ] > = { }
129130 let totalPaymentsTreated = 0
130- const pushTempGroup = ( ) : void => {
131+
132+ const pushTempGroup = ( groupKey : string ) : void => {
133+ const tempGroup = tempGroups [ groupKey ]
134+ if ( tempGroup === undefined || tempGroup . length === 0 ) return
135+ if ( tempGroup . length === 1 ) {
136+ pushTx ( tempGroup [ 0 ] )
137+ tempGroups [ groupKey ] = [ ]
138+ return
139+ }
131140 const totalAmount = tempGroup . reduce ( ( sum , p ) => sum + Number ( p . amount ) , 0 )
132141 const totalValue = tempGroup . reduce ( ( sum , p ) => sum + Number ( getTransactionValue ( p ) [ currency ] ) , 0 )
133142 const rate = totalValue / totalAmount
@@ -148,16 +157,15 @@ export const collapseSmallPayments = (
148157 notes
149158 } as TransactionFileData )
150159
151- tempGroup = [ ]
160+ tempGroups [ groupKey ] = [ ]
152161 }
162+
153163 const pushTx = ( tx : TransactionsWithPaybuttonsAndPrices ) : void => {
154164 const { timestamp, hash, address, amount } = tx
155165 const values = getTransactionValue ( tx )
156166 const value = Number ( values [ currency ] )
157167 const rate = value / Number ( amount )
158168
159- const notes = ''
160-
161169 treatedPayments . push ( {
162170 amount,
163171 value,
@@ -166,7 +174,7 @@ export const collapseSmallPayments = (
166174 rate,
167175 currency,
168176 address : address . address ,
169- notes,
177+ notes : '' ,
170178 newtworkId : address . networkId
171179 } as TransactionFileData )
172180 totalPaymentsTreated += 1
@@ -177,30 +185,29 @@ export const collapseSmallPayments = (
177185 const values = getTransactionValue ( tx )
178186 const value = Number ( values [ currency ] )
179187 const dateKey = moment . tz ( timestamp * 1000 , timezone ) . format ( 'YYYY-MM-DD' )
188+ const dateKeyUTC = moment . utc ( timestamp * 1000 ) . format ( 'YYYY-MM-DD' )
189+ const groupKey = `${ dateKey } _${ dateKeyUTC } `
190+
180191 const nextPayment = payments [ index + 1 ]
181- const nextDateKey = ( nextPayment !== undefined ) ? moment . tz ( nextPayment . timestamp * 1000 , timezone ) . format ( 'YYYY-MM-DD' ) : null
192+ const nextDateKey = nextPayment === undefined ? null : moment . tz ( nextPayment . timestamp * 1000 , timezone ) . format ( 'YYYY-MM-DD' )
193+ const nextDateKeyUTC = nextPayment === undefined ? null : moment . utc ( nextPayment . timestamp * 1000 ) . format ( 'YYYY-MM-DD' )
194+ const nextGroupKey = nextDateKey === null || nextDateKeyUTC === null ? null : `${ nextDateKey } _${ nextDateKeyUTC } `
182195
183- if ( ( value < collapseThreshold ) ) {
184- tempGroup . push ( tx )
196+ if ( value < collapseThreshold ) {
197+ if ( tempGroups [ groupKey ] === undefined ) tempGroups [ groupKey ] = [ ]
198+ tempGroups [ groupKey ] . push ( tx )
185199 } else {
186- if ( tempGroup . length > 1 ) {
187- pushTempGroup ( )
188- } else if ( tempGroup . length === 1 ) {
189- pushTx ( tempGroup [ 0 ] )
190- tempGroup = [ ]
191- }
200+ Object . keys ( tempGroups ) . forEach ( pushTempGroup )
192201 pushTx ( tx )
193202 }
194203
195- // If it's the last small payment in sequence or the next payment is from another day, collapse it
196- if ( tempGroup . length > 1 && ( ( nextPayment === undefined ) || nextDateKey !== dateKey ) ) {
197- pushTempGroup ( )
198- } else if ( ( tempGroup . length === 1 ) && nextDateKey !== dateKey ) {
199- pushTx ( tempGroup [ 0 ] )
200- tempGroup = [ ]
204+ if ( nextGroupKey !== groupKey ) {
205+ pushTempGroup ( groupKey )
201206 }
202207 } )
203208
209+ Object . keys ( tempGroups ) . forEach ( pushTempGroup )
210+
204211 if ( totalPaymentsTreated !== payments . length ) {
205212 throw new Error ( 'Error to collapse payments' )
206213 }
0 commit comments