UNPKG

json-api-nestjs

Version:
98 lines 3.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OperationController = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const input_operation_pipe_1 = require("../pipes/input-operation.pipe"); const service_1 = require("../service"); const constants_1 = require("../constants"); let OperationController = class OperationController { explorerService; executeService; async index(inputOperationData) { const paramForCall = []; let i = 0; for (const dataInput of inputOperationData) { const { ref: { relationship, id, type }, op, } = dataInput; let controller; let methodName; let module; try { controller = this.explorerService.getControllerByEntityName(type); } catch (e) { const error = { code: 'invalid_arguments', message: `Resource '${type}' does not exist`, path: [constants_1.KEY_MAIN_INPUT_SCHEMA, `${i}`, 'ref', 'type'], }; throw new common_1.NotFoundException([error]); } try { methodName = this.explorerService.getMethodNameByParam(op, id, relationship); } catch (e) { const error = { code: 'invalid_arguments', message: `Operation '${op}' not allowed`, path: [constants_1.KEY_MAIN_INPUT_SCHEMA, `${i}`, 'op'], }; throw new common_1.MethodNotAllowedException([error]); } const params = this.explorerService.getParamsForMethod(methodName, dataInput); try { module = this.explorerService.getModulesByController(controller); } catch (e) { const error = { code: 'invalid_arguments', message: `Resource '${type}' does not exist`, path: [constants_1.KEY_MAIN_INPUT_SCHEMA, `${i}`, 'ref', 'type'], }; throw new common_1.NotFoundException([error]); } paramForCall.push({ controller, methodName, params, module, }); i++; } const tmpIds = []; for (const item of inputOperationData) { if (item.op !== 'add') continue; if (!item.ref.tmpId) continue; tmpIds.push(item.ref.tmpId); } const result = await this.executeService.run(paramForCall, tmpIds); return { [constants_1.KEY_MAIN_OUTPUT_SCHEMA]: result.map((i) => ({ data: i.data, ...(i.meta ? { meta: i.meta } : {}), })), }; } }; exports.OperationController = OperationController; tslib_1.__decorate([ (0, common_1.Inject)(service_1.ExplorerService), tslib_1.__metadata("design:type", service_1.ExplorerService) ], OperationController.prototype, "explorerService", void 0); tslib_1.__decorate([ (0, common_1.Inject)(service_1.ExecuteService), tslib_1.__metadata("design:type", service_1.ExecuteService) ], OperationController.prototype, "executeService", void 0); tslib_1.__decorate([ (0, common_1.Post)(''), tslib_1.__param(0, (0, common_1.Body)(input_operation_pipe_1.InputOperationPipe)), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [Array]), tslib_1.__metadata("design:returntype", Promise) ], OperationController.prototype, "index", null); exports.OperationController = OperationController = tslib_1.__decorate([ (0, common_1.Controller)('/') ], OperationController); //# sourceMappingURL=operation.controller.js.map