import { ok, type ReadonlyDB, type PaginationInput, buildPaginatedResult, DEFAULT_PAGE_SIZE, } from "@tailor-platform/erp-kit/core"; import type { DB } from "../generated/kysely-tailordb"; export interface ListUpcomingAppointmentsInput extends PaginationInput { workerId?: string; } export async function run(db: ReadonlyDB, input: ListUpcomingAppointmentsInput) { const limit = input.limit ?? DEFAULT_PAGE_SIZE; const offset = input.offset ?? 0; const now = new Date(); let query = db.selectFrom("AppointmentHistory").selectAll().where("effectiveDate", ">", now); if (input.workerId) { query = query.where("workerId", "=", input.workerId); } // Ordered by effectiveDate ascending — a business rule, not caller-selectable. const appointments = await query .orderBy("effectiveDate", "asc") .limit(limit + 1) .offset(offset) .execute(); return ok(buildPaginatedResult(appointments, limit)); }