Files
juwan-frontend/lib/api/orders.ts
T

95 lines
2.8 KiB
TypeScript

import type { Actor } from "@/lib/actor"
import { allow, deny } from "@/lib/decision"
import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop"
import type { ApiDecision } from "@/lib/errors"
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 {
playerId: string
serviceId: string
shopId?: string
quantity: number
note?: string
}
function resolveActorContext(): { actor?: Actor; decision: ApiDecision } {
const auth = useAuthStore.getState()
if (!auth.user?.id) {
return { decision: deny(401, "请先登录") }
}
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)
}