"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatterMessageRepository = void 0;
const common_1 = require("@nestjs/common");
const chatter_message_entity_1 = require("../entities/chatter-message.entity");
const request_context_service_1 = require("../services/request-context.service");
const typeorm_1 = require("typeorm");
const security_rule_repository_1 = require("./security-rule.repository");
const solid_base_repository_1 = require("./solid-base.repository");
const lodash_1 = require("lodash");
const string_helper_1 = require("../helpers/string.helper");
let ChatterMessageRepository = class ChatterMessageRepository extends solid_base_repository_1.SolidBaseRepository {
    constructor(dataSource, requestContextService, securityRuleRepository) {
        super(chatter_message_entity_1.ChatterMessage, dataSource, requestContextService, securityRuleRepository);
        this.dataSource = dataSource;
        this.requestContextService = requestContextService;
        this.securityRuleRepository = securityRuleRepository;
        this.CO_MODEL_NAME_PATH = 'filters.coModelName.$eq';
    }
    async createSecurityRuleAwareQueryBuilder(alias, queryRunner) {
        const activeUserOrUndefined = this.requestContextService.getActiveUser();
        let qb = await super.createSecurityRuleAwareQueryBuilder(alias, queryRunner);
        if (!activeUserOrUndefined)
            return qb;
        const [coModelName, coModelAlias] = this.getCoModelNameAndAlias();
        if (!coModelName)
            return qb;
        qb = this.leftJoinCoModel(qb, coModelName);
        return this.securityRuleRepository.applySecurityRules(qb, coModelName, activeUserOrUndefined, coModelAlias);
    }
    leftJoinCoModel(qb, coModelName) {
        const entityName = (0, string_helper_1.classify)(coModelName);
        const meta = this.dataSource.getMetadata(entityName);
        const alias = (0, lodash_1.camelCase)(meta.name);
        qb.leftJoin(entityName, alias, `${alias}.id = entity.co_model_entity_id AND entity.co_model_name = :model`, { model: coModelName });
        return qb;
    }
    getCoModelNameAndAlias() {
        const requestFilter = this.requestContextService.getRequestFilter();
        if (!requestFilter)
            return [undefined, undefined];
        const coModelName = (0, lodash_1.get)(requestFilter, this.CO_MODEL_NAME_PATH);
        if (!coModelName)
            return [undefined, undefined];
        const alias = (0, lodash_1.camelCase)(coModelName);
        return [coModelName, alias];
    }
};
exports.ChatterMessageRepository = ChatterMessageRepository;
exports.ChatterMessageRepository = ChatterMessageRepository = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [typeorm_1.DataSource,
        request_context_service_1.RequestContextService,
        security_rule_repository_1.SecurityRuleRepository])
], ChatterMessageRepository);
//# sourceMappingURL=chatter-message.repository.js.map