Files
boursecu_admin/src/api/appBanners.ts
T

117 lines
3.3 KiB
TypeScript
Raw Normal View History

2026-07-18 09:55:51 +02:00
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,
parseAppBanners,
serializeAppBanners,
type AppBannersValue,
} from '@/model/appBanners'
function pickConstRow(res: Const | Const[] | null | undefined): Const | null {
if (!res) return null
if (Array.isArray(res)) return (res[0] as Const) ?? null
return res
}
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 type AppBannersConstState = {
/** فقط وقتی رکورد واقعاً با key=app_banners است؛ وگرنه null تا create شود */
id: number | null
data: AppBannersValue
}
/**
* یک API واحد برای همهٔ بنرها.
* اگر `app_banners` نبود، از کلیدهای قدیمی migrate می‌کند (فقط خواندن).
*/
export async function loadAppBannersConst(): Promise<AppBannersConstState> {
try {
const res = await useAxios.get<Const | Const[]>(`/api/const/key/${APP_BANNERS_CONST_KEY}`)
const row = pickConstRow(res)
if (row) {
return {
id: row.id ?? row.const_id ?? null,
data: parseAppBanners(row.value),
}
}
} catch {
// ادامه با مهاجرت از کلیدهای قدیمی
}
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,
productsHero1: themes.productsHero1,
productsHero2: themes.productsHero2,
productsHero3: themes.productsHero3,
referral: themes.referral,
share: themes.share,
invite: themes.invite,
slider: themes.slider?.length ? themes.slider : merged.slider,
})
}
} catch {
// ignore
}
try {
const sliderRes = await useAxios.get<Const | Const[]>(
`/api/const/key/${LEGACY_SLIDER_BANNERS_CONST_KEY}`
)
const sliderRow = pickConstRow(sliderRes)
if (sliderRow) {
const slides = parseLegacySlider(sliderRow.value)
if (slides.length) merged.slider = slides
}
} catch {
// ignore
}
// id=null → اولین ذخیره، رکورد جدید با key=app_banners می‌سازد
return { id: null, data: merged }
}
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: 'همهٔ بنرهای اپ: اسلایدر صفحه اصلی + تصاویر تم لایت/دارک',
}
if (id) {
await useAxios.put<Const>(`/admin/const/${id}`, { id, ...payload })
return id
}
const created = await useAxios.post<Const>(`/admin/const`, { id: 0, ...payload })
return created?.id ?? created?.const_id ?? null
}