"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessTemplateMutation = void 0;
const tslib_1 = require("tslib");
const type_graphql_1 = require("type-graphql");
const shell_1 = require("@things-factory/shell");
const process_template_1 = require("./process-template");
const process_1 = require("../process/process");
const process_template_type_1 = require("./process-template-type");
const thumbnail_1 = require("../../controllers/thumbnail");
let ProcessTemplateMutation = class ProcessTemplateMutation {
    async createProcessTemplate(processTemplate, context) {
        const { domain, user, tx } = context.state;
        const repository = tx.getRepository(process_template_1.ProcessTemplate);
        const oldProcessTemplate = await repository.findOneBy({
            name: processTemplate.name,
            domain: { id: domain.id }
        });
        if (oldProcessTemplate) {
            throw new Error(context.t('error.process-template name is already taken', { name: processTemplate.name }));
        }
        const newProcessTemplate = Object.assign({}, processTemplate);
        const base64 = await (0, thumbnail_1.thumbnail)({
            model: processTemplate.model,
            context
        });
        if (base64) {
            newProcessTemplate.thumbnail = 'data:image/png;base64,' + base64.toString('base64');
        }
        else {
            newProcessTemplate.thumbnail = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
        }
        return await repository.save(Object.assign(Object.assign({ domain }, newProcessTemplate), { creator: user, updater: user }));
    }
    async updateProcessTemplate(id, patch, context) {
        const { domain, user, tx } = context.state;
        const repository = tx.getRepository(process_template_1.ProcessTemplate);
        const processTemplate = await repository.findOne({
            where: { domain: { id: domain.id }, id }
        });
        if (patch.model) {
            const base64 = await (0, thumbnail_1.thumbnail)({
                model: patch.model,
                context
            });
            if (base64) {
                patch.thumbnail = 'data:image/png;base64,' + base64.toString('base64');
            }
            else {
                patch.thumbnail = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
            }
        }
        return await repository.save(Object.assign(Object.assign(Object.assign({}, processTemplate), patch), { updater: user }));
    }
    async deleteProcessTemplate(id, context) {
        const { domain, tx } = context.state;
        await tx.getRepository(process_template_1.ProcessTemplate).delete({ domain: { id: domain.id }, id });
        return true;
    }
    async registerProcessAsTemplate(id, name, description, visibility, context) {
        const { domain, user, notify, tx } = context.state;
        const boardTemplateRepository = tx.getRepository(process_template_1.ProcessTemplate);
        const process = await tx.getRepository(process_1.Process).findOne({
            where: { domain: { id: domain.id }, id }
        });
        if (!process) {
            throw `Process given id(${id}) is not found`;
        }
        name || (name = process.name);
        const { model, thumbnail } = process;
        const processTemplate = await boardTemplateRepository.findOne({
            where: { domain: { id: domain.id }, name }
        });
        const registered = processTemplate
            ? await boardTemplateRepository.save(Object.assign(Object.assign({}, processTemplate), { name, description: description || processTemplate.description, visibility: visibility || processTemplate.visibility, model,
                thumbnail, updater: user }))
            : await boardTemplateRepository.save({
                domain: { id: domain.id },
                name,
                description: description || process.description,
                model,
                thumbnail,
                visibility: visibility || 'private',
                updater: user,
                creator: user
            });
        notify &&
            notify({
                mode: 'in-app',
                title: `ProcessTemplate '${registered.name}' registered`,
                body: `ProcessTemplate '${registered.name}' registered by ${user.name}\n${registered.description}`,
                image: (0, shell_1.getRedirectSubdomainPath)(context, domain.subdomain, `/process-template-thumbnail/${registered.id}`),
                url: (0, shell_1.getRedirectSubdomainPath)(context, domain.subdomain, `/process-template-viewer/${registered.id}`)
            });
        return registered;
    }
};
exports.ProcessTemplateMutation = ProcessTemplateMutation;
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Directive)('@privilege(category: "process-template", privilege: "mutation", domainOwnerGranted: true)'),
    (0, type_graphql_1.Mutation)(returns => process_template_1.ProcessTemplate, { description: 'To create new ProcessTemplate' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('processTemplate')),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [process_template_type_1.NewProcessTemplate, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessTemplateMutation.prototype, "createProcessTemplate", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Directive)('@privilege(category: "process-template", privilege: "mutation", domainOwnerGranted: true)'),
    (0, type_graphql_1.Mutation)(returns => process_template_1.ProcessTemplate, { description: 'To modify ProcessTemplate 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_template_type_1.ProcessTemplatePatch, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessTemplateMutation.prototype, "updateProcessTemplate", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Directive)('@privilege(category: "process-template", privilege: "mutation", domainOwnerGranted: true)'),
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To delete ProcessTemplate' }),
    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)
], ProcessTemplateMutation.prototype, "deleteProcessTemplate", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Directive)('@privilege(category: "process-template", privilege: "mutation", domainOwnerGranted: true)'),
    (0, type_graphql_1.Mutation)(returns => process_template_1.ProcessTemplate, {
        description: 'To register a process as a process template with the given ID'
    }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('id', { description: 'process Id to be regiestered' })),
    tslib_1.__param(1, (0, type_graphql_1.Arg)('name', { description: 'name of process template to be regiestered' })),
    tslib_1.__param(2, (0, type_graphql_1.Arg)('description', { description: 'description of process template to be regiestered' })),
    tslib_1.__param(3, (0, type_graphql_1.Arg)('visibility', { description: 'visibility of process template to be regiestered' })),
    tslib_1.__param(4, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, String, String, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessTemplateMutation.prototype, "registerProcessAsTemplate", null);
exports.ProcessTemplateMutation = ProcessTemplateMutation = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(process_template_1.ProcessTemplate)
], ProcessTemplateMutation);
//# sourceMappingURL=process-template-mutation.js.map