feat: add hourly portfolio sync history
This commit is contained in:
@@ -8,7 +8,6 @@ import { StructuredLogger } from '../../../../common/logging/structured-logger.s
|
||||
import { isTbankErrorCode, TbankErrorCode } from '../../../../integrations/tbank/tbank-error-code';
|
||||
import {
|
||||
InstrumentMetadataService,
|
||||
type MetadataIdentity,
|
||||
metadataKey,
|
||||
TbankAccessService,
|
||||
TbankClient,
|
||||
@@ -47,47 +46,9 @@ export class PortfolioService {
|
||||
private readonly logger: StructuredLogger,
|
||||
) {}
|
||||
|
||||
async saved(userId: string, accountId: string, observe = false) {
|
||||
// Local reads never start upstream work; the hourly walk owns refreshes.
|
||||
async saved(userId: string, accountId: string) {
|
||||
const connection = await this.access.local(userId, accountId);
|
||||
if (!observe) {
|
||||
const discovery = await this.membership.request(
|
||||
userId,
|
||||
connection.revision,
|
||||
);
|
||||
if (discovery) {
|
||||
void discovery.done
|
||||
.then(async () => {
|
||||
const current = await this.access.local(userId, accountId);
|
||||
if (current.revision !== connection.revision) return;
|
||||
for (const account of current.accounts.filter(
|
||||
(account) =>
|
||||
accountId === 'all' || account.accountId === accountId,
|
||||
)) {
|
||||
await this.queueSnapshot(
|
||||
userId,
|
||||
current.revision,
|
||||
account.accountId,
|
||||
);
|
||||
}
|
||||
})
|
||||
.catch(() =>
|
||||
this.logger.event(
|
||||
'warn',
|
||||
'tbank.portfolio.discovery_not_available',
|
||||
{ userId },
|
||||
),
|
||||
);
|
||||
}
|
||||
for (const account of (discovery ? [] : connection.accounts).filter(
|
||||
(account) => accountId === 'all' || account.accountId === accountId,
|
||||
)) {
|
||||
await this.queueSnapshot(
|
||||
userId,
|
||||
connection.revision,
|
||||
account.accountId,
|
||||
);
|
||||
}
|
||||
}
|
||||
const saved = await this.repository.read(userId, connection.revision);
|
||||
if (
|
||||
saved?.membershipAt &&
|
||||
@@ -96,26 +57,6 @@ export class PortfolioService {
|
||||
) {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
if (!observe) {
|
||||
const identities = new Map<string, MetadataIdentity>();
|
||||
for (const account of (saved?.accounts ?? []).filter(
|
||||
(account) => accountId === 'all' || account.accountId === accountId,
|
||||
)) {
|
||||
const snapshot = account.portfolio?.payload as unknown as
|
||||
PortfolioDto | undefined;
|
||||
for (const position of snapshot?.positions ?? []) {
|
||||
identities.set(metadataKey(position), position);
|
||||
}
|
||||
}
|
||||
const savedMetadata = await this.metadata.savedMany([
|
||||
...identities.values(),
|
||||
]);
|
||||
for (const identity of identities.values()) {
|
||||
if (!savedMetadata.get(metadataKey(identity))?.fresh) {
|
||||
await this.metadata.request(userId, connection.revision, identity);
|
||||
}
|
||||
}
|
||||
}
|
||||
const membershipMeta = investmentResourceMeta({
|
||||
resourceKey: 'accounts/membership',
|
||||
dataRevision: String(saved?.membershipRevision ?? MISSING_DATA_REVISION),
|
||||
@@ -159,8 +100,8 @@ export class PortfolioService {
|
||||
};
|
||||
}
|
||||
|
||||
async read(userId: string, accountId: string, observe = false) {
|
||||
const saved = await this.saved(userId, accountId, observe);
|
||||
async read(userId: string, accountId: string) {
|
||||
const saved = await this.saved(userId, accountId);
|
||||
const missing =
|
||||
saved.membership.availability === 'missing' ||
|
||||
saved.accounts.some((account) => !account.snapshot);
|
||||
@@ -176,7 +117,7 @@ export class PortfolioService {
|
||||
dataRevision: source.dataRevision,
|
||||
availability: 'complete',
|
||||
fetchedAt: source.fetchedAt,
|
||||
freshForMs: tbankSourceTtlMs.metadata,
|
||||
freshForMs: portfolioFreshForMs,
|
||||
work: null,
|
||||
}),
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user