doctor-mail/apps/server/src/queue/models/post/utils.ts

33 lines
771 B
TypeScript
Raw Normal View History

2025-01-24 15:06:57 +08:00
import { db, PostState, PostType, VisitType } from '@nice/common';
2025-01-24 00:19:02 +08:00
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: {
2025-01-24 17:39:41 +08:00
id: id,
2025-01-24 00:19:02 +08:00
},
data: {
views: totalViews._sum.views || 0, // Use 0 if no visits exist
},
});
} else if (type === VisitType.LIKE) {
2025-01-24 17:39:41 +08:00
console.log('totalViews._sum.view', totalViews._sum.views);
2025-01-24 00:19:02 +08:00
await db.post.update({
where: {
2025-01-24 17:39:41 +08:00
id: id,
2025-01-24 00:19:02 +08:00
},
data: {
likes: totalViews._sum.views || 0, // Use 0 if no visits exist
},
});
}
}