From d35615c33173d78b2dad9f910879fb9b7a718cc3 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 31 Oct 2026 12:39:19 +0100 Subject: [PATCH] Fix validation and allocation of holdings in asset profiles --- .../symbol-profile.service.spec.ts | 44 ++++++++++++++ .../symbol-profile/symbol-profile.service.ts | 5 +- libs/common/src/lib/dtos/holding.dto.spec.ts | 57 +++++++++++++++++++ libs/common/src/lib/dtos/holding.dto.ts | 19 ++++++- 4 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 libs/common/src/lib/dtos/holding.dto.spec.ts diff --git a/apps/api/src/services/symbol-profile/symbol-profile.service.spec.ts b/apps/api/src/services/symbol-profile/symbol-profile.service.spec.ts index bf28718ebc..1309693577 100644 --- a/apps/api/src/services/symbol-profile/symbol-profile.service.spec.ts +++ b/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'; diff --git a/apps/api/src/services/symbol-profile/symbol-profile.service.ts b/apps/api/src/services/symbol-profile/symbol-profile.service.ts index 98a1ce1cba..3d69f7b850 100644 --- a/apps/api/src/services/symbol-profile/symbol-profile.service.ts +++ b/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 }; diff --git a/libs/common/src/lib/dtos/holding.dto.spec.ts b/libs/common/src/lib/dtos/holding.dto.spec.ts new file mode 100644 index 0000000000..0b01ffbfeb --- /dev/null +++ b/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']); + }); +}); diff --git a/libs/common/src/lib/dtos/holding.dto.ts b/libs/common/src/lib/dtos/holding.dto.ts index 8c093e3bcb..9dc0a9be59 100644 --- a/libs/common/src/lib/dtos/holding.dto.ts +++ b/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; }