refactor: replace ignored numeric literals with constants

This commit is contained in:
2026-09-28 16:37:01 +03:00
committed by ksv741
parent a970eaaf7a
commit b78c215ff1
60 changed files with 510 additions and 283 deletions
@@ -2,6 +2,9 @@ import { BadRequestException } from '@nestjs/common';
import type { InvestmentScreenDto } from './tbank-sync.dto';
import { ISO_DATE_LENGTH, ISO_DATE_START } from '../../../common/time';
import { MIN_INSTRUMENT_QUERY_LENGTH } from '../instrument-query';
// Reject fields from other screens before reserving any source work.
export function normalizeScreen(
input: InvestmentScreenDto,
@@ -61,7 +64,7 @@ export function normalizeScreen(
}
if (
['bonds', 'stocks'].includes(input.screen) &&
(!query || query.length < 2)
(!query || query.length < MIN_INSTRUMENT_QUERY_LENGTH)
) {
fail();
}
@@ -81,7 +84,7 @@ export function normalizeScreen(
if (
input.screen === 'events' &&
input.to &&
(input.from ?? new Date().toISOString().slice(0, 10)) > input.to
(input.from ?? new Date().toISOString().slice(ISO_DATE_START, ISO_DATE_LENGTH)) > input.to
) {
fail();
}
@@ -21,11 +21,14 @@ import {
ValidateNested,
} from 'class-validator';
import { MAX_INSTRUMENT_QUERY_LENGTH, MIN_INSTRUMENT_QUERY_LENGTH } from '../instrument-query';
import { InvestmentResourceMetaDto } from '../investment-resource.dto';
import { operationCategories } from '../operations/operations.api';
const MIN_ACCOUNT_ID_LENGTH = 1;
const MIN_SYNC_PAGE_SIZE = 1;
const maxAccountIdLength = 256;
const maxQueryLength = 80;
const maxPageSize = 1000;
export const investmentScreens = [
@@ -51,7 +54,7 @@ export class InvestmentScreenDto {
@ApiPropertyOptional()
@IsOptional()
@IsString()
@MinLength(1)
@MinLength(MIN_ACCOUNT_ID_LENGTH)
@MaxLength(maxAccountIdLength)
accountId?: string;
@@ -83,8 +86,8 @@ export class InvestmentScreenDto {
@ApiPropertyOptional()
@IsOptional()
@IsString()
@MinLength(2)
@MaxLength(maxQueryLength)
@MinLength(MIN_INSTRUMENT_QUERY_LENGTH)
@MaxLength(MAX_INSTRUMENT_QUERY_LENGTH)
query?: string;
@ApiPropertyOptional({ enum: ['month', 'halfYear', 'year', 'all'] })
@@ -104,10 +107,10 @@ export class InvestmentScreenDto {
@IsIn(operationCategories, { each: true })
categories?: (typeof operationCategories)[number][];
@ApiPropertyOptional({ minimum: 1, maximum: maxPageSize })
@ApiPropertyOptional({ minimum: MIN_SYNC_PAGE_SIZE, maximum: maxPageSize })
@IsOptional()
@IsInt()
@Min(1)
@Min(MIN_SYNC_PAGE_SIZE)
@Max(maxPageSize)
limit?: number;
}
@@ -8,6 +8,7 @@ import { randomUUID } from 'node:crypto';
import type { InvestmentResourceMetaDto } from '../investment-resource.dto';
import { ISO_DATE_LENGTH, ISO_DATE_START } from '../../../common/time';
import {
InstrumentMetadataService,
TbankAccessService,
@@ -41,6 +42,11 @@ import {
TbankSyncRepository,
} from './tbank-sync.repository';
const FOREGROUND_CANDLE_PRIORITY = 0;
const EXCLUSIVE_END_OFFSET_MS = 1;
const PENDING_CHILD_COUNT = 0;
const COMPLETED_CHILD_COUNT = 1;
@Injectable()
export class TbankSyncService {
private readonly runId = randomUUID();
@@ -279,8 +285,8 @@ export class TbankSyncService {
'bond-events',
],
{
from: source.from.toISOString().slice(0, 10),
to: source.to.toISOString().slice(0, 10),
from: source.from.toISOString().slice(ISO_DATE_START, ISO_DATE_LENGTH),
to: source.to.toISOString().slice(ISO_DATE_START, ISO_DATE_LENGTH),
ticker:
positions.find((position) => position.instrumentUid === uid)
?.ticker ?? undefined,
@@ -312,7 +318,7 @@ export class TbankSyncService {
},
now,
true,
0,
FOREGROUND_CANDLE_PRIORITY,
),
]);
}
@@ -479,7 +485,7 @@ export class TbankSyncService {
await add(
this.ledger.read(userId, accountId, {
from,
to: new Date(Date.parse(end) - 1).toISOString(),
to: new Date(Date.parse(end) - EXCLUSIVE_END_OFFSET_MS).toISOString(),
...(instrumentUid ? { instrumentUid } : {}),
sync: 'observe',
}),
@@ -509,7 +515,7 @@ export class TbankSyncService {
state,
errorCode: observed.errorCode,
progress: {
completed: state === 'running' ? 0 : 1,
completed: state === 'running' ? PENDING_CHILD_COUNT : COMPLETED_CHILD_COUNT,
total: 1,
discovering: false,
},