"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessTemplateQuery = void 0;
const tslib_1 = require("tslib");
const typeorm_1 = require("typeorm");
const type_graphql_1 = require("type-graphql");
const shell_1 = require("@things-factory/shell");
const auth_base_1 = require("@things-factory/auth-base");
const process_template_1 = require("./process-template");
const process_template_type_1 = require("./process-template-type");
let ProcessTemplateQuery = class ProcessTemplateQuery {
    async processTemplate(id, context) {
        const { domain, user } = context.state;
        const qb = await (0, shell_1.getRepository)(process_template_1.ProcessTemplate)
            .createQueryBuilder('ProcessTemplate')
            .where('id=:id', { id })
            .andWhere(new typeorm_1.Brackets(qb => {
            qb.where({ visibility: 'public' })
                .orWhere({
                visibility: 'domain',
                domain: { id: (0, typeorm_1.In)([domain.id, domain.parentId]) }
            })
                .orWhere({
                visibility: 'private',
                creator: { id: user.id }
            });
        }));
        return qb.getOne();
        // return await getRepository(ProcessTemplate).findOne({
        //   where: {
        //     domain: { id: In([domain.id, domain.parentId].filter(Boolean)) },
        //     id
        //   }
        // })
    }
    async processTemplatesCreatedByMe(params, context) {
        const { user } = context.state;
        const queryBuilder = (0, shell_1.getQueryBuilderFromListParams)({
            repository: (0, shell_1.getRepository)(process_template_1.ProcessTemplate),
            params,
            alias: 'template',
            searchables: ['name', 'description']
        }).andWhere('template.creator = :user', { user: user.id });
        const [items, total] = await queryBuilder.getManyAndCount();
        return { items, total };
    }
    async processTemplates(params, context) {
        const { domain, user } = context.state;
        /*
          리스트에 포함되는 보드템플릿들
          - visibility가 public 인 경우
          - visibility가 domain이며 컨텍스트의 도메인과 같은 경우
          - visibility가 private 이며, creator가 나인 경우
        */
        const queryBuilder = (0, shell_1.getQueryBuilderFromListParams)({
            params,
            repository: await (0, shell_1.getRepository)(process_template_1.ProcessTemplate),
            searchables: ['name', 'description']
        }).andWhere(new typeorm_1.Brackets(qb => {
            qb.where({ visibility: 'public' })
                .orWhere({
                visibility: 'domain',
                domain: { id: (0, typeorm_1.In)([domain.id, domain.parentId]) }
            })
                .orWhere({
                visibility: 'private',
                creator: { id: user.id }
            });
        }));
        const [items, total] = await queryBuilder.getManyAndCount();
        return { items, total };
    }
    async mine(processTemplate, context) {
        const { creatorId } = processTemplate;
        const { user } = context.state;
        return creatorId == user.id;
    }
    async domain(processTemplate) {
        return processTemplate.domainId && (await (0, shell_1.getRepository)(shell_1.Domain).findOneBy({ id: processTemplate.domainId }));
    }
    async updater(processTemplate) {
        return processTemplate.updaterId && (await (0, shell_1.getRepository)(auth_base_1.User).findOneBy({ id: processTemplate.updaterId }));
    }
    async creator(processTemplate) {
        return processTemplate.creatorId && (await (0, shell_1.getRepository)(auth_base_1.User).findOneBy({ id: processTemplate.creatorId }));
    }
};
exports.ProcessTemplateQuery = ProcessTemplateQuery;
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => process_template_1.ProcessTemplate, { nullable: true, description: 'To fetch a ProcessTemplate' }),
    (0, type_graphql_1.Directive)('@privilege(category: "process-template", privilege: "query", domainOwnerGranted: true)'),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('id')),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessTemplateQuery.prototype, "processTemplate", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => process_template_type_1.ProcessTemplateList, { description: 'To fetch ProcessTemplates created by me' }),
    tslib_1.__param(0, (0, type_graphql_1.Args)()),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [shell_1.ListParam, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessTemplateQuery.prototype, "processTemplatesCreatedByMe", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => process_template_type_1.ProcessTemplateList, { description: 'To fetch multiple ProcessTemplates' }),
    (0, type_graphql_1.Directive)('@privilege(category: "process-template", privilege: "query", domainOwnerGranted: true)'),
    tslib_1.__param(0, (0, type_graphql_1.Args)()),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [shell_1.ListParam, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessTemplateQuery.prototype, "processTemplates", null);
tslib_1.__decorate([
    (0, type_graphql_1.FieldResolver)(type => Boolean),
    tslib_1.__param(0, (0, type_graphql_1.Root)()),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [process_template_1.ProcessTemplate, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessTemplateQuery.prototype, "mine", null);
tslib_1.__decorate([
    (0, type_graphql_1.FieldResolver)(type => shell_1.Domain),
    tslib_1.__param(0, (0, type_graphql_1.Root)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [process_template_1.ProcessTemplate]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessTemplateQuery.prototype, "domain", null);
tslib_1.__decorate([
    (0, type_graphql_1.FieldResolver)(type => auth_base_1.User),
    tslib_1.__param(0, (0, type_graphql_1.Root)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [process_template_1.ProcessTemplate]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessTemplateQuery.prototype, "updater", null);
tslib_1.__decorate([
    (0, type_graphql_1.FieldResolver)(type => auth_base_1.User),
    tslib_1.__param(0, (0, type_graphql_1.Root)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [process_template_1.ProcessTemplate]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessTemplateQuery.prototype, "creator", null);
exports.ProcessTemplateQuery = ProcessTemplateQuery = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(process_template_1.ProcessTemplate)
], ProcessTemplateQuery);
//# sourceMappingURL=process-template-query.js.map