import { buildPaginatedResult, DEFAULT_PAGE_SIZE, err, ok, type PaginationInput, type ReadonlyDB, } from "@tailor-platform/erp-kit/core"; import type { DB } from "../generated/kysely-tailordb"; import { InvalidQueryParamsError } from "../lib/errors.generated"; export interface ListReportedBlocksByWorkDateInput extends PaginationInput { assignmentId: string; workDate: Date; } /** * Function: listReportedBlocksByWorkDate * Lists all ReportedTimeBlocks — including superseded history — for an * Assignment on a single workday, paginated, to review the full correction * chain for that day. */ export async function run(db: ReadonlyDB, input: ListReportedBlocksByWorkDateInput) { if (!input.assignmentId) { return err(new InvalidQueryParamsError("assignmentId")); } if (!input.workDate || Number.isNaN(input.workDate.getTime())) { return err(new InvalidQueryParamsError(`workDate=${String(input.workDate)}`)); } const limit = input.limit ?? DEFAULT_PAGE_SIZE; const offset = input.offset ?? 0; const orderBy = input.orderBy ?? "id"; const orderDirection = input.orderDirection ?? "asc"; const items = await db .selectFrom("ReportedTimeBlock") .selectAll() .where("assignmentId", "=", input.assignmentId) .where("workDate", "=", input.workDate) .orderBy(orderBy, orderDirection) .limit(limit + 1) .offset(offset) .execute(); return ok(buildPaginatedResult(items, limit)); }