import type { Insertable, Transaction, Updateable } from "../generated/kysely-tailordb"; export function createAccountingPeriodRepository(db: Transaction) { return { async findById(id: string, opts?: { forUpdate?: boolean }) { let query = db.selectFrom("AccountingPeriod").selectAll().where("id", "=", id); if (opts?.forUpdate) query = query.forUpdate(); return (await query.executeTakeFirst()) ?? null; }, async findByFiscalYearId(fiscalYearId: string) { return db .selectFrom("AccountingPeriod") .selectAll() .where("fiscalYearId", "=", fiscalYearId) .execute(); }, async hasFiscalYearPeriods(fiscalYearId: string) { return !!(await db .selectFrom("AccountingPeriod") .selectAll() .where("fiscalYearId", "=", fiscalYearId) .executeTakeFirst()); }, async insert(data: Insertable<"AccountingPeriod">) { return db .insertInto("AccountingPeriod") .values(data) .returningAll() .executeTakeFirstOrThrow(); }, async update(id: string, data: Updateable<"AccountingPeriod">) { return db .updateTable("AccountingPeriod") .set(data) .where("id", "=", id) .returningAll() .executeTakeFirstOrThrow(); }, async delete(id: string) { await db.deleteFrom("AccountingPeriod").where("id", "=", id).executeTakeFirst(); }, }; } export type AccountingPeriodRepository = ReturnType;