import { AppConfigSlug, BaseSetting, db, PostType, TaxonomySlug, VisitType, } from '@nice/common'; export async function updateTotalCourseViewCount(type: VisitType) { const courses = await db.post.findMany({ where: { type: PostType.COURSE }, select: { id: true }, }); const courseIds = courses.map((course) => course.id); const totalViews = await db.visit.aggregate({ _sum: { views: true, }, where: { postId: { in: courseIds }, type: type, }, }); const appConfig = await db.appConfig.findFirst({ where: { slug: AppConfigSlug.BASE_SETTING, }, select: { id: true, meta: true, }, }); const baseSeting = appConfig.meta as BaseSetting; await db.appConfig.update({ where: { slug: AppConfigSlug.BASE_SETTING, }, data: { meta: { ...baseSeting, reads: totalViews, }, }, }); } 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({ _sum: { views: true, }, where: { postId: id, type: type, }, }); if (type === VisitType.READED) { await db.post.update({ where: { id: id, }, data: { meta: { ...((post?.meta as any) || {}), views: totalViews._sum.views || 0, }, // Use 0 if no visits exist }, }); console.log('readed'); } else if (type === VisitType.LIKE) { await db.post.update({ where: { id: id, }, data: { meta: { ...((post?.meta as any) || {}), 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: { ...((post?.meta as any) || {}), hates: totalViews._sum.views || 0, // Use 0 if no visits exist }, }, }); } }