"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessQuery = void 0;
const tslib_1 = require("tslib");
const type_graphql_1 = require("type-graphql");
const typeorm_1 = require("typeorm");
const auth_base_1 = require("@things-factory/auth-base");
const shell_1 = require("@things-factory/shell");
const process_group_1 = require("../process-group/process-group");
const process_1 = require("./process");
const process_history_1 = require("./process-history");
const process_type_1 = require("./process-type");
let ProcessQuery = class ProcessQuery {
    async process(id, context) {
        const { domain } = context.state;
        return await (0, shell_1.getRepository)(process_1.Process).findOne({
            where: { domain: { id: (0, typeorm_1.In)([domain.id, domain.parentId].filter(Boolean)) }, id }
        });
    }
    async processByName(name, context) {
        const { domain } = context.state;
        return await (0, shell_1.getRepository)(process_1.Process).findOne({
            where: { domain: { id: (0, typeorm_1.In)([domain.id, domain.parentId].filter(Boolean)) }, name }
        });
    }
    async processes(params, context) {
        const { domain } = context.state;
        const queryBuilder = (0, shell_1.getQueryBuilderFromListParams)({
            repository: (0, shell_1.getRepository)(process_1.Process),
            params,
            domain,
            searchables: ['name', 'description']
        });
        const [items, total] = await queryBuilder.getManyAndCount();
        return { items, total };
    }
    async processesCreatedByMe(params, context) {
        const { domain, user } = context.state;
        const queryBuilder = (0, shell_1.getQueryBuilderFromListParams)({
            repository: (0, shell_1.getRepository)(process_1.Process),
            params,
            domain,
            alias: 'process',
            searchables: ['name', 'description']
        }).andWhere('process.creator = :user', { user: user.id });
        const [items, total] = await queryBuilder.getManyAndCount();
        return { items, total };
    }
    async processVersions(id, context) {
        const { domain } = context.state;
        return await (0, shell_1.getRepository)(process_history_1.ProcessHistory).find({
            where: { domain: { id: (0, typeorm_1.In)([domain.id, domain.parentId].filter(Boolean)) }, originalId: id },
            relations: ['updater'],
            order: { version: 'DESC' },
            take: 10
        });
    }
    async processPublished(id, context) {
        const { domain } = context.state;
        return await (0, shell_1.getRepository)(process_history_1.ProcessHistory).findOne({
            where: { domain: { id: (0, typeorm_1.In)([domain.id, domain.parentId].filter(Boolean)) }, originalId: id },
            order: { version: 'DESC' }
        });
    }
    async group(process) {
        return (process.groupId &&
            (await (0, shell_1.getRepository)(process_group_1.ProcessGroup).findOneBy({
                id: process.groupId
            })));
    }
    async domain(process) {
        return (process.domainId &&
            (await (0, shell_1.getRepository)(shell_1.Domain).findOneBy({
                id: process.domainId
            })));
    }
    async updater(process) {
        return await (0, shell_1.getRepository)(auth_base_1.User).findOneBy({
            id: process.updaterId
        });
    }
    async creator(process) {
        return await (0, shell_1.getRepository)(auth_base_1.User).findOneBy({
            id: process.creatorId
        });
    }
};
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => process_1.Process, { description: 'To fetch a Process' }),
    (0, type_graphql_1.Directive)('@privilege(category: "process", 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)
], ProcessQuery.prototype, "process", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => process_1.Process, { nullable: true, description: 'To fetch a Process Model by name' }),
    (0, type_graphql_1.Directive)('@privilege(category: "process", privilege: "query", domainOwnerGranted: true)'),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('name')),
    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)
], ProcessQuery.prototype, "processByName", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => process_type_1.ProcessList, { description: 'To fetch multiple Processes' }),
    (0, type_graphql_1.Directive)('@privilege(category: "process", 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)
], ProcessQuery.prototype, "processes", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => process_type_1.ProcessList, { description: 'To fetch Processs 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)
], ProcessQuery.prototype, "processesCreatedByMe", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => [process_history_1.ProcessHistory], { description: 'To fetch a Process Versions' }),
    (0, type_graphql_1.Directive)('@privilege(category: "process", 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)
], ProcessQuery.prototype, "processVersions", null);
tslib_1.__decorate([
    (0, type_graphql_1.Query)(returns => process_history_1.ProcessHistory, { description: 'To fetch the latest Process published' }),
    (0, type_graphql_1.Directive)('@privilege(category: "process", 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)
], ProcessQuery.prototype, "processPublished", null);
tslib_1.__decorate([
    (0, type_graphql_1.FieldResolver)(type => process_group_1.ProcessGroup),
    tslib_1.__param(0, (0, type_graphql_1.Root)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [process_1.Process]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessQuery.prototype, "group", 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_1.Process]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessQuery.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_1.Process]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessQuery.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_1.Process]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessQuery.prototype, "creator", null);
ProcessQuery = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(process_1.Process)
], ProcessQuery);
exports.ProcessQuery = ProcessQuery;
//# sourceMappingURL=process-query.js.map