import { err, ok, type CommandContext } from "@tailor-platform/erp-kit/core"; import * as domain from "../domain/journalEntry"; import type { Transaction } from "../generated/kysely-tailordb"; import { JournalEntryNotFoundError } from "../lib/errors.generated"; import { createJournalEntryRepository, type JournalEntryRepository, } from "../repository/journalEntryRepository"; export interface CancelJournalEntryInput { journalEntryId: string; } export async function run( db: Transaction, input: CancelJournalEntryInput, _ctx: CommandContext, journalEntryRepositoryFactory: ( db: Transaction, ) => JournalEntryRepository = createJournalEntryRepository, ) { const journalEntryRepository = journalEntryRepositoryFactory(db); const existing = await journalEntryRepository.findById(input.journalEntryId, { forUpdate: true }); if (!existing) return err(new JournalEntryNotFoundError(input.journalEntryId)); const changed = domain.cancel(existing); if (!changed.ok) return changed; const journalEntry = await journalEntryRepository.update(existing.id, changed.value); return ok({ journalEntry }); }