UNPKG

@becomes/cms

Version:

Simple CMS for building APIs.

465 lines 26.6 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.WidgetController = void 0; var purple_cheetah_1 = require("purple-cheetah"); var group_service_1 = require("../group/group.service"); var widget_service_1 = require("./widget.service"); var widget_factory_1 = require("./factories/widget.factory"); var widget_util_1 = require("./widget-util"); var prop_util_1 = require("../prop/prop-util"); var entry_1 = require("../entry"); var prop_1 = require("../prop"); var cache_control_1 = require("../cache-control"); var WidgetController = (function () { function WidgetController() { } WidgetController_1 = WidgetController; WidgetController.prototype.getAll = function (request) { return __awaiter(this, void 0, void 0, function () { var error, query, ids, jwt, jwtValid, widgets; return __generator(this, function (_a) { switch (_a.label) { case 0: error = purple_cheetah_1.HttpErrorFactory.simple('getAll', this.logger); query = request.query; ids = []; if (query.ids) { ids = query.ids.split('-'); ids.forEach(function (id, i) { if (purple_cheetah_1.StringUtility.isIdValid(id) === false) { throw error.occurred(purple_cheetah_1.HttpStatus.FORBIDDEN, "Invalid ID '" + id + "' was provided at [" + i + "]."); } }); } 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); } } if (!(ids.length > 0)) return [3, 2]; return [4, this.widgetService.findAllById(ids)]; case 1: widgets = _a.sent(); return [3, 4]; case 2: return [4, this.widgetService.findAll()]; case 3: widgets = _a.sent(); _a.label = 4; case 4: return [2, { widgets: widgets, }]; } }); }); }; WidgetController.prototype.getById = function (request) { return __awaiter(this, void 0, void 0, function () { var error, jwt, jwtValid, widget; return __generator(this, function (_a) { switch (_a.label) { case 0: 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); } } return [4, this.widgetService.findById(request.params.id)]; case 1: widget = _a.sent(); if (widget === null) { throw error.occurred(purple_cheetah_1.HttpStatus.NOT_FOUNT, "Widget with ID '" + request.params.id + "' does not exist."); } return [2, { widget: widget, }]; } }); }); }; WidgetController.prototype.add = function (request) { return __awaiter(this, void 0, void 0, function () { var error, jwt, jwtValid, widget, widgetWithSameName, addWidgetResult; 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: false, }, }, '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.RoleName.USER], 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); } } widget = widget_factory_1.WidgetFactory.instance; widget.name = widget_util_1.WidgetUtil.nameEncode(request.body.name); if (typeof request.body.desc !== 'undefined') { widget.desc = request.body.desc; } else { widget.desc = ''; } return [4, this.widgetService.findByName(widget.name)]; case 1: widgetWithSameName = _a.sent(); if (widgetWithSameName !== null) { throw error.occurred(purple_cheetah_1.HttpStatus.FORBIDDEN, "Widget with name '" + widget.name + "' already exist."); } return [4, this.widgetService.add(widget)]; case 2: addWidgetResult = _a.sent(); if (addWidgetResult === false) { throw error.occurred(purple_cheetah_1.HttpStatus.INTERNAL_SERVER_ERROR, 'Failed to add Widget to database.'); } return [2, { widget: widget, }]; } }); }); }; WidgetController.prototype.update = function (request) { return __awaiter(this, void 0, void 0, function () { var error, jwt, jwtValid, widget, changeDetected, updateName, oldWidgetName, widgetWithSameName, _a, e_1, updateWidgetResult, changes, addDetected, _b, _c, _i, i, change; return __generator(this, function (_d) { switch (_d.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: false, }, desc: { __type: 'string', __required: false, }, changes: { __type: 'object', __required: false, __child: { props: { __type: 'array', __required: true, __child: { __type: 'object', __content: prop_util_1.PropUtil.changesSchema, }, }, }, }, }, '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.RoleName.USER], 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); } } return [4, this.widgetService.findById(request.body._id)]; case 1: widget = _d.sent(); if (widget === null) { throw error.occurred(purple_cheetah_1.HttpStatus.NOT_FOUNT, "Widget with ID '" + request.body._id + "' does not exist."); } changeDetected = false; updateName = false; oldWidgetName = ''; if (!(typeof request.body.name !== 'undefined' && request.body.name !== widget.name)) return [3, 3]; changeDetected = true; updateName = true; oldWidgetName = "" + widget.name; widget.name = widget_util_1.WidgetUtil.nameEncode(request.body.name); return [4, this.widgetService.findByName(widget.name)]; case 2: widgetWithSameName = _d.sent(); if (widgetWithSameName !== null) { throw error.occurred(purple_cheetah_1.HttpStatus.FORBIDDEN, "Widget with name '" + widget.name + "' already exist."); } _d.label = 3; case 3: if (typeof request.body.desc !== 'undefined' && request.body.desc !== widget.desc) { changeDetected = true; widget.desc = request.body.desc; } if (!(typeof request.body.props !== 'undefined')) return [3, 7]; if (typeof request.body.changes === 'undefined') { throw error.occurred(purple_cheetah_1.HttpStatus.FORBIDDEN, 'When updating Props, changes must be provided.'); } changeDetected = true; _d.label = 4; case 4: _d.trys.push([4, 6, , 7]); _a = widget; return [4, prop_util_1.PropUtil.getPropsFromUntrustedObject(request.body.props, this.groupService)]; case 5: _a.props = _d.sent(); return [3, 7]; case 6: e_1 = _d.sent(); throw error.occurred(purple_cheetah_1.HttpStatus.BAD_REQUEST, e_1.message); case 7: if (changeDetected === false) { throw error.occurred(purple_cheetah_1.HttpStatus.FORBIDDEN, 'Noting to update.'); } return [4, this.widgetService.update(widget)]; case 8: updateWidgetResult = _d.sent(); if (updateWidgetResult === false) { throw error.occurred(purple_cheetah_1.HttpStatus.INTERNAL_SERVER_ERROR, "Failed to update Widget in database."); } if (!(updateName === true || typeof request.body.changes !== 'undefined')) return [3, 14]; changes = request.body.changes.props; addDetected = false; _b = []; for (_c in changes) _b.push(_c); _i = 0; _d.label = 9; case 9: if (!(_i < _b.length)) return [3, 12]; i = _b[_i]; change = changes[i]; if (!change.add) return [3, 11]; addDetected = true; return [4, widget_util_1.WidgetUtil.addNewPropsToWidgetInEntries(this.entryService, this.logger, change, request.body._id)]; case 10: _d.sent(); _d.label = 11; case 11: _i++; return [3, 9]; case 12: if (!(addDetected === false)) return [3, 14]; return [4, widget_util_1.WidgetUtil.updateEntriesWithNewWidgetData(this.entryService, this.logger, updateName === true ? oldWidgetName : widget.name, updateName === true ? widget.name : undefined, request.body.changes.props)]; case 13: _d.sent(); _d.label = 14; case 14: return [2, { widget: widget, }]; } }); }); }; WidgetController.prototype.deleteById = function (request) { return __awaiter(this, void 0, void 0, function () { var error, jwt, jwtValid, widget, deleteWidgetResult, entries; var _this = this; 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.RoleName.USER], 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); } } return [4, this.widgetService.findById(request.params.id)]; case 1: widget = _a.sent(); if (widget === null) { throw error.occurred(purple_cheetah_1.HttpStatus.NOT_FOUNT, "Widget with ID '" + request.params.id + "' does not exist."); } return [4, this.widgetService.deleteById(request.params.id)]; case 2: deleteWidgetResult = _a.sent(); if (deleteWidgetResult === false) { throw error.occurred(purple_cheetah_1.HttpStatus.INTERNAL_SERVER_ERROR, "Failed to remove Widget from database."); } return [4, this.entryService.findAllByWidgetId(request.params.id)]; case 3: entries = _a.sent(); entries = entries.map(function (entry) { entry.content = entry.content.map(function (content) { content.props = content.props.map(function (rootProp) { if (rootProp.type === prop_1.PropType.QUILL) { rootProp.value.content = rootProp.value.content.filter(function (quillContent) { if (quillContent.type === prop_1.PropQuillContentType.WIDGET) { if (quillContent.value._id === request.params.id) { return false; } } return true; }); } return rootProp; }); return content; }); return entry; }); entries.forEach(function (entry) { cache_control_1.CacheControl.Entry.update(entry) .then(function (result) { if (result === false) { _this.logger.error('.deleteById - update entry', "Failed to update entry \"" + entry._id.toHexString() + "\""); } }) .catch(function (e) { _this.logger.error('.deleteById - update entry', e); }); }); return [2, { message: 'Success.', }]; } }); }); }; var WidgetController_1; __decorate([ purple_cheetah_1.AppLogger(WidgetController_1), __metadata("design:type", purple_cheetah_1.Logger) ], WidgetController.prototype, "logger", void 0); __decorate([ purple_cheetah_1.Service(group_service_1.GroupService), __metadata("design:type", group_service_1.GroupService) ], WidgetController.prototype, "groupService", void 0); __decorate([ purple_cheetah_1.Service(widget_service_1.WidgetService), __metadata("design:type", widget_service_1.WidgetService) ], WidgetController.prototype, "widgetService", void 0); __decorate([ purple_cheetah_1.Service(entry_1.EntryService), __metadata("design:type", entry_1.EntryService) ], WidgetController.prototype, "entryService", void 0); __decorate([ purple_cheetah_1.Get('/all'), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], WidgetController.prototype, "getAll", null); __decorate([ purple_cheetah_1.Get('/:id'), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], WidgetController.prototype, "getById", null); __decorate([ purple_cheetah_1.Post(), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], WidgetController.prototype, "add", null); __decorate([ purple_cheetah_1.Put(), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], WidgetController.prototype, "update", null); __decorate([ purple_cheetah_1.Delete('/:id'), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], WidgetController.prototype, "deleteById", null); WidgetController = WidgetController_1 = __decorate([ purple_cheetah_1.Controller('/widget') ], WidgetController); return WidgetController; }()); exports.WidgetController = WidgetController; //# sourceMappingURL=widget.controller.js.map