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

137 lines
3.3 KiB
TypeScript
Raw Normal View History

2025-02-25 08:25:54 +08:00
import {
AppConfigSlug,
BaseSetting,
db,
PostType,
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: {
2025-02-27 23:50:07 +08:00
// type: { in: [PostType.COURSE, PostType.LECTURE,] },
2025-02-25 09:55:36 +08:00
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: {
2025-02-26 21:08:38 +08:00
postId: { in: posts.map((post) => post.id) },
2025-02-25 08:25:54 +08:00
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 },
2025-02-26 17:04:20 +08:00
select: { id: true, meta: true, type: true },
2025-02-24 10:16:33 +08:00
});
2025-02-26 17:04:20 +08:00
const metaFieldMap = {
[VisitType.READED]: 'views',
[VisitType.LIKE]: 'likes',
[VisitType.HATE]: 'hates',
};
if (post?.type === PostType.LECTURE) {
2025-02-27 23:50:07 +08:00
const courseAncestry = await db.postAncestry.findFirst({
2025-02-24 08:51:44 +08:00
where: {
2025-02-26 17:04:20 +08:00
descendantId: post?.id,
ancestor: {
type: PostType.COURSE,
},
2025-02-24 08:51:44 +08:00
},
2025-02-27 23:50:07 +08:00
select: { id: true, ancestorId: true },
2025-02-26 17:04:20 +08:00
});
2025-02-27 23:50:07 +08:00
const course = { id: courseAncestry.ancestorId };
const lecturesAncestry = await db.postAncestry.findMany({
2025-02-26 17:04:20 +08:00
where: { ancestorId: course.id, descendant: { type: PostType.LECTURE } },
select: {
id: true,
2025-02-27 23:50:07 +08:00
descendantId: true,
2025-02-24 08:51:44 +08:00
},
});
2025-02-27 23:50:07 +08:00
const lectures = lecturesAncestry.map((ancestry) => ({
id: ancestry.descendantId,
}));
2025-02-26 17:04:20 +08:00
const courseViews = await db.visit.aggregate({
_sum: {
views: true,
2025-02-24 08:51:44 +08:00
},
2025-02-26 17:04:20 +08:00
where: {
postId: {
in: [course.id, ...lectures.map((lecture) => lecture.id)],
2025-02-24 08:51:44 +08:00
},
2025-02-26 17:04:20 +08:00
type: type,
2025-02-24 08:51:44 +08:00
},
});
await db.post.update({
2025-02-26 17:04:20 +08:00
where: { id: course.id },
2025-02-24 08:51:44 +08:00
data: {
2025-02-27 23:50:07 +08:00
[metaFieldMap[type]]: courseViews._sum.views || 0,
2025-02-24 08:51:44 +08:00
meta: {
2025-02-24 10:16:33 +08:00
...((post?.meta as any) || {}),
2025-02-26 17:04:20 +08:00
[metaFieldMap[type]]: courseViews._sum.views || 0,
2025-02-24 08:51:44 +08:00
},
},
});
}
2025-02-26 17:04:20 +08:00
const totalViews = await db.visit.aggregate({
_sum: {
views: true,
},
where: {
postId: id,
type: type,
},
});
await db.post.update({
where: { id },
data: {
2025-02-27 23:50:07 +08:00
[metaFieldMap[type]]: totalViews._sum.views || 0,
2025-02-26 17:04:20 +08:00
meta: {
...((post?.meta as any) || {}),
[metaFieldMap[type]]: totalViews._sum.views || 0,
},
},
});
2025-02-24 08:51:44 +08:00
}