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

41 lines
916 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) {
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
},
});
2025-01-26 16:10:31 +08:00
} else if (type === VisitType.HATE) {
await db.post.update({
where: {
id: id,
},
data: {
hates: totalViews._sum.views || 0, // Use 0 if no visits exist
},
});
2025-01-24 00:19:02 +08:00
}
}