"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessGroupMutation = void 0;
const tslib_1 = require("tslib");
const type_graphql_1 = require("type-graphql");
const process_1 = require("../process/process");
const process_group_1 = require("./process-group");
const process_group_type_1 = require("./process-group-type");
let ProcessGroupMutation = class ProcessGroupMutation {
    async createProcessGroup(group, context) {
        const { domain, user, tx } = context.state;
        return await tx.getRepository(process_group_1.ProcessGroup).save(Object.assign(Object.assign({ domain }, group), { creator: user, updater: user }));
    }
    async updateProcessGroup(id, patch, context) {
        const { domain, user, tx } = context.state;
        const repository = tx.getRepository(process_group_1.ProcessGroup);
        const group = await repository.findOneBy({ domain: { id: domain.id }, id });
        return await repository.save(Object.assign(Object.assign(Object.assign({}, group), patch), { updater: user }));
    }
    async joinProcessGroup(id, processIds, context) {
        const { domain, tx } = context.state;
        const repository = tx.getRepository(process_group_1.ProcessGroup);
        const group = await repository.findOneBy({ domain: { id: domain.id }, id });
        const processRepository = tx.getRepository(process_1.Process);
        await processIds.forEach(async (processId) => {
            let process = await processRepository.findOneBy({ domain: { id: domain.id }, id: processId });
            process.group = group;
            await processRepository.save(process);
        });
        return await repository.findOne({
            where: { domain: { id: domain.id }, id },
            relations: ['processes']
        });
    }
    async deleteProcessGroup(id, context) {
        const { domain, tx } = context.state;
        const repository = tx.getRepository(process_group_1.ProcessGroup);
        /* TODO - 그룹에 소속된 보드가 있는 경우에는 그룹을 지워서는 안된다. */
        const group = await repository.findOneBy({
            domain: { id: domain.id },
            id
        });
        await repository.delete(id);
        return true;
    }
};
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => process_group_1.ProcessGroup, { nullable: true, description: 'To create new ProcessGroup' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('group')),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [process_group_type_1.NewProcessGroup, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessGroupMutation.prototype, "createProcessGroup", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => process_group_1.ProcessGroup, { description: 'To modify ProcessGroup information' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('id')),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('patch')),
    tslib_1.__param(2, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, process_group_type_1.ProcessGroupPatch, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessGroupMutation.prototype, "updateProcessGroup", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => process_group_1.ProcessGroup, { description: 'To make the process to join the group' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('id')),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('processIds', type => [String])),
    tslib_1.__param(2, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, Array, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessGroupMutation.prototype, "joinProcessGroup", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To delete ProcessGroup' }),
    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)
], ProcessGroupMutation.prototype, "deleteProcessGroup", null);
ProcessGroupMutation = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(process_group_1.ProcessGroup)
], ProcessGroupMutation);
exports.ProcessGroupMutation = ProcessGroupMutation;
//# sourceMappingURL=process-group-mutation.js.map