알림목록 지표없음 문제 수정
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* 매매 시그널 알림 — strategyId·전략명으로 StrategyDto 해석
|
||||
* (서버 DB와 알림 이력의 strategyId 불일치 시 전략명 fallback)
|
||||
*/
|
||||
import {
|
||||
isStrategyMissingOnServer,
|
||||
loadStrategies,
|
||||
loadStrategy,
|
||||
type StrategyDto,
|
||||
} from './backendApi';
|
||||
import { reconcileStrategyMissingWithValidIds } from './strategyMissingCache';
|
||||
import { coerceFiniteNumber } from './safeFormat';
|
||||
|
||||
let strategiesCache: StrategyDto[] | null = null;
|
||||
let strategiesCacheAt = 0;
|
||||
const CACHE_MS = 30_000;
|
||||
|
||||
export function normalizeStrategyId(id: unknown): number | null {
|
||||
const n = coerceFiniteNumber(id);
|
||||
return n != null && n > 0 ? Math.trunc(n) : null;
|
||||
}
|
||||
|
||||
async function getStrategiesList(force = false): Promise<StrategyDto[]> {
|
||||
const now = Date.now();
|
||||
if (!force && strategiesCache && now - strategiesCacheAt < CACHE_MS) {
|
||||
return strategiesCache;
|
||||
}
|
||||
const list = await loadStrategies();
|
||||
strategiesCache = list ?? [];
|
||||
strategiesCacheAt = now;
|
||||
reconcileStrategyMissingWithValidIds(
|
||||
strategiesCache.map(s => s.id).filter((id): id is number => id != null && id > 0),
|
||||
);
|
||||
return strategiesCache;
|
||||
}
|
||||
|
||||
export interface ResolvedNotificationStrategy {
|
||||
strategy: StrategyDto | undefined;
|
||||
/** live-conditions·차트에 사용할 유효 strategyId */
|
||||
strategyId: number | null;
|
||||
}
|
||||
|
||||
export async function resolveNotificationStrategy(
|
||||
strategyId: unknown,
|
||||
strategyName?: string | null,
|
||||
prefetched?: StrategyDto[],
|
||||
): Promise<ResolvedNotificationStrategy> {
|
||||
const normalizedId = normalizeStrategyId(strategyId);
|
||||
const list = prefetched ?? await getStrategiesList();
|
||||
|
||||
if (normalizedId != null && !isStrategyMissingOnServer(normalizedId)) {
|
||||
const fromList = list.find(s => s.id === normalizedId);
|
||||
if (fromList) {
|
||||
return { strategy: fromList, strategyId: normalizedId };
|
||||
}
|
||||
const loaded = await loadStrategy(normalizedId);
|
||||
if (loaded) {
|
||||
return { strategy: loaded, strategyId: normalizedId };
|
||||
}
|
||||
}
|
||||
|
||||
const name = strategyName?.trim();
|
||||
if (name) {
|
||||
const byName = list.find(s => s.name?.trim() === name);
|
||||
if (byName?.id != null && byName.id > 0) {
|
||||
return { strategy: byName, strategyId: byName.id };
|
||||
}
|
||||
}
|
||||
|
||||
return { strategy: undefined, strategyId: normalizedId };
|
||||
}
|
||||
|
||||
/** 알림 목록 진입 시 전략 목록 선로드 */
|
||||
export async function prefetchNotificationStrategies(): Promise<StrategyDto[]> {
|
||||
return getStrategiesList(true);
|
||||
}
|
||||
Reference in New Issue
Block a user