import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop" import { allow, deny } from "@/lib/policy/assert" import type { Actor } from "@/lib/policy/actor" import type { PolicyDecision } from "@/lib/policy/decision" import type { PlayerService } from "@/lib/types" import { useAuthStore } from "@/store/auth" import { useOrderStore } from "@/store/orders" import { useShopStore } from "@/store/shops" export function listOrders() { return useOrderStore.getState().orders } export function getOrderById(orderId: string) { return useOrderStore.getState().orders.find((order) => order.id === orderId) } export function listOrdersByConsumer(consumerId: string) { return useOrderStore.getState().orders.filter((order) => order.consumerId === consumerId) } interface CreatePaidOrderInput { consumerId: string consumerName: string playerId: string playerName: string shopId?: string shopName?: string service: PlayerService totalPrice: number note?: string } function resolveActorContext(): { actor?: Actor; decision: PolicyDecision } { const auth = useAuthStore.getState() if (!auth.user?.id) { return { decision: deny("AUTH_REQUIRED", "请先登录") } } const shopId = auth.currentRole === "owner" ? resolveOwnerShop(auth.user.id, useShopStore.getState().shops)?.id : undefined return { actor: { userId: auth.user.id, role: auth.currentRole, shopId, }, decision: allow(), } } export function createPaidOrder(input: CreatePaidOrderInput) { const { actor, decision } = resolveActorContext() if (!actor) return { decision } return useOrderStore.getState().createPaidOrder(input, actor) } export function payOrder(orderId: string) { const { actor, decision } = resolveActorContext() if (!actor) return { decision } return useOrderStore.getState().payOrder(orderId, actor) } export function acceptOrder(orderId: string) { const { actor, decision } = resolveActorContext() if (!actor) return { decision } return useOrderStore.getState().acceptOrder(orderId, actor) } export function acceptOrderAsActor(orderId: string, actor: Actor) { return useOrderStore.getState().acceptOrder(orderId, actor) } export function requestClose(orderId: string) { const { actor, decision } = resolveActorContext() if (!actor) return { decision } return useOrderStore.getState().requestClose(orderId, actor) } export function confirmClose(orderId: string) { const { actor, decision } = resolveActorContext() if (!actor) return { decision } return useOrderStore.getState().confirmClose(orderId, actor) } export function cancelPreAccept(orderId: string) { const { actor, decision } = resolveActorContext() if (!actor) return { decision } return useOrderStore.getState().cancelPreAccept(orderId, actor) } export function markDisputed(orderId: string) { const { actor, decision } = resolveActorContext() if (!actor) return { decision } return useOrderStore.getState().markDisputed(orderId, actor) }