53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import { usePrisma } from "../utils/prisma";
|
|
|
|
export async function createNotification(data: {
|
|
recipientId: string;
|
|
type: string;
|
|
actorName: string;
|
|
actorAvatar?: string;
|
|
postId?: string;
|
|
preview?: string;
|
|
}) {
|
|
const db = usePrisma();
|
|
return db.notification.create({ data });
|
|
}
|
|
|
|
export async function getNotifications(userId: string) {
|
|
const db = usePrisma();
|
|
return db.notification.findMany({
|
|
where: { recipientId: userId },
|
|
orderBy: { createdAt: "desc" },
|
|
take: 30,
|
|
});
|
|
}
|
|
|
|
export async function countUnread(userId: string) {
|
|
const db = usePrisma();
|
|
return db.notification.count({
|
|
where: { recipientId: userId, readAt: null },
|
|
});
|
|
}
|
|
|
|
export async function markAllRead(userId: string) {
|
|
const db = usePrisma();
|
|
return db.notification.updateMany({
|
|
where: { recipientId: userId, readAt: null },
|
|
data: { readAt: new Date() },
|
|
});
|
|
}
|
|
|
|
export async function deleteNotification(notifId: string, userId: string) {
|
|
const db = usePrisma();
|
|
return db.notification.deleteMany({
|
|
where: { id: notifId, recipientId: userId },
|
|
});
|
|
}
|
|
|
|
export async function deleteOldNotifications() {
|
|
const db = usePrisma();
|
|
const threeDaysAgo = new Date(Date.now() - 3 * 24 * 60 * 60 * 1000);
|
|
return db.notification.deleteMany({
|
|
where: { createdAt: { lt: threeDaysAgo } },
|
|
});
|
|
}
|