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:
@@ -216,4 +216,272 @@ suite('Lessons', { tags: ['lessons'] }, (t) => {
|
||||
const res = await t.api.get('/v1/lesson-types', { q: 'Ghost Type XYZ', limit: 100 })
|
||||
t.assert.equal(res.data.data.length, 0)
|
||||
})
|
||||
|
||||
// ─── Schedule Slots: CRUD ───
|
||||
|
||||
t.test('creates a schedule slot', { tags: ['schedule-slots', 'create'] }, async () => {
|
||||
const instructor = await t.api.post('/v1/instructors', { displayName: 'Slot Instructor' })
|
||||
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Slot Lesson', durationMinutes: 30 })
|
||||
|
||||
const res = await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 2, // Tuesday
|
||||
startTime: '16:00',
|
||||
room: 'Room A',
|
||||
maxStudents: 1,
|
||||
})
|
||||
t.assert.status(res, 201)
|
||||
t.assert.equal(res.data.dayOfWeek, 2)
|
||||
t.assert.equal(res.data.startTime, '16:00:00')
|
||||
t.assert.equal(res.data.room, 'Room A')
|
||||
t.assert.equal(res.data.maxStudents, 1)
|
||||
t.assert.equal(res.data.isActive, true)
|
||||
})
|
||||
|
||||
t.test('creates a group slot with higher max students', { tags: ['schedule-slots', 'create'] }, async () => {
|
||||
const instructor = await t.api.post('/v1/instructors', { displayName: 'Group Instructor' })
|
||||
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Group Lesson', durationMinutes: 60, lessonFormat: 'group' })
|
||||
|
||||
const res = await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 4, // Thursday
|
||||
startTime: '17:00',
|
||||
room: 'Room B',
|
||||
maxStudents: 6,
|
||||
})
|
||||
t.assert.status(res, 201)
|
||||
t.assert.equal(res.data.maxStudents, 6)
|
||||
})
|
||||
|
||||
t.test('rejects schedule slot without required fields', { tags: ['schedule-slots', 'create', 'validation'] }, async () => {
|
||||
const res = await t.api.post('/v1/schedule-slots', {})
|
||||
t.assert.status(res, 400)
|
||||
})
|
||||
|
||||
t.test('rejects invalid day of week', { tags: ['schedule-slots', 'create', 'validation'] }, async () => {
|
||||
const instructor = await t.api.post('/v1/instructors', { displayName: 'Day Validation Instructor' })
|
||||
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Day Validation Type', durationMinutes: 30 })
|
||||
|
||||
const res = await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 7,
|
||||
startTime: '10:00',
|
||||
})
|
||||
t.assert.status(res, 400)
|
||||
})
|
||||
|
||||
t.test('rejects invalid time format', { tags: ['schedule-slots', 'create', 'validation'] }, async () => {
|
||||
const instructor = await t.api.post('/v1/instructors', { displayName: 'Time Validation Instructor' })
|
||||
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Time Validation Type', durationMinutes: 30 })
|
||||
|
||||
const res = await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 1,
|
||||
startTime: '3pm',
|
||||
})
|
||||
t.assert.status(res, 400)
|
||||
})
|
||||
|
||||
t.test('detects overlapping slot for same instructor, day, and time', { tags: ['schedule-slots', 'create', 'conflict'] }, async () => {
|
||||
const instructor = await t.api.post('/v1/instructors', { displayName: 'Conflict Instructor' })
|
||||
const type1 = await t.api.post('/v1/lesson-types', { name: 'Conflict Type 1', durationMinutes: 30 })
|
||||
const type2 = await t.api.post('/v1/lesson-types', { name: 'Conflict Type 2', durationMinutes: 30 })
|
||||
|
||||
const first = await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: type1.data.id,
|
||||
dayOfWeek: 3,
|
||||
startTime: '14:00',
|
||||
})
|
||||
t.assert.status(first, 201)
|
||||
|
||||
const second = await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: type2.data.id,
|
||||
dayOfWeek: 3,
|
||||
startTime: '14:00',
|
||||
})
|
||||
t.assert.status(second, 409)
|
||||
})
|
||||
|
||||
t.test('allows same time for different instructors', { tags: ['schedule-slots', 'create', 'conflict'] }, async () => {
|
||||
const instructor1 = await t.api.post('/v1/instructors', { displayName: 'No Conflict Instructor A' })
|
||||
const instructor2 = await t.api.post('/v1/instructors', { displayName: 'No Conflict Instructor B' })
|
||||
const lessonType = await t.api.post('/v1/lesson-types', { name: 'No Conflict Type', durationMinutes: 30 })
|
||||
|
||||
const first = await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor1.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 1,
|
||||
startTime: '10:00',
|
||||
})
|
||||
t.assert.status(first, 201)
|
||||
|
||||
const second = await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor2.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 1,
|
||||
startTime: '10:00',
|
||||
})
|
||||
t.assert.status(second, 201)
|
||||
})
|
||||
|
||||
t.test('gets schedule slot by id', { tags: ['schedule-slots', 'read'] }, async () => {
|
||||
const instructor = await t.api.post('/v1/instructors', { displayName: 'Get Slot Instructor' })
|
||||
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Get Slot Type', durationMinutes: 30 })
|
||||
const created = await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 5,
|
||||
startTime: '09:00',
|
||||
})
|
||||
|
||||
const res = await t.api.get(`/v1/schedule-slots/${created.data.id}`)
|
||||
t.assert.status(res, 200)
|
||||
t.assert.equal(res.data.dayOfWeek, 5)
|
||||
})
|
||||
|
||||
t.test('returns 404 for missing schedule slot', { tags: ['schedule-slots', 'read'] }, async () => {
|
||||
const res = await t.api.get('/v1/schedule-slots/a0000000-0000-0000-0000-999999999999')
|
||||
t.assert.status(res, 404)
|
||||
})
|
||||
|
||||
t.test('updates a schedule slot', { tags: ['schedule-slots', 'update'] }, async () => {
|
||||
const instructor = await t.api.post('/v1/instructors', { displayName: 'Update Slot Instructor' })
|
||||
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Update Slot Type', durationMinutes: 30 })
|
||||
const created = await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 0,
|
||||
startTime: '11:00',
|
||||
room: 'Old Room',
|
||||
})
|
||||
|
||||
const res = await t.api.patch(`/v1/schedule-slots/${created.data.id}`, {
|
||||
room: 'New Room',
|
||||
maxStudents: 3,
|
||||
})
|
||||
t.assert.status(res, 200)
|
||||
t.assert.equal(res.data.room, 'New Room')
|
||||
t.assert.equal(res.data.maxStudents, 3)
|
||||
})
|
||||
|
||||
t.test('update detects conflict when changing time', { tags: ['schedule-slots', 'update', 'conflict'] }, async () => {
|
||||
const instructor = await t.api.post('/v1/instructors', { displayName: 'Update Conflict Instructor' })
|
||||
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Update Conflict Type', durationMinutes: 30 })
|
||||
|
||||
await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 2,
|
||||
startTime: '15:00',
|
||||
})
|
||||
const second = await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 2,
|
||||
startTime: '16:00',
|
||||
})
|
||||
|
||||
const res = await t.api.patch(`/v1/schedule-slots/${second.data.id}`, {
|
||||
startTime: '15:00',
|
||||
})
|
||||
t.assert.status(res, 409)
|
||||
})
|
||||
|
||||
t.test('soft-deletes a schedule slot', { tags: ['schedule-slots', 'delete'] }, async () => {
|
||||
const instructor = await t.api.post('/v1/instructors', { displayName: 'Delete Slot Instructor' })
|
||||
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Delete Slot Type', durationMinutes: 30 })
|
||||
const created = await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 6,
|
||||
startTime: '12:00',
|
||||
})
|
||||
|
||||
const res = await t.api.del(`/v1/schedule-slots/${created.data.id}`)
|
||||
t.assert.status(res, 200)
|
||||
t.assert.equal(res.data.isActive, false)
|
||||
})
|
||||
|
||||
// ─── Schedule Slots: List, Filter ───
|
||||
|
||||
t.test('lists schedule slots with pagination', { tags: ['schedule-slots', 'read', 'pagination'] }, async () => {
|
||||
const res = await t.api.get('/v1/schedule-slots', { limit: 100 })
|
||||
t.assert.status(res, 200)
|
||||
t.assert.ok(res.data.pagination)
|
||||
t.assert.ok(res.data.data.length >= 1)
|
||||
})
|
||||
|
||||
t.test('filters schedule slots by instructor', { tags: ['schedule-slots', 'filter'] }, async () => {
|
||||
const instructor = await t.api.post('/v1/instructors', { displayName: 'Filter Slot Instructor' })
|
||||
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Filter Slot Type', durationMinutes: 30 })
|
||||
await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 1,
|
||||
startTime: '08:00',
|
||||
})
|
||||
|
||||
const res = await t.api.get('/v1/schedule-slots', { instructorId: instructor.data.id, limit: 100 })
|
||||
t.assert.status(res, 200)
|
||||
t.assert.ok(res.data.data.every((s: any) => s.instructorId === instructor.data.id))
|
||||
})
|
||||
|
||||
t.test('filters schedule slots by day of week', { tags: ['schedule-slots', 'filter'] }, async () => {
|
||||
const instructor = await t.api.post('/v1/instructors', { displayName: 'Day Filter Instructor' })
|
||||
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Day Filter Type', durationMinutes: 30 })
|
||||
await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 0,
|
||||
startTime: '13:00',
|
||||
})
|
||||
|
||||
const res = await t.api.get('/v1/schedule-slots', { dayOfWeek: '0', limit: 100 })
|
||||
t.assert.status(res, 200)
|
||||
t.assert.ok(res.data.data.every((s: any) => s.dayOfWeek === 0))
|
||||
})
|
||||
|
||||
t.test('deleted slot does not appear in list', { tags: ['schedule-slots', 'delete', 'list'] }, async () => {
|
||||
const instructor = await t.api.post('/v1/instructors', { displayName: 'Ghost Slot Instructor' })
|
||||
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Ghost Slot Type', durationMinutes: 30 })
|
||||
const created = await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 4,
|
||||
startTime: '19:00',
|
||||
room: 'Ghost Room XYZ',
|
||||
})
|
||||
await t.api.del(`/v1/schedule-slots/${created.data.id}`)
|
||||
|
||||
const res = await t.api.get('/v1/schedule-slots', { q: 'Ghost Room XYZ', limit: 100 })
|
||||
t.assert.equal(res.data.data.length, 0)
|
||||
})
|
||||
|
||||
t.test('deactivated slot frees the time for new slot', { tags: ['schedule-slots', 'create', 'conflict'] }, async () => {
|
||||
const instructor = await t.api.post('/v1/instructors', { displayName: 'Reuse Slot Instructor' })
|
||||
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Reuse Slot Type', durationMinutes: 30 })
|
||||
|
||||
const first = await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 5,
|
||||
startTime: '18:00',
|
||||
})
|
||||
t.assert.status(first, 201)
|
||||
await t.api.del(`/v1/schedule-slots/${first.data.id}`)
|
||||
|
||||
const second = await t.api.post('/v1/schedule-slots', {
|
||||
instructorId: instructor.data.id,
|
||||
lessonTypeId: lessonType.data.id,
|
||||
dayOfWeek: 5,
|
||||
startTime: '18:00',
|
||||
})
|
||||
t.assert.status(second, 201)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user