import { DEFAULT_PAGE_SIZE, buildPaginatedResult, ok, type PaginationInput, type ReadonlyDB, } from "@tailor-platform/erp-kit/core"; import type { DB } from "../generated/kysely-tailordb"; type WorkRuleAssignmentOrderByField = "effectiveStart" | "targetType"; export interface ListWorkRuleAssignmentsInput extends PaginationInput { workRuleId?: string; } /** * Function: listWorkRuleAssignments * Description: Returns a paginated list of WorkRuleAssignment generations, history included * (closed generations alongside current ones), so the full assignment timeline of a target * is reconstructable. May be narrowed to the assignments referencing one WorkRule. */ export async function run(db: ReadonlyDB, input: ListWorkRuleAssignmentsInput) { const limit = input.limit ?? DEFAULT_PAGE_SIZE; const offset = input.offset ?? 0; const orderBy = input.orderBy ?? "id"; const orderDirection = input.orderDirection ?? "asc"; let query = db.selectFrom("WorkRuleAssignment").selectAll(); if (input.workRuleId !== undefined) { query = query.where("workRuleId", "=", input.workRuleId); } const workRuleAssignments = await query .orderBy(orderBy, orderDirection) .limit(limit + 1) .offset(offset) .execute(); return ok(buildPaginatedResult(workRuleAssignments, limit)); }