This commit was merged in pull request #154.
This commit is contained in:
@@ -218,6 +218,18 @@ describe('persistent portfolio projection', () => {
|
||||
expect(snapshot.structure.currencies[0].totalValue).toBe('121.49');
|
||||
});
|
||||
|
||||
// Test intent: a corrupted Json payload must fail loudly instead of being spread into the API response.
|
||||
it('rejects a stored snapshot payload without the persisted shape', async () => {
|
||||
repository.read.mockResolvedValue({
|
||||
membershipAt,
|
||||
membershipRevision: 1,
|
||||
accounts: [{ accountId: 'a', name: 'A', type: 'BROKERAGE', openedOn: null,
|
||||
portfolio: { payload: { positions: 'broken' }, fetchedAt: membershipAt, dataRevision: 1 } }],
|
||||
work: [],
|
||||
});
|
||||
await expect(service.read('user', 'all')).rejects.toThrow('Corrupted portfolio snapshot payload');
|
||||
});
|
||||
|
||||
it('does not turn a missing account snapshot into aggregate zero; summaries stay independent', async () => {
|
||||
const snapshot: PortfolioDto = {
|
||||
lastSuccessfulAt: membershipAt.toISOString(),
|
||||
|
||||
@@ -4,6 +4,8 @@ import {
|
||||
} from '@nestjs/common';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
|
||||
import type { Prisma } from '../../../../../generated/prisma/client';
|
||||
|
||||
import { StructuredLogger } from '../../../../common/logging/structured-logger.service';
|
||||
import { isTbankErrorCode, TbankErrorCode } from '../../../../integrations/tbank/tbank-error-code';
|
||||
import {
|
||||
@@ -26,6 +28,7 @@ import { sumMoneyByCurrency } from '../domain/portfolio-totals';
|
||||
import {
|
||||
portfolioFreshForMs,
|
||||
PortfolioRepository,
|
||||
portfolioSnapshot,
|
||||
portfolioWorkKey,
|
||||
} from '../infrastructure/portfolio.repository';
|
||||
import { mapPortfolio } from '../infrastructure/tbank-portfolio.mapper';
|
||||
@@ -88,18 +91,7 @@ export class PortfolioService {
|
||||
type: account.type,
|
||||
openedOn: account.openedOn,
|
||||
snapshot: account.portfolio
|
||||
? {
|
||||
...(account.portfolio.payload as unknown as PortfolioDto),
|
||||
positions: (account.portfolio.payload as unknown as PortfolioDto).positions.map((position) => ({
|
||||
...position,
|
||||
sector: position.sector ?? null,
|
||||
issuer: position.issuer ?? null,
|
||||
})),
|
||||
lastSuccessfulAt: account.portfolio.fetchedAt.toISOString(),
|
||||
isStale:
|
||||
Date.now() - account.portfolio.fetchedAt.valueOf() >=
|
||||
portfolioFreshForMs,
|
||||
}
|
||||
? savedSnapshot(account.portfolio)
|
||||
: null,
|
||||
meta: investmentResourceMeta({
|
||||
resourceKey: `portfolio/${encodeURIComponent(account.accountId)}`,
|
||||
@@ -326,3 +318,17 @@ export class PortfolioService {
|
||||
return mapPortfolio(upstream, money, savedMetadata, lastSuccessfulAt, classifications);
|
||||
}
|
||||
}
|
||||
|
||||
function savedSnapshot(saved: { fetchedAt: Date; payload: Prisma.JsonValue }): PortfolioDto {
|
||||
const snapshot = portfolioSnapshot(saved.payload);
|
||||
return {
|
||||
...snapshot,
|
||||
positions: snapshot.positions.map((position) => ({
|
||||
...position,
|
||||
sector: position.sector ?? null,
|
||||
issuer: position.issuer ?? null,
|
||||
})),
|
||||
lastSuccessfulAt: saved.fetchedAt.toISOString(),
|
||||
isStale: Date.now() - saved.fetchedAt.valueOf() >= portfolioFreshForMs,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -200,3 +200,16 @@ export class PortfolioRepository {
|
||||
export function portfolioWorkKey(accountId: string): string {
|
||||
return `portfolio:${JSON.stringify(accountId)}`;
|
||||
}
|
||||
|
||||
// Json payload is written only by publish(); a shape check turns corruption into an explicit 500.
|
||||
export function portfolioSnapshot(value: Prisma.JsonValue): PortfolioDto {
|
||||
const snapshot = value as null | Partial<PortfolioDto>;
|
||||
if (
|
||||
!Array.isArray(snapshot?.positions) ||
|
||||
!Array.isArray(snapshot.totalsByCurrency) ||
|
||||
!Array.isArray(snapshot.cashBalances)
|
||||
) {
|
||||
throw new Error('Corrupted portfolio snapshot payload');
|
||||
}
|
||||
return snapshot as PortfolioDto;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user