import { ok, type ReadonlyDB, type PaginationInput, buildPaginatedResult, DEFAULT_PAGE_SIZE, } from "@tailor-platform/erp-kit/core"; import type { DB } from "../generated/kysely-tailordb"; type ShiftPatternOrderByField = "code" | "name" | "kind" | "createdAt"; export type ListShiftPatternsInput = PaginationInput; /** * Function: listShiftPatterns * Returns a paginated, unfiltered list of all reusable ShiftPatterns, for * pattern-picker UIs and administrative pattern management screens. */ export async function run(db: ReadonlyDB, input: ListShiftPatternsInput) { const limit = input.limit ?? DEFAULT_PAGE_SIZE; const offset = input.offset ?? 0; const orderBy = input.orderBy ?? "id"; const orderDirection = input.orderDirection ?? "asc"; const shiftPatterns = await db .selectFrom("ShiftPattern") .selectAll() .orderBy(orderBy, orderDirection) // Total order: a non-unique sort column alone lets same-value rows shift between pages. .orderBy("id", "asc") .limit(limit + 1) .offset(offset) .execute(); return ok(buildPaginatedResult(shiftPatterns, limit)); }