import { err, ok, type CommandContext } from "@tailor-platform/erp-kit/core"; import { deactivate } from "../domain/customerAccount"; import type { Transaction } from "../generated/kysely-tailordb"; import { CustomerAccountNotFoundError } from "../lib/errors.generated"; import { createCustomerAccountRepository, type CustomerAccountRepository, } from "../repository/customerAccountRepository"; export interface DeactivateCustomerAccountInput { id: string; } export async function run( db: Transaction, input: DeactivateCustomerAccountInput, _ctx: CommandContext, repositoryFactory: ( db: Transaction, ) => CustomerAccountRepository = createCustomerAccountRepository, ) { const repository = repositoryFactory(db); const account = await repository.findById(input.id, { forUpdate: true }); if (!account) return err(new CustomerAccountNotFoundError(input.id)); const deactivated = deactivate(account); if (!deactivated.ok) return deactivated; await repository.save(deactivated.value); return ok({ customerAccountId: deactivated.value.id }); }