server/helpers/typeRouteUtils.js

"use strict";

require("core-js/modules/es.object.get-own-property-descriptor.js");
require("core-js/modules/es.weak-map.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
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.symbol.js");
require("core-js/modules/es.symbol.description.js");
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.array.includes.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.date.to-json.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.parse-int.js");
require("core-js/modules/es.promise.js");
require("core-js/modules/es.string.includes.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var error = _interopRequireWildcard(require("../../common/helpers/error"));
var cache;
function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var r, c, a = Object.defineProperty, n = { __proto__: null, default: e }; if (Object(e) !== e) return n; if (cache || "function" != typeof WeakMap || (cache = new WeakMap()), cache) { if (cache.has(e)) return cache.get(e); cache.set(e, n); } for (c in e) "default" !== c && {}.hasOwnProperty.call(e, c) && ((r = a && Object.getOwnPropertyDescriptor(e, c)) && (r.get || r.set) ? a(n, c, r) : n[c] = e[c]); return n; }
/*
 * 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, _t;
    return _regenerator.default.wrap(function (_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 = 1;
          return bookshelf.transaction();
        case 1:
          trx = _context3.sent;
          if (req.params.id) {
            _context3.next = 3;
            break;
          }
          _context3.next = 2;
          return RelationshipType.forge();
        case 2:
          newRelationshipType = _context3.sent;
          method = 'insert';
          _context3.next = 5;
          break;
        case 3:
          _context3.next = 4;
          return RelationshipType.forge({
            id: parseInt(req.params.id, 10)
          }).fetch({
            require: true
          });
        case 4:
          newRelationshipType = _context3.sent;
          method = 'update';
        case 5:
          _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 = 6;
          return newRelationshipType.save(null, {
            method: method
          }, {
            transacting: trx
          });
        case 6:
          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 (_context) {
                while (1) switch (_context.prev = _context.next) {
                  case 0:
                    _context.next = 1;
                    return new RelationshipTypeAttributeType().query(function (qb) {
                      qb.where('relationship_type', newRelationshipType.id);
                      qb.where('attribute_type', attributeID);
                    }).destroy({
                      transacting: trx
                    });
                  case 1:
                  case "end":
                    return _context.stop();
                }
              }, _callee);
            }));
            return function (_x6) {
              return _ref.apply(this, arguments);
            };
          }());
          _context3.next = 7;
          return Promise.all(attributesToBeRemovedPromises);
        case 7:
          attributesToBeAddedPromises = attributesToBeAdded.map(/*#__PURE__*/function () {
            var _ref2 = (0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee2(attributeID) {
              var newRelTypeAttrType;
              return _regenerator.default.wrap(function (_context2) {
                while (1) switch (_context2.prev = _context2.next) {
                  case 0:
                    _context2.next = 1;
                    return new RelationshipTypeAttributeType();
                  case 1:
                    newRelTypeAttrType = _context2.sent;
                    newRelTypeAttrType.set('relationshipType', relationshipType.id);
                    newRelTypeAttrType.set('attributeType', attributeID);
                    _context2.next = 2;
                    return newRelTypeAttrType.save(null, {
                      method: 'insert'
                    }, {
                      transacting: trx
                    });
                  case 2:
                  case "end":
                    return _context2.stop();
                }
              }, _callee2);
            }));
            return function (_x7) {
              return _ref2.apply(this, arguments);
            };
          }());
          _context3.next = 8;
          return Promise.all(attributesToBeAddedPromises);
        case 8:
          _context3.next = 9;
          return trx.commit();
        case 9:
          return _context3.abrupt("return", res.send(relationshipType.toJSON()));
        case 10:
          _context3.prev = 10;
          _t = _context3["catch"](0);
          return _context3.abrupt("return", next(_t));
        case 11:
        case "end":
          return _context3.stop();
      }
    }, _callee3, null, [[0, 10]]);
  }));
  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, _t2;
    return _regenerator.default.wrap(function (_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 = 2;
            break;
          }
          _context4.next = 1;
          return IdentifierType.forge();
        case 1:
          newIdentifierType = _context4.sent;
          method = 'insert';
          _context4.next = 4;
          break;
        case 2:
          _context4.next = 3;
          return IdentifierType.forge({
            id: parseInt(req.params.id, 10)
          }).fetch({
            require: true
          });
        case 3:
          newIdentifierType = _context4.sent;
          method = 'update';
        case 4:
          _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 = 5;
          return newIdentifierType.save(null, {
            method: method
          });
        case 5:
          identifierType = _context4.sent;
          return _context4.abrupt("return", res.status(200).send(identifierType.toJSON()));
        case 6:
          _context4.prev = 6;
          _t2 = _context4["catch"](0);
          return _context4.abrupt("return", error.sendErrorAsJSON(res, new error.SiteError('A problem occurred while saving the identifier type')));
        case 7:
        case "end":
          return _context4.stop();
      }
    }, _callee4, null, [[0, 6]]);
  }));
  return _identifierTypeCreateOrEditHandler.apply(this, arguments);
}
//# sourceMappingURL=typeRouteUtils.js.map