feat: add editable ordered trail pages
This commit is contained in:
20
server/migrations/015_seed_trails_from_legacy_categories.sql
Normal file
20
server/migrations/015_seed_trails_from_legacy_categories.sql
Normal file
@@ -0,0 +1,20 @@
|
||||
-- Course categories were previously presented as trilhas but only stored a
|
||||
-- label. Turn every existing label into a real ordered trilha without moving
|
||||
-- or deleting any course.
|
||||
insert into learning_paths (title, description, status, published_at)
|
||||
select category.name, '', 'draft', null
|
||||
from course_categories category
|
||||
where not exists (
|
||||
select 1 from learning_paths path where lower(path.title) = lower(category.name)
|
||||
);
|
||||
|
||||
insert into learning_path_courses (learning_path_id, course_id, position)
|
||||
select path.id, course.id, ranked.position
|
||||
from (
|
||||
select id, category, row_number() over (partition by category order by published_at nulls last, title, id)::integer as position
|
||||
from courses
|
||||
where status <> 'archived'
|
||||
) ranked
|
||||
join courses course on course.id = ranked.id
|
||||
join learning_paths path on lower(path.title) = lower(ranked.category)
|
||||
on conflict (learning_path_id, course_id) do nothing;
|
||||
@@ -271,7 +271,18 @@ export const manageCourseRoutes: FastifyPluginAsync = async (app) => {
|
||||
const { courseId } = paramsSchema.parse(request.params);
|
||||
const accessError = await assertCanManageCourse(courseId, request.user);
|
||||
if (accessError) return reply.code(accessError.statusCode).send({ error: accessError.error });
|
||||
await pool.query(`update courses set status = 'archived', published_at = null where id = $1`, [courseId]);
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await client.query('begin');
|
||||
await client.query(`update courses set status = 'archived', published_at = null where id = $1`, [courseId]);
|
||||
await client.query('delete from learning_path_courses where course_id = $1', [courseId]);
|
||||
await client.query('commit');
|
||||
} catch (error) {
|
||||
await client.query('rollback');
|
||||
throw error;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
await recordAudit({ actorId: request.user.id, action: 'course.archived', subjectType: 'course', subjectId: courseId, ipAddress: request.ip });
|
||||
return reply.code(204).send();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user