refactor(errors): migrate decisions to {code,msg}
This commit is contained in:
+11
-11
@@ -1,4 +1,4 @@
|
||||
import { allow, deny } from "@/lib/policy/assert"
|
||||
import { allow, deny } from "@/lib/decision"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { useChatStore } from "@/store/chat"
|
||||
|
||||
@@ -16,18 +16,18 @@ export function listChatMessages(sessionId: string) {
|
||||
|
||||
export function sendTextMessage(sessionId: string, content: string) {
|
||||
const userId = useAuthStore.getState().user?.id
|
||||
if (!userId) return deny("AUTH_REQUIRED", "请先登录")
|
||||
if (!userId) return deny(401, "请先登录")
|
||||
|
||||
const chatState = useChatStore.getState()
|
||||
const session = chatState.sessions.find((item) => item.id === sessionId)
|
||||
if (!session) return deny("NOT_FOUND", "会话不存在")
|
||||
if (session.readonly) return deny("INVALID_STATUS", "当前会话只读")
|
||||
if (!session) return deny(404, "会话不存在")
|
||||
if (session.readonly) return deny(400, "当前会话只读")
|
||||
if (!session.participants.some((participant) => participant.id === userId)) {
|
||||
return deny("NOT_PARTICIPANT", "仅会话参与方可发送消息")
|
||||
return deny(403, "仅会话参与方可发送消息")
|
||||
}
|
||||
|
||||
if (!content.trim()) {
|
||||
return deny("VALIDATION_FAILED", "消息不能为空")
|
||||
return deny(400, "消息不能为空")
|
||||
}
|
||||
|
||||
chatState.sendTextMessage(sessionId, userId, content)
|
||||
@@ -36,18 +36,18 @@ export function sendTextMessage(sessionId: string, content: string) {
|
||||
|
||||
export function sendImageMessage(sessionId: string, imageUrl: string) {
|
||||
const userId = useAuthStore.getState().user?.id
|
||||
if (!userId) return deny("AUTH_REQUIRED", "请先登录")
|
||||
if (!userId) return deny(401, "请先登录")
|
||||
|
||||
const chatState = useChatStore.getState()
|
||||
const session = chatState.sessions.find((item) => item.id === sessionId)
|
||||
if (!session) return deny("NOT_FOUND", "会话不存在")
|
||||
if (session.readonly) return deny("INVALID_STATUS", "当前会话只读")
|
||||
if (!session) return deny(404, "会话不存在")
|
||||
if (session.readonly) return deny(400, "当前会话只读")
|
||||
if (!session.participants.some((participant) => participant.id === userId)) {
|
||||
return deny("NOT_PARTICIPANT", "仅会话参与方可发送消息")
|
||||
return deny(403, "仅会话参与方可发送消息")
|
||||
}
|
||||
|
||||
if (!imageUrl.trim()) {
|
||||
return deny("VALIDATION_FAILED", "图片地址无效")
|
||||
return deny(400, "图片地址无效")
|
||||
}
|
||||
|
||||
chatState.sendImageMessage(sessionId, userId, imageUrl)
|
||||
|
||||
+6
-6
@@ -1,5 +1,5 @@
|
||||
import { addNotification } from "@/lib/api/notifications"
|
||||
import { allow, deny } from "@/lib/policy/assert"
|
||||
import { allow, deny } from "@/lib/decision"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { useCommentStore } from "@/store/comments"
|
||||
import { usePostStore } from "@/store/posts"
|
||||
@@ -15,17 +15,17 @@ export function listCommentsByPost(postId: string) {
|
||||
export function addComment(postId: string, content: string) {
|
||||
const user = useAuthStore.getState().user
|
||||
if (!user) {
|
||||
return deny("AUTH_REQUIRED", "请先登录")
|
||||
return deny(401, "请先登录")
|
||||
}
|
||||
|
||||
const post = usePostStore.getState().posts.find((item) => item.id === postId)
|
||||
if (!post) {
|
||||
return deny("NOT_FOUND", "帖子不存在")
|
||||
return deny(404, "帖子不存在")
|
||||
}
|
||||
|
||||
const comment = useCommentStore.getState().addComment(postId, user, content)
|
||||
if (!comment) {
|
||||
return deny("VALIDATION_FAILED", "评论内容不能为空")
|
||||
return deny(400, "评论内容不能为空")
|
||||
}
|
||||
|
||||
usePostStore.getState().incrementCommentCount(postId)
|
||||
@@ -43,12 +43,12 @@ export function addComment(postId: string, content: string) {
|
||||
export function toggleCommentLike(commentId: string) {
|
||||
const user = useAuthStore.getState().user
|
||||
if (!user) {
|
||||
return deny("AUTH_REQUIRED", "请先登录")
|
||||
return deny(401, "请先登录")
|
||||
}
|
||||
|
||||
const comment = useCommentStore.getState().comments.find((item) => item.id === commentId)
|
||||
if (!comment) {
|
||||
return deny("NOT_FOUND", "评论不存在")
|
||||
return deny(404, "评论不存在")
|
||||
}
|
||||
|
||||
useCommentStore.getState().toggleCommentLike(commentId)
|
||||
|
||||
+4
-4
@@ -1,4 +1,4 @@
|
||||
import { deny } from "@/lib/policy/assert"
|
||||
import { deny } from "@/lib/decision"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { useDisputeStore } from "@/store/disputes"
|
||||
|
||||
@@ -13,7 +13,7 @@ export function getDisputeByOrderId(orderId: string) {
|
||||
export function submitDispute(input: { orderId: string; reason: string; evidence: string[] }) {
|
||||
const user = useAuthStore.getState().user
|
||||
if (!user?.id || !user.nickname) {
|
||||
return { decision: deny("AUTH_REQUIRED", "请先登录") }
|
||||
return { decision: deny(401, "请先登录") }
|
||||
}
|
||||
|
||||
return useDisputeStore.getState().submitDispute({
|
||||
@@ -32,7 +32,7 @@ export function submitDisputeResponse(input: {
|
||||
}) {
|
||||
const userId = useAuthStore.getState().user?.id
|
||||
if (!userId) {
|
||||
return deny("AUTH_REQUIRED", "请先登录")
|
||||
return deny(401, "请先登录")
|
||||
}
|
||||
|
||||
return useDisputeStore
|
||||
@@ -43,7 +43,7 @@ export function submitDisputeResponse(input: {
|
||||
export function submitDisputeAppeal(input: { disputeId: string; reason: string }) {
|
||||
const userId = useAuthStore.getState().user?.id
|
||||
if (!userId) {
|
||||
return deny("AUTH_REQUIRED", "请先登录")
|
||||
return deny(401, "请先登录")
|
||||
}
|
||||
|
||||
return useDisputeStore.getState().submitAppeal(input.disputeId, userId, input.reason)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { allow, deny } from "@/lib/policy/assert"
|
||||
import { allow, deny } from "@/lib/decision"
|
||||
import type { Notification } from "@/lib/types"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { useNotificationStore } from "@/store/notifications"
|
||||
@@ -21,7 +21,7 @@ export function addNotification(input: {
|
||||
link?: string
|
||||
}) {
|
||||
if (!isNotificationEnabled(input.type)) {
|
||||
return deny("IDEMPOTENT_NOOP", "该类通知已关闭")
|
||||
return deny(400, "该类通知已关闭")
|
||||
}
|
||||
|
||||
useNotificationStore.getState().addNotification(input)
|
||||
|
||||
+5
-5
@@ -1,7 +1,7 @@
|
||||
import type { Actor } from "@/lib/actor"
|
||||
import { allow, deny } from "@/lib/decision"
|
||||
import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop"
|
||||
import type { Actor } from "@/lib/policy/actor"
|
||||
import { allow, deny } from "@/lib/policy/assert"
|
||||
import type { PolicyDecision } from "@/lib/policy/decision"
|
||||
import type { ApiDecision } from "@/lib/errors"
|
||||
import type { PlayerService } from "@/lib/types"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { useOrderStore } from "@/store/orders"
|
||||
@@ -31,10 +31,10 @@ interface CreatePaidOrderInput {
|
||||
note?: string
|
||||
}
|
||||
|
||||
function resolveActorContext(): { actor?: Actor; decision: PolicyDecision } {
|
||||
function resolveActorContext(): { actor?: Actor; decision: ApiDecision } {
|
||||
const auth = useAuthStore.getState()
|
||||
if (!auth.user?.id) {
|
||||
return { decision: deny("AUTH_REQUIRED", "请先登录") }
|
||||
return { decision: deny(401, "请先登录") }
|
||||
}
|
||||
|
||||
const shopId =
|
||||
|
||||
+3
-3
@@ -1,5 +1,5 @@
|
||||
import { addNotification } from "@/lib/api/notifications"
|
||||
import { allow, deny } from "@/lib/policy/assert"
|
||||
import { allow, deny } from "@/lib/decision"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { usePostStore } from "@/store/posts"
|
||||
|
||||
@@ -18,12 +18,12 @@ export function listPostsByAuthor(userId: string) {
|
||||
export function togglePostLike(postId: string) {
|
||||
const user = useAuthStore.getState().user
|
||||
if (!user) {
|
||||
return deny("AUTH_REQUIRED", "请先登录")
|
||||
return deny(401, "请先登录")
|
||||
}
|
||||
|
||||
const post = usePostStore.getState().posts.find((item) => item.id === postId)
|
||||
if (!post) {
|
||||
return deny("NOT_FOUND", "帖子不存在")
|
||||
return deny(404, "帖子不存在")
|
||||
}
|
||||
|
||||
const shouldNotify = !post.liked
|
||||
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
import { deny } from "@/lib/policy/assert"
|
||||
import { deny } from "@/lib/decision"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { useReviewStore } from "@/store/reviews"
|
||||
|
||||
@@ -17,7 +17,7 @@ export function listReviewsByTargetUser(userId: string) {
|
||||
export function submitReview(input: { orderId: string; rating: number; content?: string }) {
|
||||
const userId = useAuthStore.getState().user?.id
|
||||
if (!userId) {
|
||||
return deny("AUTH_REQUIRED", "请先登录")
|
||||
return deny(401, "请先登录")
|
||||
}
|
||||
|
||||
return useReviewStore.getState().submitReview({
|
||||
|
||||
Reference in New Issue
Block a user