feat: add hourly portfolio sync history
This commit is contained in:
@@ -2,6 +2,7 @@ import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
import { MoneyDto } from '../../../common/money.dto';
|
||||
import { InvestmentResourceMetaDto } from '../investment-resource.dto';
|
||||
import { HourlySyncStatusDto } from '../tbank-sync/tbank-sync.dto';
|
||||
|
||||
export class PortfolioSummaryDto {
|
||||
@ApiProperty({ type: MoneyDto, isArray: true })
|
||||
@@ -55,4 +56,12 @@ export class BrokerageAccountsResourceDto {
|
||||
|
||||
@ApiProperty({ type: InvestmentResourceMetaDto })
|
||||
declare meta: InvestmentResourceMetaDto;
|
||||
|
||||
@ApiProperty({
|
||||
type: HourlySyncStatusDto,
|
||||
nullable: true,
|
||||
description:
|
||||
'Read-only status of the last hourly walk for the current connection. Null when no walk attempt exists yet.',
|
||||
})
|
||||
declare sync: HourlySyncStatusDto | null;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import type { Request } from 'express';
|
||||
|
||||
import { Controller, Get, Query, Req, UseGuards } from '@nestjs/common';
|
||||
import { Controller, Get, Req, UseGuards } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOkResponse, ApiTags } from '@nestjs/swagger';
|
||||
|
||||
import { ApiProblemResponses } from '../../../common/decorators/api-safe-error-response.decorator';
|
||||
import { AccessPayload, JwtAuthGuard } from '../../auth/auth.api';
|
||||
import { InvestmentReadQueryDto } from '../investment-resource.dto';
|
||||
import { BrokerageAccountsResourceDto } from './brokerage-account.dto';
|
||||
import { BrokerageAccountsService } from './brokerage-accounts.service';
|
||||
|
||||
@@ -19,10 +18,7 @@ export class BrokerageAccountsController {
|
||||
|
||||
@Get()
|
||||
@ApiOkResponse({ type: BrokerageAccountsResourceDto })
|
||||
list(
|
||||
@Req() request: Request & { user: AccessPayload },
|
||||
@Query() query: InvestmentReadQueryDto,
|
||||
) {
|
||||
return this.accounts.list(request.user.sub, query.sync === 'observe');
|
||||
list(@Req() request: Request & { user: AccessPayload }) {
|
||||
return this.accounts.list(request.user.sub);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,12 @@ import { Module } from '@nestjs/common';
|
||||
import { TbankModule } from '../../../integrations/tbank/tbank.module';
|
||||
import { AuthModule } from '../../auth/auth.module';
|
||||
import { PortfolioModule } from '../portfolio/portfolio.module';
|
||||
import { TbankSyncModule } from '../tbank-sync/tbank-sync.module';
|
||||
import { BrokerageAccountsController } from './brokerage-accounts.controller';
|
||||
import { BrokerageAccountsService } from './brokerage-accounts.service';
|
||||
|
||||
@Module({
|
||||
imports: [AuthModule, TbankModule, PortfolioModule],
|
||||
imports: [AuthModule, TbankModule, PortfolioModule, TbankSyncModule],
|
||||
controllers: [BrokerageAccountsController],
|
||||
providers: [BrokerageAccountsService],
|
||||
exports: [BrokerageAccountsService],
|
||||
|
||||
@@ -19,16 +19,17 @@ const meta = investmentResourceMeta({
|
||||
|
||||
describe('saved brokerage account projection', () => {
|
||||
const portfolio = { saved: jest.fn() };
|
||||
const service = new BrokerageAccountsService(portfolio as never);
|
||||
const sync = { hourly: jest.fn().mockResolvedValue(null) };
|
||||
const service = new BrokerageAccountsService(portfolio as never, sync as never);
|
||||
|
||||
it('distinguishes unknown membership from a confirmed empty list', async () => {
|
||||
portfolio.saved.mockResolvedValueOnce({
|
||||
membership: { ...meta, availability: 'missing' },
|
||||
accounts: [],
|
||||
});
|
||||
expect((await service.list('owner', true)).data).toBeNull();
|
||||
expect((await service.list('owner')).data).toBeNull();
|
||||
portfolio.saved.mockResolvedValueOnce({ membership: meta, accounts: [] });
|
||||
expect((await service.list('owner', true)).data).toEqual([]);
|
||||
expect((await service.list('owner')).data).toEqual([]);
|
||||
});
|
||||
|
||||
it('keeps exact saved totals and independent missing summaries', async () => {
|
||||
@@ -58,7 +59,7 @@ describe('saved brokerage account projection', () => {
|
||||
},
|
||||
],
|
||||
});
|
||||
const response = await service.list('owner', true);
|
||||
const response = await service.list('owner');
|
||||
expect(response.data?.[0].portfolioSummary).toEqual({
|
||||
totalsByCurrency: [{ currency: 'RUB', value: '100000000.000000001' }],
|
||||
stockPositionCount: 1,
|
||||
|
||||
@@ -3,14 +3,18 @@ import { createHash } from 'node:crypto';
|
||||
|
||||
import { investmentResourceMeta } from '../investment-resource.mapper';
|
||||
import { PortfolioService } from '../portfolio/portfolio.api';
|
||||
import { BrokerageAccountDto } from './brokerage-account.dto';
|
||||
import { TbankSyncRepository } from '../tbank-sync/tbank-sync.repository';
|
||||
import { BrokerageAccountDto, BrokerageAccountsResourceDto } from './brokerage-account.dto';
|
||||
|
||||
@Injectable()
|
||||
export class BrokerageAccountsService {
|
||||
constructor(private readonly portfolio: PortfolioService) {}
|
||||
constructor(
|
||||
private readonly portfolio: PortfolioService,
|
||||
private readonly sync: TbankSyncRepository,
|
||||
) {}
|
||||
|
||||
async list(userId: string, observe = false) {
|
||||
const saved = await this.portfolio.saved(userId, 'all', observe);
|
||||
async list(userId: string): Promise<BrokerageAccountsResourceDto> {
|
||||
const saved = await this.portfolio.saved(userId, 'all');
|
||||
const data: BrokerageAccountDto[] | null =
|
||||
saved.membership.availability === 'missing'
|
||||
? null
|
||||
@@ -31,6 +35,8 @@ export class BrokerageAccountsService {
|
||||
}
|
||||
: null,
|
||||
}));
|
||||
// The read-only result of the last hourly walk; never starts one.
|
||||
const hourly = await this.sync.hourly(userId);
|
||||
return {
|
||||
data,
|
||||
meta: investmentResourceMeta({
|
||||
@@ -55,12 +61,20 @@ export class BrokerageAccountsService {
|
||||
fetchedAt: saved.membership.lastSuccessfulAt
|
||||
? new Date(saved.membership.lastSuccessfulAt)
|
||||
: null,
|
||||
freshForMs: 300_000,
|
||||
freshForMs: 3_600_000,
|
||||
work: saved.membership.sync.id
|
||||
? { ...saved.membership.sync, id: saved.membership.sync.id }
|
||||
: null,
|
||||
dependencies: saved.accounts.map((account) => account.meta),
|
||||
}),
|
||||
sync: hourly
|
||||
? {
|
||||
state: hourly.state,
|
||||
startedAt: hourly.startedAt.toISOString(),
|
||||
finishedAt: hourly.finishedAt?.toISOString() ?? null,
|
||||
sections: hourly.sections,
|
||||
}
|
||||
: null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user