Browse Source

Fix validation and allocation of holdings in asset profiles

pull/8359/head
Thomas Kaul 4 weeks ago
parent
commit
d35615c331
  1. 44
      apps/api/src/services/symbol-profile/symbol-profile.service.spec.ts
  2. 5
      apps/api/src/services/symbol-profile/symbol-profile.service.ts
  3. 57
      libs/common/src/lib/dtos/holding.dto.spec.ts
  4. 19
      libs/common/src/lib/dtos/holding.dto.ts

44
apps/api/src/services/symbol-profile/symbol-profile.service.spec.ts

@ -6,6 +6,50 @@ import { DataSource } from '@prisma/client';
import { SymbolProfileService } from './symbol-profile.service';
describe('SymbolProfileService', () => {
describe('getSymbolProfiles', () => {
const getHoldings = async (holdings: object[]) => {
const symbolProfileService = new SymbolProfileService({
symbolProfile: {
findMany: jest.fn().mockResolvedValue([
{
holdings,
_count: { activities: 0, watchedBy: 0 },
activities: [],
assetProfileOverrides: null,
countries: [],
currency: 'USD',
dataSource: DataSource.MANUAL,
sectors: [],
symbol: 'symbol'
}
])
}
} as unknown as PrismaService);
const [assetProfile] = await symbolProfileService.getSymbolProfiles([
{ dataSource: DataSource.MANUAL, symbol: 'symbol' }
]);
return assetProfile.holdings;
};
it('gives the allocation of a holding with a weight', async () => {
await expect(
getHoldings([{ name: 'Apple', weight: 0.5 }])
).resolves.toEqual([
expect.objectContaining({ allocationInPercentage: 0.5, name: 'Apple' })
]);
});
it('gives the allocation of a holding of an import', async () => {
await expect(
getHoldings([{ allocationInPercentage: 0.5, name: 'Apple' }])
).resolves.toEqual([
expect.objectContaining({ allocationInPercentage: 0.5, name: 'Apple' })
]);
});
});
describe('getSymbolProfilesOfUser', () => {
const userId = 'user-id';

5
apps/api/src/services/symbol-profile/symbol-profile.service.ts

@ -349,10 +349,11 @@ export class SymbolProfileService {
}
return aHoldings.map((holding) => {
const { name, weight } = holding as Prisma.JsonObject;
const { allocationInPercentage, name, weight } =
holding as Prisma.JsonObject;
return {
allocationInPercentage: weight as number,
allocationInPercentage: (weight ?? allocationInPercentage) as number,
name: (name as string) ?? UNKNOWN_KEY,
valueInBaseCurrency: undefined
};

57
libs/common/src/lib/dtos/holding.dto.spec.ts

@ -0,0 +1,57 @@
import { plainToInstance } from 'class-transformer';
import { validateSync } from 'class-validator';
import { HoldingDto } from './holding.dto';
describe('HoldingDto', () => {
const getInvalidProperties = (holding: object) => {
return validateSync(plainToInstance(HoldingDto, holding), {
forbidNonWhitelisted: true,
whitelist: true
}).map(({ property }) => {
return property;
});
};
it('Accepts the allocation as weight', () => {
expect(getInvalidProperties({ name: 'Apple', weight: 0.5 })).toEqual([]);
});
it('Accepts the allocation as allocationInPercentage', () => {
expect(
getInvalidProperties({ allocationInPercentage: 0.5, name: 'Apple' })
).toEqual([]);
});
it('Rejects a holding without an allocation', () => {
expect(getInvalidProperties({ name: 'Apple' })).toEqual([
'allocationInPercentage',
'weight'
]);
});
it('Rejects an allocation out of range', () => {
expect(getInvalidProperties({ name: 'Apple', weight: 1.5 })).toEqual([
'weight'
]);
expect(
getInvalidProperties({ allocationInPercentage: -0.5, name: 'Apple' })
).toEqual(['allocationInPercentage']);
});
it('Rejects an invalid allocation next to a valid one', () => {
expect(
getInvalidProperties({
allocationInPercentage: 1.5,
name: 'Apple',
weight: 0.5
})
).toEqual(['allocationInPercentage']);
});
it('Rejects an unknown property', () => {
expect(
getInvalidProperties({ name: 'Apple', value: 100, weight: 0.5 })
).toEqual(['value']);
});
});

19
libs/common/src/lib/dtos/holding.dto.ts

@ -1,11 +1,26 @@
import { IsNumber, IsString, Max, Min } from 'class-validator';
import { IsNumber, IsString, Max, Min, ValidateIf } from 'class-validator';
/**
* Accepts the allocation as weight (format of the asset profile) or as
* allocationInPercentage (format of the export), at least one is required
*/
export class HoldingDto {
@IsNumber()
@Max(1)
@Min(0)
allocationInPercentage: number;
@ValidateIf(({ weight }, value) => {
return value !== undefined || weight === undefined;
})
allocationInPercentage?: number;
@IsString()
name: string;
@IsNumber()
@Max(1)
@Min(0)
@ValidateIf(({ allocationInPercentage }, value) => {
return value !== undefined || allocationInPercentage === undefined;
})
weight?: number;
}

Loading…
Cancel
Save