Notification UI and house invitation
This commit is contained in:
80
src/service/notify.api.ts
Normal file
80
src/service/notify.api.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { prisma } from '@/db';
|
||||
import { parseError } from '@lib/errors';
|
||||
import { authMiddleware } from '@lib/middleware';
|
||||
import { createServerFn } from '@tanstack/react-start';
|
||||
import { notificationListSchema } from './notify.schema';
|
||||
|
||||
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,
|
||||
});
|
||||
|
||||
return list;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
const { message, code } = parseError(error);
|
||||
throw { message, code };
|
||||
}
|
||||
});
|
||||
|
||||
export const getAllNotifications = createServerFn({ method: 'GET' })
|
||||
.middleware([authMiddleware])
|
||||
.inputValidator(notificationListSchema)
|
||||
.handler(async ({ data, context: { user } }) => {
|
||||
try {
|
||||
const skip = (data.page - 1) * data.limit;
|
||||
|
||||
const [list, total]: [NotificationWithUser[], number] =
|
||||
await prisma.$transaction([
|
||||
prisma.notification.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
},
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
image: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'asc',
|
||||
},
|
||||
take: data.limit,
|
||||
skip,
|
||||
}),
|
||||
prisma.notification.count({
|
||||
where: {
|
||||
userId: user.id,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
const totalPage = Math.ceil(+total / data.limit);
|
||||
|
||||
return {
|
||||
result: list,
|
||||
pagination: {
|
||||
currentPage: data.page,
|
||||
totalPage,
|
||||
totalItem: total,
|
||||
},
|
||||
};
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
const { message, code } = parseError(error);
|
||||
throw { message, code };
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user