Files
boursecu_admin/src/utils/config.ts
T

172 lines
5.9 KiB
TypeScript
Raw Normal View History

2026-06-25 00:13:03 +03:30
import { ref } from 'vue'
2026-06-23 22:31:07 +03:30
import { setCachedMainMetal } from '@/utils/metalLabels'
2026-07-05 16:41:45 +03:30
export type AccountingProvider = 'none' | 'pargar' | 'tahesab'
2026-01-24 17:45:54 +03:30
export function getConfigApp(key: string) {
2026-06-12 20:30:00 +03:30
const config = window.__APP_CONFIG__ ? window.__APP_CONFIG__.user : {}
2026-01-24 17:45:54 +03:30
if (config) {
return config[key]
}
return null
}
2026-06-11 22:29:28 +03:30
2026-06-12 20:30:00 +03:30
let storeModeFromSettings: boolean | null = null
2026-06-15 15:48:10 +03:30
let settingsBootstrapped = false
2026-06-12 20:30:00 +03:30
2026-06-25 00:13:03 +03:30
/** reactive — برای فیلتر منوی «بنرها» در پیکربندی */
export const bannerEnabledSetting = ref(false)
2026-06-26 17:41:52 +03:30
/** reactive — نمایش UI ته‌حsاب در حسابداری و ... */
export const tahesabEnabledSetting = ref(false)
2026-07-05 16:41:45 +03:30
/** reactive — نمایش UI پرگar در حسابداری و ... */
export const pargarEnabledSetting = ref(false)
export const accountingProviderSetting = ref<AccountingProvider>('none')
2026-06-26 17:41:52 +03:30
let tahesabEnabledFromSettings: boolean | null = null
2026-07-05 16:41:45 +03:30
let pargarEnabledFromSettings: boolean | null = null
let accountingProviderFromSettings: AccountingProvider | null = null
2026-06-26 17:41:52 +03:30
2026-06-12 20:30:00 +03:30
function isSwitchOn(value: string | undefined | null): boolean {
const v = (value ?? '').trim().toLowerCase()
return v === 'true' || v === '1' || v === 'yes'
}
2026-07-05 16:41:45 +03:30
function normalizeAccountingProvider(value: string | undefined | null): AccountingProvider {
const v = (value ?? '').trim().toLowerCase()
if (v === 'pargar' || v === 'tahesab') return v
return 'none'
}
function resolveAccountingProviderFromRows(
list: Array<{ key?: string; value?: string | null }>
): AccountingProvider {
const row = list.find((item) => item.key === 'accounting_provider')
if (row?.value !== undefined && row?.value !== null && String(row.value).trim() !== '') {
return normalizeAccountingProvider(row.value)
}
const pargarLegacy = list.find((item) => item.key === 'send_to_pargar')
if (pargarLegacy && isSwitchOn(pargarLegacy.value)) return 'pargar'
const tahesabLegacy = list.find((item) => item.key === 'send_to_tahesab')
if (tahesabLegacy && isSwitchOn(tahesabLegacy.value)) return 'tahesab'
return 'none'
}
export function applyAccountingProviderFromRows(
list: Array<{ key?: string; value?: string | null }>
): void {
const provider = resolveAccountingProviderFromRows(list)
accountingProviderFromSettings = provider
accountingProviderSetting.value = provider
setTahesabEnabledFromSettings(provider === 'tahesab')
setPargarEnabledFromSettings(provider === 'pargar')
}
2026-06-12 20:30:00 +03:30
export function setStoreModeFromSettings(value: boolean): void {
storeModeFromSettings = value
}
2026-06-25 00:13:03 +03:30
export function setBannerEnabledFromSettings(value: boolean): void {
bannerEnabledSetting.value = value
}
2026-06-26 17:41:52 +03:30
export function setTahesabEnabledFromSettings(value: boolean): void {
tahesabEnabledFromSettings = value
tahesabEnabledSetting.value = value
}
2026-07-05 16:41:45 +03:30
export function setPargarEnabledFromSettings(value: boolean): void {
pargarEnabledFromSettings = value
pargarEnabledSetting.value = value
}
2026-06-25 00:13:03 +03:30
export function applyBannerEnabledFromRows(rows: Array<{ key?: string; value?: string | null }>) {
const row = rows.find((item) => item.key === 'banner_enabled')
setBannerEnabledFromSettings(row ? isSwitchOn(row.value) : false)
}
2026-06-15 15:48:10 +03:30
async function bootstrapSettingsFromApi(): Promise<void> {
if (settingsBootstrapped) return
2026-06-12 20:30:00 +03:30
try {
const { getConfiguration } = await import('@/api/configuration')
2026-06-15 15:48:10 +03:30
const { parseMainWalletProductIds } = await import('@/components/mobile/utils/wallet')
2026-06-12 20:30:00 +03:30
const rows = await getConfiguration()
2026-06-15 15:48:10 +03:30
const list = Array.isArray(rows) ? rows : []
const isStoreRow = list.find((item) => item.key === 'is_store')
const mainWalletRow = list.find((item) => item.key === 'main_wallet_products')
2026-06-23 22:31:07 +03:30
const mainMetalRow = list.find((item) => item.key === 'main_metal')
2026-06-15 15:48:10 +03:30
storeModeFromSettings = isStoreRow ? isSwitchOn(isStoreRow.value) : false
2026-07-05 16:41:45 +03:30
applyAccountingProviderFromRows(list)
2026-06-25 00:13:03 +03:30
applyBannerEnabledFromRows(list)
2026-06-23 22:31:07 +03:30
setCachedMainMetal(mainMetalRow?.value)
2026-06-15 15:48:10 +03:30
const { setCachedMainWalletProductIds } = await import('@/components/mobile/utils/wallet')
setCachedMainWalletProductIds(parseMainWalletProductIds(mainWalletRow?.value))
2026-06-12 20:30:00 +03:30
} catch {
storeModeFromSettings = false
2026-06-25 00:13:03 +03:30
setBannerEnabledFromSettings(false)
2026-07-05 16:41:45 +03:30
applyAccountingProviderFromRows([])
2026-06-23 22:31:07 +03:30
setCachedMainMetal(null)
2026-06-15 15:48:10 +03:30
const { setCachedMainWalletProductIds } = await import('@/components/mobile/utils/wallet')
setCachedMainWalletProductIds([])
} finally {
settingsBootstrapped = true
2026-06-12 20:30:00 +03:30
}
2026-06-15 15:48:10 +03:30
}
2026-06-12 20:30:00 +03:30
2026-06-15 15:48:10 +03:30
/** is_store و main_wallet_products از API تنظیمات — نه از get_user */
export async function initStoreModeFromSettings(): Promise<boolean> {
if (storeModeFromSettings !== null) return storeModeFromSettings
await bootstrapSettingsFromApi()
return storeModeFromSettings === true
}
export async function initWalletSettingsFromApi(): Promise<void> {
await bootstrapSettingsFromApi()
2026-06-12 20:30:00 +03:30
}
2026-06-11 22:29:28 +03:30
export function isStoreApp(): boolean {
2026-06-12 20:30:00 +03:30
return storeModeFromSettings === true
2026-06-11 22:29:28 +03:30
}
2026-06-26 17:41:52 +03:30
export function isTahesabEnabled(): boolean {
return tahesabEnabledFromSettings === true
}
2026-07-05 16:41:45 +03:30
export function isPargarEnabled(): boolean {
return pargarEnabledFromSettings === true
}
export function getAccountingProvider(): AccountingProvider {
return accountingProviderFromSettings ?? 'none'
}
/** بارگذاری accounting_provider و فلگ‌های UI */
export async function initAccountingSettingsFromApi(): Promise<AccountingProvider> {
if (accountingProviderFromSettings !== null) return accountingProviderFromSettings
2026-06-26 17:41:52 +03:30
await bootstrapSettingsFromApi()
2026-07-05 16:41:45 +03:30
return accountingProviderFromSettings ?? 'none'
}
/** @deprecated از initAccountingSettingsFromApi استفاده کنید */
export async function initTahesabSettingFromApi(): Promise<boolean> {
await initAccountingSettingsFromApi()
2026-06-26 17:41:52 +03:30
return tahesabEnabledFromSettings === true
}
2026-07-05 16:41:45 +03:30
/** @deprecated از initAccountingSettingsFromApi استفاده کنید */
export async function initPargarSettingFromApi(): Promise<boolean> {
await initAccountingSettingsFromApi()
return pargarEnabledFromSettings === true
}