Add lessons Phase 3: enrollments with capacity and time conflict checks

Links members to schedule slots via enrollments. Enforces max_students
capacity on slots and prevents members from double-booking the same
day/time. Supports status transitions and filtering. 11 new tests
(51 total lessons tests).
This commit is contained in:
Ryan Moon
2026-03-30 09:23:43 -05:00
parent f777ce5184
commit 93405af3b2
8 changed files with 489 additions and 3 deletions

View File

@@ -484,4 +484,206 @@ suite('Lessons', { tags: ['lessons'] }, (t) => {
})
t.assert.status(second, 201)
})
// ─── Enrollments: CRUD ───
t.test('creates an enrollment', { tags: ['enrollments', 'create'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Enrollment Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Emma', lastName: 'Chen' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Enrollment Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Enrollment Type', durationMinutes: 30 })
const slot = 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.post('/v1/enrollments', {
memberId: member.data.id,
accountId: acct.data.id,
scheduleSlotId: slot.data.id,
instructorId: instructor.data.id,
startDate: '2026-01-15',
monthlyRate: 120,
notes: 'Beginner piano student',
})
t.assert.status(res, 201)
t.assert.equal(res.data.status, 'active')
t.assert.equal(res.data.memberId, member.data.id)
t.assert.equal(res.data.monthlyRate, '120.00')
t.assert.equal(res.data.startDate, '2026-01-15')
t.assert.equal(res.data.makeupCredits, 0)
})
t.test('rejects enrollment without required fields', { tags: ['enrollments', 'create', 'validation'] }, async () => {
const res = await t.api.post('/v1/enrollments', {})
t.assert.status(res, 400)
})
t.test('enforces slot capacity', { tags: ['enrollments', 'create', 'capacity'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Capacity Account', billingMode: 'consolidated' })
const m1 = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Student', lastName: 'One' })
const m2 = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Student', lastName: 'Two' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Capacity Instructor' })
const lessonType = await t.api.post('/v1/lesson-types', { name: 'Capacity Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id,
lessonTypeId: lessonType.data.id,
dayOfWeek: 3,
startTime: '14:00',
maxStudents: 1,
})
const first = await t.api.post('/v1/enrollments', {
memberId: m1.data.id,
accountId: acct.data.id,
scheduleSlotId: slot.data.id,
instructorId: instructor.data.id,
startDate: '2026-01-15',
})
t.assert.status(first, 201)
const second = await t.api.post('/v1/enrollments', {
memberId: m2.data.id,
accountId: acct.data.id,
scheduleSlotId: slot.data.id,
instructorId: instructor.data.id,
startDate: '2026-01-15',
})
t.assert.status(second, 409)
})
t.test('prevents member from enrolling in conflicting time', { tags: ['enrollments', 'create', 'conflict'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Time Conflict Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Busy', lastName: 'Student' })
const i1 = await t.api.post('/v1/instructors', { displayName: 'Conflict Instructor A' })
const i2 = await t.api.post('/v1/instructors', { displayName: 'Conflict Instructor B' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Conflict LT', durationMinutes: 30 })
const slot1 = await t.api.post('/v1/schedule-slots', {
instructorId: i1.data.id, lessonTypeId: lt.data.id, dayOfWeek: 1, startTime: '10:00',
})
const slot2 = await t.api.post('/v1/schedule-slots', {
instructorId: i2.data.id, lessonTypeId: lt.data.id, dayOfWeek: 1, startTime: '10:00',
})
const first = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot1.data.id, instructorId: i1.data.id, startDate: '2026-01-15',
})
t.assert.status(first, 201)
const second = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot2.data.id, instructorId: i2.data.id, startDate: '2026-01-15',
})
t.assert.status(second, 409)
})
t.test('gets enrollment by id', { tags: ['enrollments', 'read'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Get Enrollment Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Get', lastName: 'Student' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Get Enrollment Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Get Enrollment Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 4, startTime: '15:00',
})
const created = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-02-01',
})
const res = await t.api.get(`/v1/enrollments/${created.data.id}`)
t.assert.status(res, 200)
t.assert.equal(res.data.id, created.data.id)
})
t.test('returns 404 for missing enrollment', { tags: ['enrollments', 'read'] }, async () => {
const res = await t.api.get('/v1/enrollments/a0000000-0000-0000-0000-999999999999')
t.assert.status(res, 404)
})
t.test('updates an enrollment', { tags: ['enrollments', 'update'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Update Enrollment Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Update', lastName: 'Student' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Update Enrollment Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Update Enrollment Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 5, startTime: '17:00',
})
const created = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-02-01',
})
const res = await t.api.patch(`/v1/enrollments/${created.data.id}`, {
monthlyRate: 150,
notes: 'Updated rate',
endDate: '2026-06-30',
})
t.assert.status(res, 200)
t.assert.equal(res.data.monthlyRate, '150.00')
t.assert.equal(res.data.notes, 'Updated rate')
t.assert.equal(res.data.endDate, '2026-06-30')
})
// ─── Enrollments: Status Transitions ───
t.test('status lifecycle: active → paused → active → cancelled', { tags: ['enrollments', 'status'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Status Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Status', lastName: 'Student' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Status Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Status Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 6, startTime: '09:00',
})
const created = await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-01-01',
})
t.assert.equal(created.data.status, 'active')
const paused = await t.api.post(`/v1/enrollments/${created.data.id}/status`, { status: 'paused' })
t.assert.equal(paused.data.status, 'paused')
const resumed = await t.api.post(`/v1/enrollments/${created.data.id}/status`, { status: 'active' })
t.assert.equal(resumed.data.status, 'active')
const cancelled = await t.api.post(`/v1/enrollments/${created.data.id}/status`, { status: 'cancelled' })
t.assert.equal(cancelled.data.status, 'cancelled')
})
// ─── Enrollments: List, Filter ───
t.test('lists enrollments with pagination', { tags: ['enrollments', 'read', 'pagination'] }, async () => {
const res = await t.api.get('/v1/enrollments', { limit: 100 })
t.assert.status(res, 200)
t.assert.ok(res.data.pagination)
t.assert.ok(res.data.data.length >= 1)
})
t.test('filters enrollments by instructor', { tags: ['enrollments', 'filter'] }, async () => {
const acct = await t.api.post('/v1/accounts', { name: 'Filter Enrollment Account', billingMode: 'consolidated' })
const member = await t.api.post(`/v1/accounts/${acct.data.id}/members`, { firstName: 'Filter', lastName: 'Student' })
const instructor = await t.api.post('/v1/instructors', { displayName: 'Filter Enrollment Instructor' })
const lt = await t.api.post('/v1/lesson-types', { name: 'Filter Enrollment Type', durationMinutes: 30 })
const slot = await t.api.post('/v1/schedule-slots', {
instructorId: instructor.data.id, lessonTypeId: lt.data.id, dayOfWeek: 0, startTime: '11:00',
})
await t.api.post('/v1/enrollments', {
memberId: member.data.id, accountId: acct.data.id,
scheduleSlotId: slot.data.id, instructorId: instructor.data.id, startDate: '2026-03-01',
})
const res = await t.api.get('/v1/enrollments', { instructorId: instructor.data.id, limit: 100 })
t.assert.status(res, 200)
t.assert.ok(res.data.data.every((e: any) => e.instructorId === instructor.data.id))
})
t.test('filters enrollments by status', { tags: ['enrollments', 'filter'] }, async () => {
const res = await t.api.get('/v1/enrollments', { status: 'active', limit: 100 })
t.assert.status(res, 200)
t.assert.ok(res.data.data.every((e: any) => e.status === 'active'))
})
})