refactor: replace ignored numeric literals with constants
Backend security / security (push) Successful in 8s
Backend verify / verify (push) Failing after 2m17s
Conventional commits / commits (pull_request) Successful in 31s
Backend security / security (pull_request) Successful in 8s
Backend verify / verify (pull_request) Failing after 1m56s

This commit is contained in:
2026-09-28 11:24:59 +03:00
parent 9b3558f96c
commit 9cc9fbdcff
60 changed files with 510 additions and 283 deletions
@@ -11,6 +11,7 @@ import { accountHistory } from '../../investment-sync-plan';
import {
type OperationInterval,
operationIntervals,
operationsSyncPriority,
planOperationRead,
uncoveredOperationIntervals,
} from '../domain/operations-sync-plan';
@@ -19,8 +20,9 @@ import {
type OperationAttempt,
} from '../infrastructure/brokerage-operations.repository';
const UPSTREAM_RANGE_MARGIN_SECONDS = 1;
const operationsPageSize = 1000;
const priorities = { screen: 1, history: 2 } as const;
@Injectable()
export class BrokerageOperationsSyncService {
@@ -129,7 +131,7 @@ export class BrokerageOperationsSyncService {
userId: string,
intervals: OperationInterval[],
atomic: boolean,
priority: keyof typeof priorities = 'screen',
priority: keyof typeof operationsSyncPriority = 'screen',
revision?: string,
): Promise<null | string> {
const connection = await this.access.local(userId);
@@ -162,7 +164,7 @@ export class BrokerageOperationsSyncService {
atomic,
this.runningAttempts,
// eslint-disable-next-line security/detect-object-injection -- The priority belongs to the internal priority union.
priorities[priority],
operationsSyncPriority[priority],
);
if (!requested) return null;
const { id, attemptId, revision } = requested.work;
@@ -257,12 +259,12 @@ export class BrokerageOperationsSyncService {
// Upstream inclusivity is not assumed; storage selects exact [from,to).
from: {
seconds: String(
Math.floor(interval.from.valueOf() / MILLISECONDS_PER_SECOND) - 1,
Math.floor(interval.from.valueOf() / MILLISECONDS_PER_SECOND) - UPSTREAM_RANGE_MARGIN_SECONDS,
),
},
to: {
seconds: String(
Math.ceil(interval.to.valueOf() / MILLISECONDS_PER_SECOND) + 1,
Math.ceil(interval.to.valueOf() / MILLISECONDS_PER_SECOND) + UPSTREAM_RANGE_MARGIN_SECONDS,
),
},
...(interval.instrumentUid ?
@@ -19,6 +19,7 @@ import {
} from '../../investment-sync-plan';
import {
operationIntervalsOverlap,
operationsSyncPriority,
planOperationRead,
uncoveredOperationIntervals,
} from '../domain/operations-sync-plan';
@@ -30,6 +31,14 @@ import {
} from '../presentation/brokerage-operation.dto';
import { BrokerageOperationsSyncService } from './brokerage-operations-sync.service';
const EXCLUSIVE_END_OFFSET_MS = 1;
const FIRST_RESULT_OFFSET = 0;
const MISSING_MEMBERSHIP_REVISION = 0;
const STALE_FRESHNESS_MS = 0;
const UNKNOWN_AS_OF_MS = 0;
const LAST_ROW_INDEX = -1;
const UNIX_EPOCH_MS = 0;
type LocalOperationsCursor = {
accountId: string;
at: string;
@@ -106,7 +115,7 @@ export class BrokerageOperationsService {
screen: 'operations',
accountId,
from: from.toISOString(),
to: new Date(to.valueOf() - 1).toISOString(),
to: new Date(to.valueOf() - EXCLUSIVE_END_OFFSET_MS).toISOString(),
},
{
accounts: selected,
@@ -244,7 +253,7 @@ export class BrokerageOperationsService {
const knownBoundary = accounts.every(
(account) => account.openedOn !== null,
);
const rows = saved?.rows.slice(0, limit) ?? [];
const rows = saved?.rows.slice(FIRST_RESULT_OFFSET, limit) ?? [];
const instruments = await this.metadata.savedMany(
rows.map((row) => row.payload as TbankOperationItem),
);
@@ -278,7 +287,7 @@ export class BrokerageOperationsService {
}
const membershipMeta = investmentResourceMeta({
resourceKey: 'accounts/membership',
dataRevision: String(saved?.connection.membershipRevision ?? 0),
dataRevision: String(saved?.connection.membershipRevision ?? MISSING_MEMBERSHIP_REVISION),
availability: available ? 'complete' : 'missing',
fetchedAt: saved?.connection.membershipAt ?? null,
freshForMs: tbankSourceTtlMs.membership,
@@ -296,7 +305,7 @@ export class BrokerageOperationsService {
'missing',
fetchedAt: lastSuccessfulAt,
freshForMs: plan.intervals.some((interval) => interval.stale) ?
0 :
STALE_FRESHNESS_MS :
Number.POSITIVE_INFINITY,
work: work ?? saved?.membershipWork ?? null,
dependencies: [membershipMeta, ...metadataDependencies],
@@ -321,7 +330,7 @@ export class BrokerageOperationsService {
(interval) =>
interval.accountId === account.accountId,
)
.map((interval) => interval.asOf?.valueOf() ?? 0),
.map((interval) => interval.asOf?.valueOf() ?? UNKNOWN_AS_OF_MS),
),
),
),
@@ -329,11 +338,11 @@ export class BrokerageOperationsService {
null,
historyBoundary: knownBoundary ? 'known' : 'unknown',
historyState: saved?.work.some(
(work) => work.state === 'running' && work.priority > 1,
(work) => work.state === 'running' && work.priority > operationsSyncPriority.screen,
) ?
'loading' :
saved?.work.some(
(work) => work.state === 'failed' && work.priority > 1,
(work) => work.state === 'failed' && work.priority > operationsSyncPriority.screen,
) ?
'failed' :
knownBoundary &&
@@ -346,7 +355,7 @@ export class BrokerageOperationsService {
'missing',
},
});
const last = rows.at(-1);
const last = rows.at(LAST_ROW_INDEX);
const hasNext = Boolean(saved && saved.rows.length > limit);
return {
data:
@@ -448,7 +457,7 @@ export class BrokerageOperationsService {
userId,
revision: connection.revision,
accountId,
from: new Date(0),
from: new Date(UNIX_EPOCH_MS),
to: new Date(),
categories: ['income'],
filterKey: `income/${accountId}`,
@@ -486,7 +495,7 @@ export class BrokerageOperationsService {
const now = new Date();
const membership = investmentResourceMeta({
resourceKey: 'accounts/membership',
dataRevision: String(saved?.connection.membershipRevision ?? 0),
dataRevision: String(saved?.connection.membershipRevision ?? MISSING_MEMBERSHIP_REVISION),
availability: saved?.connection.membershipAt ? 'complete' : 'missing',
fetchedAt: saved?.connection.membershipAt ?? null,
freshForMs: tbankSourceTtlMs.membership,
@@ -559,7 +568,7 @@ export class BrokerageOperationsService {
'partial' :
'missing',
fetchedAt,
freshForMs: plan.intervals.some((range) => range.stale) ? 0 : Infinity,
freshForMs: plan.intervals.some((range) => range.stale) ? STALE_FRESHNESS_MS : Infinity,
work,
dependencies: [membership],
coverage: {