fix moshtari code
This commit is contained in:
+38
-30
@@ -6,6 +6,8 @@ import {
|
||||
LEGACY_SLIDER_BANNERS_CONST_KEY,
|
||||
LEGACY_THEME_BANNERS_CONST_KEY,
|
||||
emptyAppBanners,
|
||||
isThemeBannersEmpty,
|
||||
mergeAppBannersFromLegacy,
|
||||
parseAppBanners,
|
||||
serializeAppBanners,
|
||||
type AppBannersValue,
|
||||
@@ -51,6 +53,8 @@ export type AppBannersConstState = {
|
||||
/** فقط وقتی رکورد واقعاً با key=app_banners است؛ وگرنه null تا create شود */
|
||||
id: number | null
|
||||
data: AppBannersValue
|
||||
/** از settings_profile_banners پر شده — بهتر است یکبار ذخیره شود */
|
||||
healedFromLegacy?: boolean
|
||||
}
|
||||
|
||||
async function loadLegacySliderInto(merged: AppBannersValue): Promise<void> {
|
||||
@@ -67,9 +71,22 @@ async function loadLegacySliderInto(merged: AppBannersValue): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadLegacyThemeBanners(): Promise<AppBannersValue> {
|
||||
try {
|
||||
const themeRes = await useAxios.get<Const | Const[]>(
|
||||
`/api/const/key/${LEGACY_THEME_BANNERS_CONST_KEY}`
|
||||
)
|
||||
const themeRow = pickConstRow(themeRes)
|
||||
if (!themeRow) return emptyAppBanners()
|
||||
return parseAppBanners(themeRow.value)
|
||||
} catch {
|
||||
return emptyAppBanners()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* یک API واحد برای همهٔ بنرها.
|
||||
* اگر `app_banners` نبود یا اسلایدر خالی بود، از کلیدهای قدیمی migrate میکند (فقط خواندن).
|
||||
* اگر `app_banners` نبود یا اسلاتهای تم خالی بود، از settings_profile_banners پر میکند.
|
||||
*/
|
||||
export async function loadAppBannersConst(): Promise<AppBannersConstState> {
|
||||
let appRow: Const | null = null
|
||||
@@ -81,44 +98,36 @@ export async function loadAppBannersConst(): Promise<AppBannersConstState> {
|
||||
}
|
||||
|
||||
if (appRow) {
|
||||
const data = parseAppBanners(appRow.value)
|
||||
// رکورد جدید خالی: اسلایدر را از header_banner پر کن
|
||||
if (!data.slider.length) {
|
||||
await loadLegacySliderInto(data)
|
||||
let data = parseAppBanners(appRow.value)
|
||||
let healedFromLegacy = false
|
||||
|
||||
// رکورد پوچ app_banners روی URLهای واقعی legacy سایه نیندازد
|
||||
if (isThemeBannersEmpty(data) || !data.slider.length) {
|
||||
const before = serializeAppBanners(data)
|
||||
if (isThemeBannersEmpty(data)) {
|
||||
const legacyTheme = await loadLegacyThemeBanners()
|
||||
data = mergeAppBannersFromLegacy(data, legacyTheme)
|
||||
}
|
||||
if (!data.slider.length) {
|
||||
await loadLegacySliderInto(data)
|
||||
}
|
||||
healedFromLegacy = serializeAppBanners(data) !== before
|
||||
}
|
||||
|
||||
return {
|
||||
id: appRow.id ?? appRow.const_id ?? null,
|
||||
data,
|
||||
healedFromLegacy,
|
||||
}
|
||||
}
|
||||
|
||||
const merged = emptyAppBanners()
|
||||
|
||||
try {
|
||||
const themeRes = await useAxios.get<Const | Const[]>(
|
||||
`/api/const/key/${LEGACY_THEME_BANNERS_CONST_KEY}`
|
||||
)
|
||||
const themeRow = pickConstRow(themeRes)
|
||||
if (themeRow) {
|
||||
const themes = parseAppBanners(themeRow.value)
|
||||
Object.assign(merged, {
|
||||
homeAsset: themes.homeAsset,
|
||||
piggyCreate: themes.piggyCreate,
|
||||
productsHero: themes.productsHero?.length ? themes.productsHero : merged.productsHero,
|
||||
referral: themes.referral,
|
||||
share: themes.share,
|
||||
invite: themes.invite,
|
||||
slider: themes.slider?.length ? themes.slider : merged.slider,
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
|
||||
const legacyTheme = await loadLegacyThemeBanners()
|
||||
Object.assign(merged, mergeAppBannersFromLegacy(merged, legacyTheme))
|
||||
await loadLegacySliderInto(merged)
|
||||
|
||||
// id=null → اولین ذخیره، رکورد جدید با key=app_banners میسازد
|
||||
return { id: null, data: merged }
|
||||
const hasLegacy = !isThemeBannersEmpty(merged) || merged.slider.length > 0
|
||||
return { id: null, data: merged, healedFromLegacy: hasLegacy }
|
||||
}
|
||||
|
||||
async function syncLegacySliderConst(slides: IBenefits[]): Promise<void> {
|
||||
@@ -177,7 +186,6 @@ export async function saveAppBannersConst(
|
||||
nextId = created?.id ?? created?.const_id ?? null
|
||||
}
|
||||
|
||||
// اپ مشتری هنوز header_banner میخواند — اسلایدر را همگام نگه دار
|
||||
try {
|
||||
await syncLegacySliderConst(data.slider ?? [])
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user