server/helpers/typeRouteUtils.js

"use strict";

require("core-js/modules/es.weak-map.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.identifierTypeCreateOrEditHandler = identifierTypeCreateOrEditHandler;
exports.relationshipTypeCreateOrEditHandler = relationshipTypeCreateOrEditHandler;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.array.includes.js");
require("core-js/modules/es.string.includes.js");
require("core-js/modules/es.parse-int.js");
require("core-js/modules/es.symbol.js");
require("core-js/modules/es.symbol.description.js");
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.promise.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.date.to-json.js");
require("core-js/modules/web.url.to-json.js");
var error = _interopRequireWildcard(require("../../common/helpers/error"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/*
 * Copyright (C) 2023 Shivam Awasthi
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

function getChangedAttributeTypes(oldAttributes, newAttributes) {
  var commonAttributes = oldAttributes.filter(function (value) {
    return newAttributes.includes(value);
  });
  var attributesToBeRemoved = oldAttributes.filter(function (value) {
    return !commonAttributes.includes(value);
  });
  var attributesToBeAdded = newAttributes.filter(function (value) {
    return !commonAttributes.includes(value);
  });
  return {
    attributesToBeAdded: attributesToBeAdded,
    attributesToBeRemoved: attributesToBeRemoved
  };
}

/**
 * A handler for create or edit actions on relationship types.
 * @param {object} req - request object
 * @param {object} res - response object
 * @param {object} next - next object
 * @returns {promise} res.send promise
 * @description
 * Creates a new reationship type or updates an existing relationship type
 */
function relationshipTypeCreateOrEditHandler(_x, _x2, _x3) {
  return _relationshipTypeCreateOrEditHandler.apply(this, arguments);
}
/**
 * A handler for create or edit actions on identifier types.
 * @param {object} req - request object
 * @param {object} res - response object
 * @returns {promise} res.send promise
 * @description
 * Creates a new identifier type or updates an existing identifier type
 */
function _relationshipTypeCreateOrEditHandler() {
  _relationshipTypeCreateOrEditHandler = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(req, res, next) {
    var _req$app$locals$orm, RelationshipType, RelationshipTypeAttributeType, bookshelf, trx, newRelationshipType, method, _req$body, attributeTypes, childOrder, deprecated, description, label, linkPhrase, oldAttributeTypes, parentId, reverseLinkPhrase, sourceEntityType, targetEntityType, relationshipType, _getChangedAttributeT, attributesToBeAdded, attributesToBeRemoved, attributesToBeRemovedPromises, attributesToBeAddedPromises;
    return _regenerator.default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            _context3.prev = 0;
            _req$app$locals$orm = req.app.locals.orm, RelationshipType = _req$app$locals$orm.RelationshipType, RelationshipTypeAttributeType = _req$app$locals$orm.RelationshipTypeAttributeType, bookshelf = _req$app$locals$orm.bookshelf;
            _context3.next = 4;
            return bookshelf.transaction();
          case 4:
            trx = _context3.sent;
            if (req.params.id) {
              _context3.next = 12;
              break;
            }
            _context3.next = 8;
            return RelationshipType.forge();
          case 8:
            newRelationshipType = _context3.sent;
            method = 'insert';
            _context3.next = 16;
            break;
          case 12:
            _context3.next = 14;
            return RelationshipType.forge({
              id: parseInt(req.params.id, 10)
            }).fetch({
              require: true
            });
          case 14:
            newRelationshipType = _context3.sent;
            method = 'update';
          case 16:
            _req$body = req.body, attributeTypes = _req$body.attributeTypes, childOrder = _req$body.childOrder, deprecated = _req$body.deprecated, description = _req$body.description, label = _req$body.label, linkPhrase = _req$body.linkPhrase, oldAttributeTypes = _req$body.oldAttributeTypes, parentId = _req$body.parentId, reverseLinkPhrase = _req$body.reverseLinkPhrase, sourceEntityType = _req$body.sourceEntityType, targetEntityType = _req$body.targetEntityType;
            newRelationshipType.set('description', description);
            newRelationshipType.set('label', label);
            newRelationshipType.set('deprecated', deprecated);
            newRelationshipType.set('linkPhrase', linkPhrase);
            newRelationshipType.set('reverseLinkPhrase', reverseLinkPhrase);
            newRelationshipType.set('childOrder', childOrder);
            newRelationshipType.set('parentId', parentId);
            newRelationshipType.set('sourceEntityType', sourceEntityType);
            newRelationshipType.set('targetEntityType', targetEntityType);
            _context3.next = 28;
            return newRelationshipType.save(null, {
              method: method
            }, {
              transacting: trx
            });
          case 28:
            relationshipType = _context3.sent;
            // Attributes
            _getChangedAttributeT = getChangedAttributeTypes(oldAttributeTypes, attributeTypes), attributesToBeAdded = _getChangedAttributeT.attributesToBeAdded, attributesToBeRemoved = _getChangedAttributeT.attributesToBeRemoved;
            attributesToBeRemovedPromises = attributesToBeRemoved.map( /*#__PURE__*/function () {
              var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(attributeID) {
                return _regenerator.default.wrap(function _callee$(_context) {
                  while (1) {
                    switch (_context.prev = _context.next) {
                      case 0:
                        _context.next = 2;
                        return new RelationshipTypeAttributeType().query(function (qb) {
                          qb.where('relationship_type', newRelationshipType.id);
                          qb.where('attribute_type', attributeID);
                        }).destroy({
                          transacting: trx
                        });
                      case 2:
                      case "end":
                        return _context.stop();
                    }
                  }
                }, _callee);
              }));
              return function (_x6) {
                return _ref.apply(this, arguments);
              };
            }());
            _context3.next = 33;
            return Promise.all(attributesToBeRemovedPromises);
          case 33:
            attributesToBeAddedPromises = attributesToBeAdded.map( /*#__PURE__*/function () {
              var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(attributeID) {
                var newRelTypeAttrType;
                return _regenerator.default.wrap(function _callee2$(_context2) {
                  while (1) {
                    switch (_context2.prev = _context2.next) {
                      case 0:
                        _context2.next = 2;
                        return new RelationshipTypeAttributeType();
                      case 2:
                        newRelTypeAttrType = _context2.sent;
                        newRelTypeAttrType.set('relationshipType', relationshipType.id);
                        newRelTypeAttrType.set('attributeType', attributeID);
                        _context2.next = 7;
                        return newRelTypeAttrType.save(null, {
                          method: 'insert'
                        }, {
                          transacting: trx
                        });
                      case 7:
                      case "end":
                        return _context2.stop();
                    }
                  }
                }, _callee2);
              }));
              return function (_x7) {
                return _ref2.apply(this, arguments);
              };
            }());
            _context3.next = 36;
            return Promise.all(attributesToBeAddedPromises);
          case 36:
            _context3.next = 38;
            return trx.commit();
          case 38:
            return _context3.abrupt("return", res.send(relationshipType.toJSON()));
          case 41:
            _context3.prev = 41;
            _context3.t0 = _context3["catch"](0);
            return _context3.abrupt("return", next(_context3.t0));
          case 44:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3, null, [[0, 41]]);
  }));
  return _relationshipTypeCreateOrEditHandler.apply(this, arguments);
}
function identifierTypeCreateOrEditHandler(_x4, _x5) {
  return _identifierTypeCreateOrEditHandler.apply(this, arguments);
}
function _identifierTypeCreateOrEditHandler() {
  _identifierTypeCreateOrEditHandler = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(req, res) {
    var IdentifierType, newIdentifierType, method, _req$body2, childOrder, deprecated, description, detectionRegex, displayTemplate, entityType, label, parentId, validationRegex, identifierType;
    return _regenerator.default.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            _context4.prev = 0;
            IdentifierType = req.app.locals.orm.IdentifierType;
            if (req.params.id) {
              _context4.next = 9;
              break;
            }
            _context4.next = 5;
            return IdentifierType.forge();
          case 5:
            newIdentifierType = _context4.sent;
            method = 'insert';
            _context4.next = 13;
            break;
          case 9:
            _context4.next = 11;
            return IdentifierType.forge({
              id: parseInt(req.params.id, 10)
            }).fetch({
              require: true
            });
          case 11:
            newIdentifierType = _context4.sent;
            method = 'update';
          case 13:
            _req$body2 = req.body, childOrder = _req$body2.childOrder, deprecated = _req$body2.deprecated, description = _req$body2.description, detectionRegex = _req$body2.detectionRegex, displayTemplate = _req$body2.displayTemplate, entityType = _req$body2.entityType, label = _req$body2.label, parentId = _req$body2.parentId, validationRegex = _req$body2.validationRegex;
            newIdentifierType.set('description', description);
            newIdentifierType.set('label', label);
            newIdentifierType.set('deprecated', deprecated);
            newIdentifierType.set('detectionRegex', detectionRegex);
            newIdentifierType.set('displayTemplate', displayTemplate);
            newIdentifierType.set('childOrder', childOrder);
            newIdentifierType.set('parentId', parentId);
            newIdentifierType.set('entityType', entityType);
            newIdentifierType.set('validationRegex', validationRegex);
            _context4.next = 25;
            return newIdentifierType.save(null, {
              method: method
            });
          case 25:
            identifierType = _context4.sent;
            return _context4.abrupt("return", res.status(200).send(identifierType.toJSON()));
          case 29:
            _context4.prev = 29;
            _context4.t0 = _context4["catch"](0);
            return _context4.abrupt("return", error.sendErrorAsJSON(res, new error.SiteError('A problem occurred while saving the identifier type')));
          case 32:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4, null, [[0, 29]]);
  }));
  return _identifierTypeCreateOrEditHandler.apply(this, arguments);
}
//# sourceMappingURL=typeRouteUtils.js.map