feat: expand admin and instructor tools
This commit is contained in:
@@ -23,6 +23,7 @@ const assetSchema = z.object({
|
||||
});
|
||||
|
||||
const lessonSchema = z.object({
|
||||
id: z.string().uuid().optional(),
|
||||
title: z.string().trim().min(1).max(255),
|
||||
description: z.string().max(5000).default(''),
|
||||
durationSeconds: z.number().int().nonnegative().nullable().optional(),
|
||||
@@ -79,20 +80,52 @@ async function insertAssets(client: PoolClient, parent: { courseId?: string; les
|
||||
}
|
||||
|
||||
async function replaceCourseContents(client: PoolClient, courseId: string, input: CourseInput) {
|
||||
// This replacement strategy is safe before student progress exists. The next
|
||||
// iteration will switch to per-lesson updates to preserve historical progress.
|
||||
await client.query('delete from lessons where course_id = $1', [courseId]);
|
||||
const existingLessons = await client.query<{ id: string }>('select id from lessons where course_id = $1', [courseId]);
|
||||
const existingIds = new Set(existingLessons.rows.map((lesson) => lesson.id));
|
||||
const submittedIds = new Set(input.lessons.flatMap((lesson) => lesson.id ? [lesson.id] : []));
|
||||
const unknownLessonId = [...submittedIds].find((lessonId) => !existingIds.has(lessonId));
|
||||
if (unknownLessonId) throw new Error('A lesson being edited does not belong to this course.');
|
||||
|
||||
const removedLessonIds = [...existingIds].filter((lessonId) => !submittedIds.has(lessonId));
|
||||
if (removedLessonIds.length > 0) {
|
||||
const usage = await client.query<{ id: string }>(
|
||||
`select l.id
|
||||
from lessons l
|
||||
where l.id = any($1::uuid[])
|
||||
and (exists (select 1 from lesson_progress lp where lp.lesson_id = l.id)
|
||||
or exists (select 1 from comments c where c.lesson_id = l.id))`,
|
||||
[removedLessonIds],
|
||||
);
|
||||
if (usage.rowCount) {
|
||||
throw new Error('A lesson with student progress or comments cannot be removed. Keep it or archive the course instead.');
|
||||
}
|
||||
await client.query('delete from lessons where id = any($1::uuid[])', [removedLessonIds]);
|
||||
}
|
||||
|
||||
// Move existing positions away first so drag/reordering cannot violate the
|
||||
// unique (course_id, position) constraint while updates are applied.
|
||||
await client.query('update lessons set position = position + 10000 where course_id = $1', [courseId]);
|
||||
await client.query('delete from assets where course_id = $1', [courseId]);
|
||||
|
||||
await insertAssets(client, { courseId }, input.assets);
|
||||
for (const [index, lesson] of input.lessons.entries()) {
|
||||
const lessonResult = await client.query<{ id: string }>(
|
||||
const lessonId = lesson.id ?? (await client.query<{ id: string }>(
|
||||
`insert into lessons (course_id, title, description, position, duration_seconds, access_level)
|
||||
values ($1, $2, $3, $4, $5, $6)
|
||||
returning id`,
|
||||
[courseId, lesson.title, lesson.description, index + 1, lesson.durationSeconds ?? null, lesson.accessLevel],
|
||||
);
|
||||
const lessonId = lessonResult.rows[0].id;
|
||||
)).rows[0].id;
|
||||
|
||||
if (lesson.id) {
|
||||
await client.query(
|
||||
`update lessons
|
||||
set title = $2, description = $3, position = $4, duration_seconds = $5, access_level = $6
|
||||
where id = $1`,
|
||||
[lessonId, lesson.title, lesson.description, index + 1, lesson.durationSeconds ?? null, lesson.accessLevel],
|
||||
);
|
||||
await client.query('delete from lesson_media where lesson_id = $1', [lessonId]);
|
||||
await client.query('delete from assets where lesson_id = $1', [lessonId]);
|
||||
}
|
||||
|
||||
for (const media of lesson.media) {
|
||||
await client.query(
|
||||
@@ -127,6 +160,25 @@ export const manageCourseRoutes: FastifyPluginAsync = async (app) => {
|
||||
return { data: result.rows };
|
||||
});
|
||||
|
||||
app.get('/analytics', manageAccess, async (request) => {
|
||||
const isAdmin = request.user.role === 'admin';
|
||||
const result = await pool.query(
|
||||
`select
|
||||
count(distinct c.id)::int as "courses",
|
||||
count(distinct l.id)::int as "lessons",
|
||||
count(distinct lp.user_id)::int as "learners",
|
||||
count(distinct lp.lesson_id) filter (where lp.completed_at is not null)::int as "completedLessons",
|
||||
count(distinct cm.id)::int as "comments"
|
||||
from courses c
|
||||
left join lessons l on l.course_id = c.id
|
||||
left join lesson_progress lp on lp.lesson_id = l.id
|
||||
left join comments cm on cm.course_id = c.id
|
||||
where c.status <> 'archived' and ($1::boolean or c.instructor_id = $2)`,
|
||||
[isAdmin, request.user.id],
|
||||
);
|
||||
return { data: result.rows[0] };
|
||||
});
|
||||
|
||||
app.post('/', manageAccess, async (request, reply) => {
|
||||
const input = courseSchema.parse(request.body);
|
||||
const client = await pool.connect();
|
||||
|
||||
Reference in New Issue
Block a user