This commit is contained in:
ditiqi 2025-02-24 10:16:33 +08:00
parent 8652488bad
commit 29f49846b7
1 changed files with 7 additions and 0 deletions

View File

@ -1,5 +1,9 @@
import { db, VisitType } from '@nice/common'; import { db, VisitType } from '@nice/common';
export async function updatePostViewCount(id: string, type: VisitType) { export async function updatePostViewCount(id: string, type: VisitType) {
const post = await db.post.findFirst({
where: { id },
select: { id: true, meta: true },
});
const totalViews = await db.visit.aggregate({ const totalViews = await db.visit.aggregate({
_sum: { _sum: {
views: true, views: true,
@ -16,6 +20,7 @@ export async function updatePostViewCount(id: string, type: VisitType) {
}, },
data: { data: {
meta: { meta: {
...((post?.meta as any) || {}),
views: totalViews._sum.views || 0, views: totalViews._sum.views || 0,
}, // Use 0 if no visits exist }, // Use 0 if no visits exist
}, },
@ -28,6 +33,7 @@ export async function updatePostViewCount(id: string, type: VisitType) {
}, },
data: { data: {
meta: { meta: {
...((post?.meta as any) || {}),
likes: totalViews._sum.views || 0, // Use 0 if no visits exist likes: totalViews._sum.views || 0, // Use 0 if no visits exist
}, },
}, },
@ -39,6 +45,7 @@ export async function updatePostViewCount(id: string, type: VisitType) {
}, },
data: { data: {
meta: { meta: {
...((post?.meta as any) || {}),
hates: totalViews._sum.views || 0, // Use 0 if no visits exist hates: totalViews._sum.views || 0, // Use 0 if no visits exist
}, },
}, },