fix bugs and added some feature for withdraw handling

This commit is contained in:
2026-07-19 15:49:52 +03:30
parent b446c21afc
commit 3cb1e1a0b0
17 changed files with 442 additions and 58 deletions
+86 -25
View File
@@ -13,8 +13,23 @@ import {
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
if (Array.isArray(res)) {
const first = (res[0] as Const) ?? null
return isMeaningfulConstRow(first) ? first : null
}
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[] {
@@ -33,24 +48,45 @@ export type AppBannersConstState = {
data: AppBannersValue
}
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
}
}
/**
* یک API واحد برای همهٔ بنرها.
* اگر `app_banners` نبود، از کلیدهای قدیمی migrate می‌کند (فقط خواندن).
* اگر `app_banners` نبود یا اسلایدر خالی بود، از کلیدهای قدیمی migrate می‌کند (فقط خواندن).
*/
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}`)
const row = pickConstRow(res)
if (row) {
return {
id: row.id ?? row.const_id ?? null,
data: parseAppBanners(row.value),
}
}
appRow = pickConstRow(res)
} catch {
// ادامه با مهاجرت از کلیدهای قدیمی
}
if (appRow) {
const data = parseAppBanners(appRow.value)
// رکورد جدید خالی: اسلایدر را از header_banner پر کن
if (!data.slider.length) {
await loadLegacySliderInto(data)
}
return {
id: appRow.id ?? appRow.const_id ?? null,
data,
}
}
const merged = emptyAppBanners()
try {
@@ -76,23 +112,39 @@ export async function loadAppBannersConst(): Promise<AppBannersConstState> {
// 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
}
await loadLegacySliderInto(merged)
// id=null → اولین ذخیره، رکورد جدید با key=app_banners می‌سازد
return { id: null, data: merged }
}
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
@@ -106,11 +158,20 @@ export async function saveAppBannersConst(
description: 'همهٔ بنرهای اپ: اسلایدر صفحه اصلی + تصاویر تم لایت/دارک',
}
let nextId = id
if (id) {
await useAxios.put<Const>(`/admin/const/${id}`, { id, ...payload })
return id
} else {
const created = await useAxios.post<Const>(`/admin/const`, { id: 0, ...payload })
nextId = created?.id ?? created?.const_id ?? null
}
const created = await useAxios.post<Const>(`/admin/const`, { id: 0, ...payload })
return created?.id ?? created?.const_id ?? null
// اپ مشتری هنوز header_banner می‌خواند — اسلایدر را همگام نگه دار
try {
await syncLegacySliderConst(data.slider ?? [])
} catch (error) {
console.error('همگام‌سازی header_banner ناموفق بود:', error)
}
return nextId
}