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

118 lines
2.6 KiB
TypeScript
Raw Normal View History

2025-02-25 08:25:54 +08:00
import {
AppConfigSlug,
BaseSetting,
db,
PostType,
TaxonomySlug,
VisitType,
} from '@nice/common';
export async function updateTotalCourseViewCount(type: VisitType) {
2025-02-25 09:55:36 +08:00
const posts = await db.post.findMany({
where: {
type: { in: [PostType.COURSE, PostType.LECTURE] },
deletedAt: null,
},
select: { id: true, type: true },
2025-02-25 08:25:54 +08:00
});
2025-02-25 09:55:36 +08:00
const courseIds = posts
.filter((post) => post.type === PostType.COURSE)
.map((course) => course.id);
const lectures = posts.filter((post) => post.type === PostType.LECTURE);
2025-02-25 08:25:54 +08:00
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,
},
});
2025-02-25 09:55:36 +08:00
const staffs = await db.staff.count({
where: { deletedAt: null },
});
2025-02-25 08:25:54 +08:00
const baseSeting = appConfig.meta as BaseSetting;
await db.appConfig.update({
where: {
slug: AppConfigSlug.BASE_SETTING,
},
data: {
meta: {
...baseSeting,
2025-02-25 09:55:36 +08:00
appConfig: {
...(baseSeting?.appConfig || {}),
statistics: {
reads: totalViews._sum.views || 0,
courses: courseIds?.length || 0,
staffs: staffs || 0,
lectures: lectures?.length || 0,
},
},
2025-02-25 08:25:54 +08:00
},
},
});
}
2025-02-24 08:51:44 +08:00
export async function updatePostViewCount(id: string, type: VisitType) {
2025-02-24 10:16:33 +08:00
const post = await db.post.findFirst({
where: { id },
select: { id: true, meta: true },
});
2025-02-24 08:51:44 +08:00
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: {
2025-02-24 10:16:33 +08:00
...((post?.meta as any) || {}),
2025-02-24 08:51:44 +08:00
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: {
2025-02-24 10:16:33 +08:00
...((post?.meta as any) || {}),
2025-02-24 08:51:44 +08:00
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: {
2025-02-24 10:16:33 +08:00
...((post?.meta as any) || {}),
2025-02-24 08:51:44 +08:00
hates: totalViews._sum.views || 0, // Use 0 if no visits exist
},
},
});
}
}