UNPKG

@becomes/cms

Version:

Simple CMS for building APIs.

299 lines 17.3 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebhookController = void 0; var purple_cheetah_1 = require("purple-cheetah"); var webhook_cash_service_1 = require("./webhook-cash.service"); var webhook_factory_1 = require("./factories/webhook.factory"); var WebhookController = (function () { function WebhookController() { } WebhookController_1 = WebhookController; WebhookController.prototype.getAll = function (request) { return __awaiter(this, void 0, void 0, function () { var error, jwt, jwtValid; return __generator(this, function (_a) { error = purple_cheetah_1.HttpErrorFactory.simple('getAll', this.logger); jwt = purple_cheetah_1.JWTEncoding.decode(request.headers.authorization); if (jwt instanceof Error) { throw error.occurred(purple_cheetah_1.HttpStatus.UNAUTHORIZED, jwt.message); } else { jwtValid = purple_cheetah_1.JWTSecurity.validateAndCheckTokenPermissions(jwt, [purple_cheetah_1.RoleName.ADMIN, purple_cheetah_1.RoleName.USER], purple_cheetah_1.PermissionName.READ, purple_cheetah_1.JWTConfigService.get('user-token-config')); if (jwtValid instanceof Error) { throw error.occurred(purple_cheetah_1.HttpStatus.UNAUTHORIZED, jwtValid.message); } } return [2, { webhooks: webhook_cash_service_1.WebhookCashService.findAll(), }]; }); }); }; WebhookController.prototype.getById = function (request) { return __awaiter(this, void 0, void 0, function () { var error, jwt, jwtValid, webhook; return __generator(this, function (_a) { error = purple_cheetah_1.HttpErrorFactory.simple('getById', this.logger); if (purple_cheetah_1.StringUtility.isIdValid(request.params.id) === false) { throw error.occurred(purple_cheetah_1.HttpStatus.FORBIDDEN, "Invalid ID '" + request.params.id + "' was provided."); } jwt = purple_cheetah_1.JWTEncoding.decode(request.headers.authorization); if (jwt instanceof Error) { throw error.occurred(purple_cheetah_1.HttpStatus.UNAUTHORIZED, jwt.message); } else { jwtValid = purple_cheetah_1.JWTSecurity.validateAndCheckTokenPermissions(jwt, [purple_cheetah_1.RoleName.ADMIN, purple_cheetah_1.RoleName.USER], purple_cheetah_1.PermissionName.READ, purple_cheetah_1.JWTConfigService.get('user-token-config')); if (jwtValid instanceof Error) { throw error.occurred(purple_cheetah_1.HttpStatus.UNAUTHORIZED, jwtValid.message); } } webhook = webhook_cash_service_1.WebhookCashService.findById(request.params.id); if (webhook === null) { throw error.occurred(purple_cheetah_1.HttpStatus.NOT_FOUNT, "Webhook with ID '" + request.params.id + "' does not exist."); } return [2, { webhook: webhook, }]; }); }); }; WebhookController.prototype.add = function (request) { return __awaiter(this, void 0, void 0, function () { var error, jwt, jwtValid, webhook, addWebhookResult; return __generator(this, function (_a) { switch (_a.label) { case 0: error = purple_cheetah_1.HttpErrorFactory.simple('add', this.logger); try { purple_cheetah_1.ObjectUtility.compareWithSchema(request.body, { name: { __type: 'string', __required: true, }, desc: { __type: 'string', __required: true, }, body: { __type: 'string', __required: true, }, }, 'body'); } catch (e) { throw error.occurred(purple_cheetah_1.HttpStatus.BAD_REQUEST, e.message); } jwt = purple_cheetah_1.JWTEncoding.decode(request.headers.authorization); if (jwt instanceof Error) { throw error.occurred(purple_cheetah_1.HttpStatus.UNAUTHORIZED, jwt.message); } else { jwtValid = purple_cheetah_1.JWTSecurity.validateAndCheckTokenPermissions(jwt, [purple_cheetah_1.RoleName.ADMIN], purple_cheetah_1.PermissionName.WRITE, purple_cheetah_1.JWTConfigService.get('user-token-config')); if (jwtValid instanceof Error) { throw error.occurred(purple_cheetah_1.HttpStatus.UNAUTHORIZED, jwtValid.message); } } webhook = webhook_factory_1.WebhookFactory.instance; webhook.name = request.body.name; webhook.desc = request.body.desc; webhook.body = request.body.body; return [4, webhook_cash_service_1.WebhookCashService.add(webhook)]; case 1: addWebhookResult = _a.sent(); if (addWebhookResult === false) { throw error.occurred(purple_cheetah_1.HttpStatus.INTERNAL_SERVER_ERROR, 'Failed to add Webhook to the database.'); } return [2, { webhook: webhook, }]; } }); }); }; WebhookController.prototype.update = function (request) { return __awaiter(this, void 0, void 0, function () { var error, jwt, jwtValid, webhook, updateWebhookResult; return __generator(this, function (_a) { switch (_a.label) { case 0: error = purple_cheetah_1.HttpErrorFactory.simple('update', this.logger); try { purple_cheetah_1.ObjectUtility.compareWithSchema(request.body, { _id: { __type: 'string', __required: true, }, name: { __type: 'string', __required: true, }, desc: { __type: 'string', __required: true, }, body: { __type: 'string', __required: true, }, }, 'body'); } catch (e) { throw error.occurred(purple_cheetah_1.HttpStatus.BAD_REQUEST, e.message); } if (purple_cheetah_1.StringUtility.isIdValid(request.body._id) === false) { throw error.occurred(purple_cheetah_1.HttpStatus.FORBIDDEN, "Invalid ID '" + request.body._id + "' was provided."); } jwt = purple_cheetah_1.JWTEncoding.decode(request.headers.authorization); if (jwt instanceof Error) { throw error.occurred(purple_cheetah_1.HttpStatus.UNAUTHORIZED, jwt.message); } else { jwtValid = purple_cheetah_1.JWTSecurity.validateAndCheckTokenPermissions(jwt, [purple_cheetah_1.RoleName.ADMIN], purple_cheetah_1.PermissionName.WRITE, purple_cheetah_1.JWTConfigService.get('user-token-config')); if (jwtValid instanceof Error) { throw error.occurred(purple_cheetah_1.HttpStatus.UNAUTHORIZED, jwtValid.message); } } webhook = webhook_cash_service_1.WebhookCashService.findById(request.body._id); if (webhook === null) { throw error.occurred(purple_cheetah_1.HttpStatus.NOT_FOUNT, "Webhook with ID '" + request.body._id + "' does not exist."); } webhook.name = request.body.name; webhook.desc = request.body.desc; webhook.body = request.body.body; return [4, webhook_cash_service_1.WebhookCashService.update(webhook)]; case 1: updateWebhookResult = _a.sent(); if (updateWebhookResult === false) { throw error.occurred(purple_cheetah_1.HttpStatus.INTERNAL_SERVER_ERROR, 'Failed to update Webhook in the database.'); } return [2, { webhook: webhook, }]; } }); }); }; WebhookController.prototype.deleteById = function (request) { return __awaiter(this, void 0, void 0, function () { var error, jwt, jwtValid, webhook, deleteWebhookResult; return __generator(this, function (_a) { switch (_a.label) { case 0: error = purple_cheetah_1.HttpErrorFactory.simple('deleteById', this.logger); if (purple_cheetah_1.StringUtility.isIdValid(request.params.id) === false) { throw error.occurred(purple_cheetah_1.HttpStatus.FORBIDDEN, "Invalid ID '" + request.params.id + "' was provided."); } jwt = purple_cheetah_1.JWTEncoding.decode(request.headers.authorization); if (jwt instanceof Error) { throw error.occurred(purple_cheetah_1.HttpStatus.UNAUTHORIZED, jwt.message); } else { jwtValid = purple_cheetah_1.JWTSecurity.validateAndCheckTokenPermissions(jwt, [purple_cheetah_1.RoleName.ADMIN], purple_cheetah_1.PermissionName.DELETE, purple_cheetah_1.JWTConfigService.get('user-token-config')); if (jwtValid instanceof Error) { throw error.occurred(purple_cheetah_1.HttpStatus.UNAUTHORIZED, jwtValid.message); } } webhook = webhook_cash_service_1.WebhookCashService.findById(request.params.id); if (webhook === null) { throw error.occurred(purple_cheetah_1.HttpStatus.NOT_FOUNT, "Webhook with ID '" + request.params.id + "' does not exist."); } return [4, webhook_cash_service_1.WebhookCashService.deleteById(request.params.id)]; case 1: deleteWebhookResult = _a.sent(); if (deleteWebhookResult === false) { throw error.occurred(purple_cheetah_1.HttpStatus.INTERNAL_SERVER_ERROR, 'Failed to remove Webhook from the database.'); } return [2, { message: 'Success.', }]; } }); }); }; var WebhookController_1; __decorate([ purple_cheetah_1.AppLogger(WebhookController_1), __metadata("design:type", purple_cheetah_1.Logger) ], WebhookController.prototype, "logger", void 0); __decorate([ purple_cheetah_1.Get('/all'), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], WebhookController.prototype, "getAll", null); __decorate([ purple_cheetah_1.Get('/:id'), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], WebhookController.prototype, "getById", null); __decorate([ purple_cheetah_1.Post(), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], WebhookController.prototype, "add", null); __decorate([ purple_cheetah_1.Put(), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], WebhookController.prototype, "update", null); __decorate([ purple_cheetah_1.Delete('/:id'), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], WebhookController.prototype, "deleteById", null); WebhookController = WebhookController_1 = __decorate([ purple_cheetah_1.Controller('/webhook') ], WebhookController); return WebhookController; }()); exports.WebhookController = WebhookController; //# sourceMappingURL=webhook.controller.js.map