"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrganizationsService = void 0;
const Organization_model_1 = require("../models/Organization.model");
const Borrower_model_1 = require("../models/Borrower.model");
const main_helper_1 = require("../helpers/main.helper");
const OrganizationEmails_model_1 = require("../models/OrganizationEmails.model");
const initialOrganizations = [
    {
        name: 'GemCap',
        isDefault: false,
        subfolder: 'gemcap',
        emails: [
            { organizationId: null, type: OrganizationEmails_model_1.OrganizationEmailsType.SENDER, address: 'compliance@gemcapsolutions.com', title: 'GemCap' },
            { organizationId: null, type: OrganizationEmails_model_1.OrganizationEmailsType.PORTFOLIO, address: 'portfolio@gemcapsolutions.com', title: 'GemCap' },
            { organizationId: null, type: OrganizationEmails_model_1.OrganizationEmailsType.FINANCIAL, address: 'finance@gemcapsolutions.com', title: 'GemCap Finance Department' },
            { organizationId: null, type: OrganizationEmails_model_1.OrganizationEmailsType.HIDDEN_RECIPIENT, address: 'compliance@gemcapsolutions.com', title: 'GemCap' },
        ],
    },
    {
        name: 'Black Feather',
        isDefault: true,
        subfolder: 'black_feather',
        emails: [
            { organizationId: null, type: OrganizationEmails_model_1.OrganizationEmailsType.SENDER, address: 'compliance@blackfeatherfunding.com', title: 'Black Feather' },
            { organizationId: null, type: OrganizationEmails_model_1.OrganizationEmailsType.PORTFOLIO, address: 'portfolio@blackfeatherfunding.com', title: 'Black Feather' },
            { organizationId: null, type: OrganizationEmails_model_1.OrganizationEmailsType.FINANCIAL, address: 'finance@blackfeatherfunding.com', title: 'Black Feather Finance Department' },
            { organizationId: null, type: OrganizationEmails_model_1.OrganizationEmailsType.HIDDEN_RECIPIENT, address: 'compliance@blackfeatherfunding.com', title: 'Black Feather' },
        ],
    },
];
class OrganizationsService {
    async getOrganizations() {
        const organizations = await Organization_model_1.Organization.find().sort({ name: 1 });
        return organizations.map((i) => (0, main_helper_1.removeFields)(i, main_helper_1.fieldsToUnset));
    }
    async createInitialOrganizations() {
        await Promise.all(initialOrganizations.map(async (organization) => {
            const updatedOrganization = await Organization_model_1.Organization.findOneAndUpdate({ name: organization.name }, {
                $setOnInsert: {
                    name: organization.name,
                    subfolder: organization.subfolder,
                    isDefault: organization.isDefault,
                },
            }, { upsert: true, new: true });
            await Promise.all(organization.emails.map(async (email) => {
                await OrganizationEmails_model_1.OrganizationEmail.findOneAndUpdate({ type: email.type, organizationId: updatedOrganization._id }, {
                    $setOnInsert: {
                        organizationId: updatedOrganization._id,
                        type: email.type,
                        title: email.title,
                        address: email.address,
                    },
                }, { upsert: true, new: true });
            }));
        }));
    }
    async getDefaultOrganization() {
        return Organization_model_1.Organization.findOne({ isDefault: true }).lean().exec();
    }
    async getOrganizationForBorrower(borrowerId) {
        const borrower = await Borrower_model_1.BorrowerModel.findById(borrowerId).lean().exec();
        const defaultOrganization = await this.getDefaultOrganization();
        if (!borrower) {
            return defaultOrganization;
        }
        const organization = await Organization_model_1.Organization.findById(borrower.organizationId).lean().exec();
        if (!organization) {
            return defaultOrganization;
        }
        return organization;
    }
}
exports.OrganizationsService = OrganizationsService;
