Skip to content

Commit 53bc329

Browse files
committed
fix: 🐛 return ResultSet for getHistoricalInstructions
BREAKING CHANGE: 🧨 changes data structure returned by methods (Identity.getHistoricalInstructions & Settlements.getHistoricalInstructions)
1 parent 543d17e commit 53bc329

File tree

4 files changed

+80
-12
lines changed

4 files changed

+80
-12
lines changed

src/api/client/Settlements.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import {
2121
InstructionAffirmationOperation,
2222
InstructionIdParams,
2323
ProcedureMethod,
24+
ResultSet,
2425
} from '~/types';
2526
import { Ensured } from '~/types/utils';
2627
import { middlewareInstructionToHistoricInstruction } from '~/utils/conversion';
27-
import { createProcedureMethod } from '~/utils/internal';
28+
import { calculateNextKey, createProcedureMethod } from '~/utils/internal';
2829

2930
/**
3031
* Handles all Settlement related functionality
@@ -135,19 +136,29 @@ export class Settlements {
135136
*/
136137
public async getHistoricalInstructions(
137138
filter: HistoricalInstructionFilters
138-
): Promise<HistoricInstruction[]> {
139+
): Promise<ResultSet<HistoricInstruction>> {
139140
const { context } = this;
140141

141142
const query = await historicalInstructionsQuery(filter, context);
142143

143144
const {
144145
data: {
145-
instructions: { nodes: instructionsResult },
146+
instructions: { nodes: instructionsResult, totalCount },
146147
},
147148
} = await context.queryMiddleware<Ensured<Query, 'instructions'>>(query);
148149

149-
return instructionsResult.map(instruction =>
150-
middlewareInstructionToHistoricInstruction(instruction!, context)
150+
const data = instructionsResult.map(middlewareInstruction =>
151+
middlewareInstructionToHistoricInstruction(middlewareInstruction, context)
151152
);
153+
154+
const count = new BigNumber(totalCount);
155+
156+
const next = calculateNextKey(count, data.length, filter.start);
157+
158+
return {
159+
data,
160+
next,
161+
count,
162+
};
152163
}
153164
}

src/api/client/__tests__/Settlements.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ describe('Settlements Class', () => {
210210

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

213-
expect(result).toEqual([mockHistoricInstruction]);
213+
expect(result).toEqual({
214+
data: [mockHistoricInstruction],
215+
next: new BigNumber(1),
216+
count: new BigNumber(5),
217+
});
214218
});
215219
});
216220
});

src/api/entities/Identity/__tests__/index.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,12 @@ describe('Identity class', () => {
12811281
});
12821282

12831283
describe('method: getHistoricalInstructions', () => {
1284+
let calculateNextSpy: jest.SpyInstance;
1285+
1286+
beforeAll(() => {
1287+
calculateNextSpy = jest.spyOn(utilsInternalModule, 'calculateNextKey');
1288+
});
1289+
12841290
it('should return the list of all instructions where the Identity was involved', async () => {
12851291
const identity = new Identity({ did: 'someDid' }, context);
12861292
const middlewareInstructionToHistoricInstructionSpy = jest.spyOn(
@@ -1305,7 +1311,47 @@ describe('Identity class', () => {
13051311

13061312
const result = await identity.getHistoricalInstructions();
13071313

1308-
expect(result).toEqual([mockHistoricInstruction]);
1314+
expect(result).toEqual({
1315+
data: [mockHistoricInstruction],
1316+
next: new BigNumber(1),
1317+
count: new BigNumber(5),
1318+
});
1319+
expect(calculateNextSpy).toHaveBeenCalledWith(new BigNumber(5), 1, undefined);
1320+
});
1321+
1322+
it('should return the list of all instructions where the Identity was involved when using pagination', async () => {
1323+
const identity = new Identity({ did: 'someDid' }, context);
1324+
const start = new BigNumber(0);
1325+
1326+
const middlewareInstructionToHistoricInstructionSpy = jest.spyOn(
1327+
utilsConversionModule,
1328+
'middlewareInstructionToHistoricInstruction'
1329+
);
1330+
1331+
calculateNextSpy = jest.spyOn(utilsInternalModule, 'calculateNextKey');
1332+
const instructionsResponse = {
1333+
totalCount: 5,
1334+
nodes: [{ id: '1' }],
1335+
};
1336+
1337+
const query = await historicalInstructionsQuery({ identity: identity.did }, context);
1338+
1339+
dsMockUtils.createApolloQueryMock(query, {
1340+
instructions: instructionsResponse,
1341+
});
1342+
1343+
const mockHistoricInstruction = 'mockData' as unknown as HistoricInstruction;
1344+
1345+
middlewareInstructionToHistoricInstructionSpy.mockReturnValue(mockHistoricInstruction);
1346+
1347+
const result = await identity.getHistoricalInstructions({ start });
1348+
1349+
expect(result).toEqual({
1350+
data: [mockHistoricInstruction],
1351+
next: new BigNumber(1),
1352+
count: new BigNumber(5),
1353+
});
1354+
expect(calculateNextSpy).toHaveBeenCalledWith(new BigNumber(5), 1, start);
13091355
});
13101356
});
13111357

src/api/entities/Identity/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -907,20 +907,27 @@ export class Identity extends Entity<UniqueIdentifiers, string> {
907907
*/
908908
public async getHistoricalInstructions(
909909
filter?: Omit<HistoricalInstructionFilters, 'identity'>
910-
): Promise<HistoricInstruction[]> {
910+
): Promise<ResultSet<HistoricInstruction>> {
911911
const { context, did } = this;
912912

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

915915
const {
916916
data: {
917-
instructions: { nodes: instructionsResult },
917+
instructions: { nodes: instructionsResult, totalCount },
918918
},
919919
} = await context.queryMiddleware<Ensured<Query, 'instructions'>>(query);
920920

921-
return instructionsResult.map(instruction =>
922-
middlewareInstructionToHistoricInstruction(instruction, context)
923-
);
921+
const count = new BigNumber(totalCount);
922+
const next = calculateNextKey(count, instructionsResult.length, filter?.start);
923+
924+
return {
925+
data: instructionsResult.map(instruction =>
926+
middlewareInstructionToHistoricInstruction(instruction, context)
927+
),
928+
next,
929+
count,
930+
};
924931
}
925932

926933
/**

0 commit comments

Comments
 (0)