Add lessons Phase 2: schedule slots with conflict detection
Recurring weekly time slots linking instructors to lesson types. Includes day/time overlap detection, instructor and day-of-week filtering, and 17 new integration tests (40 total lessons tests).
This commit is contained in:
@@ -5,8 +5,10 @@ import {
|
||||
InstructorUpdateSchema,
|
||||
LessonTypeCreateSchema,
|
||||
LessonTypeUpdateSchema,
|
||||
ScheduleSlotCreateSchema,
|
||||
ScheduleSlotUpdateSchema,
|
||||
} from '@lunarfront/shared/schemas'
|
||||
import { InstructorService, LessonTypeService } from '../../services/lesson.service.js'
|
||||
import { InstructorService, LessonTypeService, ScheduleSlotService } from '../../services/lesson.service.js'
|
||||
|
||||
export const lessonRoutes: FastifyPluginAsync = async (app) => {
|
||||
// --- Instructors ---
|
||||
@@ -92,4 +94,57 @@ export const lessonRoutes: FastifyPluginAsync = async (app) => {
|
||||
if (!lessonType) return reply.status(404).send({ error: { message: 'Lesson type not found', statusCode: 404 } })
|
||||
return reply.send(lessonType)
|
||||
})
|
||||
|
||||
// --- Schedule Slots ---
|
||||
|
||||
app.post('/schedule-slots', { preHandler: [app.authenticate, app.requirePermission('lessons.admin')] }, async (request, reply) => {
|
||||
const parsed = ScheduleSlotCreateSchema.safeParse(request.body)
|
||||
if (!parsed.success) {
|
||||
return reply.status(400).send({ error: { message: 'Validation failed', details: parsed.error.flatten(), statusCode: 400 } })
|
||||
}
|
||||
const result = await ScheduleSlotService.create(app.db, parsed.data)
|
||||
if ('error' in result) {
|
||||
return reply.status(409).send({ error: { message: result.error, statusCode: 409 } })
|
||||
}
|
||||
return reply.status(201).send(result)
|
||||
})
|
||||
|
||||
app.get('/schedule-slots', { preHandler: [app.authenticate, app.requirePermission('lessons.view')] }, async (request, reply) => {
|
||||
const query = request.query as Record<string, string | undefined>
|
||||
const params = PaginationSchema.parse(query)
|
||||
const filters = {
|
||||
instructorId: query.instructorId,
|
||||
dayOfWeek: query.dayOfWeek !== undefined ? Number(query.dayOfWeek) : undefined,
|
||||
}
|
||||
const result = await ScheduleSlotService.list(app.db, params, filters)
|
||||
return reply.send(result)
|
||||
})
|
||||
|
||||
app.get('/schedule-slots/:id', { preHandler: [app.authenticate, app.requirePermission('lessons.view')] }, async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const slot = await ScheduleSlotService.getById(app.db, id)
|
||||
if (!slot) return reply.status(404).send({ error: { message: 'Schedule slot not found', statusCode: 404 } })
|
||||
return reply.send(slot)
|
||||
})
|
||||
|
||||
app.patch('/schedule-slots/:id', { preHandler: [app.authenticate, app.requirePermission('lessons.admin')] }, async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const parsed = ScheduleSlotUpdateSchema.safeParse(request.body)
|
||||
if (!parsed.success) {
|
||||
return reply.status(400).send({ error: { message: 'Validation failed', details: parsed.error.flatten(), statusCode: 400 } })
|
||||
}
|
||||
const result = await ScheduleSlotService.update(app.db, id, parsed.data)
|
||||
if (!result) return reply.status(404).send({ error: { message: 'Schedule slot not found', statusCode: 404 } })
|
||||
if ('error' in result) {
|
||||
return reply.status(409).send({ error: { message: result.error, statusCode: 409 } })
|
||||
}
|
||||
return reply.send(result)
|
||||
})
|
||||
|
||||
app.delete('/schedule-slots/:id', { preHandler: [app.authenticate, app.requirePermission('lessons.admin')] }, async (request, reply) => {
|
||||
const { id } = request.params as { id: string }
|
||||
const slot = await ScheduleSlotService.delete(app.db, id)
|
||||
if (!slot) return reply.status(404).send({ error: { message: 'Schedule slot not found', statusCode: 404 } })
|
||||
return reply.send(slot)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user