feat: complete platform operations roadmap
This commit is contained in:
@@ -48,6 +48,7 @@ const paramsSchema = z.object({ courseId: z.string().uuid() });
|
||||
type CourseInput = z.infer<typeof courseSchema>;
|
||||
|
||||
class CourseContentConflict extends Error {}
|
||||
class CoursePublicationBlocked extends Error {}
|
||||
|
||||
function slugify(value: string) {
|
||||
return value
|
||||
@@ -82,6 +83,21 @@ async function insertAssets(client: PoolClient, parent: { courseId?: string; les
|
||||
}
|
||||
}
|
||||
|
||||
async function assertActiveCategory(category: string) {
|
||||
const result = await pool.query('select 1 from course_categories where name = $1 and is_active', [category]);
|
||||
if (!result.rowCount) throw new CourseContentConflict('Select an active category created by the superadmin.');
|
||||
}
|
||||
|
||||
function assertPublishable(input: CourseInput) {
|
||||
if (input.status !== 'published') return;
|
||||
const unplayableLesson = input.lessons.find((lesson) =>
|
||||
lesson.media.length === 0 || lesson.media.some((media) => media.status !== 'ready'),
|
||||
);
|
||||
if (unplayableLesson) {
|
||||
throw new CoursePublicationBlocked(`The course cannot be published while "${unplayableLesson.title}" has no ready video.`);
|
||||
}
|
||||
}
|
||||
|
||||
async function replaceCourseContents(client: PoolClient, courseId: string, input: CourseInput) {
|
||||
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));
|
||||
@@ -184,6 +200,8 @@ export const manageCourseRoutes: FastifyPluginAsync = async (app) => {
|
||||
|
||||
app.post('/', manageAccess, async (request, reply) => {
|
||||
const input = courseSchema.parse(request.body);
|
||||
await assertActiveCategory(input.category);
|
||||
assertPublishable(input);
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await client.query('begin');
|
||||
@@ -200,7 +218,7 @@ export const manageCourseRoutes: FastifyPluginAsync = async (app) => {
|
||||
return reply.code(201).send({ data: { id: course.rows[0].id } });
|
||||
} catch (error) {
|
||||
await client.query('rollback');
|
||||
if (error instanceof CourseContentConflict) return reply.code(409).send({ error: error.message });
|
||||
if (error instanceof CourseContentConflict || error instanceof CoursePublicationBlocked) return reply.code(409).send({ error: error.message });
|
||||
throw error;
|
||||
} finally {
|
||||
client.release();
|
||||
@@ -212,6 +230,8 @@ export const manageCourseRoutes: FastifyPluginAsync = async (app) => {
|
||||
const input = courseSchema.parse(request.body);
|
||||
const accessError = await assertCanManageCourse(courseId, request.user);
|
||||
if (accessError) return reply.code(accessError.statusCode).send({ error: accessError.error });
|
||||
await assertActiveCategory(input.category);
|
||||
assertPublishable(input);
|
||||
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
@@ -230,7 +250,7 @@ export const manageCourseRoutes: FastifyPluginAsync = async (app) => {
|
||||
return { data: { id: courseId } };
|
||||
} catch (error) {
|
||||
await client.query('rollback');
|
||||
if (error instanceof CourseContentConflict) return reply.code(409).send({ error: error.message });
|
||||
if (error instanceof CourseContentConflict || error instanceof CoursePublicationBlocked) return reply.code(409).send({ error: error.message });
|
||||
throw error;
|
||||
} finally {
|
||||
client.release();
|
||||
@@ -245,4 +265,65 @@ export const manageCourseRoutes: FastifyPluginAsync = async (app) => {
|
||||
await recordAudit({ actorId: request.user.id, action: 'course.archived', subjectType: 'course', subjectId: courseId, ipAddress: request.ip });
|
||||
return reply.code(204).send();
|
||||
});
|
||||
|
||||
app.post('/:courseId/duplicate', manageAccess, async (request, reply) => {
|
||||
const { courseId } = paramsSchema.parse(request.params);
|
||||
const accessError = await assertCanManageCourse(courseId, request.user);
|
||||
if (accessError) return reply.code(accessError.statusCode).send({ error: accessError.error });
|
||||
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await client.query('begin');
|
||||
const original = await client.query<{ title: string; description: string; category: string; cover_image_url: string | null; instructor_id: string }>(
|
||||
`select title, description, category, cover_image_url, instructor_id from courses where id = $1`, [courseId],
|
||||
);
|
||||
if (!original.rows[0]) {
|
||||
await client.query('rollback');
|
||||
return reply.code(404).send({ error: 'Course not found' });
|
||||
}
|
||||
const source = original.rows[0];
|
||||
const title = `${source.title} (cópia)`;
|
||||
const slug = await uniqueSlug(client, undefined, title);
|
||||
const copiedCourse = await client.query<{ id: string }>(
|
||||
`insert into courses (slug, title, description, category, cover_image_url, status, instructor_id)
|
||||
values ($1, $2, $3, $4, $5, 'draft', $6) returning id`,
|
||||
[slug, title, source.description, source.category, source.cover_image_url, source.instructor_id],
|
||||
);
|
||||
const newCourseId = copiedCourse.rows[0].id;
|
||||
await client.query(
|
||||
`insert into assets (course_id, lesson_id, name, kind, url, size_bytes, description, access_level)
|
||||
select $2, null, name, kind, url, size_bytes, description, access_level from assets where course_id = $1`,
|
||||
[courseId, newCourseId],
|
||||
);
|
||||
const sourceLessons = await client.query<{ id: string; title: string; description: string; position: number; duration_seconds: number | null; access_level: 'public' | 'enrolled' }>(
|
||||
`select id, title, description, position, duration_seconds, access_level from lessons where course_id = $1 order by position`, [courseId],
|
||||
);
|
||||
for (const lesson of sourceLessons.rows) {
|
||||
const copiedLesson = 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`,
|
||||
[newCourseId, lesson.title, lesson.description, lesson.position, lesson.duration_seconds, lesson.access_level],
|
||||
);
|
||||
const newLessonId = copiedLesson.rows[0].id;
|
||||
await client.query(
|
||||
`insert into lesson_media (lesson_id, provider, external_id, playback_url, embed_url, status, duration_seconds, metadata)
|
||||
select $2, provider, external_id, playback_url, embed_url, status, duration_seconds, metadata from lesson_media where lesson_id = $1`,
|
||||
[lesson.id, newLessonId],
|
||||
);
|
||||
await client.query(
|
||||
`insert into assets (course_id, lesson_id, name, kind, url, size_bytes, description, access_level)
|
||||
select null, $2, name, kind, url, size_bytes, description, access_level from assets where lesson_id = $1`,
|
||||
[lesson.id, newLessonId],
|
||||
);
|
||||
}
|
||||
await client.query('commit');
|
||||
await recordAudit({ actorId: request.user.id, action: 'course.duplicated', subjectType: 'course', subjectId: newCourseId, metadata: { sourceCourseId: courseId, title }, ipAddress: request.ip });
|
||||
return reply.code(201).send({ data: { id: newCourseId } });
|
||||
} catch (error) {
|
||||
await client.query('rollback');
|
||||
throw error;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user