diff --git a/src/identities/identities.controller.spec.ts b/src/identities/identities.controller.spec.ts index 86f15a44..2ddb890e 100644 --- a/src/identities/identities.controller.spec.ts +++ b/src/identities/identities.controller.spec.ts @@ -140,7 +140,7 @@ describe('IdentitiesController', () => { describe('getPendingInstructions', () => { it("should return the Identity's pending Instructions", async () => { const expectedInstructionIds = [new BigNumber(1), new BigNumber(2), new BigNumber(3)]; - mockSettlementsService.findPendingInstructionsByDid.mockResolvedValue({ + mockSettlementsService.findGroupedInstructionsByDid.mockResolvedValue({ pending: expectedInstructionIds.map(id => ({ id })), }); @@ -637,4 +637,24 @@ describe('IdentitiesController', () => { }); }); }); + + describe('getGroupedInstructions', () => { + it("should return the Identity's Instructions", async () => { + const expectedInstructionIds = [new BigNumber(1), new BigNumber(2), new BigNumber(3)]; + + mockSettlementsService.findGroupedInstructionsByDid.mockResolvedValue({ + affirmed: expectedInstructionIds.map(id => id), + pending: expectedInstructionIds.map(id => id), + failed: expectedInstructionIds.map(id => id), + }); + + const result = await controller.getGroupedInstructions({ did: '0x1' }); + + expect(result).toEqual({ + affirmed: expectedInstructionIds, + pending: expectedInstructionIds, + failed: expectedInstructionIds, + }); + }); + }); }); diff --git a/src/identities/identities.controller.ts b/src/identities/identities.controller.ts index b45ff0d5..cdeef88f 100644 --- a/src/identities/identities.controller.ts +++ b/src/identities/identities.controller.ts @@ -56,6 +56,7 @@ import { CreatedIdentityModel } from '~/identities/models/created-identity.model import { IdentityModel } from '~/identities/models/identity.model'; import { createIdentityResolver } from '~/identities/models/identity.util'; import { PolymeshLogger } from '~/logger/polymesh-logger.service'; +import { GroupedInstructionModel } from '~/settlements/models/grouped-instructions.model'; import { SettlementsService } from '~/settlements/settlements.service'; import { TickerReservationsService } from '~/ticker-reservations/ticker-reservations.service'; @@ -273,7 +274,7 @@ export class IdentitiesController { }) @Get(':did/pending-instructions') public async getPendingInstructions(@Param() { did }: DidDto): Promise> { - const { pending } = await this.settlementsService.findPendingInstructionsByDid(did); + const { pending } = await this.settlementsService.findGroupedInstructionsByDid(did); return new ResultsModel({ results: pending.map(({ id }) => id) }); } @@ -600,4 +601,22 @@ export class IdentitiesController { return new ResultsModel({ results }); } + + @Get(':did/grouped-instructions') + @ApiParam({ + name: 'did', + description: 'The DID of the Identity for which to get grouped Instructions', + type: 'string', + required: true, + example: '0x0600000000000000000000000000000000000000000000000000000000000000', + }) + @ApiOkResponse({ + description: 'Returns grouped Instructions for the Identity', + type: GroupedInstructionModel, + }) + public async getGroupedInstructions(@Param() { did }: DidDto): Promise { + const result = await this.settlementsService.findGroupedInstructionsByDid(did); + + return new GroupedInstructionModel(result); + } } diff --git a/src/settlements/models/grouped-instructions.model.ts b/src/settlements/models/grouped-instructions.model.ts new file mode 100644 index 00000000..9dd5465a --- /dev/null +++ b/src/settlements/models/grouped-instructions.model.ts @@ -0,0 +1,38 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { BigNumber } from '@polymeshassociation/polymesh-sdk'; +import { GroupedInstructions } from '@polymeshassociation/polymesh-sdk/types'; + +import { FromEntityObject } from '~/common/decorators/transformation'; + +export class GroupedInstructionModel { + @ApiProperty({ + description: 'List of affirmed Instruction ids', + isArray: true, + type: 'number', + example: [123], + }) + @FromEntityObject() + readonly affirmed: BigNumber[]; + + @ApiProperty({ + description: 'List of pending Instruction ids', + isArray: true, + type: 'number', + example: [123], + }) + @FromEntityObject() + readonly pending: BigNumber[]; + + @ApiProperty({ + description: 'List of failed Instruction ids', + isArray: true, + type: 'number', + example: [123], + }) + @FromEntityObject() + readonly failed: BigNumber[]; + + constructor(instructions: GroupedInstructions) { + Object.assign(this, instructions); + } +} diff --git a/src/settlements/settlements.service.spec.ts b/src/settlements/settlements.service.spec.ts index b04f1fc2..ee4d8ce2 100644 --- a/src/settlements/settlements.service.spec.ts +++ b/src/settlements/settlements.service.spec.ts @@ -94,7 +94,7 @@ describe('SettlementsService', () => { mockIdentity.getInstructions.mockResolvedValue(mockInstructions); - const result = await service.findPendingInstructionsByDid('0x01'); + const result = await service.findGroupedInstructionsByDid('0x01'); expect(result).toEqual(mockInstructions); }); diff --git a/src/settlements/settlements.service.ts b/src/settlements/settlements.service.ts index 3600c611..32cbf4e2 100644 --- a/src/settlements/settlements.service.ts +++ b/src/settlements/settlements.service.ts @@ -31,7 +31,7 @@ export class SettlementsService { private readonly assetsService: AssetsService ) {} - public async findPendingInstructionsByDid(did: string): Promise { + public async findGroupedInstructionsByDid(did: string): Promise { const identity = await this.identitiesService.findOne(did); return identity.getInstructions(); diff --git a/src/test-utils/service-mocks.ts b/src/test-utils/service-mocks.ts index 7ceb8570..abf98591 100644 --- a/src/test-utils/service-mocks.ts +++ b/src/test-utils/service-mocks.ts @@ -146,7 +146,7 @@ export class MockSettlementsService { createVenue = jest.fn(); modifyVenue = jest.fn(); canTransfer = jest.fn(); - findPendingInstructionsByDid = jest.fn(); + findGroupedInstructionsByDid = jest.fn(); findVenuesByOwner = jest.fn(); withdrawAffirmation = jest.fn(); rescheduleInstruction = jest.fn();