<commit_before>/**
 * @file
 * User transformer.
 *
 * Wraps userstatus backend.
 */
import {pbkdf2} from 'crypto';

/**
 * Maps loan item from backend response to serviceprovider api
 * @param {Object} loanItem
 * @returns {Object} response with mapped keys
 */
function loan(loanItem) {

  const result = {
    loanId: loanItem.loanId.$,
    dueDate: loanItem.dateDue.$,
    title: loanItem.title.$
  };

  if (loanItem.author) {
    result.creator = loanItem.author.$;
  }

  if (loanItem.bibliographicItemId) {
    result.materialId = loanItem.bibliographicItemId.$;
  }

  if (loanItem.bibliographicRecordId) {
    result.titleId = loanItem.bibliographicRecordId.$;
  }

  return result;
}

/**
 * Maps debt item from backend response to serviceprovider api
 * @param {Object} debtItem openuserstatus orders response
 * @returns {Object} response with mapped keys
 */
function debt(debtItem) {

  const result = {
    amount: debtItem.fiscalTransactionAmount.$,
    currency: debtItem.fiscalTransactionCurrency.$,
    date: debtItem.fiscalTransactionDate.$.split('T')[0],
    title: debtItem.title.$
  };

  if (debtItem.author) {
    result.creator = debtItem.author.$;
  }
  return result;
}

/**
 * Maps order item from backend response to serviceprovider api
 * @param {Object} orderItem openuserstatus orders response
 * @returns {Object} response with mapped keys
 */
function order(orderItem) {
  const result = {
    orderId: `${orderItem.orderType.$}:${orderItem.orderId.$}`,
    status: orderItem.orderStatus.$,
    library: orderItem.pickUpAgency.$
  };

  const orderFields = ['holdQueuePosition', 'creator', 'title', 'orderDate', 'pickUpExpiryDate', 'pickUpId'];

  orderFields.forEach(key => {
    if (key === 'creator' && orderItem.author && orderItem.author.$) {
      result[key] = orderItem.author.$;
    }
    else if (orderItem[key] && orderItem[key].$) {
      result[key] = orderItem[key].$;
    }
  });

  return result;
}

/**
 * Default transformer.
 * Wraps openuserstatus backend and returns user info
 *
 * @param {Object} request parameters from the user (no entries from this object is used)
 * @param {Object} context The context object fetched from smaug
 * @returns {Object|Promise} promise with result
 * @api public
 */
export default (request, context) => {

  if (!(context.get('user.id') && context.get('user.pin'))) {
    return Promise.resolve({
      statusCode: 403,
      error: 'not logged in'
    });
  }

  const params = {
    agencyId: context.get('user.agency', true),
    userId: context.get('user.id'),
    userPincode: context.get('user.pin'),
    'authentication.groupIdAut': context.get('netpunkt.group', true),
    'authentication.passwordAut': context.get('netpunkt.password', true),
    'authentication.userIdAut': context.get('netpunkt.user', true),
    action: 'getUserStatus',
    outputType: 'json'
  };

  const idPromise = new Promise((resolve, reject) =>
    pbkdf2(context.get('user.agency', true).replace(/^DK-/, '') + ' ' + context.get('user.id'),
      context.get('user.salt'), 100000, 24, 'sha512', (err, key) => err ? reject(err) : resolve(key)));

  return context.call('openuserstatus', params).then(body => idPromise.then(id => {
    if (body.data.getUserStatusResponse.getUserStatusError) {
      return Promise.resolve({
        statusCode: 500,
        error: body.data.getUserStatusResponse.getUserStatusError.$
      });
    }

    let loans = [];
    if (body.data.getUserStatusResponse.userStatus.loanedItems.loan) {
      loans = body.data.getUserStatusResponse.userStatus.loanedItems.loan;
    }
    let orders = [];
    if (body.data.getUserStatusResponse.userStatus.orderedItems.order) {
      orders = body.data.getUserStatusResponse.userStatus.orderedItems.order;
    }

    let debts = [];
    if (body.data.getUserStatusResponse.userStatus.fiscalAccount) {
      debts = body.data.getUserStatusResponse.userStatus.fiscalAccount.fiscalTransaction || [];
    }

    const data = {
      id: id.toString('base64'),
      loans: loans.map(loan),
      orders: orders.map(order),
      debt: debts.map(debt)
    };

    if (context.get('services.ddbcmsapi')) {
      data.ddbcmsapi = context.get('services.ddbcmsapi');
    }

    return Promise.resolve({
      statusCode: 200,
      data: data
    });
  }));<commit_msg>added materialId / titleId to response<commit_after>/**
 * @file
 * User transformer.
 *
 * Wraps userstatus backend.
 */
import {pbkdf2} from 'crypto';

/**
 * Maps loan item from backend response to serviceprovider api
 * @param {Object} loanItem
 * @returns {Object} response with mapped keys
 */
function loan(loanItem) {

  const result = {
    loanId: loanItem.loanId.$,
    dueDate: loanItem.dateDue.$,
    title: loanItem.title.$
  };

  if (loanItem.author) {
    result.creator = loanItem.author.$;
  }

  if (loanItem.bibliographicItemId) {
    result.materialId = loanItem.bibliographicItemId.$;
  }

  if (loanItem.bibliographicRecordId) {
    result.titleId = loanItem.bibliographicRecordId.$;
  }

  return result;
}

/**
 * Maps debt item from backend response to serviceprovider api
 * @param {Object} debtItem openuserstatus orders response
 * @returns {Object} response with mapped keys
 */
function debt(debtItem) {
  const result = {
    amount: debtItem.fiscalTransactionAmount.$,
    currency: debtItem.fiscalTransactionCurrency.$,
    date: debtItem.fiscalTransactionDate.$.split('T')[0],
    title: debtItem.title.$
  };

  if (debtItem.author) {
    result.creator = debtItem.author.$;
  }
  return result;
}

/**
 * Maps order item from backend response to serviceprovider api
 * @param {Object} orderItem openuserstatus orders response
 * @returns {Object} response with mapped keys
 */
function order(orderItem) {
  const result = {
    orderId: `${orderItem.orderType.$}:${orderItem.orderId.$}`,
    status: orderItem.orderStatus.$,
    library: orderItem.pickUpAgency.$
  };

  const orderFields = ['holdQueuePosition', 'creator', 'title', 'orderDate', 'pickUpExpiryDate', 'pickUpId', 'titleId'];

  orderFields.forEach(key => {
    if (key === 'creator' && orderItem.author && orderItem.author.$) {
      result[key] = orderItem.author.$;
    }
    else if (key === 'titleId' && orderItem.bibliographicRecordId && orderItem.bibliographicRecordId.$) {
      result[key] = orderItem.bibliographicRecordId.$;
    }
    else if (orderItem[key] && orderItem[key].$) {
      result[key] = orderItem[key].$;
    }
  });

  return result;
}

/**
 * Default transformer.
 * Wraps openuserstatus backend and returns user info
 *
 * @param {Object} request parameters from the user (no entries from this object is used)
 * @param {Object} context The context object fetched from smaug
 * @returns {Object|Promise} promise with result
 * @api public
 */
export default (request, context) => {

  if (!(context.get('user.id') && context.get('user.pin'))) {
    return Promise.resolve({
      statusCode: 403,
      error: 'not logged in'
    });
  }

  const params = {
    agencyId: context.get('user.agency', true),
    userId: context.get('user.id'),
    userPincode: context.get('user.pin'),
    'authentication.groupIdAut': context.get('netpunkt.group', true),
    'authentication.passwordAut': context.get('netpunkt.password', true),
    'authentication.userIdAut': context.get('netpunkt.user', true),
    action: 'getUserStatus',
    outputType: 'json'
  };

  const idPromise = new Promise((resolve, reject) =>
    pbkdf2(context.get('user.agency', true).replace(/^DK-/, '') + ' ' + context.get('user.id'),
      context.get('user.salt'), 100000, 24, 'sha512', (err, key) => err ? reject(err) : resolve(key)));

  return context.call('openuserstatus', params).then(body => idPromise.then(id => {
    if (body.data.getUserStatusResponse.getUserStatusError) {
      return Promise.resolve({
        statusCode: 500,
        error: body.data.getUserStatusResponse.getUserStatusError.$
      });
    }

    let loans = [];
    if (body.data.getUserStatusResponse.userStatus.loanedItems.loan) {
      loans = body.data.getUserStatusResponse.userStatus.loanedItems.loan;
    }
    let orders = [];
    if (body.data.getUserStatusResponse.userStatus.orderedItems.order) {
      orders = body.data.getUserStatusResponse.userStatus.orderedItems.order;
    }

    let debts = [];
    console.log(body.data.getUserStatusResponse.userStatus);
    if (body.data.getUserStatusResponse.userStatus.fiscalAccount) {
      debts = body.data.getUserStatusResponse.userStatus.fiscalAccount.fiscalTransaction || [];
    }

    const data = {
      id: id.toString('base64'),
      loans: loans.map(loan),
      orders: orders.map(order),
      debt: debts.map(debt)
    };

    if (context.get('services.ddbcmsapi')) {
      data.ddbcmsapi = context.get('services.ddbcmsapi');
    }

    return Promise.resolve({
      statusCode: 200,
      data: data
    });
  }));
{'fraction_non_alphanumeric': 0.14488348530901723, 'fraction_numerical': 0.005628729032984352, 'mean_word_length': 3.544245524296675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '3795077', 'n_tokens_mistral': 2867, 'n_tokens_neox': 2759, 'n_words': 594}