refactor: response result

This commit is contained in:
Methapon2001 2025-03-05 11:04:21 +07:00
parent 34af1f9dcd
commit 53c0c0fce9

View file

@ -18,6 +18,7 @@ import prisma from "../db";
import { Prisma } from "@prisma/client"; import { Prisma } from "@prisma/client";
import { queryOrNot } from "../utils/relation"; import { queryOrNot } from "../utils/relation";
import { notFoundError } from "../utils/error"; import { notFoundError } from "../utils/error";
import dayjs from "dayjs";
type NotificationCreate = {}; type NotificationCreate = {};
type NotificationUpdate = {}; type NotificationUpdate = {};
@ -50,14 +51,24 @@ export class NotificationController extends Controller {
], ],
}, },
], ],
NOT: {
readByUser: { some: { id: req.user.sub } },
createdAt: dayjs().subtract(7, "days").toDate(),
},
}; };
const [result, total] = await prisma.$transaction([ const [result, total] = await prisma.$transaction([
prisma.notification.findMany({ where }), prisma.notification.findMany({ where, include: { readByUser: true } }),
prisma.notification.count({ where }), prisma.notification.count({ where }),
]); ]);
return { return {
result, result: result.map((v) => ({
id: v.id,
title: v.title,
detail: v.detail,
createdAt: v.createdAt,
read: v.readByUser.some((v) => v.id === req.user.sub),
})),
page, page,
pageSize, pageSize,
total, total,