108 lines
2.8 KiB
TypeScript
108 lines
2.8 KiB
TypeScript
import { useSettingsStore } from '@/store/settingsStore'
|
|
import { lastPrice } from '@/utils/price'
|
|
|
|
export interface StoreTradingHours {
|
|
start: string
|
|
end: string
|
|
closePriceEnabled: boolean
|
|
}
|
|
|
|
export interface EffectiveTradingHours {
|
|
start: string
|
|
end: string
|
|
enforced: boolean
|
|
}
|
|
|
|
let productTradingHours: { start: string; end: string } | null = null
|
|
|
|
function parseTimeToSeconds(time: string): number {
|
|
const parts = time.trim().split(':').map(Number)
|
|
const [hours = 0, minutes = 0, seconds = 0] = parts
|
|
return hours * 3600 + minutes * 60 + seconds
|
|
}
|
|
|
|
export function formatTradingTimeDisplay(time: string): string {
|
|
const parts = time.trim().split(':')
|
|
if (parts.length >= 3) {
|
|
return `${parts[0]}:${parts[1]}:${parts[2]}`
|
|
}
|
|
if (parts.length >= 2) {
|
|
return `${parts[0]}:${parts[1]}:00`
|
|
}
|
|
return time
|
|
}
|
|
|
|
export function setProductTradingHours(start?: string | null, end?: string | null) {
|
|
if (start && end) {
|
|
productTradingHours = { start, end }
|
|
}
|
|
}
|
|
|
|
export function getStoreTradingHours(): StoreTradingHours {
|
|
const settings = useSettingsStore()
|
|
const closeRaw = settings.getSettingByKey('close_price')
|
|
const closePriceEnabled =
|
|
closeRaw === true ||
|
|
closeRaw === 1 ||
|
|
String(closeRaw ?? 'false').trim().toLowerCase() === 'true'
|
|
|
|
return {
|
|
start: String(settings.getSettingByKey('startDay') ?? '00:00'),
|
|
end: String(settings.getSettingByKey('endDay') ?? '23:59'),
|
|
closePriceEnabled,
|
|
}
|
|
}
|
|
|
|
export function getEffectiveTradingHours(): EffectiveTradingHours {
|
|
const settings = getStoreTradingHours()
|
|
if (settings.closePriceEnabled) {
|
|
return {
|
|
start: settings.start,
|
|
end: settings.end,
|
|
enforced: true,
|
|
}
|
|
}
|
|
|
|
if (productTradingHours?.start && productTradingHours?.end) {
|
|
return {
|
|
start: productTradingHours.start,
|
|
end: productTradingHours.end,
|
|
enforced: true,
|
|
}
|
|
}
|
|
|
|
return {
|
|
start: settings.start,
|
|
end: settings.end,
|
|
enforced: false,
|
|
}
|
|
}
|
|
|
|
export function isStoreClosedBySchedule(now = new Date()): boolean {
|
|
const { start, end, enforced } = getEffectiveTradingHours()
|
|
if (!enforced) {
|
|
return false
|
|
}
|
|
|
|
const currentSeconds = now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds()
|
|
const openSeconds = parseTimeToSeconds(start)
|
|
const closeSeconds = parseTimeToSeconds(end)
|
|
|
|
return currentSeconds < openSeconds || currentSeconds > closeSeconds
|
|
}
|
|
|
|
export function isStoreClosed(): boolean {
|
|
if (lastPrice.value?.closed === true) {
|
|
return true
|
|
}
|
|
|
|
return isStoreClosedBySchedule()
|
|
}
|
|
|
|
export function getStoreClosedMessage(): string {
|
|
const { start, end } = getEffectiveTradingHours()
|
|
const open = formatTradingTimeDisplay(start)
|
|
const close = formatTradingTimeDisplay(end)
|
|
return `فروشگاه در حال حاضر بسته است. ساعت کاری از ${open} تا ${close} میباشد.`
|
|
}
|