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

@@ -1,6 +1,6 @@
import { eq, and, count, type Column, type SQL } from 'drizzle-orm'
import { eq, and, ne, count, type Column, type SQL } from 'drizzle-orm'
import type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'
import { instructors, lessonTypes, scheduleSlots } from '../db/schema/lessons.js'
import { instructors, lessonTypes, scheduleSlots, enrollments } from '../db/schema/lessons.js'
import type {
InstructorCreateInput,
InstructorUpdateInput,
@@ -8,6 +8,8 @@ import type {
LessonTypeUpdateInput,
ScheduleSlotCreateInput,
ScheduleSlotUpdateInput,
EnrollmentCreateInput,
EnrollmentUpdateInput,
PaginationInput,
} from '@lunarfront/shared/schemas'
import {
@@ -279,3 +281,127 @@ export const ScheduleSlotService = {
return slot ?? null
},
}
export const EnrollmentService = {
async create(db: PostgresJsDatabase<any>, input: EnrollmentCreateInput) {
// Check slot capacity for group lessons
const slot = await ScheduleSlotService.getById(db, input.scheduleSlotId)
if (!slot) return { error: 'Schedule slot not found' }
const [{ activeCount }] = await db
.select({ activeCount: count() })
.from(enrollments)
.where(
and(
eq(enrollments.scheduleSlotId, input.scheduleSlotId),
eq(enrollments.status, 'active'),
),
)
if (activeCount >= slot.maxStudents) {
return { error: 'Schedule slot is at capacity' }
}
// Check member not already enrolled in a slot at same day/time
const memberSlots = await db
.select({
dayOfWeek: scheduleSlots.dayOfWeek,
startTime: scheduleSlots.startTime,
})
.from(enrollments)
.innerJoin(scheduleSlots, eq(enrollments.scheduleSlotId, scheduleSlots.id))
.where(
and(
eq(enrollments.memberId, input.memberId),
eq(enrollments.status, 'active'),
),
)
const hasConflict = memberSlots.some(
(s) => s.dayOfWeek === slot.dayOfWeek && s.startTime === slot.startTime,
)
if (hasConflict) {
return { error: 'Member already has a lesson at this day and time' }
}
const [enrollment] = await db
.insert(enrollments)
.values({
memberId: input.memberId,
accountId: input.accountId,
scheduleSlotId: input.scheduleSlotId,
instructorId: input.instructorId,
status: 'active',
startDate: input.startDate,
endDate: input.endDate,
monthlyRate: input.monthlyRate?.toString(),
notes: input.notes,
})
.returning()
return enrollment
},
async getById(db: PostgresJsDatabase<any>, id: string) {
const [enrollment] = await db
.select()
.from(enrollments)
.where(eq(enrollments.id, id))
.limit(1)
return enrollment ?? null
},
async list(db: PostgresJsDatabase<any>, params: PaginationInput, filters?: {
memberId?: string
accountId?: string
instructorId?: string
status?: string[]
}) {
const conditions: SQL[] = []
if (filters?.memberId) conditions.push(eq(enrollments.memberId, filters.memberId))
if (filters?.accountId) conditions.push(eq(enrollments.accountId, filters.accountId))
if (filters?.instructorId) conditions.push(eq(enrollments.instructorId, filters.instructorId))
if (filters?.status?.length) {
const { inArray } = await import('drizzle-orm')
conditions.push(inArray(enrollments.status, filters.status as any))
}
const where = conditions.length > 0 ? and(...conditions) : undefined
const sortableColumns: Record<string, Column> = {
start_date: enrollments.startDate,
status: enrollments.status,
created_at: enrollments.createdAt,
}
let query = db.select().from(enrollments).where(where).$dynamic()
query = withSort(query, params.sort, params.order, sortableColumns, enrollments.createdAt)
query = withPagination(query, params.page, params.limit)
const [data, [{ total }]] = await Promise.all([
query,
db.select({ total: count() }).from(enrollments).where(where),
])
return paginatedResponse(data, total, params.page, params.limit)
},
async update(db: PostgresJsDatabase<any>, id: string, input: EnrollmentUpdateInput) {
const values: Record<string, unknown> = { ...input, updatedAt: new Date() }
if (input.monthlyRate !== undefined) values.monthlyRate = input.monthlyRate.toString()
const [enrollment] = await db
.update(enrollments)
.set(values)
.where(eq(enrollments.id, id))
.returning()
return enrollment ?? null
},
async updateStatus(db: PostgresJsDatabase<any>, id: string, status: string) {
const [enrollment] = await db
.update(enrollments)
.set({ status: status as any, updatedAt: new Date() })
.where(eq(enrollments.id, id))
.returning()
return enrollment ?? null
},
}