<commit_before>'use strict';

/**
 * This controller handles trigger requests from the Cumulus system that fires
 * when assets are updated, inserted or deleted.
 *
 * An example request is:
 *
 * req.body = {
 *   id: 'FHM-25757',
 *   action: 'asset-update',
 *   collection: 'Frihedsmuseet',
 *   apiKey: ''
 * }
 */

const ds = require('collections-online/lib/services/documents');
const config = require('collections-online/lib/config');

if(!config.es || !config.es.index || typeof(config.es.index) !== 'string') {
  throw new Error('Need exactly one index for Cumulus triggers to work.');
}

const indexing = require('../indexing/modes/run');

var helpers = {
  thousandsSeparator: function(number) {
    return number.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1.');
  }
};

function updateAsset(catalogAlias, assetId) {
  var state = {
    context: {
      index: config.es.index,
    },
    es: ds,
    mode: 'single',
    reference: catalogAlias + '/' + assetId
  };
  return indexing(state);
}

function deleteAsset(catalogAlias, assetId) {
  return ds.delete({
    index: config.es.index,
    type: 'asset',
    id: catalogAlias + '-' + assetId
  });
}

exports.asset = function(req, res, next) {
  const action = req.body.action || null;
  const catalogName = req.body.collection || null;
  let id = req.body.id || '';
  let catalogAlias = null;

  console.log('Index asset called with body: ', JSON.stringify(req.body));

  // If the catalog alias is not sat in the ID
  if(id.indexOf('-') > -1) {
    [catalogAlias, id] = id.split('-');
  } else if(catalogName) {
    // No slash in the id - the catalog should be read from .collection
    catalogAlias = Object.keys(config.cip.catalogs)
    .reduce(function(result, alias) {
      const candidateCatalogName = config.cip.catalogs[alias];
      if(candidateCatalogName === catalogName) {
        return alias;
      } else {
        return result;
      }
    }, null);
  }

  if (!catalogAlias) {
    throw new Error('Failed to determine catalog alias');
  }

  function success() {
    res.json({
      'success': true
    });
  }

  if (id && action) {
    if (action === 'asset-update') {
      updateAsset(catalogAlias, id).then(success, next);
    } else if (action === 'asset-create') {
      updateAsset(catalogAlias, id).then(success, next);
    } else if (action === 'asset-delete') {
      deleteAsset(catalogAlias, id).then(success, next);
    } else {
      next(new Error('Unexpected action from Cumulus: ' + action));
    }
  } else {
    var requestBody = JSON.stringify(req.body);
    next(new Error('Missing an id or an action, requested: ' + requestBody));<commit_msg>Succeed asset updates right away<commit_after>'use strict';

/**
 * This controller handles trigger requests from the Cumulus system that fires
 * when assets are updated, inserted or deleted.
 *
 * An example request is:
 *
 * req.body = {
 *   id: 'FHM-25757',
 *   action: 'asset-update',
 *   collection: 'Frihedsmuseet',
 *   apiKey: ''
 * }
 */

const ds = require('collections-online/lib/services/documents');
const config = require('collections-online/lib/config');

if(!config.es || !config.es.index || typeof(config.es.index) !== 'string') {
  throw new Error('Need exactly one index for Cumulus triggers to work.');
}

const indexing = require('../indexing/modes/run');

var helpers = {
  thousandsSeparator: function(number) {
    return number.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1.');
  }
};

function updateAsset(catalogAlias, assetId) {
  var state = {
    context: {
      index: config.es.index,
    },
    es: ds,
    mode: 'single',
    reference: catalogAlias + '/' + assetId
  };
  return indexing(state);
}

function deleteAsset(catalogAlias, assetId) {
  return ds.delete({
    index: config.es.index,
    type: 'asset',
    id: catalogAlias + '-' + assetId
  });
}

exports.asset = function(req, res, next) {
  const action = req.body.action || null;
  const catalogName = req.body.collection || null;
  let id = req.body.id || '';
  let catalogAlias = null;

  console.log('Index asset called with body: ', JSON.stringify(req.body));

  // If the catalog alias is not sat in the ID
  if(id.indexOf('-') > -1) {
    [catalogAlias, id] = id.split('-');
  } else if(catalogName) {
    // No slash in the id - the catalog should be read from .collection
    catalogAlias = Object.keys(config.cip.catalogs)
    .reduce(function(result, alias) {
      const candidateCatalogName = config.cip.catalogs[alias];
      if(candidateCatalogName === catalogName) {
        return alias;
      } else {
        return result;
      }
    }, null);
  }

  if (!catalogAlias) {
    throw new Error('Failed to determine catalog alias');
  }

  function success() {
    res.json({
      'success': true
    });
  }

  if (id && action) {
    if (action === 'asset-update') {
      updateAsset(catalogAlias, id).then(null, next);
      // Go succeed right away
      success();
    } else if (action === 'asset-create') {
      updateAsset(catalogAlias, id).then(success, next);
    } else if (action === 'asset-delete') {
      deleteAsset(catalogAlias, id).then(success, next);
    } else {
      next(new Error('Unexpected action from Cumulus: ' + action));
    }
  } else {
    var requestBody = JSON.stringify(req.body);
    next(new Error('Missing an id or an action, requested: ' + requestBody));
{'fraction_non_alphanumeric': 0.1570048309178744, 'fraction_numerical': 0.002601263470828688, 'mean_word_length': 3.0997715156131, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '726975', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1675, 'n_words': 442}