"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UsersService = void 0;
const jwt_decode_1 = require("jwt-decode");
const axios_1 = __importDefault(require("axios"));
const BorrowerCompliance_model_1 = require("../models/BorrowerCompliance.model");
const UserMobileAccess_model_1 = require("../models/UserMobileAccess.model");
const UserLog_model_1 = require("../models/UserLog.model");
class UsersService {
    config;
    constructor(config) {
        this.config = config;
    }
    async getUserAccessByRequest(req) {
        const authorization = req.get('Authorization');
        if (!authorization) {
            return null;
        }
        const { sub } = (0, jwt_decode_1.jwtDecode)(authorization);
        const userAccess = await this.getUserAccess(sub);
        if (!userAccess) {
            return null;
        }
        return userAccess;
    }
    async getUserById(userId) {
        const { data } = await axios_1.default.get(`${this.config.baseUrl}/users/by-id/${userId}`);
        return data ?? null;
    }
    async getUserByUsername(username) {
        const { data } = await axios_1.default.get(`${this.config.baseUrl}/users/by-username/${username}`);
        return data ?? null;
    }
    async getUserByToken(req) {
        const authorization = req.get('Authorization');
        if (!authorization) {
            return null;
        }
        const { sub } = (0, jwt_decode_1.jwtDecode)(authorization);
        return this.getUserById(sub);
    }
    async getUserIdByToken(req) {
        const user = await this.getUserByToken(req);
        if (user) {
            return user.id;
        }
        return null;
    }
    async getUserList() {
        const { data } = await axios_1.default.get(`${this.config.baseUrl}/users/users`);
        return data ?? null;
    }
    async getAllRoles() {
        const { data } = await axios_1.default.get(`${this.config.baseUrl}/users/roles`);
        return data ?? [];
    }
    async getUserRoles(userId) {
        const user = await this.getUserById(userId);
        const roles = await this.getAllRoles();
        const userRoleIds = Object.values(user.roles).reduce((acc, roleIds) => [...acc, ...roleIds], []);
        return roles
            .filter((role) => userRoleIds.includes(role.id))
            .map((role) => role.name);
    }
    async updateMobileUser(userId, keyValue) {
        await UserMobileAccess_model_1.UserMobileAccess.findOneAndUpdate({ userId }, keyValue, { upsert: true });
    }
    async getUserAccess(userId) {
        const foundUser = await this.getUserById(userId);
        const borrowersAccess = foundUser.borrowersAccess;
        const complianceBorrowersAccess = (await BorrowerCompliance_model_1.BorrowerCompliance.find({ 'borrower': { $in: borrowersAccess } }))
            .map((b) => b._id.toString());
        return { allBorrowers: foundUser.allBorrowers, borrowersAccess, complianceBorrowersAccess };
    }
    async getUserLogByDate(userId, date) {
        return UserLog_model_1.UserLog.findOne({ userId, timestamp: date }).lean();
    }
}
exports.UsersService = UsersService;
