"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoanTransactionItem = void 0;
const lodash_1 = __importDefault(require("lodash"));
const column_desciptions_helper_1 = require("../helpers/column-desciptions.helper");
const collaterals_enum_1 = require("../enums/collaterals.enum");
const LoanTransaction_model_1 = require("../models/LoanTransaction.model");
const LoanProducts_model_1 = require("../models/LoanProducts.model");
const Borrower_model_1 = require("../models/Borrower.model");
const banks_service_1 = require("../services/banks.service");
const loan_charges_service_1 = require("../services/loan-charges.service");
class LoanTransactionItem {
    borrowersDB;
    amount = 0;
    customerId = '';
    date = new Date();
    description = '';
    ledgerAccountCode = '';
    reference = '';
    bankAccountNumber = '';
    transactionType = null;
    isStatement = false;
    constructor(item, borrowersDB) {
        this.borrowersDB = borrowersDB;
        const itemWithDates = (0, column_desciptions_helper_1.convertIntoDates)(item, collaterals_enum_1.ECollaterals.LOAN_TRANSACTIONS);
        const itemWithoutNulls = lodash_1.default.omitBy(itemWithDates, (value) => value === null);
        this.transactionType = item.amount < 0 ? LoanTransaction_model_1.ELoanTransactionTypes.COLLECTION : LoanTransaction_model_1.ELoanTransactionTypes.DISBURSEMENT;
        Object.assign(this, itemWithoutNulls);
    }
    async initialize() {
        return await this.validateData();
    }
    async validateData() {
        const errors = [];
        const banksService = new banks_service_1.BanksService();
        const foundBank = await banksService.findBankByNumber(this.bankAccountNumber);
        const loanChargesService = new loan_charges_service_1.LoanChargesService(this.borrowersDB);
        const foundProductId = await loanChargesService.getLoanProductIdByCode(this.ledgerAccountCode);
        const foundChargeId = await loanChargesService.getLoanChargeIdByCode(this.ledgerAccountCode);
        if (foundChargeId) {
            this.isStatement = true;
        }
        if (!foundBank) {
            errors.push({ bankAccountNumber: [{ error: `no bank with this code` }] });
        }
        if (!foundProductId && !foundChargeId) {
            errors.push({ ledgerAccountCode: [{ error: `no product with this code` }] });
        }
        if (foundProductId) {
            const product = await LoanProducts_model_1.LoanProduct.findById(foundProductId).lean();
            if (product) {
                const borrower = await Borrower_model_1.BorrowerModel.findById(product.borrowerId).lean();
                if (!borrower.active) {
                    errors.push({ borrower: [{ error: `borrower is not active` }] });
                }
            }
        }
        return errors;
    }
}
exports.LoanTransactionItem = LoanTransactionItem;
