"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DesignPackageQuery = 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 design_package_1 = require("./design-package");
const design_package_type_1 = require("./design-package-type");
let DesignPackageQuery = class DesignPackageQuery {
    async designPackage(id, context) {
        const { domain, user } = context.state;
        const qb = await (0, shell_1.getRepository)(design_package_1.DesignPackage)
            .createQueryBuilder('DesignPackage')
            .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(DesignPackage).findOne({
        //   where: {
        //     domain: { id: In([domain.id, domain.parentId].filter(Boolean)) },
        //     id
        //   }
        // })
    }
    async designPackagesCreatedByMe(params, context) {
        const { user } = context.state;
        const queryBuilder = (0, shell_1.getQueryBuilderFromListParams)({
            repository: (0, shell_1.getRepository)(design_package_1.DesignPackage),
            params,
            alias: 'template',
            searchables: ['name', 'description']
        }).andWhere('template.creator = :user', { user: user.id });
        const [items, total] = await queryBuilder.getManyAndCount();
        return { items, total };
    }
    async designPackages(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)(design_package_1.DesignPackage),
            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(designPackage, context) {
        const { creatorId } = designPackage;
        const { user } = context.state;
        return creatorId == user.id;
    }
    async domain(designPackage) {
        return designPackage.domainId && (await (0, shell_1.getRepository)(shell_1.Domain).findOneBy({ id: designPackage.domainId }));
    }
    async updater(designPackage) {
        return designPackage.updaterId && (await (0, shell_1.getRepository)(auth_base_1.User).findOneBy({ id: designPackage.updaterId }));
    }
    async creator(designPackage) {
        return designPackage.creatorId && (await (0, shell_1.getRepository)(auth_base_1.User).findOneBy({ id: designPackage.creatorId }));
    }
};
exports.DesignPackageQuery = DesignPackageQuery;
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => design_package_1.DesignPackage, { nullable: true, description: 'To fetch a DesignPackage' }),
    (0, type_graphql_1.Directive)('@privilege(category: "design-package", 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)
], DesignPackageQuery.prototype, "designPackage", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => design_package_type_1.DesignPackageList, { description: 'To fetch DesignPackages 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)
], DesignPackageQuery.prototype, "designPackagesCreatedByMe", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => design_package_type_1.DesignPackageList, { description: 'To fetch multiple DesignPackages' }),
    (0, type_graphql_1.Directive)('@privilege(category: "design-package", 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)
], DesignPackageQuery.prototype, "designPackages", 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", [design_package_1.DesignPackage, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], DesignPackageQuery.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", [design_package_1.DesignPackage]),
    tslib_1.__metadata("design:returntype", Promise)
], DesignPackageQuery.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", [design_package_1.DesignPackage]),
    tslib_1.__metadata("design:returntype", Promise)
], DesignPackageQuery.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", [design_package_1.DesignPackage]),
    tslib_1.__metadata("design:returntype", Promise)
], DesignPackageQuery.prototype, "creator", null);
exports.DesignPackageQuery = DesignPackageQuery = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(design_package_1.DesignPackage)
], DesignPackageQuery);
//# sourceMappingURL=design-package-query.js.map