250 lines
7.8 KiB
TypeScript
250 lines
7.8 KiB
TypeScript
import useAxios from '@core/utils/useAxios'
|
|
import type { Const } from '@/model/const'
|
|
import type { IBenefits } from '@/model/banner'
|
|
import {
|
|
APP_BANNERS_CONST_KEY,
|
|
LEGACY_SLIDER_BANNERS_CONST_KEY,
|
|
LEGACY_THEME_BANNERS_CONST_KEY,
|
|
emptyAppBanners,
|
|
isThemeBannersEmpty,
|
|
mergeAppBannersFromLegacy,
|
|
parseAppBanners,
|
|
serializeAppBanners,
|
|
type AppBannersValue,
|
|
} from '@/model/appBanners'
|
|
|
|
function pickConstRow(res: Const | Const[] | null | undefined): Const | null {
|
|
if (!res) return null
|
|
if (Array.isArray(res)) {
|
|
const rows = res.filter(isMeaningfulConstRow)
|
|
if (!rows.length) return null
|
|
return rows.reduce((best, row) => {
|
|
const a = Number(best.id ?? best.const_id ?? 0)
|
|
const b = Number(row.id ?? row.const_id ?? 0)
|
|
return b > a ? row : best
|
|
})
|
|
}
|
|
return isMeaningfulConstRow(res) ? res : null
|
|
}
|
|
|
|
/** API برای کلید ناموجود گاهی `{}` یا null برمیگرداند — نباید مهاجرت را قطع کند */
|
|
function isMeaningfulConstRow(row: Const | null | undefined): row is Const {
|
|
if (!row || typeof row !== 'object') return false
|
|
const hasId = row.id != null || row.const_id != null
|
|
const raw = row.value
|
|
const hasValue =
|
|
typeof raw === 'string'
|
|
? raw.trim() !== ''
|
|
: raw != null && !(typeof raw === 'object' && !Array.isArray(raw) && Object.keys(raw as object).length === 0)
|
|
return hasId || hasValue
|
|
}
|
|
|
|
function parseLegacySlider(value: string | null | undefined): IBenefits[] {
|
|
if (!value) return []
|
|
try {
|
|
const parsed = JSON.parse(value)
|
|
return Array.isArray(parsed) ? (parsed as IBenefits[]) : []
|
|
} catch {
|
|
return []
|
|
}
|
|
}
|
|
|
|
export const ASSETS_BANNER_CONST_KEY = 'assets_banner'
|
|
|
|
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> {
|
|
try {
|
|
const sliderRes = await useAxios.get<Const | Const[]>(
|
|
`/api/const/key/${LEGACY_SLIDER_BANNERS_CONST_KEY}`
|
|
)
|
|
const sliderRow = pickConstRow(sliderRes)
|
|
if (!sliderRow) return
|
|
const slides = parseLegacySlider(sliderRow.value)
|
|
if (slides.length) merged.slider = slides
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
async function loadAssetsBannerInto(merged: AppBannersValue): Promise<void> {
|
|
try {
|
|
const res = await useAxios.get<Const | Const[]>(
|
|
`/api/const/key/${ASSETS_BANNER_CONST_KEY}`
|
|
)
|
|
const row = pickConstRow(res)
|
|
if (!row) return
|
|
const items = parseLegacySlider(row.value)
|
|
if (items.length) merged.assetsBanner = items
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
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` نبود یا اسلاتهای تم خالی بود، از settings_profile_banners پر میکند.
|
|
*/
|
|
export async function loadAppBannersConst(): Promise<AppBannersConstState> {
|
|
let appRow: Const | null = null
|
|
try {
|
|
const res = await useAxios.get<Const | Const[]>(`/api/const/key/${APP_BANNERS_CONST_KEY}`)
|
|
appRow = pickConstRow(res)
|
|
} catch {
|
|
// ادامه با مهاجرت از کلیدهای قدیمی
|
|
}
|
|
|
|
if (appRow) {
|
|
let data = parseAppBanners(appRow.value)
|
|
let healedFromLegacy = false
|
|
|
|
// رکورد پوچ app_banners روی URLهای واقعی legacy سایه نیندازد
|
|
if (isThemeBannersEmpty(data) || !data.slider.length || !(data.assetsBanner ?? []).length) {
|
|
const before = serializeAppBanners(data)
|
|
if (isThemeBannersEmpty(data)) {
|
|
const legacyTheme = await loadLegacyThemeBanners()
|
|
data = mergeAppBannersFromLegacy(data, legacyTheme)
|
|
}
|
|
if (!data.slider.length) {
|
|
await loadLegacySliderInto(data)
|
|
}
|
|
if (!(data.assetsBanner ?? []).length) {
|
|
await loadAssetsBannerInto(data)
|
|
}
|
|
healedFromLegacy = serializeAppBanners(data) !== before
|
|
}
|
|
|
|
return {
|
|
id: appRow.id ?? appRow.const_id ?? null,
|
|
data,
|
|
healedFromLegacy,
|
|
}
|
|
}
|
|
|
|
const merged = emptyAppBanners()
|
|
const legacyTheme = await loadLegacyThemeBanners()
|
|
Object.assign(merged, mergeAppBannersFromLegacy(merged, legacyTheme))
|
|
await loadLegacySliderInto(merged)
|
|
await loadAssetsBannerInto(merged)
|
|
|
|
const hasLegacy = !isThemeBannersEmpty(merged) || merged.slider.length > 0 || (merged.assetsBanner ?? []).length > 0
|
|
return { id: null, data: merged, healedFromLegacy: hasLegacy }
|
|
}
|
|
|
|
async function syncLegacySliderConst(slides: IBenefits[]): Promise<void> {
|
|
const value = JSON.stringify(slides ?? [])
|
|
const payload = {
|
|
key: LEGACY_SLIDER_BANNERS_CONST_KEY,
|
|
value,
|
|
title_fa: 'بنرهای اسلایدر',
|
|
is_active: true,
|
|
description: 'اسلایدر بالای صفحه اصلی اپ مشتری (سازگاری با اپ)',
|
|
}
|
|
|
|
try {
|
|
const existingRes = await useAxios.get<Const | Const[]>(
|
|
`/api/const/key/${LEGACY_SLIDER_BANNERS_CONST_KEY}`
|
|
)
|
|
const existing = pickConstRow(existingRes)
|
|
const existingId = existing?.id ?? existing?.const_id ?? null
|
|
if (existingId) {
|
|
await useAxios.put<Const>(`/admin/const/${existingId}`, { id: existingId, ...payload })
|
|
return
|
|
}
|
|
} catch {
|
|
// create below
|
|
}
|
|
|
|
await useAxios.post<Const>(`/admin/const`, { id: 0, ...payload })
|
|
}
|
|
|
|
export async function saveAppBannersConst(
|
|
id: number | null,
|
|
data: AppBannersValue
|
|
): Promise<number | null> {
|
|
const value = serializeAppBanners(data)
|
|
const payload = {
|
|
key: APP_BANNERS_CONST_KEY,
|
|
value,
|
|
title_fa: 'بنرهای اپ',
|
|
is_active: true,
|
|
description: 'همهٔ بنرهای اپ: اسلایدر صفحه اصلی + تصاویر تم لایت/دارک',
|
|
}
|
|
|
|
let nextId = id
|
|
try {
|
|
const existingRes = await useAxios.get<Const | Const[]>(`/api/const/key/${APP_BANNERS_CONST_KEY}`)
|
|
const existing = pickConstRow(existingRes)
|
|
nextId = existing?.id ?? existing?.const_id ?? id
|
|
} catch {
|
|
// create below
|
|
}
|
|
|
|
if (nextId) {
|
|
await useAxios.put<Const>(`/admin/const/${nextId}`, { id: nextId, ...payload })
|
|
} else {
|
|
const created = await useAxios.post<Const>(`/admin/const`, { id: 0, ...payload })
|
|
nextId = created?.id ?? created?.const_id ?? null
|
|
}
|
|
|
|
try {
|
|
await syncLegacySliderConst(data.slider ?? [])
|
|
} catch (error) {
|
|
console.error('همگامسازی header_banner ناموفق بود:', error)
|
|
}
|
|
|
|
try {
|
|
await syncAssetsBannerConst(data.assetsBanner ?? [])
|
|
} catch (error) {
|
|
console.error('همگامسازی assets_banner ناموفق بود:', error)
|
|
}
|
|
|
|
return nextId
|
|
}
|
|
|
|
export async function syncAssetsBannerConst(items: IBenefits[]): Promise<void> {
|
|
const value = JSON.stringify(items ?? [])
|
|
const payload = {
|
|
key: ASSETS_BANNER_CONST_KEY,
|
|
value,
|
|
title_fa: 'بنر صفحه داراییها',
|
|
is_active: true,
|
|
description: 'بنر صفحهٔ داراییهای اپ مشتری (زیر نمودار)',
|
|
}
|
|
|
|
try {
|
|
const existingRes = await useAxios.get<Const | Const[]>(
|
|
`/api/const/key/${ASSETS_BANNER_CONST_KEY}`
|
|
)
|
|
const existing = pickConstRow(existingRes)
|
|
const existingId = existing?.id ?? existing?.const_id ?? null
|
|
if (existingId) {
|
|
await useAxios.put<Const>(`/admin/const/${existingId}`, { id: existingId, ...payload })
|
|
return
|
|
}
|
|
} catch {
|
|
// create below
|
|
}
|
|
|
|
await useAxios.post<Const>(`/admin/const`, { id: 0, ...payload })
|
|
}
|