Added SSE function and add readAt for notification
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { prisma } from '@/db';
|
||||
import { Notification } from '@/generated/prisma/client';
|
||||
import { parseError } from '@lib/errors';
|
||||
import { authMiddleware } from '@lib/middleware';
|
||||
import { createServerFn } from '@tanstack/react-start';
|
||||
@@ -8,17 +9,30 @@ export const getTopFiveNotification = createServerFn({ method: 'GET' })
|
||||
.middleware([authMiddleware])
|
||||
.handler(async ({ context: { user } }) => {
|
||||
try {
|
||||
const list = await prisma.notification.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'asc',
|
||||
},
|
||||
take: 5,
|
||||
});
|
||||
const [list, total]: [Notification[], number] = await prisma.$transaction(
|
||||
[
|
||||
prisma.notification.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'asc',
|
||||
},
|
||||
take: 5,
|
||||
}),
|
||||
prisma.notification.count({
|
||||
where: {
|
||||
userId: user.id,
|
||||
readAt: null,
|
||||
},
|
||||
}),
|
||||
],
|
||||
);
|
||||
|
||||
return list;
|
||||
return {
|
||||
list,
|
||||
hasNewNotify: total > 0,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
const { message, code } = parseError(error);
|
||||
@@ -78,3 +92,24 @@ export const getAllNotifications = createServerFn({ method: 'GET' })
|
||||
throw { message, code };
|
||||
}
|
||||
});
|
||||
|
||||
export const updateReadedNotification = createServerFn({ method: 'POST' })
|
||||
.middleware([authMiddleware])
|
||||
.handler(async ({ context: { user } }) => {
|
||||
try {
|
||||
const result = await prisma.notification.findMany({
|
||||
where: { userId: user.id, readAt: null },
|
||||
});
|
||||
|
||||
if (result.length > 0) {
|
||||
await prisma.notification.updateMany({
|
||||
where: { userId: user.id, readAt: null },
|
||||
data: { readAt: new Date() },
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
const { message, code } = parseError(error);
|
||||
throw { message, code };
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user