From ddf818ede43bee24389c64fca809f946a8d382bd Mon Sep 17 00:00:00 2001 From: adonis Date: Thu, 11 Jun 2026 21:42:06 +0330 Subject: [PATCH] fix bug 2 --- src/model/price.ts | 2 ++ src/utils/productEditorPrice.ts | 32 +++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/model/price.ts b/src/model/price.ts index 70f7b3c..567cc42 100644 --- a/src/model/price.ts +++ b/src/model/price.ts @@ -11,4 +11,6 @@ export interface last { sell: number buy: number price_last: number + /** آخرین مظنه ذخیره‌شده در جدول prices — برای fallback محصول */ + last_historical?: number } \ No newline at end of file diff --git a/src/utils/productEditorPrice.ts b/src/utils/productEditorPrice.ts index 6608422..052bc54 100644 --- a/src/utils/productEditorPrice.ts +++ b/src/utils/productEditorPrice.ts @@ -58,13 +58,19 @@ export type ProductEditorPricePreview = function resolveStoredFallbackPrice( storedPrice: number | null | undefined, - useLastPriceIfZero: boolean | undefined + useLastPriceIfZero: boolean | undefined, + lastHistoricalPrice?: number | null ): number { if (!useLastPriceIfZero) { return 0 } - return Number(storedPrice) || 0 + const stored = Number(storedPrice) || 0 + if (stored > 0) { + return stored + } + + return Number(lastHistoricalPrice) || 0 } export function computeProductEditorPricePreview(input: { @@ -78,7 +84,7 @@ export function computeProductEditorPricePreview(input: { methodOption: string meltedPriceOption: MeltedPriceSource pricesData: MethodePrices[] - appLastPrice?: (Partial & { price?: number }) | null + appLastPrice?: (Partial & { price?: number; last_historical?: number }) | null }): ProductEditorPricePreview { const offsetPercent = Number(input.form.price_offset_percent ?? 0) const melted = isMeltedTradeGroup(input.form.group_type) @@ -100,13 +106,19 @@ export function computeProductEditorPricePreview(input: { source: 'قیمت صفحه اصلی اپ' } } + const stored = Number(input.form.price) || 0 const fallback = resolveStoredFallbackPrice( input.form.price, - input.form.use_last_price_if_zero ?? false + input.form.use_last_price_if_zero ?? false, + input.appLastPrice?.last_historical ) - return fallback > 0 - ? { status: 'ready', amount: fallback, source: 'آخرین قیمت ذخیره‌شده' } - : { status: 'empty', message: 'قیمت provider صفر است' } + if (fallback > 0) { + const amount = stored > 0 + ? fallback + : adjustedProductPrice(fallback, 0, offsetPercent) + return { status: 'ready', amount, source: 'آخرین قیمت ذخیره‌شده' } + } + return { status: 'empty', message: 'قیمت provider صفر است' } } if (!input.form.reading_method) { @@ -123,7 +135,8 @@ export function computeProductEditorPricePreview(input: { } const fallback = resolveStoredFallbackPrice( input.form.price, - input.form.use_last_price_if_zero ?? false + input.form.use_last_price_if_zero ?? false, + input.appLastPrice?.last_historical ) return fallback > 0 ? { status: 'ready', amount: fallback, source: 'آخرین قیمت ذخیره‌شده' } @@ -151,7 +164,8 @@ export function computeProductEditorPricePreview(input: { } const fallback = resolveStoredFallbackPrice( input.form.price, - input.form.use_last_price_if_zero ?? false + input.form.use_last_price_if_zero ?? false, + input.appLastPrice?.last_historical ) return fallback > 0 ? { status: 'ready', amount: fallback, source: 'آخرین قیمت ذخیره‌شده' }