"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BanksService = void 0;
const Banks_model_1 = require("../models/Banks.model");
const QuickbooksAccount_model_1 = require("../models/QuickbooksAccount.model");
const fieldsToUnset = ['order', '__v', 'createdAt', 'updatedAt'];
class BanksService {
    async getBanks() {
        return Banks_model_1.Bank.aggregate([
            {
                $sort: {
                    'order': 1,
                },
            }, {
                $unset: fieldsToUnset,
            }, {
                $addFields: {
                    valid: true,
                },
            },
        ]);
    }
    async createBank(bank) {
        const newBank = new Banks_model_1.Bank(bank);
        await newBank.save();
    }
    async editBank(bank) {
        await Banks_model_1.Bank.findByIdAndUpdate(bank._id, bank);
    }
    async findBankByNumber(bankAccountNumber) {
        return Banks_model_1.Bank.findOne({ bankAccountNumber }).lean();
    }
    async findBankByAccountCode(accountCode) {
        const account = await QuickbooksAccount_model_1.QuickbooksAccount.findOne({ accountCode }).lean();
        if (!account) {
            return null;
        }
        const banks = await Banks_model_1.Bank.aggregate([
            {
                '$match': {
                    '$expr': {
                        '$gt': [
                            {
                                '$size': {
                                    '$filter': {
                                        'input': {
                                            '$objectToArray': '$ledgerAccountCodes',
                                        },
                                        'as': 'entry',
                                        'cond': {
                                            '$eq': [
                                                '$$entry.v', account._id,
                                            ],
                                        },
                                    },
                                },
                            }, 0,
                        ],
                    },
                },
            }, {
                '$sort': {
                    'bankAccountNumber': 1,
                },
            }, {
                '$limit': 1,
            },
        ]);
        if (banks.length > 0) {
            return banks[0];
        }
        return null;
    }
    async updateBanks(banks) {
        const existingBanks = await Banks_model_1.Bank.find().lean();
        const existingBankIds = existingBanks.map((bank) => bank._id.toString());
        const updatedBankIds = banks.map((bank) => bank._id.toString());
        const idsToRemove = existingBankIds.filter((id) => !updatedBankIds.includes(id));
        await Promise.all(banks.map(async (bank) => {
            const { _id, ...rest } = bank;
            if (_id.toString().includes('new_')) {
                await this.createBank({ _id: null, ...rest });
            }
            else {
                await this.editBank(bank);
            }
        }));
        await Banks_model_1.Bank.updateMany({ _id: { $in: idsToRemove } }, { isDeleted: true });
    }
}
exports.BanksService = BanksService;
