refactor(wallet): remove local balance logic and orphaned domain modules
Delete store/wallet.ts which is no longer referenced anywhere. Remove lib/domain/income.ts, order-machine.ts, resolve-current-shop.ts and their test files — all depended on removed local state machines. Wallet page already uses lib/api/transactions.ts for backend API calls.
This commit is contained in:
@@ -1,35 +0,0 @@
|
||||
import type { Shop } from "@/lib/types"
|
||||
|
||||
type ShopCommission = Pick<Shop, "commissionType" | "commissionValue">
|
||||
|
||||
export interface IncomeCalculation {
|
||||
income: number
|
||||
commissionLabel: string
|
||||
}
|
||||
|
||||
function roundCurrency(amount: number) {
|
||||
return Number(amount.toFixed(2))
|
||||
}
|
||||
|
||||
export function calculateOrderIncome(totalPrice: number, shop?: ShopCommission): IncomeCalculation {
|
||||
if (!shop) {
|
||||
return {
|
||||
income: roundCurrency(totalPrice),
|
||||
commissionLabel: "独立接单无抽成",
|
||||
}
|
||||
}
|
||||
|
||||
const commission = Number(shop.commissionValue)
|
||||
|
||||
if (shop.commissionType === "percentage") {
|
||||
return {
|
||||
income: roundCurrency(totalPrice * (1 - commission / 100)),
|
||||
commissionLabel: `扣除${shop.commissionValue}%抽成`,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
income: roundCurrency(Math.max(0, totalPrice - commission)),
|
||||
commissionLabel: `扣除¥${shop.commissionValue}固定抽成`,
|
||||
}
|
||||
}
|
||||
@@ -1,164 +0,0 @@
|
||||
import type { Actor } from "@/lib/actor"
|
||||
import { allow, deny, requireAuth } from "@/lib/decision"
|
||||
import type { ApiDecision } from "@/lib/errors"
|
||||
import type { OrderStatus } from "@/lib/types"
|
||||
|
||||
export type OrderAction =
|
||||
| "PAY"
|
||||
| "ACCEPT"
|
||||
| "REQUEST_CLOSE"
|
||||
| "CONFIRM_CLOSE"
|
||||
| "CANCEL_PRE_ACCEPT"
|
||||
| "OPEN_DISPUTE"
|
||||
| "RESOLVE_DISPUTE"
|
||||
| "SUBMIT_REVIEW"
|
||||
| "AUTO_TIMEOUT_PENDING_ACCEPT"
|
||||
| "AUTO_TIMEOUT_PENDING_CLOSE"
|
||||
| "AUTO_TIMEOUT_PENDING_REVIEW"
|
||||
|
||||
export type OrderTransitionSideEffectType =
|
||||
| "CLEAR_TIMEOUT"
|
||||
| "SCHEDULE_TIMEOUT"
|
||||
| "SYNC_CHAT_SESSION"
|
||||
| "PAYOUT_INCOME"
|
||||
|
||||
export interface OrderTransitionSideEffect {
|
||||
type: OrderTransitionSideEffectType
|
||||
status?: OrderStatus
|
||||
}
|
||||
|
||||
interface TransitionContext {
|
||||
actor?: Actor | null
|
||||
order: {
|
||||
status: OrderStatus
|
||||
}
|
||||
action: OrderAction
|
||||
}
|
||||
|
||||
export interface OrderTransitionResult {
|
||||
decision: ApiDecision
|
||||
nextStatus?: OrderStatus
|
||||
sideEffects: OrderTransitionSideEffect[]
|
||||
}
|
||||
|
||||
export const orderTransitionTable: Record<
|
||||
OrderStatus,
|
||||
Partial<Record<OrderAction, OrderStatus>>
|
||||
> = {
|
||||
pending_payment: {
|
||||
PAY: "pending_accept",
|
||||
},
|
||||
pending_accept: {
|
||||
ACCEPT: "in_progress",
|
||||
CANCEL_PRE_ACCEPT: "cancelled",
|
||||
AUTO_TIMEOUT_PENDING_ACCEPT: "cancelled",
|
||||
},
|
||||
in_progress: {
|
||||
REQUEST_CLOSE: "pending_close",
|
||||
OPEN_DISPUTE: "disputed",
|
||||
},
|
||||
pending_close: {
|
||||
CONFIRM_CLOSE: "pending_review",
|
||||
OPEN_DISPUTE: "disputed",
|
||||
AUTO_TIMEOUT_PENDING_CLOSE: "pending_review",
|
||||
},
|
||||
pending_review: {
|
||||
SUBMIT_REVIEW: "completed",
|
||||
AUTO_TIMEOUT_PENDING_REVIEW: "completed",
|
||||
},
|
||||
disputed: {
|
||||
RESOLVE_DISPUTE: "pending_review",
|
||||
},
|
||||
completed: {},
|
||||
cancelled: {},
|
||||
}
|
||||
|
||||
function isAutoAction(action: OrderAction): boolean {
|
||||
return action.startsWith("AUTO_TIMEOUT_")
|
||||
}
|
||||
|
||||
function checkRolePermission(action: OrderAction, actor?: Actor | null): ApiDecision {
|
||||
if (isAutoAction(action) || (action === "RESOLVE_DISPUTE" && !actor?.userId)) {
|
||||
return allow()
|
||||
}
|
||||
|
||||
const authDecision = requireAuth(actor)
|
||||
if (!authDecision.ok) {
|
||||
return authDecision
|
||||
}
|
||||
|
||||
if (!actor) {
|
||||
return deny(401, "请先登录")
|
||||
}
|
||||
|
||||
if (action === "PAY" || action === "CANCEL_PRE_ACCEPT") {
|
||||
return actor.role === "consumer" ? allow() : deny(403, "仅客户可执行该操作")
|
||||
}
|
||||
|
||||
if (action === "ACCEPT") {
|
||||
return actor.role === "player" || actor.role === "owner"
|
||||
? allow()
|
||||
: deny(403, "仅打手或店主可执行该操作")
|
||||
}
|
||||
|
||||
if (action === "RESOLVE_DISPUTE") {
|
||||
return actor.role === "owner" ? allow() : deny(403, "仅店主可执行该操作")
|
||||
}
|
||||
|
||||
return actor.role === "consumer" || actor.role === "player"
|
||||
? allow()
|
||||
: deny(403, "当前身份不可执行该操作")
|
||||
}
|
||||
|
||||
function buildSideEffects(nextStatus: OrderStatus): OrderTransitionSideEffect[] {
|
||||
const sideEffects: OrderTransitionSideEffect[] = [{ type: "CLEAR_TIMEOUT" }]
|
||||
|
||||
if (
|
||||
nextStatus === "pending_accept" ||
|
||||
nextStatus === "pending_close" ||
|
||||
nextStatus === "pending_review"
|
||||
) {
|
||||
sideEffects.push({ type: "SCHEDULE_TIMEOUT", status: nextStatus })
|
||||
}
|
||||
|
||||
if (nextStatus !== "pending_payment" && nextStatus !== "cancelled") {
|
||||
sideEffects.push({ type: "SYNC_CHAT_SESSION" })
|
||||
}
|
||||
|
||||
if (nextStatus === "completed") {
|
||||
sideEffects.push({ type: "PAYOUT_INCOME" })
|
||||
}
|
||||
|
||||
return sideEffects
|
||||
}
|
||||
|
||||
export function evaluateOrderTransition(context: TransitionContext): OrderTransitionResult {
|
||||
const roleDecision = checkRolePermission(context.action, context.actor)
|
||||
if (!roleDecision.ok) {
|
||||
return {
|
||||
decision: roleDecision,
|
||||
sideEffects: [],
|
||||
}
|
||||
}
|
||||
|
||||
const nextStatus = orderTransitionTable[context.order.status][context.action]
|
||||
if (!nextStatus) {
|
||||
return {
|
||||
decision: deny(400, "当前状态不可执行该操作"),
|
||||
sideEffects: [],
|
||||
}
|
||||
}
|
||||
|
||||
if (nextStatus === context.order.status) {
|
||||
return {
|
||||
decision: deny(400, "状态未变化"),
|
||||
sideEffects: [],
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
decision: allow(),
|
||||
nextStatus,
|
||||
sideEffects: buildSideEffects(nextStatus),
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
import type { Shop } from "@/lib/types"
|
||||
|
||||
export function resolveOwnerShop(userId: string | undefined, shops: Shop[]): Shop | null {
|
||||
if (!userId) return null
|
||||
return shops.find((shop) => shop.owner.id === userId) ?? null
|
||||
}
|
||||
Reference in New Issue
Block a user