import { ok, type ReadonlyDB } from "@tailor-platform/erp-kit/core"; import type { DB } from "../generated/kysely-tailordb"; export interface GetPeriodByDateInput { companyId: string; date: Date; } /** * Function: getPeriodByDate * * Finds the operating period containing a given date for a specific company. * Searches where startDate <= date <= endDate and periodType = OPERATING. * Only OPERATING periods returned. Returns null if no operating period covers the date. */ export async function run(db: ReadonlyDB, input: GetPeriodByDateInput) { const period = await db .selectFrom("AccountingPeriod") .selectAll() .where("companyId", "=", input.companyId) .where("periodType", "=", "OPERATING") .where("startDate", "<=", input.date) .where("endDate", ">=", input.date) .executeTakeFirst(); return ok({ accountingPeriod: period ?? null }); }