"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProcessMutation = void 0;
const tslib_1 = require("tslib");
const type_graphql_1 = require("type-graphql");
const shell_1 = require("@things-factory/shell");
const thumbnail_1 = require("../../controllers/thumbnail");
const process_group_1 = require("../process-group/process-group");
const process_1 = require("./process");
const process_type_1 = require("./process-type");
let ProcessMutation = class ProcessMutation {
    async createProcess(process, context) {
        const { domain, user, notify, tx } = context.state;
        const repository = tx.getRepository(process_1.Process);
        const groupRepository = tx.getRepository(process_group_1.ProcessGroup);
        const oldProcess = await repository.findOneBy({
            name: process.name,
            domain: { id: domain.id },
            creator: { id: user.id }
        });
        if (oldProcess) {
            throw new Error(context.t('error.process name is already taken', { name: process.name }));
        }
        const newProcess = Object.assign({}, process);
        if (process.groupId) {
            newProcess.group = await groupRepository.findOneBy({
                id: process.groupId
            });
        }
        const base64 = await (0, thumbnail_1.thumbnail)({
            model: process.model,
            context
        });
        if (base64) {
            newProcess.thumbnail = 'data:image/png;base64,' + base64.toString('base64');
        }
        else {
            newProcess.thumbnail = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
        }
        const created = await repository.save(Object.assign(Object.assign({ domain }, newProcess), { creator: user, updater: user }));
        notify &&
            notify({
                mode: 'in-app',
                title: `Process '${created.name}' created`,
                body: `Process '${created.name}' created by ${user.name}\n${created.description}`,
                url: (0, shell_1.getRedirectSubdomainPath)(context, domain.subdomain, `/process-viewer/${created.id}`)
            });
        return created;
    }
    async updateProcess(id, patch, context) {
        const { domain, user, notify, tx } = context.state;
        const repository = tx.getRepository(process_1.Process);
        const process = await repository.findOneBy({ 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==';
            }
        }
        if (patch.groupId !== undefined) {
            const groupRepository = tx.getRepository(process_group_1.ProcessGroup);
            process.group =
                (await groupRepository.findOneBy({
                    domain: { id: domain.id },
                    id: patch.groupId
                })) || null;
            delete patch.groupId;
        }
        const updated = await repository.save(Object.assign(Object.assign(Object.assign({}, process), patch), { updater: user }));
        notify &&
            notify({
                mode: 'in-app',
                title: `Process '${updated.name}' updated`,
                body: `Process '${updated.name}' updated by ${user.name}\n${updated.description}`,
                image: (0, shell_1.getRedirectSubdomainPath)(context, domain.subdomain, `/process-thumbnail/${updated.id}`),
                url: (0, shell_1.getRedirectSubdomainPath)(context, domain.subdomain, `/process-viewer/${updated.id}`)
            });
        return updated;
    }
    async deleteProcess(id, context) {
        const { domain, user, notify, tx } = context.state;
        const repository = tx.getRepository(process_1.Process);
        const process = await repository.findOneBy({ domain: { id: domain.id }, id });
        const deleted = await repository.delete(id);
        notify &&
            notify({
                mode: 'in-app',
                title: `Process '${process.name}' deleted`,
                body: `Process '${process.name}' deleted by ${user.name}\n${process.description}`
            });
        return true;
    }
};
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => process_1.Process, { description: 'To create new Process' }),
    tslib_1.__param(0, (0, type_graphql_1.Arg)('process')),
    tslib_1.__param(1, (0, type_graphql_1.Ctx)()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [process_type_1.NewProcess, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessMutation.prototype, "createProcess", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => process_1.Process, { description: 'To modify Process 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_type_1.ProcessPatch, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], ProcessMutation.prototype, "updateProcess", null);
tslib_1.__decorate([
    (0, type_graphql_1.Directive)('@transaction'),
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To delete Process' }),
    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)
], ProcessMutation.prototype, "deleteProcess", null);
ProcessMutation = tslib_1.__decorate([
    (0, type_graphql_1.Resolver)(process_1.Process)
], ProcessMutation);
exports.ProcessMutation = ProcessMutation;
//# sourceMappingURL=process-mutation.js.map