Skip to content

Commit

Permalink
Merge pull request #211 from PolymeshAssociation/feat/DA-49
Browse files Browse the repository at this point in the history
feat: 🎸 add endpoint to retrieve grouped instructions
  • Loading branch information
VictorVicente authored Sep 18, 2023
2 parents f868609 + af80fd5 commit 3fb5b6e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 5 deletions.
22 changes: 21 additions & 1 deletion src/identities/identities.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })),
});

Expand Down Expand Up @@ -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,
});
});
});
});
21 changes: 20 additions & 1 deletion src/identities/identities.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -273,7 +274,7 @@ export class IdentitiesController {
})
@Get(':did/pending-instructions')
public async getPendingInstructions(@Param() { did }: DidDto): Promise<ResultsModel<BigNumber>> {
const { pending } = await this.settlementsService.findPendingInstructionsByDid(did);
const { pending } = await this.settlementsService.findGroupedInstructionsByDid(did);

return new ResultsModel({ results: pending.map(({ id }) => id) });
}
Expand Down Expand Up @@ -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<GroupedInstructionModel> {
const result = await this.settlementsService.findGroupedInstructionsByDid(did);

return new GroupedInstructionModel(result);
}
}
38 changes: 38 additions & 0 deletions src/settlements/models/grouped-instructions.model.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion src/settlements/settlements.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion src/settlements/settlements.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class SettlementsService {
private readonly assetsService: AssetsService
) {}

public async findPendingInstructionsByDid(did: string): Promise<GroupedInstructions> {
public async findGroupedInstructionsByDid(did: string): Promise<GroupedInstructions> {
const identity = await this.identitiesService.findOne(did);

return identity.getInstructions();
Expand Down
2 changes: 1 addition & 1 deletion src/test-utils/service-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 3fb5b6e

Please sign in to comment.