"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var NestAuthPlatformAccess_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NestAuthPlatformAccess = void 0;
const typeorm_1 = require("typeorm");
const user_entity_1 = require("./user.entity");
const role_entity_1 = require("../../role/entities/role.entity");
const lodash_1 = require("lodash");
const role_mapper_util_1 = require("../../role/utils/role-mapper.util");
let NestAuthPlatformAccess = NestAuthPlatformAccess_1 = class NestAuthPlatformAccess extends typeorm_1.BaseEntity {
    id;
    userId;
    user;
    roles;
    isActive;
    createdAt;
    updatedAt;
    async getPermissions() {
        const roles = await this.getRoles(true);
        return (0, lodash_1.chain)(roles)
            .map((role) => (0, role_mapper_util_1.getRolePermissionNames)(role))
            .flatten()
            .uniq()
            .value();
    }
    async getRoles(withPermissions = false) {
        const access = await NestAuthPlatformAccess_1.findOne({
            where: { userId: this.userId, isActive: true },
            relations: ['roles', ...(withPermissions ? ['roles.rolePermissions', 'roles.rolePermissions.permission'] : [])],
        });
        return access?.roles?.length ? access.roles : [];
    }
    async assignRoles(roleIds, manager) {
        const ids = Array.isArray(roleIds) ? roleIds : [roleIds];
        const roleRepo = manager
            ? manager.getRepository(role_entity_1.NestAuthRole)
            : role_entity_1.NestAuthRole.getRepository();
        const accessRepo = manager
            ? manager.getRepository(NestAuthPlatformAccess_1)
            : NestAuthPlatformAccess_1.getRepository();
        this.roles = ids.length
            ? await roleRepo.find({ where: { id: (0, typeorm_1.In)(ids) } })
            : [];
        await accessRepo.save(this);
    }
};
exports.NestAuthPlatformAccess = NestAuthPlatformAccess;
__decorate([
    (0, typeorm_1.PrimaryGeneratedColumn)('uuid'),
    __metadata("design:type", String)
], NestAuthPlatformAccess.prototype, "id", void 0);
__decorate([
    (0, typeorm_1.Column)({ nullable: false, unique: true }),
    (0, typeorm_1.Index)(),
    __metadata("design:type", String)
], NestAuthPlatformAccess.prototype, "userId", void 0);
__decorate([
    (0, typeorm_1.OneToOne)(() => user_entity_1.NestAuthUser, user => user.platformAccess, { onDelete: 'CASCADE' }),
    (0, typeorm_1.JoinColumn)({ name: 'userId' }),
    __metadata("design:type", user_entity_1.NestAuthUser)
], NestAuthPlatformAccess.prototype, "user", void 0);
__decorate([
    (0, typeorm_1.ManyToMany)(() => role_entity_1.NestAuthRole, role => role.platformAccesses, { onDelete: 'CASCADE' }),
    (0, typeorm_1.JoinTable)({
        name: 'nest_auth_platform_access_roles',
        joinColumn: { name: 'nestAuthPlatformAccessId', referencedColumnName: 'id' },
        inverseJoinColumn: { name: 'nestAuthRolesId', referencedColumnName: 'id' },
    }),
    __metadata("design:type", Array)
], NestAuthPlatformAccess.prototype, "roles", void 0);
__decorate([
    (0, typeorm_1.Column)({ default: true }),
    __metadata("design:type", Boolean)
], NestAuthPlatformAccess.prototype, "isActive", void 0);
__decorate([
    (0, typeorm_1.CreateDateColumn)(),
    __metadata("design:type", Date)
], NestAuthPlatformAccess.prototype, "createdAt", void 0);
__decorate([
    (0, typeorm_1.UpdateDateColumn)(),
    __metadata("design:type", Date)
], NestAuthPlatformAccess.prototype, "updatedAt", void 0);
exports.NestAuthPlatformAccess = NestAuthPlatformAccess = NestAuthPlatformAccess_1 = __decorate([
    (0, typeorm_1.Entity)('nest_auth_platform_accesses')
], NestAuthPlatformAccess);
//# sourceMappingURL=platform-access.entity.js.map