feat(notifications): add notification system and wire order/dispute events
This commit is contained in:
+26
-2
@@ -1,17 +1,31 @@
|
||||
import { create } from "zustand"
|
||||
import type { User, UserRole, VerificationStatus } from "@/lib/types"
|
||||
|
||||
interface NotificationPrefs {
|
||||
order: boolean
|
||||
community: boolean
|
||||
system: boolean
|
||||
}
|
||||
|
||||
const defaultNotificationPrefs: NotificationPrefs = {
|
||||
order: true,
|
||||
community: true,
|
||||
system: false,
|
||||
}
|
||||
|
||||
interface AuthState {
|
||||
isAuthenticated: boolean
|
||||
currentRole: UserRole
|
||||
verifiedRoles: UserRole[]
|
||||
verificationStatus: Partial<Record<UserRole, VerificationStatus>>
|
||||
verificationReasons: Partial<Record<UserRole, string>>
|
||||
notificationPrefs: NotificationPrefs
|
||||
user: User | null
|
||||
switchRole: (role: UserRole) => void
|
||||
submitVerification: (role: UserRole) => void
|
||||
approveVerification: (role: UserRole) => void
|
||||
rejectVerification: (role: UserRole, reason: string) => void
|
||||
setNotificationPref: (type: keyof NotificationPrefs, enabled: boolean) => void
|
||||
updateProfile: (patch: { nickname?: string; bio?: string; avatar?: string }) => void
|
||||
login: (user: User, verifiedRoles?: UserRole[]) => void
|
||||
logout: () => void
|
||||
@@ -23,6 +37,7 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
verifiedRoles: ["consumer"],
|
||||
verificationStatus: { consumer: "approved" },
|
||||
verificationReasons: {},
|
||||
notificationPrefs: defaultNotificationPrefs,
|
||||
user: null,
|
||||
switchRole: (role) => {
|
||||
const { verifiedRoles } = get()
|
||||
@@ -84,6 +99,13 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
},
|
||||
}
|
||||
}),
|
||||
setNotificationPref: (type, enabled) =>
|
||||
set((state) => ({
|
||||
notificationPrefs: {
|
||||
...state.notificationPrefs,
|
||||
[type]: enabled,
|
||||
},
|
||||
})),
|
||||
updateProfile: (patch) =>
|
||||
set((state) => {
|
||||
if (!state.user) return state
|
||||
@@ -98,7 +120,7 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
}
|
||||
}),
|
||||
login: (user, verifiedRoles = ["consumer"]) =>
|
||||
set({
|
||||
set((state) => ({
|
||||
isAuthenticated: true,
|
||||
user,
|
||||
currentRole: user.role,
|
||||
@@ -111,7 +133,8 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
{},
|
||||
),
|
||||
verificationReasons: {},
|
||||
}),
|
||||
notificationPrefs: state.notificationPrefs,
|
||||
})),
|
||||
logout: () =>
|
||||
set({
|
||||
isAuthenticated: false,
|
||||
@@ -119,6 +142,7 @@ export const useAuthStore = create<AuthState>((set, get) => ({
|
||||
verifiedRoles: ["consumer"],
|
||||
verificationStatus: { consumer: "approved" },
|
||||
verificationReasons: {},
|
||||
notificationPrefs: defaultNotificationPrefs,
|
||||
user: null,
|
||||
}),
|
||||
}))
|
||||
|
||||
@@ -6,6 +6,8 @@ import type { Actor } from "@/lib/policy/actor"
|
||||
import type { PolicyDecision } from "@/lib/policy/decision"
|
||||
import { mockDisputes } from "@/lib/mock"
|
||||
import type { Dispute } from "@/lib/types"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { useNotificationStore } from "@/store/notifications"
|
||||
import { useOrderStore } from "@/store/orders"
|
||||
|
||||
type DisputeTimelineType = "created" | "response" | "reviewing" | "resolved" | "appealed"
|
||||
@@ -120,6 +122,19 @@ function asRecord(dispute: Dispute): DisputeRecord {
|
||||
}
|
||||
}
|
||||
|
||||
function notifyDispute(orderId: string, title: string, content: string) {
|
||||
if (!useAuthStore.getState().notificationPrefs.order) {
|
||||
return
|
||||
}
|
||||
|
||||
useNotificationStore.getState().addNotification({
|
||||
type: "order",
|
||||
title,
|
||||
content,
|
||||
link: `/dispute/${orderId}`,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDisputeStore = create<DisputeState>((set, get) => {
|
||||
const scheduleProgress = (disputeId: string) => {
|
||||
clearProgressTimers(disputeId)
|
||||
@@ -148,11 +163,14 @@ export const useDisputeStore = create<DisputeState>((set, get) => {
|
||||
}, DISPUTE_TO_REVIEWING_MS)
|
||||
|
||||
const toResolved = setTimeout(() => {
|
||||
let resolvedOrderId: string | null = null
|
||||
set((state) => ({
|
||||
disputes: state.disputes.map((dispute) => {
|
||||
if (dispute.id !== disputeId) return dispute
|
||||
if (dispute.status !== "reviewing" && dispute.status !== "appealed") return dispute
|
||||
|
||||
resolvedOrderId = dispute.orderId
|
||||
|
||||
return {
|
||||
...dispute,
|
||||
status: "resolved",
|
||||
@@ -169,6 +187,11 @@ export const useDisputeStore = create<DisputeState>((set, get) => {
|
||||
}
|
||||
}),
|
||||
}))
|
||||
|
||||
if (resolvedOrderId) {
|
||||
notifyDispute(resolvedOrderId, "争议已处理", "平台已给出争议处理结果")
|
||||
}
|
||||
|
||||
clearProgressTimers(disputeId)
|
||||
}, DISPUTE_TO_RESOLVED_MS)
|
||||
|
||||
@@ -275,6 +298,8 @@ export const useDisputeStore = create<DisputeState>((set, get) => {
|
||||
}),
|
||||
}))
|
||||
|
||||
notifyDispute(dispute.orderId, "争议收到回应", "对方已提交争议回应材料")
|
||||
|
||||
return allow()
|
||||
},
|
||||
submitAppeal: (disputeId, actorId, reason) => {
|
||||
@@ -322,6 +347,8 @@ export const useDisputeStore = create<DisputeState>((set, get) => {
|
||||
}),
|
||||
}))
|
||||
|
||||
notifyDispute(dispute.orderId, "争议已申诉", "申诉已提交,平台将继续复核")
|
||||
|
||||
scheduleProgress(disputeId)
|
||||
return allow()
|
||||
},
|
||||
|
||||
@@ -1,14 +1,47 @@
|
||||
import { create } from "zustand"
|
||||
import { generateId } from "@/lib/id"
|
||||
import { mockNotifications } from "@/lib/mock"
|
||||
import type { Notification } from "@/lib/types"
|
||||
|
||||
interface CreateNotificationInput {
|
||||
type: Notification["type"]
|
||||
title: string
|
||||
content: string
|
||||
link?: string
|
||||
}
|
||||
|
||||
interface NotificationState {
|
||||
notifications: Notification[]
|
||||
addNotification: (input: CreateNotificationInput) => Notification
|
||||
markAsRead: (notificationId: string) => void
|
||||
markAllAsRead: () => void
|
||||
}
|
||||
|
||||
export const useNotificationStore = create<NotificationState>((set) => ({
|
||||
notifications: mockNotifications,
|
||||
addNotification: (input) => {
|
||||
const notification: Notification = {
|
||||
id: generateId("notif"),
|
||||
type: input.type,
|
||||
title: input.title,
|
||||
content: input.content,
|
||||
link: input.link,
|
||||
read: false,
|
||||
createdAt: new Date().toISOString(),
|
||||
}
|
||||
|
||||
set((state) => ({
|
||||
notifications: [notification, ...state.notifications],
|
||||
}))
|
||||
|
||||
return notification
|
||||
},
|
||||
markAsRead: (notificationId) =>
|
||||
set((state) => ({
|
||||
notifications: state.notifications.map((notification) =>
|
||||
notification.id === notificationId ? { ...notification, read: true } : notification,
|
||||
),
|
||||
})),
|
||||
markAllAsRead: () =>
|
||||
set((state) => ({
|
||||
notifications: state.notifications.map((notification) => ({ ...notification, read: true })),
|
||||
|
||||
+62
-2
@@ -7,7 +7,9 @@ import type { Actor } from "@/lib/policy/actor"
|
||||
import type { PolicyDecision } from "@/lib/policy/decision"
|
||||
import { mockOrders } from "@/lib/mock"
|
||||
import type { Order, OrderStatus, PlayerService } from "@/lib/types"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { useChatStore } from "@/store/chat"
|
||||
import { useNotificationStore } from "@/store/notifications"
|
||||
import { useWalletStore } from "@/store/wallet"
|
||||
|
||||
interface CreateOrderInput {
|
||||
@@ -191,6 +193,55 @@ function scheduleOrderTimeout(orderId: string, status: OrderStatus) {
|
||||
orderTimeouts.set(orderId, timer)
|
||||
}
|
||||
|
||||
function notifyOrderStatus(order: Order) {
|
||||
if (!useAuthStore.getState().notificationPrefs.order) {
|
||||
return
|
||||
}
|
||||
|
||||
const mapping: Partial<Record<OrderStatus, { title: string; content: string }>> = {
|
||||
pending_accept: {
|
||||
title: "订单待接单",
|
||||
content: `${order.service.title} 已支付,等待接单`,
|
||||
},
|
||||
in_progress: {
|
||||
title: "订单已接单",
|
||||
content: `${order.playerName} 已开始服务`,
|
||||
},
|
||||
pending_close: {
|
||||
title: "订单发起结单",
|
||||
content: `订单 ${order.id} 等待确认结单`,
|
||||
},
|
||||
pending_review: {
|
||||
title: "订单待评价",
|
||||
content: "服务已结束,可提交双向评价",
|
||||
},
|
||||
completed: {
|
||||
title: "订单已完成",
|
||||
content: `订单 ${order.id} 已完成`,
|
||||
},
|
||||
cancelled: {
|
||||
title: "订单已取消",
|
||||
content: `订单 ${order.id} 已取消`,
|
||||
},
|
||||
disputed: {
|
||||
title: "订单进入争议",
|
||||
content: "已发起争议,等待平台处理",
|
||||
},
|
||||
}
|
||||
|
||||
const payload = mapping[order.status]
|
||||
if (!payload) {
|
||||
return
|
||||
}
|
||||
|
||||
useNotificationStore.getState().addNotification({
|
||||
type: "order",
|
||||
title: payload.title,
|
||||
content: payload.content,
|
||||
link: `/order/${order.id}`,
|
||||
})
|
||||
}
|
||||
|
||||
export const useOrderStore = create<OrderState>((set, get) => {
|
||||
const applyTransition = (
|
||||
orderId: string,
|
||||
@@ -226,7 +277,13 @@ export const useOrderStore = create<OrderState>((set, get) => {
|
||||
}
|
||||
|
||||
if (previousOrder.status !== "completed" && updatedOrder.status === "completed") {
|
||||
useWalletStore.getState().addIncome(updatedOrder.id, updatedOrder.totalPrice)
|
||||
useWalletStore
|
||||
.getState()
|
||||
.addIncome(updatedOrder.id, updatedOrder.totalPrice, updatedOrder.shopId)
|
||||
}
|
||||
|
||||
if (previousOrder.status !== updatedOrder.status) {
|
||||
notifyOrderStatus(updatedOrder)
|
||||
}
|
||||
|
||||
const shouldRefund =
|
||||
@@ -299,6 +356,7 @@ export const useOrderStore = create<OrderState>((set, get) => {
|
||||
}))
|
||||
|
||||
useChatStore.getState().ensureOrderSession(order)
|
||||
notifyOrderStatus(order)
|
||||
return { decision: allow(), order }
|
||||
},
|
||||
payOrder: (orderId, actor) => applyTransition(orderId, "PAY", actor),
|
||||
@@ -323,7 +381,9 @@ export const useOrderStore = create<OrderState>((set, get) => {
|
||||
|
||||
if (previousOrder && updatedOrder) {
|
||||
if (previousOrder.status !== "completed" && updatedOrder.status === "completed") {
|
||||
useWalletStore.getState().addIncome(updatedOrder.id, updatedOrder.totalPrice)
|
||||
useWalletStore
|
||||
.getState()
|
||||
.addIncome(updatedOrder.id, updatedOrder.totalPrice, updatedOrder.shopId)
|
||||
}
|
||||
|
||||
if (previousOrder.status === "pending_accept" && updatedOrder.status === "cancelled") {
|
||||
|
||||
Reference in New Issue
Block a user