book_manage/apps/server/src/queue/models/post/utils.ts

48 lines
1009 B
TypeScript
Raw Normal View History

2025-02-24 08:51:44 +08:00
import { db, VisitType } from '@nice/common';
export async function updatePostViewCount(id: string, type: VisitType) {
const totalViews = await db.visit.aggregate({
_sum: {
views: true,
},
where: {
postId: id,
type: type,
},
});
if (type === VisitType.READED) {
await db.post.update({
where: {
id: id,
},
data: {
meta: {
views: totalViews._sum.views || 0,
}, // Use 0 if no visits exist
},
});
2025-02-24 09:32:55 +08:00
console.log('readed');
2025-02-24 08:51:44 +08:00
} else if (type === VisitType.LIKE) {
await db.post.update({
where: {
id: id,
},
data: {
meta: {
likes: totalViews._sum.views || 0, // Use 0 if no visits exist
},
},
});
} else if (type === VisitType.HATE) {
await db.post.update({
where: {
id: id,
},
data: {
meta: {
hates: totalViews._sum.views || 0, // Use 0 if no visits exist
},
},
});
}
}