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:
Ryan Moon
2026-03-30 09:20:03 -05:00
parent 5dbe837c08
commit f777ce5184
8 changed files with 513 additions and 3 deletions

View File

@@ -1,11 +1,13 @@
import { eq, and, count, type Column } from 'drizzle-orm'
import { eq, and, count, type Column, type SQL } from 'drizzle-orm'
import type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'
import { instructors, lessonTypes } from '../db/schema/lessons.js'
import { instructors, lessonTypes, scheduleSlots } from '../db/schema/lessons.js'
import type {
InstructorCreateInput,
InstructorUpdateInput,
LessonTypeCreateInput,
LessonTypeUpdateInput,
ScheduleSlotCreateInput,
ScheduleSlotUpdateInput,
PaginationInput,
} from '@lunarfront/shared/schemas'
import {
@@ -152,3 +154,128 @@ export const LessonTypeService = {
return lessonType ?? null
},
}
export const ScheduleSlotService = {
async create(db: PostgresJsDatabase<any>, input: ScheduleSlotCreateInput) {
// Check for overlapping slot on same instructor, day, and time
const [conflict] = await db
.select({ id: scheduleSlots.id })
.from(scheduleSlots)
.where(
and(
eq(scheduleSlots.instructorId, input.instructorId),
eq(scheduleSlots.dayOfWeek, input.dayOfWeek),
eq(scheduleSlots.startTime, input.startTime),
eq(scheduleSlots.isActive, true),
),
)
.limit(1)
if (conflict) {
return { error: 'Instructor already has a slot at this day and time' }
}
const [slot] = await db
.insert(scheduleSlots)
.values({
instructorId: input.instructorId,
lessonTypeId: input.lessonTypeId,
dayOfWeek: input.dayOfWeek,
startTime: input.startTime,
room: input.room,
maxStudents: input.maxStudents,
})
.returning()
return slot
},
async getById(db: PostgresJsDatabase<any>, id: string) {
const [slot] = await db
.select()
.from(scheduleSlots)
.where(eq(scheduleSlots.id, id))
.limit(1)
return slot ?? null
},
async list(db: PostgresJsDatabase<any>, params: PaginationInput, filters?: {
instructorId?: string
dayOfWeek?: number
}) {
const conditions: SQL[] = [eq(scheduleSlots.isActive, true)]
if (params.q) {
const search = buildSearchCondition(params.q, [scheduleSlots.room])
if (search) conditions.push(search)
}
if (filters?.instructorId) {
conditions.push(eq(scheduleSlots.instructorId, filters.instructorId))
}
if (filters?.dayOfWeek !== undefined) {
conditions.push(eq(scheduleSlots.dayOfWeek, filters.dayOfWeek))
}
const where = and(...conditions)
const sortableColumns: Record<string, Column> = {
day_of_week: scheduleSlots.dayOfWeek,
start_time: scheduleSlots.startTime,
room: scheduleSlots.room,
created_at: scheduleSlots.createdAt,
}
let query = db.select().from(scheduleSlots).where(where).$dynamic()
query = withSort(query, params.sort, params.order, sortableColumns, scheduleSlots.dayOfWeek)
query = withPagination(query, params.page, params.limit)
const [data, [{ total }]] = await Promise.all([
query,
db.select({ total: count() }).from(scheduleSlots).where(where),
])
return paginatedResponse(data, total, params.page, params.limit)
},
async update(db: PostgresJsDatabase<any>, id: string, input: ScheduleSlotUpdateInput) {
// If changing day/time/instructor, check for conflicts
if (input.instructorId || input.dayOfWeek !== undefined || input.startTime) {
const existing = await ScheduleSlotService.getById(db, id)
if (!existing) return null
const checkInstructorId = input.instructorId ?? existing.instructorId
const checkDay = input.dayOfWeek ?? existing.dayOfWeek
const checkTime = input.startTime ?? existing.startTime
const [conflict] = await db
.select({ id: scheduleSlots.id })
.from(scheduleSlots)
.where(
and(
eq(scheduleSlots.instructorId, checkInstructorId),
eq(scheduleSlots.dayOfWeek, checkDay),
eq(scheduleSlots.startTime, checkTime),
eq(scheduleSlots.isActive, true),
),
)
.limit(1)
if (conflict && conflict.id !== id) {
return { error: 'Instructor already has a slot at this day and time' }
}
}
const [slot] = await db
.update(scheduleSlots)
.set({ ...input, updatedAt: new Date() })
.where(eq(scheduleSlots.id, id))
.returning()
return slot ?? null
},
async delete(db: PostgresJsDatabase<any>, id: string) {
const [slot] = await db
.update(scheduleSlots)
.set({ isActive: false, updatedAt: new Date() })
.where(eq(scheduleSlots.id, id))
.returning()
return slot ?? null
},
}