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:
@@ -1,3 +1,4 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import {
|
||||
add,
|
||||
@@ -6,6 +7,11 @@ import {
|
||||
formatDecimal,
|
||||
subtract,
|
||||
} from '../../../common/decimal';
|
||||
import { investmentResourceMeta } from '../investment-resource.mapper';
|
||||
import {
|
||||
IncomeAnalyticsResourceDto,
|
||||
IncomeSummaryResourceDto,
|
||||
} from './income-analytics.dto';
|
||||
import { DateOnlyRange, moscowDate } from '../investment-calendar';
|
||||
import {
|
||||
BrokerageOperationDto,
|
||||
@@ -13,7 +19,6 @@ import {
|
||||
} from '../operations/operations.api';
|
||||
|
||||
import {
|
||||
IncomeAnalyticsDto,
|
||||
IncomeAnalyticsEntryDto,
|
||||
IncomeComparisonDto,
|
||||
IncomeCurrencySummaryDto,
|
||||
@@ -35,24 +40,32 @@ type Total = {
|
||||
export class IncomeAnalyticsService {
|
||||
constructor(private readonly operations: BrokerageOperationsService) {}
|
||||
|
||||
async summary(userId: string, accountId: string): Promise<IncomeSummaryDto> {
|
||||
let plan: ReturnType<typeof planIncomePeriod> | undefined;
|
||||
const collected = await this.operations.collectRanges(
|
||||
async summary(
|
||||
userId: string,
|
||||
accountId: string,
|
||||
observe = false,
|
||||
): Promise<IncomeSummaryResourceDto> {
|
||||
let plan = planIncomePeriod(undefined, undefined);
|
||||
const [selected] = await this.operations.collectRanges(
|
||||
userId,
|
||||
accountId,
|
||||
(openedAt) => {
|
||||
const planned = planIncomePeriod(undefined, undefined, openedAt);
|
||||
plan = planned;
|
||||
return [planned.selected];
|
||||
plan = planIncomePeriod(undefined, undefined, openedAt);
|
||||
return [plan.selected];
|
||||
},
|
||||
observe,
|
||||
);
|
||||
if (!plan) throw new Error('Income period was not planned');
|
||||
return summaryResponse(
|
||||
accountId,
|
||||
plan.selected,
|
||||
qualifying(collected.pages[0].flatMap((page) => page.items)),
|
||||
new Date().toISOString(),
|
||||
);
|
||||
if (selected.items === null) return { data: null, meta: selected.meta };
|
||||
assert(selected.meta.lastSuccessfulAt);
|
||||
return {
|
||||
data: summaryResponse(
|
||||
accountId,
|
||||
plan.selected,
|
||||
qualifying(selected.items),
|
||||
selected.meta.lastSuccessfulAt,
|
||||
),
|
||||
meta: selected.meta,
|
||||
};
|
||||
}
|
||||
|
||||
async analytics(
|
||||
@@ -60,52 +73,80 @@ export class IncomeAnalyticsService {
|
||||
accountId: string,
|
||||
from: string | undefined,
|
||||
to: string | undefined,
|
||||
): Promise<IncomeAnalyticsDto> {
|
||||
let plan: ReturnType<typeof planIncomePeriod> | undefined;
|
||||
const collected = await this.operations.collectRanges(
|
||||
observe = false,
|
||||
): Promise<IncomeAnalyticsResourceDto> {
|
||||
let plan = planIncomePeriod(from, to);
|
||||
const periods = await this.operations.collectRanges(
|
||||
userId,
|
||||
accountId,
|
||||
(openedAt) => {
|
||||
const planned = planIncomePeriod(from, to, openedAt);
|
||||
plan = planned;
|
||||
plan = planIncomePeriod(from, to, openedAt);
|
||||
return [
|
||||
planned.selected,
|
||||
planned.previous,
|
||||
...(planned.yearAgo ? [planned.yearAgo] : []),
|
||||
plan.selected,
|
||||
plan.previous,
|
||||
...(plan.yearAgo ? [plan.yearAgo] : []),
|
||||
];
|
||||
},
|
||||
observe,
|
||||
);
|
||||
if (!plan) throw new Error('Income period was not planned');
|
||||
const selected = qualifying(
|
||||
collected.pages[0].flatMap((page) => page.items),
|
||||
).map((entry) => ({
|
||||
const [current, prior, year] = periods;
|
||||
const dependencies = periods.map((period) => period.meta);
|
||||
if (!plan.yearAgo)
|
||||
dependencies.push({
|
||||
...investmentResourceMeta({
|
||||
resourceKey: 'income/yearAgo', // gitleaks:allow public resource identifier
|
||||
dataRevision: 'unavailable',
|
||||
availability: 'missing',
|
||||
fetchedAt: null,
|
||||
freshForMs: 0,
|
||||
work: null,
|
||||
unavailableReason: 'CALENDAR_OR_ACCOUNT_HISTORY',
|
||||
}),
|
||||
});
|
||||
const states = periods.map((period) => period.meta.sync);
|
||||
const work =
|
||||
states.find((state) => state.state === 'running') ??
|
||||
states.find((state) => state.state === 'failed');
|
||||
const meta = investmentResourceMeta({
|
||||
resourceKey: `income/${accountId}/${plan.selected.from}/${plan.selected.to}`,
|
||||
dataRevision: current.meta.dataRevision,
|
||||
availability: current.meta.availability,
|
||||
fetchedAt: current.meta.lastSuccessfulAt
|
||||
? new Date(current.meta.lastSuccessfulAt)
|
||||
: null,
|
||||
freshForMs: current.meta.freshness === 'fresh' ? Infinity : 0,
|
||||
work: work?.id ? { ...work, id: work.id } : null,
|
||||
coverage: current.meta.coverage,
|
||||
dependencies,
|
||||
});
|
||||
if (current.items === null) return { data: null, meta };
|
||||
assert(current.meta.lastSuccessfulAt);
|
||||
const selected = qualifying(current.items).map((entry) => ({
|
||||
...entry,
|
||||
instrumentKey: `${instrumentKey(entry)}:${entry.payment?.currency ?? ''}`,
|
||||
}));
|
||||
const previous = qualifying(
|
||||
collected.pages[1].flatMap((page) => page.items),
|
||||
);
|
||||
const yearAgo = plan.yearAgo
|
||||
? qualifying(collected.pages[2].flatMap((page) => page.items))
|
||||
: null;
|
||||
const calculatedAt = new Date().toISOString();
|
||||
const base = summaryResponse(
|
||||
accountId,
|
||||
plan.selected,
|
||||
selected,
|
||||
calculatedAt,
|
||||
);
|
||||
return {
|
||||
...base,
|
||||
previous: comparison(selected, plan.previous, previous),
|
||||
yearComparison:
|
||||
plan.yearAgo && yearAgo
|
||||
? comparison(selected, plan.yearAgo, yearAgo)
|
||||
: null,
|
||||
bucketGranularity: plan.granularity,
|
||||
buckets: buckets(plan.selected, plan.granularity, selected),
|
||||
instruments: instruments(selected),
|
||||
entries: selected,
|
||||
data: {
|
||||
...summaryResponse(
|
||||
accountId,
|
||||
plan.selected,
|
||||
selected,
|
||||
current.meta.lastSuccessfulAt,
|
||||
),
|
||||
previous:
|
||||
prior.items === null
|
||||
? null
|
||||
: comparison(selected, plan.previous, qualifying(prior.items)),
|
||||
yearComparison:
|
||||
plan.yearAgo && year?.items !== null && year
|
||||
? comparison(selected, plan.yearAgo, qualifying(year.items))
|
||||
: null,
|
||||
bucketGranularity: plan.granularity,
|
||||
buckets: buckets(plan.selected, plan.granularity, selected),
|
||||
instruments: instruments(selected),
|
||||
entries: selected,
|
||||
},
|
||||
meta,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user