Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add missing ResultSet interface to paginated queries #1441

Draft
wants to merge 4 commits into
base: alpha
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/api/client/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ export class Network {
/**
* Retrieve a list of events. Can be filtered using parameters
*
* @deprecated
* @param opts.moduleId - type of the module to fetch
* @param opts.eventId - type of the event to fetch
* @param opts.eventArg0 - event parameter value to filter by in position 0
Expand Down
21 changes: 16 additions & 5 deletions src/api/client/Settlements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import {
InstructionAffirmationOperation,
InstructionIdParams,
ProcedureMethod,
ResultSet,
} from '~/types';
import { Ensured } from '~/types/utils';
import { middlewareInstructionToHistoricInstruction } from '~/utils/conversion';
import { createProcedureMethod } from '~/utils/internal';
import { calculateNextKey, createProcedureMethod } from '~/utils/internal';

/**
* Handles all Settlement related functionality
Expand Down Expand Up @@ -135,19 +136,29 @@ export class Settlements {
*/
public async getHistoricalInstructions(
filter: HistoricalInstructionFilters
): Promise<HistoricInstruction[]> {
): Promise<ResultSet<HistoricInstruction>> {
const { context } = this;

const query = await historicalInstructionsQuery(filter, context);

const {
data: {
instructions: { nodes: instructionsResult },
instructions: { nodes: instructionsResult, totalCount },
},
} = await context.queryMiddleware<Ensured<Query, 'instructions'>>(query);

return instructionsResult.map(instruction =>
middlewareInstructionToHistoricInstruction(instruction!, context)
const data = instructionsResult.map(middlewareInstruction =>
middlewareInstructionToHistoricInstruction(middlewareInstruction, context)
);

const count = new BigNumber(totalCount);

const next = calculateNextKey(count, data.length, filter.start);

return {
data,
next,
count,
};
}
}
6 changes: 5 additions & 1 deletion src/api/client/__tests__/Settlements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ describe('Settlements Class', () => {

const result = await settlements.getHistoricalInstructions({});

expect(result).toEqual([mockHistoricInstruction]);
expect(result).toEqual({
data: [mockHistoricInstruction],
next: new BigNumber(1),
count: new BigNumber(5),
});
});
});
});
1 change: 1 addition & 0 deletions src/api/entities/Asset/NonFungible/AssetHolders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class AssetHolders extends Namespace<NftCollection> {

return {
data,
count: new BigNumber(totalCount),
next,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ describe('AssetHolder class', () => {
}),
]),
next: null,
count: new BigNumber(2),
});
});
});
Expand Down
48 changes: 47 additions & 1 deletion src/api/entities/Identity/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,12 @@ describe('Identity class', () => {
});

describe('method: getHistoricalInstructions', () => {
let calculateNextSpy: jest.SpyInstance;

beforeAll(() => {
calculateNextSpy = jest.spyOn(utilsInternalModule, 'calculateNextKey');
});

it('should return the list of all instructions where the Identity was involved', async () => {
const identity = new Identity({ did: 'someDid' }, context);
const middlewareInstructionToHistoricInstructionSpy = jest.spyOn(
Expand All @@ -1305,7 +1311,47 @@ describe('Identity class', () => {

const result = await identity.getHistoricalInstructions();

expect(result).toEqual([mockHistoricInstruction]);
expect(result).toEqual({
data: [mockHistoricInstruction],
next: new BigNumber(1),
count: new BigNumber(5),
});
expect(calculateNextSpy).toHaveBeenCalledWith(new BigNumber(5), 1, undefined);
});

it('should return the list of all instructions where the Identity was involved when using pagination', async () => {
const identity = new Identity({ did: 'someDid' }, context);
const start = new BigNumber(0);

const middlewareInstructionToHistoricInstructionSpy = jest.spyOn(
utilsConversionModule,
'middlewareInstructionToHistoricInstruction'
);

calculateNextSpy = jest.spyOn(utilsInternalModule, 'calculateNextKey');
const instructionsResponse = {
totalCount: 5,
nodes: [{ id: '1' }],
};

const query = await historicalInstructionsQuery({ identity: identity.did }, context);

dsMockUtils.createApolloQueryMock(query, {
instructions: instructionsResponse,
});

const mockHistoricInstruction = 'mockData' as unknown as HistoricInstruction;

middlewareInstructionToHistoricInstructionSpy.mockReturnValue(mockHistoricInstruction);

const result = await identity.getHistoricalInstructions({ start });

expect(result).toEqual({
data: [mockHistoricInstruction],
next: new BigNumber(1),
count: new BigNumber(5),
});
expect(calculateNextSpy).toHaveBeenCalledWith(new BigNumber(5), 1, start);
});
});

Expand Down
17 changes: 12 additions & 5 deletions src/api/entities/Identity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -907,20 +907,27 @@ export class Identity extends Entity<UniqueIdentifiers, string> {
*/
public async getHistoricalInstructions(
filter?: Omit<HistoricalInstructionFilters, 'identity'>
): Promise<HistoricInstruction[]> {
): Promise<ResultSet<HistoricInstruction>> {
const { context, did } = this;

const query = await historicalInstructionsQuery({ ...filter, identity: did }, context);

const {
data: {
instructions: { nodes: instructionsResult },
instructions: { nodes: instructionsResult, totalCount },
},
} = await context.queryMiddleware<Ensured<Query, 'instructions'>>(query);

return instructionsResult.map(instruction =>
middlewareInstructionToHistoricInstruction(instruction, context)
);
const count = new BigNumber(totalCount);
const next = calculateNextKey(count, instructionsResult.length, filter?.start);

return {
data: instructionsResult.map(instruction =>
middlewareInstructionToHistoricInstruction(instruction, context)
),
next,
count,
};
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/base/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,7 @@ export class Context {
* Retrieve POLYX transactions for a given identity or list of accounts
*
* @note uses the middleware V2
* @note supports pagination
*/
public async getPolyxTransactions(args: {
identity?: string | Identity;
Expand Down
Loading