Skip to content

Commit

Permalink
feat: 🎸 return legs information when creating instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
sansan committed Apr 9, 2024
1 parent 7983657 commit 88d6d5c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 19 deletions.
10 changes: 10 additions & 0 deletions src/settlements/models/created-instruction.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import { ApiProperty } from '@nestjs/swagger';
import { Instruction } from '@polymeshassociation/polymesh-sdk/types';
import { Type } from 'class-transformer';

import { FromEntity } from '~/common/decorators/transformation';
import { TransactionQueueModel } from '~/common/models/transaction-queue.model';
import { LegModel } from '~/settlements/models/leg.model';

export class CreatedInstructionModel extends TransactionQueueModel {
@ApiProperty({
Expand All @@ -15,6 +17,14 @@ export class CreatedInstructionModel extends TransactionQueueModel {
@FromEntity()
readonly instruction: Instruction;

@ApiProperty({
description: 'List of Legs in the Instruction',
type: LegModel,
isArray: true,
})
@Type(() => LegModel)
readonly legs: LegModel[];

constructor(model: CreatedInstructionModel) {
const { transactions, details, ...rest } = model;
super({ transactions, details });
Expand Down
10 changes: 8 additions & 2 deletions src/settlements/settlements.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TransferError,
VenueType,
} from '@polymeshassociation/polymesh-sdk/types';
import { when } from 'jest-when';

import { PaginatedResultsModel } from '~/common/models/paginated-results.model';
import { createPortfolioIdentifierModel } from '~/portfolios/portfolios.util';
Expand Down Expand Up @@ -105,9 +106,13 @@ describe('SettlementsController', () => {

describe('createInstruction', () => {
it('should create an instruction and return the data returned by the service', async () => {
const mockInstruction = new MockInstruction();

when(mockInstruction.getLegs).calledWith().mockResolvedValue({ data: [] });

const mockData = {
...txResult,
result: 'fakeInstruction',
result: mockInstruction,
};
mockSettlementsService.createInstruction.mockResolvedValue(mockData);

Expand All @@ -116,7 +121,8 @@ describe('SettlementsController', () => {

expect(result).toEqual({
...txResult,
instruction: 'fakeInstruction',
instruction: mockInstruction, // in jest the @FromEntity decorator is not applied
legs: [],
});
});
});
Expand Down
12 changes: 8 additions & 4 deletions src/settlements/settlements.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { InstructionAffirmationModel } from '~/settlements/models/instruction-af
import { TransferBreakdownModel } from '~/settlements/models/transfer-breakdown.model';
import { VenueDetailsModel } from '~/settlements/models/venue-details.model';
import { SettlementsService } from '~/settlements/settlements.service';
import { createInstructionModel } from '~/settlements/settlements.util';
import { createInstructionModel, legsToLegModel } from '~/settlements/settlements.util';

@ApiTags('settlements')
@Controller()
Expand Down Expand Up @@ -81,16 +81,20 @@ export class SettlementsController {
): Promise<TransactionResponseModel> {
const serviceResult = await this.settlementsService.createInstruction(id, createInstructionDto);

const resolver: TransactionResolver<Instruction> = ({
const resolver: TransactionResolver<Instruction> = async ({
result: instruction,
transactions,
details,
}) =>
new CreatedInstructionModel({
}) => {
const { data: legs } = await instruction.getLegs();

return new CreatedInstructionModel({
instruction,
details,
transactions,
legs: legsToLegModel(legs),
});
};

return handleServiceResult(serviceResult, resolver);
}
Expand Down
34 changes: 21 additions & 13 deletions src/settlements/settlements.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Instruction,
InstructionStatus,
InstructionType,
Leg,
} from '@polymeshassociation/polymesh-sdk/types';

import { EventIdentifierModel } from '~/common/models/event-identifier.model';
Expand All @@ -10,18 +11,13 @@ import { createPortfolioIdentifierModel } from '~/portfolios/portfolios.util';
import { InstructionModel } from '~/settlements/models/instruction.model';
import { LegModel } from '~/settlements/models/leg.model';

export async function createInstructionModel(instruction: Instruction): Promise<InstructionModel> {
const [details, legsResultSet, instructionStatus, mediators] = await Promise.all([
instruction.details(),
instruction.getLegs(),
instruction.getStatus(),
instruction.getMediators(),
]);

const { status, createdAt, tradeDate, valueDate, venue, type, memo } = details;
export function legsToLegModel(legs: Leg[]): LegModel[] {
if (!legs.length) {
return [];
}

const legs = legsResultSet.data
?.map(leg => {
return legs
.map(leg => {
const { from: legFrom, to: legTo, asset } = leg;
const from = createPortfolioIdentifierModel(legFrom);
const to = createPortfolioIdentifierModel(legTo);
Expand All @@ -45,10 +41,22 @@ export async function createInstructionModel(instruction: Instruction): Promise<
});
}

/* istanbul ignore next */
return null;
})
.filter(leg => !!leg) as LegModel[]; // filters out "off chain" legs, in case they were used
.filter(leg => !!leg) as LegModel[];
}

export async function createInstructionModel(instruction: Instruction): Promise<InstructionModel> {
const [details, legsResultSet, instructionStatus, mediators] = await Promise.all([
instruction.details(),
instruction.getLegs(),
instruction.getStatus(),
instruction.getMediators(),
]);

const { status, createdAt, tradeDate, valueDate, venue, type, memo } = details;

const legs = legsToLegModel(legsResultSet.data);

let instructionModelParams: ConstructorParameters<typeof InstructionModel>[0] = {
status,
Expand Down
6 changes: 6 additions & 0 deletions src/test-utils/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ export class MockInstruction {
public affirmAsMediator = jest.fn();
public rejectAsMediator = jest.fn();
public withdrawAsMediator = jest.fn();
public toHuman = jest.fn().mockImplementation(() => {
return {
id: '1',
did,
};
});
}

export class MockVenue {
Expand Down

0 comments on commit 88d6d5c

Please sign in to comment.