import { buildPaginatedResult, DEFAULT_PAGE_SIZE, ok, type PaginationInput, type ReadonlyDB, } from "@tailor-platform/erp-kit/core"; import type { DB } from "../generated/kysely-tailordb"; export interface ListPendingLeaveApprovalsInput extends PaginationInput {} /** * Function: listPendingLeaveApprovals * Description: Lists the LeaveRequest rows awaiting a decision, for an * approver's inbox. * * SIMPLIFIED IMPLEMENTATION (known gap — see docs/query/ListPendingLeaveApprovals.md): * the full intent per ADR-003 is to return PENDING and CANCEL_PENDING requests, * joined to their mirroring approval-module request, scoped to only the * requests the caller is an eligible approver for (NOT_ASSIGNEE otherwise, and * self-approval never surfaced). The "eligible approver" concept lives in the * approval module and has no injected cross-module query wired up yet, so this * query simply filters status = PENDING with no per-caller scoping, no * approval-module join, and no NOT_ASSIGNEE check. Every PENDING request is * returned to every caller until that integration lands. */ export async function run(db: ReadonlyDB, input: ListPendingLeaveApprovalsInput) { const limit = input.limit ?? DEFAULT_PAGE_SIZE; const offset = input.offset ?? 0; const orderBy = input.orderBy ?? "id"; const orderDirection = input.orderDirection ?? "asc"; const leaveRequests = await db .selectFrom("LeaveRequest") .selectAll() .where("status", "=", "PENDING") .orderBy(orderBy, orderDirection) .limit(limit + 1) .offset(offset) .execute(); return ok(buildPaginatedResult(leaveRequests, limit)); }