"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectNodeController = void 0;
const tslib_1 = require("tslib");
/* eslint-disable @typescript-eslint/no-explicit-any */
const authentication_1 = require("@loopback/authentication");
const authorization_1 = require("@loopback/authorization");
const core_1 = require("@loopback/core");
const rest_1 = require("@loopback/rest");
const lodash_1 = require("lodash");
const models_1 = require("../models");
const application_service_1 = require("../services/application.service");
const object_node_service_1 = require("../services/object-node/object-node.service");
const access_rights_tree_const_1 = require("./../services/access-rights/access-rights-tree.const");
const access_rights_const_1 = require("./../services/access-rights/access-rights.const");
const action_entity_service_1 = require("./../services/action-entity/action-entity.service");
const object_node_content_service_1 = require("./../services/object-node/object-node-content.service");
let ObjectNodeController = class ObjectNodeController {
    constructor(objectNodeService, objectNodeContentService, actionEntityService) {
        this.objectNodeService = objectNodeService;
        this.objectNodeContentService = objectNodeContentService;
        this.actionEntityService = actionEntityService;
    }
    create(objectNode, ctx) {
        return this.objectNodeService.add(objectNode, ctx, false, true, true);
    }
    updateById(id, objectNode, ctx) {
        return this.objectNodeService.modifyById(id, objectNode, ctx);
    }
    async deleteById(id, ctx) {
        await this.objectNodeService.removeById(id, ctx);
    }
    async downloadFile(id, contentType, contentId, response, ctx) {
        switch (contentType) {
            case 'file': {
                const file = (await this.objectNodeContentService.getContent(id, 'contentFile', 'ContentFile', {
                    contentId: contentId,
                }, ctx));
                response.download(file.filePath, file.fileName);
                return response;
            }
            default: {
                const contentTypeExtension = contentType.charAt(0).toUpperCase() + contentType.substr(1);
                const text = await this.objectNodeContentService.getContent(id, 'content' + contentTypeExtension, 'Content' + contentTypeExtension, {
                    contentId: contentId,
                }, ctx);
                if (lodash_1.isObject(text)) {
                    if (text.base64 && text.name) {
                        response.header('Content-Disposition', 'attachment; filename="' + text.name + '"');
                        if (text.type) {
                            response.type(text.type);
                        }
                        const file = Buffer.from(text.base64, 'base64');
                        response.header('Content-Length', JSON.stringify(file.length));
                        response.send(file);
                        response.end();
                        return response;
                    }
                    response.json(text);
                    return response;
                }
                else {
                    response.set('Content-Type', 'text/plain');
                    response.send(text);
                    return response;
                }
            }
        }
    }
    async findById(id, ctx) {
        return this.objectNodeService.getNode(id, ctx);
    }
    async findOwnerTree(ownerType, ownerName, ctx) {
        return this.objectNodeService.getOwner(ownerType, ownerName, ctx);
    }
    async findTree(ownerType, ownerName, namespaceType, namespaceName, treeType, treeName, ctx) {
        return this.objectNodeService.getTree(ownerType, ownerName, namespaceType, namespaceName, treeType, treeName, ctx);
    }
    async findNamespaceTree(ownerType, ownerName, namespaceType, namespaceName, ctx) {
        return this.objectNodeService.getNamespace(ownerType, ownerName, namespaceType, namespaceName, ctx);
    }
    async findNode(ownerType, ownerName, namespaceType, namespaceName, treeType, treeName, nodeType, nodeName, ctx) {
        return this.objectNodeService.getANodeOfTree(ownerType, ownerName, namespaceType, namespaceName, treeType, treeName, nodeType, nodeName, ctx);
    }
};
tslib_1.__decorate([
    authentication_1.authenticate('jwt'),
    authorization_1.authorize({
        resource: access_rights_const_1.AccessRightsEntity.objectNode,
        scopes: [access_rights_const_1.AccessRightsScope.create],
    }),
    rest_1.post('/object-nodes', {
        responses: {
            '200': {
                description: 'ObjectNode model instance',
                content: { 'application/json': { schema: rest_1.getModelSchemaRef(models_1.ObjectNode) } },
            },
        },
    }),
    tslib_1.__param(0, rest_1.requestBody({
        content: {
            'application/json': {
                schema: rest_1.getModelSchemaRef(models_1.ObjectNode, {
                    title: 'NewObjectNode',
                    exclude: ['id'],
                }),
            },
        },
    })),
    tslib_1.__param(1, core_1.inject(application_service_1.CURRENT_CONTEXT)),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [Object, application_service_1.CurrentContext]),
    tslib_1.__metadata("design:returntype", Promise)
], ObjectNodeController.prototype, "create", null);
tslib_1.__decorate([
    authentication_1.authenticate('jwt'),
    authorization_1.authorize({
        resource: access_rights_const_1.AccessRightsEntity.objectNode,
        scopes: [access_rights_const_1.AccessRightsScope.update],
    }),
    rest_1.patch('/object-nodes/{id}', {
        responses: {
            '204': {
                description: 'ObjectNode PATCH success',
                content: { 'application/json': { schema: rest_1.getModelSchemaRef(models_1.ObjectNode) } },
            },
        },
    }),
    tslib_1.__param(0, rest_1.param.path.string('id')),
    tslib_1.__param(1, rest_1.requestBody({
        content: {
            'application/json': {
                schema: rest_1.getModelSchemaRef(models_1.ObjectNode, { partial: true }),
            },
        },
    })),
    tslib_1.__param(2, core_1.inject(application_service_1.CURRENT_CONTEXT)),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, models_1.ObjectNode,
        application_service_1.CurrentContext]),
    tslib_1.__metadata("design:returntype", Promise)
], ObjectNodeController.prototype, "updateById", null);
tslib_1.__decorate([
    authentication_1.authenticate('jwt'),
    authorization_1.authorize({
        resource: access_rights_const_1.AccessRightsEntity.objectNode,
        scopes: [access_rights_const_1.AccessRightsScope.delete],
    }),
    rest_1.del('/object-nodes/{id}', {
        responses: {
            '204': {
                description: 'ObjectNode DELETE success',
            },
        },
    }),
    tslib_1.__param(0, rest_1.param.path.string('id')),
    tslib_1.__param(1, core_1.inject(application_service_1.CURRENT_CONTEXT)),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, application_service_1.CurrentContext]),
    tslib_1.__metadata("design:returntype", Promise)
], ObjectNodeController.prototype, "deleteById", null);
tslib_1.__decorate([
    authorization_1.authorize({
        resource: access_rights_const_1.AccessRightsEntity.objectNodeContent,
        scopes: [access_rights_const_1.AccessRightsScope.read],
    }),
    rest_1.get('/object-nodes/{id}/content-{contentType}/{contentId}'),
    rest_1.oas.response.file(),
    tslib_1.__param(0, rest_1.param.path.string('id')),
    tslib_1.__param(1, rest_1.param.path.string('contentType')),
    tslib_1.__param(2, rest_1.param.path.string('contentId')),
    tslib_1.__param(3, core_1.inject(rest_1.RestBindings.Http.RESPONSE)),
    tslib_1.__param(4, core_1.inject(application_service_1.CURRENT_CONTEXT)),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, String, Object, application_service_1.CurrentContext]),
    tslib_1.__metadata("design:returntype", Promise)
], ObjectNodeController.prototype, "downloadFile", null);
tslib_1.__decorate([
    authorization_1.authorize({
        resource: access_rights_const_1.AccessRightsEntity.objectNode,
        scopes: [access_rights_const_1.AccessRightsScope.read],
    }),
    rest_1.get('/object-nodes/{id}', {
        responses: {
            '200': {
                description: 'ObjectNode model instance',
                content: {
                    'application/json': {
                        schema: rest_1.getModelSchemaRef(models_1.ObjectNode, { includeRelations: true }),
                    },
                },
            },
        },
    }),
    tslib_1.__param(0, rest_1.param.path.string('id')),
    tslib_1.__param(1, core_1.inject(application_service_1.CURRENT_CONTEXT)),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, application_service_1.CurrentContext]),
    tslib_1.__metadata("design:returntype", Promise)
], ObjectNodeController.prototype, "findById", null);
tslib_1.__decorate([
    authorization_1.authorize({
        resource: access_rights_const_1.AccessRightsEntity.objectNode,
        scopes: [access_rights_const_1.AccessRightsScope.read, access_rights_tree_const_1.AccessRightsTreeScope.searchOwner],
    }),
    rest_1.get('/object-nodes/owner/{ownerType}/{ownerName}', {
        responses: {
            '200': {
                description: 'ObjectNode model',
                content: {
                    'application/json': {
                        schema: {
                            items: rest_1.getModelSchemaRef(models_1.ObjectNode),
                        },
                    },
                },
            },
        },
    }),
    tslib_1.__param(0, rest_1.param.path.string('ownerType')),
    tslib_1.__param(1, rest_1.param.path.string('ownerName')),
    tslib_1.__param(2, core_1.inject(application_service_1.CURRENT_CONTEXT)),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, application_service_1.CurrentContext]),
    tslib_1.__metadata("design:returntype", Promise)
], ObjectNodeController.prototype, "findOwnerTree", null);
tslib_1.__decorate([
    authorization_1.authorize({
        resource: access_rights_const_1.AccessRightsEntity.objectNode,
        scopes: [access_rights_const_1.AccessRightsScope.read, access_rights_tree_const_1.AccessRightsTreeScope.searchTree],
    }),
    rest_1.get('/object-nodes/tree/{ownerType}/{ownerName}/{namespaceType}/{namespaceName}/{treeType}/{treeName}', {
        responses: {
            '200': {
                description: 'ObjectNode model',
                content: {
                    'application/json': {
                        schema: {
                            items: rest_1.getModelSchemaRef(models_1.ObjectNode),
                        },
                    },
                },
            },
        },
    }),
    tslib_1.__param(0, rest_1.param.path.string('ownerType')),
    tslib_1.__param(1, rest_1.param.path.string('ownerName')),
    tslib_1.__param(2, rest_1.param.path.string('namespaceType')),
    tslib_1.__param(3, rest_1.param.path.string('namespaceName')),
    tslib_1.__param(4, rest_1.param.path.string('treeType')),
    tslib_1.__param(5, rest_1.param.path.string('treeName')),
    tslib_1.__param(6, core_1.inject(application_service_1.CURRENT_CONTEXT)),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, String, String, String, String, application_service_1.CurrentContext]),
    tslib_1.__metadata("design:returntype", Promise)
], ObjectNodeController.prototype, "findTree", null);
tslib_1.__decorate([
    authorization_1.authorize({
        resource: access_rights_const_1.AccessRightsEntity.objectNode,
        scopes: [access_rights_const_1.AccessRightsScope.read, access_rights_tree_const_1.AccessRightsTreeScope.searchNamespace],
    }),
    rest_1.get('/object-nodes/namespace/{ownerType}/{ownerName}/{namespaceType}/{namespaceName}', {
        responses: {
            '200': {
                description: 'ObjectNode model',
                content: {
                    'application/json': {
                        schema: {
                            items: rest_1.getModelSchemaRef(models_1.ObjectNode),
                        },
                    },
                },
            },
        },
    }),
    tslib_1.__param(0, rest_1.param.path.string('ownerType')),
    tslib_1.__param(1, rest_1.param.path.string('ownerName')),
    tslib_1.__param(2, rest_1.param.path.string('namespaceType')),
    tslib_1.__param(3, rest_1.param.path.string('namespaceName')),
    tslib_1.__param(4, core_1.inject(application_service_1.CURRENT_CONTEXT)),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, String, String, application_service_1.CurrentContext]),
    tslib_1.__metadata("design:returntype", Promise)
], ObjectNodeController.prototype, "findNamespaceTree", null);
tslib_1.__decorate([
    authorization_1.authorize({
        resource: access_rights_const_1.AccessRightsEntity.objectNode,
        scopes: [access_rights_const_1.AccessRightsScope.read, access_rights_tree_const_1.AccessRightsTreeScope.searchNode],
    }),
    rest_1.get('/object-nodes/node/{ownerType}/{ownerName}/{namespaceType}/{namespaceName}/{treeType}/{treeName}/{nodeType}/{nodeName}', {
        responses: {
            '200': {
                description: 'ObjectNode model',
                content: {
                    'application/json': {
                        schema: {
                            items: rest_1.getModelSchemaRef(models_1.ObjectNode),
                        },
                    },
                },
            },
        },
    }),
    tslib_1.__param(0, rest_1.param.path.string('ownerType')),
    tslib_1.__param(1, rest_1.param.path.string('ownerName')),
    tslib_1.__param(2, rest_1.param.path.string('namespaceType')),
    tslib_1.__param(3, rest_1.param.path.string('namespaceName')),
    tslib_1.__param(4, rest_1.param.path.string('treeType')),
    tslib_1.__param(5, rest_1.param.path.string('treeName')),
    tslib_1.__param(6, rest_1.param.path.string('nodeType')),
    tslib_1.__param(7, rest_1.param.path.string('nodeName')),
    tslib_1.__param(8, core_1.inject(application_service_1.CURRENT_CONTEXT)),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [String, String, String, String, String, String, String, String, application_service_1.CurrentContext]),
    tslib_1.__metadata("design:returntype", Promise)
], ObjectNodeController.prototype, "findNode", null);
ObjectNodeController = tslib_1.__decorate([
    tslib_1.__param(0, core_1.service(object_node_service_1.ObjectNodeService)),
    tslib_1.__param(1, core_1.service(object_node_content_service_1.ObjectNodeContentService)),
    tslib_1.__param(2, core_1.service(action_entity_service_1.ActionEntityService)),
    tslib_1.__metadata("design:paramtypes", [object_node_service_1.ObjectNodeService,
        object_node_content_service_1.ObjectNodeContentService,
        action_entity_service_1.ActionEntityService])
], ObjectNodeController);
exports.ObjectNodeController = ObjectNodeController;
//# sourceMappingURL=object-node.controller.js.map