diff --git a/src/models/home.ts b/src/models/home.ts index 0617c94..c9f55c2 100644 --- a/src/models/home.ts +++ b/src/models/home.ts @@ -31,4 +31,8 @@ export interface Price { time?: string main_product?: number montzar?: number + zaarbox?: number + /** montzar | zaarbox | main_product | store = مظنه زنده، database_last = آخرین قیمت ذخیره‌شده */ + price_source?: string | null + last_historical?: number } diff --git a/src/utils/storeHours.ts b/src/utils/storeHours.ts index 6a8d325..9522a34 100644 --- a/src/utils/storeHours.ts +++ b/src/utils/storeHours.ts @@ -266,36 +266,47 @@ export function shouldShowLastSavedPrice(): boolean { return useSettingsStore().isSettingTruthy('show_last_price') } +/** فقط این منابع «آخرین قیمت ذخیره‌شده» هستند؛ باقی price_source مظنهٔ زنده است */ +const STORED_PRICE_SOURCES = ['database_last'] + function hasShowableHomePrice(p = lastPrice.value): boolean { if (!p) return false - if (Number(p.buy) > 0 || Number(p.sell) > 0 || Number(p.price_last) > 0 || Number(p.price) > 0) { - return true - } - if (shouldShowLastSavedPrice() && Number((p as { last_historical?: number }).last_historical ?? 0) > 0) { - return true - } - return false + return Number(p.buy) > 0 || Number(p.sell) > 0 || Number(p.price_last) > 0 || Number(p.price) > 0 +} + +/** قیمت از مرجع زنده آمده یا از آخرین ردیف ذخیره‌شده */ +function hasLiveQuote(p = lastPrice.value): boolean { + if (!hasShowableHomePrice(p)) return false + + const source = String((p as { price_source?: string })?.price_source ?? '').trim().toLowerCase() + if (source) return !STORED_PRICE_SOURCES.includes(source) + + return ( + Number(p?.montzar ?? 0) > 0 + || Number((p as { zaarbox?: number })?.zaarbox ?? 0) > 0 + || Number(p?.main_product ?? 0) > 0 + ) } /** * وضعیت نمایش قیمت در صفحه اصلی — همان منطق بک‌اند: - * close_price خاموش / داخل ساعت → قیمت هست open، وگرنه no_quote - * close_price روشن و خارج ساعت → show_home_price_when_closed روشن: آخرین قیمت؛ خاموش: closed + * خارج از ساعت کاری (close_price روشن) → show_home_price_when_closed روشن: آخرین قیمت؛ خاموش: closed + * داخل ساعت کاری → مظنه زنده: open ؛ وگرنه فقط با show_last_price آخرین قیمت؛ وگرنه no_quote */ export function getPriceDisplayState(): PriceDisplayState { void productTradingHoursVersion.value void useSettingsStore().settingsRevision const p = lastPrice.value - const hasPrice = hasShowableHomePrice(p) if (isOutsideGlobalStoreHours()) { - if (shouldShowHomePriceWhenClosed()) { - return hasPrice ? 'open' : 'no_quote' - } - return 'closed' + if (!shouldShowHomePriceWhenClosed()) return 'closed' + return hasShowableHomePrice(p) ? 'open' : 'no_quote' } - return hasPrice ? 'open' : 'no_quote' + if (hasLiveQuote(p)) return 'open' + if (shouldShowLastSavedPrice() && hasShowableHomePrice(p)) return 'open' + + return 'no_quote' } export function getPriceDisplayMessage(state: PriceDisplayState = getPriceDisplayState()): string {