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
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:
@@ -0,0 +1,157 @@
|
||||
import {
|
||||
tbankSourceTtlMs,
|
||||
tbankSourceNeedsRefresh,
|
||||
} from '../../../../integrations/tbank/tbank.api';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { PrismaService } from '../../../../prisma/prisma.service';
|
||||
import type { Prisma } from '../../../../../generated/prisma/client';
|
||||
import type { PortfolioDto } from '../presentation/portfolio.dto';
|
||||
|
||||
type PortfolioAttempt = {
|
||||
id: string;
|
||||
attemptId: string;
|
||||
userId: string;
|
||||
revision: string;
|
||||
};
|
||||
export const portfolioFreshForMs = tbankSourceTtlMs.portfolio;
|
||||
|
||||
@Injectable()
|
||||
export class PortfolioRepository {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
read(userId: string, revision: string) {
|
||||
return this.prisma.$transaction((tx) =>
|
||||
tx.tbankConnection.findUnique({
|
||||
where: { userId_revision: { userId, revision }, invalidToken: false },
|
||||
select: {
|
||||
membershipAt: true,
|
||||
membershipRevision: true,
|
||||
accounts: {
|
||||
orderBy: { upstreamOrder: 'asc' },
|
||||
include: { portfolio: true },
|
||||
},
|
||||
work: {
|
||||
where: {
|
||||
OR: [
|
||||
{ scopeKey: 'membership' },
|
||||
{ scopeKey: { startsWith: 'portfolio:' } },
|
||||
],
|
||||
},
|
||||
select: { scopeKey: true, id: true, state: true, errorCode: true },
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
reserve(
|
||||
userId: string,
|
||||
revision: string,
|
||||
accountId: string,
|
||||
processRunId: string,
|
||||
active: ReadonlySet<string>,
|
||||
force: boolean,
|
||||
) {
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
const current = await tx.tbankConnection.updateMany({
|
||||
where: {
|
||||
userId,
|
||||
revision,
|
||||
invalidToken: false,
|
||||
accounts: { some: { accountId } },
|
||||
},
|
||||
data: { membershipRevision: { increment: 0 } },
|
||||
});
|
||||
if (!current.count) return null;
|
||||
const snapshot = await tx.tbankPortfolio.findUnique({
|
||||
where: { userId_revision_accountId: { userId, revision, accountId } },
|
||||
});
|
||||
if (
|
||||
!force &&
|
||||
snapshot &&
|
||||
!tbankSourceNeedsRefresh('portfolio', snapshot.fetchedAt, new Date())
|
||||
)
|
||||
return null;
|
||||
const scope = { userId, revision, scopeKey: portfolioWorkKey(accountId) };
|
||||
const previous = await tx.tbankSyncWork.findUnique({
|
||||
where: { userId_revision_scopeKey: scope },
|
||||
});
|
||||
if (
|
||||
previous?.state === 'running' &&
|
||||
previous.processRunId === processRunId &&
|
||||
active.has(previous.attemptId)
|
||||
)
|
||||
return { work: previous, launch: false };
|
||||
const data = {
|
||||
state: 'running',
|
||||
attemptId: randomUUID(),
|
||||
processRunId,
|
||||
priority: 1,
|
||||
intent: { source: 'portfolio', accountId },
|
||||
progress: {},
|
||||
errorCode: null,
|
||||
};
|
||||
return {
|
||||
work: await tx.tbankSyncWork.upsert({
|
||||
where: { userId_revision_scopeKey: scope },
|
||||
create: { ...scope, ...data, id: randomUUID() },
|
||||
update: data,
|
||||
}),
|
||||
launch: true,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
publish(
|
||||
attempt: PortfolioAttempt,
|
||||
accountId: string,
|
||||
snapshot: PortfolioDto,
|
||||
) {
|
||||
return this.prisma.$transaction(async (tx) => {
|
||||
const claimed = await tx.tbankSyncWork.updateMany({
|
||||
where: {
|
||||
...attempt,
|
||||
state: 'running',
|
||||
connection: {
|
||||
invalidToken: false,
|
||||
accounts: { some: { accountId } },
|
||||
},
|
||||
},
|
||||
data: { state: 'complete', progress: {} },
|
||||
});
|
||||
if (!claimed.count) return false;
|
||||
const scope = {
|
||||
userId: attempt.userId,
|
||||
revision: attempt.revision,
|
||||
accountId,
|
||||
};
|
||||
const data = {
|
||||
payload: JSON.parse(JSON.stringify(snapshot)) as Prisma.InputJsonObject,
|
||||
fetchedAt: new Date(snapshot.lastSuccessfulAt),
|
||||
};
|
||||
await tx.tbankPortfolio.upsert({
|
||||
where: { userId_revision_accountId: scope },
|
||||
create: { ...scope, ...data },
|
||||
update: { ...data, dataRevision: { increment: 1 } },
|
||||
});
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
async fail(attempt: PortfolioAttempt) {
|
||||
const result = await this.prisma.tbankSyncWork.updateMany({
|
||||
where: {
|
||||
...attempt,
|
||||
state: 'running',
|
||||
connection: { invalidToken: false },
|
||||
},
|
||||
data: { state: 'failed', errorCode: 'TBANK_UNAVAILABLE' },
|
||||
});
|
||||
return result.count === 1;
|
||||
}
|
||||
}
|
||||
|
||||
export function portfolioWorkKey(accountId: string): string {
|
||||
return `portfolio:${JSON.stringify(accountId)}`;
|
||||
}
|
||||
Reference in New Issue
Block a user