"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UpdateTransactionUseCase = void 0;
const not_found_error_1 = require("../../../domain/errors/not-found-error");
const validation_error_1 = require("../../../domain/errors/validation-error");
const transaction_updated_event_1 = require("../../../domain/events/transaction-updated.event");
class UpdateTransactionUseCase {
    transactionRepository;
    eventPublisher;
    constructor(transactionRepository, eventPublisher) {
        this.transactionRepository = transactionRepository;
        this.eventPublisher = eventPublisher;
    }
    async execute(id, input) {
        const transaction = await this.transactionRepository.findById(id);
        if (!transaction) {
            throw new not_found_error_1.NotFoundError('Transaction', id);
        }
        if (input.status === undefined && input.block === undefined) {
            throw new validation_error_1.ValidationError('At least one field must be provided for update', {
                status: ['Missing'],
                block: ['Missing'],
            });
        }
        const updatedStatus = input.status ?? transaction.status;
        const updatedBlock = input.block !== undefined ? input.block : transaction.block;
        const updated = transaction.updateStatus(updatedStatus, updatedBlock ?? undefined);
        const saved = await this.transactionRepository.update(updated);
        this.eventPublisher.publish(new transaction_updated_event_1.TransactionUpdatedEvent(saved));
        return saved;
    }
}
exports.UpdateTransactionUseCase = UpdateTransactionUseCase;
//# sourceMappingURL=update-transaction.use-case.js.map