Files
classroomio/apps/api/src/routes/course/content.ts
T

69 lines
2.7 KiB
TypeScript
Raw Normal View History

2026-03-18 23:39:12 +01:00
import { ZCourseContentDelete, ZCourseContentReorder, ZCourseContentUpdate } from '@cio/utils/validation/course';
import { deleteCourseContent, reorderCourseContent, updateCourseContent } from '@cio/core/services/course/content';
2026-03-09 11:18:22 +01:00
import { Hono } from '@api/utils/hono';
2026-03-18 23:39:12 +01:00
import { authOrAutomationKeyMiddleware } from '@api/middlewares/auth-or-automation-key';
2026-03-09 11:18:22 +01:00
import { authMiddleware } from '@api/middlewares/auth';
2026-03-18 23:39:12 +01:00
import { courseTeamMemberOrAutomationKeyMiddleware } from '@api/middlewares/course-team-member-or-automation-key';
2026-03-09 11:18:22 +01:00
import { courseTeamMemberMiddleware } from '@api/middlewares/course-team-member';
2026-03-18 23:39:12 +01:00
import { assertMcpAutomationUsageAllowed, recordMcpAutomationUsage } from '@api/services/organization/automation-usage';
2026-03-09 11:18:22 +01:00
import { handleError } from '@api/utils/errors';
import { zValidator } from '@hono/zod-validator';
export const contentRouter = new Hono()
2026-03-18 23:39:12 +01:00
.put(
'/reorder',
authOrAutomationKeyMiddleware,
courseTeamMemberOrAutomationKeyMiddleware(['course:write']),
zValidator('json', ZCourseContentReorder),
async (c) => {
try {
const courseId = c.req.param('courseId')!;
const automationKey = c.get('automationKey');
const payload = c.req.valid('json');
if (automationKey?.type === 'mcp') {
await assertMcpAutomationUsageAllowed(automationKey, 'reorder_course_content');
}
const result = await reorderCourseContent(courseId, payload);
if (automationKey?.type === 'mcp') {
await recordMcpAutomationUsage(automationKey, 'reorder_course_content', {
courseId,
sectionCount: payload.sections?.length ?? 0,
itemCount: payload.items?.length ?? 0
});
}
return c.json({ success: true, data: result }, 200);
} catch (error) {
return handleError(c, error, 'Failed to reorder course content');
}
}
)
2026-03-09 11:18:22 +01:00
.put('/', authMiddleware, courseTeamMemberMiddleware, zValidator('json', ZCourseContentUpdate), async (c) => {
try {
const courseId = c.req.param('courseId')!;
const { items } = c.req.valid('json');
await updateCourseContent(courseId, items);
return c.json({ success: true }, 200);
} catch (error) {
return handleError(c, error, 'Failed to update course content');
}
})
.delete('/', authMiddleware, courseTeamMemberMiddleware, zValidator('json', ZCourseContentDelete), async (c) => {
try {
const courseId = c.req.param('courseId')!;
const payload = c.req.valid('json');
await deleteCourseContent(courseId, payload);
return c.json({ success: true }, 200);
} catch (error) {
return handleError(c, error, 'Failed to delete course content');
}
});