Files
boursecu_admin/src/utils/smsPanelAccess.ts
T
2026-07-04 14:21:27 +03:30

48 lines
1.3 KiB
TypeScript

import type { User } from '@/model/user'
import { isDeployAdmin } from '@/utils/deployAdmin'
export const SMS_PANEL_OWNER_MOBILE = '09128081392'
export function normalizeMobile(mobile: unknown): string {
let digits = String(mobile ?? '').replace(/\D/g, '')
if (digits.startsWith('98') && digits.length === 12) {
digits = `0${digits.slice(2)}`
} else if (digits.length === 10 && digits.startsWith('9')) {
digits = `0${digits}`
}
return digits
}
function parseSettingBool(value: unknown, defaultValue = true): boolean {
if (value == null || String(value).trim() === '') {
return defaultValue
}
const normalized = String(value).trim().toLowerCase()
if (['false', '0', 'no', 'off'].includes(normalized)) {
return false
}
return true
}
/** sms_panel = false → بدون کیف پول/billing؛ ارسال مثل قبل */
export function isSmsBillingEnabled(user?: User | null): boolean {
if (!user) return false
if (user.sms_billing_enabled === false) return false
const panel = user.settings?.find((s) => s.key === 'sms_panel')?.value
return parseSettingBool(panel, true)
}
export function hasSmsPanelAccess(user?: User | null): boolean {
if (!isSmsBillingEnabled(user)) return false
if (user?.sms_panel_access === true) return true
return isDeployAdmin(user?.mobile)
}