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

34 lines
958 B
TypeScript

import { allow, deny } from "@/lib/decision"
import type { Notification } from "@/lib/types"
import { useAuthStore } from "@/store/auth"
import { useNotificationStore } from "@/store/notifications"
export function listNotifications() {
return useNotificationStore.getState().notifications
}
function isNotificationEnabled(type: Notification["type"]) {
const prefs = useAuthStore.getState().notificationPrefs
if (type === "order") return prefs.order
if (type === "community") return prefs.community
return prefs.system
}
export function addNotification(input: {
type: Notification["type"]
title: string
content: string
link?: string
}) {
if (!isNotificationEnabled(input.type)) {
return deny(400, "该类通知已关闭")
}
useNotificationStore.getState().addNotification(input)
return allow()
}
export function markNotificationAsRead(notificationId: string) {
useNotificationStore.getState().markAsRead(notificationId)
}