feat!: persist T-Bank data and synchronize reads
Backend security / security (push) Successful in 8s
Backend verify / verify (push) Failing after 10m45s
Conventional commits / commits (pull_request) Successful in 29s
Backend security / security (pull_request) Successful in 8s
Backend verify / verify (pull_request) Failing after 10m37s

Store account membership, portfolio, operations, forecasts and market sources in SQLite.
Coalesce scoped refreshes, fence connection revisions, preserve complete financial
snapshots and stabilize operation period state.

BREAKING CHANGE: investment reads return data/meta resource envelopes and locally
persisted snapshots; consumers must use the matching OpenAPI contract and explicit
synchronization endpoints.
This commit is contained in:
2026-09-26 21:29:15 +03:00
parent 0862d56c4e
commit d6e6c4ace5
116 changed files with 18321 additions and 5409 deletions
@@ -2,9 +2,9 @@ import { BadRequestException, Inject, Injectable } from '@nestjs/common';
import {
TbankClient,
TbankTokenService,
supportedTbankAccounts,
} from '../../../integrations/tbank/tbank.api';
import { PrismaService } from '../../../prisma/prisma.service';
import { PortfolioService } from '../portfolio/portfolio.api';
type TbankTokenValidator = Pick<TbankClient, 'validateToken'>;
@@ -14,7 +14,6 @@ export class TbankConnectionService {
private readonly prisma: PrismaService,
private readonly tokens: TbankTokenService,
@Inject(TbankClient) private readonly tbank: TbankTokenValidator,
private readonly portfolio: PortfolioService,
) {}
async status(userId: string) {
@@ -30,23 +29,34 @@ export class TbankConnectionService {
async put(userId: string, value: string) {
const token = value.trim();
if (!token) throw new BadRequestException({ code: 'VALIDATION_ERROR' });
await this.tokens.translateInvalidToken(() =>
const accounts = await this.tokens.translateInvalidToken(() =>
this.tbank.validateToken(token),
);
const encryptedToken = this.tokens.encrypt(token);
const connection = await this.prisma.tbankConnection.upsert({
where: { userId },
create: {
userId,
encryptedToken,
connectedAt: new Date(),
},
update: {
encryptedToken,
connectedAt: new Date(),
},
const connection = await this.prisma.$transaction(async (tx) => {
// Cascade old private data and attempts in the same transaction that
// publishes the validated membership and the new connection revision.
await tx.tbankConnection.deleteMany({ where: { userId } });
const now = new Date();
return tx.tbankConnection.create({
data: {
userId,
encryptedToken,
connectedAt: now,
membershipAt: now,
membershipRevision: 1,
accounts: {
create: supportedTbankAccounts(accounts).map(
({ id, ...account }, upstreamOrder) => ({
...account,
accountId: id,
upstreamOrder,
}),
),
},
},
});
});
this.portfolio.invalidate(userId);
return {
connected: true,
connectedAt: connection.connectedAt.toISOString(),
@@ -55,6 +65,5 @@ export class TbankConnectionService {
async delete(userId: string): Promise<void> {
await this.prisma.tbankConnection.deleteMany({ where: { userId } });
this.portfolio.invalidate(userId);
}
}