import { err, ok, type CommandContext } from "@tailor-platform/erp-kit/core"; import { reactivate } 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 ReactivateCustomerAccountInput { id: string; } export async function run( db: Transaction, input: ReactivateCustomerAccountInput, _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 reactivated = reactivate(account); if (!reactivated.ok) return reactivated; await repository.save(reactivated.value); return ok({ customerAccountId: reactivated.value.id }); }