Skip to content

Commit

Permalink
NNS1-3486: updates reporting transactions button to provide period to…
Browse files Browse the repository at this point in the history
… the service (#6033)

# Motivation

We want to filter transactions by a specific period.
Follow up PR will hook the `PeriodDateRangeSelector` to the
`PeriodTransactionsButton` so users selection will be propagated to the
service.

# Changes

- Forwards the `period` property from the `ReportingTransactionsButton`
to the service.
- Changes the utility `filterTransactionsByRange` to exclude dates that
are equal to `to`
this.https://github.com/dfinity/nns-dapp/blob/bc7b3b64cafd286bb76a5996ae29447849220163/frontend/src/lib/types/reporting.ts#L6

# Tests

- New unit tests for the component
- Updated test for the service

# Todos

- [ ] Add entry to changelog (if necessary).
Not necessary
  • Loading branch information
yhabib authored Dec 18, 2024
1 parent 082723e commit cb89929
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { ICPToken, nonNullish } from "@dfinity/utils";
import {
buildTransactionsDatasets,
convertPeriodToNanosecondRange,
CsvGenerationError,
FileSystemAccessError,
generateCsvFileToSave,
Expand All @@ -24,6 +25,9 @@
import { sortNeuronsByStake } from "$lib/utils/neuron.utils";
import { nnsAccountsListStore } from "$lib/derived/accounts-list.derived";
import { startBusy, stopBusy } from "$lib/stores/busy.store";
import type { ReportingPeriod } from "$lib/types/reporting";
export let period: ReportingPeriod = "all";
let identity: Identity | null | undefined;
let swapCanisterAccounts: Set<string>;
Expand Down Expand Up @@ -70,9 +74,11 @@
);
const entities = [...nnsAccounts, ...nnsNeurons];
const range = convertPeriodToNanosecondRange(period);
const transactions = await getAccountTransactionsConcurrently({
entities,
identity: signIdentity,
range,
});
const datasets = buildTransactionsDatasets({
transactions,
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/lib/services/reporting.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ export const mapAccountOrNeuronToTransactionEntity = (
export const getAccountTransactionsConcurrently = async ({
entities,
identity,
range,
}: {
entities: (Account | NeuronInfo)[];
identity: SignIdentity;
range?: TransactionsDateRange;
}): Promise<TransactionResults> => {
const transactionEntities = entities.map(
mapAccountOrNeuronToTransactionEntity
Expand All @@ -51,6 +53,7 @@ export const getAccountTransactionsConcurrently = async ({
getAllTransactionsFromAccountAndIdentity({
accountId: entity.identifier,
identity,
range,
})
);

Expand Down Expand Up @@ -177,7 +180,7 @@ const filterTransactionsByRange = (
}

const to = range.to;
if (nonNullish(to) && timestamp > to) {
if (nonNullish(to) && timestamp >= to) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as icpIndexApi from "$lib/api/icp-index.api";
import ReportingTransactionsButton from "$lib/components/reporting/ReportingTransactionsButton.svelte";
import * as exportDataService from "$lib/services/reporting.services";
import * as toastsStore from "$lib/stores/toasts.store";
import type { ReportingPeriod } from "$lib/types/reporting";
import * as exportToCsv from "$lib/utils/reporting.utils";
import { mockIdentity, resetIdentity } from "$tests/mocks/auth.store.mock";
import {
Expand Down Expand Up @@ -73,8 +74,15 @@ describe("ReportingTransactionsButton", () => {
});
});

const renderComponent = ({ onTrigger }: { onTrigger?: () => void } = {}) => {
const { container, component } = render(ReportingTransactionsButton);
const renderComponent = (
{
onTrigger,
period,
}: { onTrigger?: () => void; period: ReportingPeriod } = { period: "all" }
) => {
const { container, component } = render(ReportingTransactionsButton, {
period,
});
const po = ReportingTransactionsButtonPo.under({
element: new JestPageObjectElement(container),
});
Expand Down Expand Up @@ -186,6 +194,7 @@ describe("ReportingTransactionsButton", () => {
expect(spyExportDataService).toHaveBeenCalledWith({
entities: expectation,
identity: mockIdentity,
range: {},
});
});

Expand Down Expand Up @@ -226,6 +235,41 @@ describe("ReportingTransactionsButton", () => {
expect(spyExportDataService).toHaveBeenCalledWith({
entities: expectation,
identity: mockIdentity,
range: {},
});
});

it("should fetch transactions filtered by period", async () => {
const beginningOfYear = new Date("2023-01-01T00:00:00Z");
const NANOS_IN_MS = BigInt(1_000_000);
const beginningOfYearInNanoseconds =
BigInt(beginningOfYear.getTime()) * NANOS_IN_MS;

resetAccountsForTesting();
setAccountsForTesting({
main: mockMainAccount,
});

const mockNeurons: NeuronInfo[] = [mockNeuron];
spyQueryNeurons.mockResolvedValue(mockNeurons);

const po = renderComponent({ period: "year-to-date" });

expect(spyExportDataService).toBeCalledTimes(0);
expect(spyQueryNeurons).toBeCalledTimes(0);

await po.click();
await runResolvedPromises();

const expectation = [mockMainAccount, mockNeuron];
expect(spyQueryNeurons).toBeCalledTimes(1);
expect(spyExportDataService).toHaveBeenCalledTimes(1);
expect(spyExportDataService).toHaveBeenCalledWith({
entities: expectation,
identity: mockIdentity,
range: {
from: beginningOfYearInNanoseconds,
},
});
});

Expand Down
46 changes: 43 additions & 3 deletions frontend/src/tests/lib/services/reporting.services.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe("reporting service", () => {
expect(spyGetTransactions).toHaveBeenCalledTimes(2);
});

it('should filter "to" the provided date', async () => {
it('should filter "to" the provided date excluding to', async () => {
const allTransactions = [
createTransactionWithId({
id: 3n,
Expand Down Expand Up @@ -166,8 +166,8 @@ describe("reporting service", () => {
},
});

expect(result).toHaveLength(2);
expect(result).toEqual(allTransactions.slice(1));
expect(result).toHaveLength(1);
expect(result).toEqual(allTransactions.slice(2));
expect(spyGetTransactions).toHaveBeenCalledTimes(1);
});

Expand Down Expand Up @@ -455,6 +455,46 @@ describe("reporting service", () => {
expect(result[2].error).toBeUndefined();
});

it("should fetch transactions for the specified period", async () => {
const allTransactions = [
createTransactionWithId({
id: 3n,
timestamp: new Date("2023-01-02T00:00:00.000Z"),
}),
createTransactionWithId({
id: 2n,
timestamp: new Date("2023-01-01T00:00:00.000Z"),
}),
createTransactionWithId({
id: 1n,
timestamp: new Date("2022-12-31T00:00:00.000Z"),
}),
];
spyGetTransactions.mockResolvedValue({
transactions: allTransactions,
oldestTxId: 1n,
});

const beginningOfYear = dateToNanoSeconds(
new Date("2023-01-01T00:00:00.000Z")
);

const result = await getAccountTransactionsConcurrently({
entities: [mockMainAccount],
identity: mockIdentity,
range: {
from: beginningOfYear,
},
});

expect(result).toHaveLength(1);
expect(spyGetTransactions).toHaveBeenCalledTimes(1);

expect(result[0].entity).toEqual(mainAccountEntity);
expect(result[0].transactions).toEqual(allTransactions.slice(0, 2));
expect(result[0].error).toBeUndefined();
});

// TODO: To be implemented once getAccountTransactionsConcurrently handles errors
it.skip("should handle failed transactions fetch for some accounts", async () => {
spyGetTransactions
Expand Down

0 comments on commit cb89929

Please sign in to comment.