Skip to content

Commit c26a47f

Browse files
Fix asset transaction filtering
1 parent 20e39d3 commit c26a47f

5 files changed

Lines changed: 90 additions & 8 deletions

File tree

src/Reports/Processor.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import { mockAssetTransactions } from '../tests/mocks/mockAssetTransactions.js'
99
import { PoolSnapshot } from '../queries/poolSnapshots.js'
1010
import { Currency } from '../utils/BigInt.js'
1111
import { PoolFeeSnapshot, PoolFeeSnapshotsByDate } from '../queries/poolFeeSnapshots.js'
12-
import { ProfitAndLossReportPrivateCredit, ProfitAndLossReportPublicCredit } from './types.js'
12+
import {
13+
AssetTransactionReportFilter,
14+
ProfitAndLossReportPrivateCredit,
15+
ProfitAndLossReportPublicCredit,
16+
} from './types.js'
1317
import { InvestorTransaction } from '../queries/investorTransactions.js'
1418

1519
describe('Processor', () => {
@@ -477,6 +481,34 @@ describe('Processor', () => {
477481
})
478482
expect(result).to.have.lengthOf(3)
479483
})
484+
it('should filter by assetId', () => {
485+
const result = processor.assetTransactions(
486+
{
487+
assetTransactions: mockAssetTransactions,
488+
},
489+
{ assetId: 'asset-1' }
490+
)
491+
expect(result).to.have.lengthOf(2)
492+
})
493+
it('should filter by transaction type', () => {
494+
const types: { type: AssetTransactionReportFilter['transactionType']; expected: number }[] = [
495+
{ type: 'created', expected: 0 },
496+
{ type: 'financed', expected: 1 },
497+
{ type: 'repaid', expected: 1 },
498+
{ type: 'priced', expected: 0 },
499+
{ type: 'closed', expected: 0 },
500+
{ type: 'cashTransfer', expected: 1 },
501+
]
502+
for (const { type, expected } of types) {
503+
const result = processor.assetTransactions(
504+
{
505+
assetTransactions: mockAssetTransactions,
506+
},
507+
{ transactionType: type }
508+
)
509+
expect(result).to.have.lengthOf(expected)
510+
}
511+
})
480512
})
481513
describe('applyGrouping', () => {
482514
const applyGrouping = processor['applyGrouping']

src/Reports/Processor.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,31 @@ export class Processor {
213213
assetTransactions(data: AssetTransactionsData, filter?: AssetTransactionReportFilter): AssetTransactionReport[] {
214214
return data.assetTransactions
215215
.filter((day) => {
216-
return (
217-
(!filter?.assetId || filter.assetId === day.asset.id) &&
218-
(!filter?.transactionType || filter.transactionType === day.type)
219-
)
216+
if (!filter?.transactionType || filter.transactionType === 'all') {
217+
return true
218+
}
219+
if (filter.transactionType === 'created') {
220+
return day.type === 'CREATED'
221+
}
222+
if (filter.transactionType === 'financed') {
223+
return day.type === 'BORROWED'
224+
}
225+
if (filter.transactionType === 'repaid') {
226+
return day.type === 'REPAID'
227+
}
228+
if (filter.transactionType === 'priced') {
229+
return day.type === 'PRICED'
230+
}
231+
if (filter.transactionType === 'closed') {
232+
return day.type === 'CLOSED'
233+
}
234+
if (filter.transactionType === 'cashTransfer') {
235+
return day.type === 'CASH_TRANSFER'
236+
}
237+
return true
238+
})
239+
.filter((day) => {
240+
return !filter?.assetId || filter.assetId === day.asset.id.split('-')[1]
220241
})
221242
.map((day) => ({
222243
type: 'assetTransactions',

src/Reports/Reports.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,38 @@ describe('Reports', () => {
229229
const pool = await centrifuge.pool(anemoyPoolId)
230230
const report = await pool.reports.assetTransactions({
231231
from: '2024-01-01T22:11:29.776Z',
232-
to: '2024-10-03T22:11:29.776Z',
232+
to: '2024-01-03T22:11:29.776Z',
233233
})
234234
expect(report.length).to.equal(5)
235235
})
236+
it('should return empty array when no transactions found', async () => {
237+
const anemoyPoolId = '4139607887'
238+
const pool = await centrifuge.pool(anemoyPoolId)
239+
const report = await pool.reports.assetTransactions({
240+
to: '2024-01-01T22:11:29.776Z',
241+
from: '2024-01-01T22:11:29.776Z',
242+
})
243+
expect(report).to.deep.equal([])
244+
})
245+
it('should filter by transaction type', async () => {
246+
const anemoyPoolId = '4139607887'
247+
const pool = await centrifuge.pool(anemoyPoolId)
248+
const report = await pool.reports.assetTransactions({
249+
from: '2024-06-30T22:11:29.776Z',
250+
to: '2024-12-04T22:11:29.776Z',
251+
transactionType: 'financed',
252+
})
253+
expect(report.length).to.equal(13)
254+
})
255+
it('should filter by asset id', async () => {
256+
const anemoyPoolId = '4139607887'
257+
const pool = await centrifuge.pool(anemoyPoolId)
258+
const report = await pool.reports.assetTransactions({
259+
from: '2024-01-01T22:11:29.776Z',
260+
to: '2024-12-04T22:11:29.776Z',
261+
assetId: '14',
262+
})
263+
expect(report.length).to.equal(2)
264+
})
236265
})
237266
})

src/Reports/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class Reports extends Entity {
6969
/**
7070
* Reports are split into two types:
7171
* - A `Report` is a standard report: balanceSheet, cashflow, profitAndLoss
72-
* - A `DataReport` is a custom report: investorTransactions
72+
* - A `DataReport` is a custom report: investorTransactions, assetTransactions
7373
*/
7474
_generateReport<T>(type: Report, filter?: ReportFilter): Query<T[]>
7575
_generateReport<T>(type: DataReport, filter?: DataReportFilter): Query<T[]>

src/Reports/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,5 +170,5 @@ export type AssetTransactionReportFilter = {
170170
from?: string
171171
to?: string
172172
assetId?: string
173-
transactionType?: AssetTransactionType
173+
transactionType?: 'created' | 'financed' | 'repaid' | 'priced' | 'closed' | 'cashTransfer' | 'all'
174174
}

0 commit comments

Comments
 (0)