Files
boursecu_admin/src/store/smsWalletStore.ts
T

103 lines
2.8 KiB
TypeScript

import { computed, ref } from 'vue'
import { defineStore } from 'pinia'
import { getSmsWalletSummary, type SmsDepositInfo } from '@/api/smsPanel'
import { useUserStore } from '@/store/userStore'
const EMPTY_DEPOSIT: SmsDepositInfo = {
card_number: '',
sheba: '',
account_holder: '',
note: ''
}
const NEGATIVE_WARNING_SESSION_KEY = 'khanegi_admin_sms_negative_warning_shown'
function wasNegativeWarningShownThisSession() {
try {
return sessionStorage.getItem(NEGATIVE_WARNING_SESSION_KEY) === '1'
} catch {
return false
}
}
function markNegativeWarningShownThisSession() {
try {
sessionStorage.setItem(NEGATIVE_WARNING_SESSION_KEY, '1')
} catch {
// ignore storage failures
}
}
function clearNegativeWarningShownThisSession() {
try {
sessionStorage.removeItem(NEGATIVE_WARNING_SESSION_KEY)
} catch {
// ignore storage failures
}
}
/** وضعیت کیف پول پیامک برای نمایش «اعتبار پیامک» در هدر (همه ادمین‌ها). */
export const useSmsWalletStore = defineStore('smsWallet', () => {
const userStore = useUserStore()
const balance = ref(0)
const isBlocked = ref(false)
const deposit = ref<SmsDepositInfo>({ ...EMPTY_DEPOSIT })
const loaded = ref(false)
const loading = ref(false)
/** هشدار موجودی منفی — فقط یک‌بار در هر باز شدن نشست ادمین */
const negativeWarningVisible = ref(false)
/** ویجت فقط وقتی billing/پنل پیامک فعال است نمایش داده می‌شود. */
const visible = computed(() => userStore.smsBillingEnabled)
async function refresh() {
if (!visible.value) {
loaded.value = false
negativeWarningVisible.value = false
return
}
if (loading.value) return
loading.value = true
try {
const res = await getSmsWalletSummary()
balance.value = res.balance
isBlocked.value = res.is_blocked
deposit.value = res.deposit ?? { ...EMPTY_DEPOSIT }
loaded.value = true
if (res.balance >= 0) {
negativeWarningVisible.value = false
clearNegativeWarningShownThisSession()
} else if (!wasNegativeWarningShownThisSession()) {
negativeWarningVisible.value = true
markNegativeWarningShownThisSession()
} else {
negativeWarningVisible.value = false
}
} catch {
// خطای موجودی نباید مزاحم کاربر شود؛ ویجت در صورت نبود داده مخفی می‌ماند
} finally {
loading.value = false
}
}
function dismissNegativeWarning() {
negativeWarningVisible.value = false
markNegativeWarningShownThisSession()
}
return {
balance,
isBlocked,
deposit,
loaded,
loading,
visible,
negativeWarningVisible,
refresh,
dismissNegativeWarning
}
})