server/helpers/merge.js

"use strict";

require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/es.weak-map.js");
require("core-js/modules/web.dom-collections.iterator.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.basicRelations = void 0;
exports.getEntityFetchPropertiesByType = getEntityFetchPropertiesByType;
exports.getEntitySectionByType = getEntitySectionByType;
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/web.dom-collections.for-each.js");
require("core-js/modules/es.object.assign.js");
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.function.name.js");
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _uniqBy2 = _interopRequireDefault(require("lodash/uniqBy"));
var _flatMap2 = _interopRequireDefault(require("lodash/flatMap"));
var _isNil2 = _interopRequireDefault(require("lodash/isNil"));
var _get2 = _interopRequireDefault(require("lodash/get"));
var entityRoutes = _interopRequireWildcard(require("../routes/entity/entity"));
var utils = _interopRequireWildcard(require("./utils"));
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; }
var basicRelations = ['aliasSet.aliases.language', 'annotation.lastRevision', 'defaultAlias', 'disambiguation', 'identifierSet.identifiers.type', 'relationshipSet', 'revision.revision'];
exports.basicRelations = basicRelations;
function getEntityFetchPropertiesByType(entityType) {
  switch (entityType) {
    case 'Author':
      return ['authorType', 'beginArea', 'endArea', 'gender'];
    case 'Edition':
      return ['authorCredit.names.author.defaultAlias', 'editionGroup.defaultAlias', 'languageSet.languages', 'editionFormat', 'editionStatus', 'releaseEventSet.releaseEvents', 'publisherSet.publishers.defaultAlias'];
    case 'EditionGroup':
      return ['authorCredit.names.author.defaultAlias', 'editionGroupType', 'editions.defaultAlias', 'editions.disambiguation', 'editions.releaseEventSet.releaseEvents', 'editions.identifierSet.identifiers.type', 'editions.editionFormat'];
    case 'Publisher':
      return ['publisherType', 'area'];
    case 'Work':
      return ['languageSet.languages', 'workType'];
    case 'Series':
      return ['seriesOrderingType'];
    default:
      return [];
  }
}

/**
 * @param {object} targetObject - Object to modify
 * @param {string} propName - Property name to set on the targetObject
 * @param {object} sourceObject - Source object to copy from
 * @param {string|string[]} [sourcePath=propName] - Path of the property to check on the sourceObject.
 * @description Mutates the targetObject, setting the value at propName if it isn't already set.
 * Accepts an optional path string or array of strings to allow get the source value at another path
 */
function assignIfNotSet(targetObject, propName, sourceObject) {
  var sourcePath = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : propName;
  var sourceValue = (0, _get2.default)(sourceObject, sourcePath);
  if ((0, _isNil2.default)(targetObject[propName]) && !(0, _isNil2.default)(sourceValue)) {
    targetObject[propName] = sourceValue;
  }
}

/**
 * @name getAuthorEntityMergeSection
 * @description Returns the initial form state for the Author merging page, based on the multiple entities.
 * The returned section has some properties transformed to a state acceptable by the reducer.
 * @param {object[]} entities - The array of entities to merge the properties of
 * @returns {object} - The Author merge section for the initialState
 */
function getAuthorEntityMergeSection(entities) {
  var authorSection = {};
  entities.forEach(function (entity) {
    assignIfNotSet(authorSection, 'type', entity, 'authorType.id');
    assignIfNotSet(authorSection, 'gender', entity, 'gender.id');
    assignIfNotSet(authorSection, 'beginDate', entity);
    assignIfNotSet(authorSection, 'endDate', entity);
    assignIfNotSet(authorSection, 'beginArea', entity);
    assignIfNotSet(authorSection, 'endArea', entity);
    assignIfNotSet(authorSection, 'ended', entity);
  });
  return Object.assign(authorSection, {
    beginArea: entityRoutes.areaToOption(authorSection.beginArea),
    endArea: entityRoutes.areaToOption(authorSection.endArea),
    /** If one of the entities has an end date or area and another doesn't,
     * those endDate/Area properties will be automatically selected as the only option on the merge display page
     * We want to emulate this for the initialState to match, so if there's any endDate/Are, set ended to true
     */
    ended: !(0, _isNil2.default)(authorSection.endArea) || !(0, _isNil2.default)(authorSection.endDate) || authorSection.ended
  });
}

/**
 * @name getEditionEntityMergeSection
 * @description Returns the initial form state for the Edition merging page, based on the multiple entities.
 * The returned section has some properties transformed to a state acceptable by the reducer.
 * @param {object[]} entities - The array of entities to merge the properties of
 * @returns {object} - The Edition merge section for the initialState
 */
function getEditionEntityMergeSection(entities) {
  var editionSection = {};
  entities.forEach(function (entity) {
    assignIfNotSet(editionSection, 'authorCredit', entity);
    assignIfNotSet(editionSection, 'depth', entity);
    assignIfNotSet(editionSection, 'editionGroup', entity);
    assignIfNotSet(editionSection, 'format', entity, 'editionFormat.id');
    assignIfNotSet(editionSection, 'height', entity);
    assignIfNotSet(editionSection, 'pages', entity);
    assignIfNotSet(editionSection, 'publisher', entity, 'publisherSet.publishers[0]');
    assignIfNotSet(editionSection, 'releaseDate', entity, 'releaseEventSet.releaseEvents[0].date');
    assignIfNotSet(editionSection, 'status', entity, 'editionStatus.id');
    assignIfNotSet(editionSection, 'weight', entity);
    assignIfNotSet(editionSection, 'width', entity);
  });
  var languages = (0, _flatMap2.default)(entities, function (entity) {
    return (0, _get2.default)(entity, 'languageSet.languages', []).map(function (_ref) {
      var id = _ref.id,
        name = _ref.name;
      return {
        label: name,
        value: id
      };
    });
  });
  return Object.assign(editionSection, {
    editionGroup: utils.entityToOption(editionSection.editionGroup),
    languages: (0, _uniqBy2.default)(languages, 'value'),
    publisher: utils.entityToOption(editionSection.publisher)
  });
}

/**
 * @name getEditionGroupEntityMergeSection
 * @description Returns the initial form state for the Edition Group merging page, based on the multiple entities.
 * The returned section has some properties transformed to a state acceptable by the reducer.
 * @param {object[]} entities - The array of entities to merge the properties of
 * @returns {object} - The Edition Group merge section for the initialState
 */
function getEditionGroupEntityMergeSection(entities) {
  var editionGroupSection = {
    editions: []
  };
  entities.forEach(function (entity) {
    var _editionGroupSection$;
    assignIfNotSet(editionGroupSection, 'type', entity, 'typeId');
    (_editionGroupSection$ = editionGroupSection.editions).push.apply(_editionGroupSection$, (0, _toConsumableArray2.default)(entity.editions));
  });
  return editionGroupSection;
}

/**
 * @name getPublisherEntityMergeSection
 * @description Returns the initial form state for the Publisher merging page, based on the multiple entities.
 * The returned section has some properties transformed to a state acceptable by the reducer.
 * @param {object[]} entities - The array of entities to merge the properties of
 * @returns {object} - The Publisher merge section for the initialState
 */
function getPublisherEntityMergeSection(entities) {
  var publisherSection = {};
  entities.forEach(function (entity) {
    assignIfNotSet(publisherSection, 'area', entity);
    assignIfNotSet(publisherSection, 'beginDate', entity);
    assignIfNotSet(publisherSection, 'endDate', entity);
    assignIfNotSet(publisherSection, 'ended', entity);
    assignIfNotSet(publisherSection, 'type', entity, 'typeId');
  });
  return Object.assign(publisherSection, {
    area: entityRoutes.areaToOption(publisherSection.area),
    /** If one of the entities has an end date and another doesn't,
     * that endDate property will be automatically selected as the only option on the merge display page
     * We want to emulate this for the initialState to match, so if there's any endDate, set ended to true
     */
    ended: !(0, _isNil2.default)(publisherSection.endDate) || publisherSection.ended
  });
}

/**
 * @name getSeriesEntityMergeSection
 * @description Returns the initial form state for the Series merging page, based on the multiple entities.
 * The returned section has some properties transformed to a state acceptable by the reducer.
 * @param {object[]} entities - The array of entities to merge the properties of
 * @returns {object} - The Series merge section for the initialState
 */
function getSeriesEntityMergeSection(entities) {
  var seriesSection = {};
  entities.forEach(function (entity) {
    assignIfNotSet(seriesSection, 'seriesType', entity, 'entityType');
    assignIfNotSet(seriesSection, 'orderType', entity, 'seriesOrderingType.id');
  });
  seriesSection.seriesItems = {};
  return seriesSection;
}

/**
 * @name getWorkEntityMergeSection
 * @description Returns the initial form state for the Work merging page, based on the multiple entities.
 * The returned section has some properties transformed to a state acceptable by the reducer.
 * @param {object[]} entities - The array of entities to merge the properties of
 * @returns {object} - The Work merge section for the initialState
 */
function getWorkEntityMergeSection(entities) {
  var workSection = {};
  entities.forEach(function (entity) {
    assignIfNotSet(workSection, 'type', entity, 'typeId');
  });
  var languages = (0, _flatMap2.default)(entities, function (entity) {
    return (0, _get2.default)(entity, 'languageSet.languages', []).map(function (_ref2) {
      var id = _ref2.id,
        name = _ref2.name;
      return {
        label: name,
        value: id
      };
    });
  });
  return Object.assign(workSection, {
    languages: (0, _uniqBy2.default)(languages, 'value')
  });
}

/**
 * @description Returns the initial form state for the $entity$ section depending on the entity type
 * @param {string} entityType - Entity type string (lowercased)
 * @param {object[]} entities - Array of entities to merge
 * @returns {object} - The entity section initial form state
 * @throws Will throw an error if the entityType is not one of the know entity types, lowercased.
 */
function getEntitySectionByType(entityType, entities) {
  switch (entityType) {
    case 'author':
      return getAuthorEntityMergeSection(entities);
    case 'edition':
      return getEditionEntityMergeSection(entities);
    case 'editionGroup':
      return getEditionGroupEntityMergeSection(entities);
    case 'publisher':
      return getPublisherEntityMergeSection(entities);
    case 'series':
      return getSeriesEntityMergeSection(entities);
    case 'work':
      return getWorkEntityMergeSection(entities);
    default:
      throw new Error("Invalid entity type: '".concat(entityType, "'"));
  }
}
//# sourceMappingURL=merge.js.map