Skip to content

Commit

Permalink
Fix asset transaction filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
sophialittlejohn committed Dec 4, 2024
1 parent 20e39d3 commit c26a47f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
34 changes: 33 additions & 1 deletion src/Reports/Processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { mockAssetTransactions } from '../tests/mocks/mockAssetTransactions.js'
import { PoolSnapshot } from '../queries/poolSnapshots.js'
import { Currency } from '../utils/BigInt.js'
import { PoolFeeSnapshot, PoolFeeSnapshotsByDate } from '../queries/poolFeeSnapshots.js'
import { ProfitAndLossReportPrivateCredit, ProfitAndLossReportPublicCredit } from './types.js'
import {
AssetTransactionReportFilter,
ProfitAndLossReportPrivateCredit,
ProfitAndLossReportPublicCredit,
} from './types.js'
import { InvestorTransaction } from '../queries/investorTransactions.js'

describe('Processor', () => {
Expand Down Expand Up @@ -477,6 +481,34 @@ describe('Processor', () => {
})
expect(result).to.have.lengthOf(3)
})
it('should filter by assetId', () => {
const result = processor.assetTransactions(
{
assetTransactions: mockAssetTransactions,
},
{ assetId: 'asset-1' }
)
expect(result).to.have.lengthOf(2)
})
it('should filter by transaction type', () => {
const types: { type: AssetTransactionReportFilter['transactionType']; expected: number }[] = [
{ type: 'created', expected: 0 },
{ type: 'financed', expected: 1 },
{ type: 'repaid', expected: 1 },
{ type: 'priced', expected: 0 },
{ type: 'closed', expected: 0 },
{ type: 'cashTransfer', expected: 1 },
]
for (const { type, expected } of types) {
const result = processor.assetTransactions(
{
assetTransactions: mockAssetTransactions,
},
{ transactionType: type }
)
expect(result).to.have.lengthOf(expected)
}
})
})
describe('applyGrouping', () => {
const applyGrouping = processor['applyGrouping']
Expand Down
29 changes: 25 additions & 4 deletions src/Reports/Processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,31 @@ export class Processor {
assetTransactions(data: AssetTransactionsData, filter?: AssetTransactionReportFilter): AssetTransactionReport[] {
return data.assetTransactions
.filter((day) => {
return (
(!filter?.assetId || filter.assetId === day.asset.id) &&
(!filter?.transactionType || filter.transactionType === day.type)
)
if (!filter?.transactionType || filter.transactionType === 'all') {
return true
}
if (filter.transactionType === 'created') {
return day.type === 'CREATED'
}
if (filter.transactionType === 'financed') {
return day.type === 'BORROWED'
}
if (filter.transactionType === 'repaid') {
return day.type === 'REPAID'
}
if (filter.transactionType === 'priced') {
return day.type === 'PRICED'
}
if (filter.transactionType === 'closed') {
return day.type === 'CLOSED'
}
if (filter.transactionType === 'cashTransfer') {
return day.type === 'CASH_TRANSFER'
}
return true
})
.filter((day) => {
return !filter?.assetId || filter.assetId === day.asset.id.split('-')[1]
})
.map((day) => ({
type: 'assetTransactions',
Expand Down
31 changes: 30 additions & 1 deletion src/Reports/Reports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,38 @@ describe('Reports', () => {
const pool = await centrifuge.pool(anemoyPoolId)
const report = await pool.reports.assetTransactions({
from: '2024-01-01T22:11:29.776Z',
to: '2024-10-03T22:11:29.776Z',
to: '2024-01-03T22:11:29.776Z',
})
expect(report.length).to.equal(5)
})
it('should return empty array when no transactions found', async () => {
const anemoyPoolId = '4139607887'
const pool = await centrifuge.pool(anemoyPoolId)
const report = await pool.reports.assetTransactions({
to: '2024-01-01T22:11:29.776Z',
from: '2024-01-01T22:11:29.776Z',
})
expect(report).to.deep.equal([])
})
it('should filter by transaction type', async () => {
const anemoyPoolId = '4139607887'
const pool = await centrifuge.pool(anemoyPoolId)
const report = await pool.reports.assetTransactions({
from: '2024-06-30T22:11:29.776Z',
to: '2024-12-04T22:11:29.776Z',
transactionType: 'financed',
})
expect(report.length).to.equal(13)
})
it('should filter by asset id', async () => {
const anemoyPoolId = '4139607887'
const pool = await centrifuge.pool(anemoyPoolId)
const report = await pool.reports.assetTransactions({
from: '2024-01-01T22:11:29.776Z',
to: '2024-12-04T22:11:29.776Z',
assetId: '14',
})
expect(report.length).to.equal(2)
})
})
})
2 changes: 1 addition & 1 deletion src/Reports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class Reports extends Entity {
/**
* Reports are split into two types:
* - A `Report` is a standard report: balanceSheet, cashflow, profitAndLoss
* - A `DataReport` is a custom report: investorTransactions
* - A `DataReport` is a custom report: investorTransactions, assetTransactions
*/
_generateReport<T>(type: Report, filter?: ReportFilter): Query<T[]>
_generateReport<T>(type: DataReport, filter?: DataReportFilter): Query<T[]>
Expand Down
2 changes: 1 addition & 1 deletion src/Reports/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,5 @@ export type AssetTransactionReportFilter = {
from?: string
to?: string
assetId?: string
transactionType?: AssetTransactionType
transactionType?: 'created' | 'financed' | 'repaid' | 'priced' | 'closed' | 'cashTransfer' | 'all'
}

0 comments on commit c26a47f

Please sign in to comment.