<commit_before>/**
 * @file
 * cancelOrder transformer.
 *
 * Wraps cancelOrder functionality of openorder backend.
 */
import {log} from '../utils';
import * as IsilUtils from './utils/isil.utils';

/**
 * Validate parameters
 *
 * @param {object} params parameters to validate
 * @throws if validation fails
 */
function validateParams(params) {
  if (!params.orderId) {
    throw ('missing orderId. Needed to cancel order');
  }
}

/**
 * Constructs soap request to perform cancelOrder request
 * @param {object} params Parameters to substitute into soap request
 * @returns soap request string
 */
function constructSoap(params) {

  let soap = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:open="http://oss.dbc.dk/ns/openuserstatus">
   <soapenv:Header/>
   <soapenv:Body>
      <open:cancelOrderRequest>
         <open:agencyId>${params.agencyId}</open:agencyId>
         <open:authentication>
            <open:groupIdAut>${params['authentication.groupIdAut']}</open:groupIdAut>
            <open:passwordAut>${params['authentication.passwordAut']}</open:passwordAut>
            <open:userIdAut>${params['authentication.userIdAut']}</open:userIdAut>
         </open:authentication>
         <open:cancelOrder>
            <open:orderId>${params['cancelOrder.orderId']}</open:orderId>
            <open:orderType>{params['cancelOrder.orderType']}</open:orderType>
         </open:cancelOrder>
         <open:outputType>json</open:outputType>
         <open:userId>${params.userId}</open:userId>
         <open:userPincode>${params.userPincode}</open:userPincode>
      </open:cancelOrderRequest>
   </soapenv:Body>
</soapenv:Envelope>`;

  return soap;
}

/**
 * Default transformer.
 * Wraps the cancelOrder functionality of openorder backend and
 * returns result of call
 *
 * @param {Object} request parameters from the user (no entries from this object is used)
 * @param {Object} context The context object fetched from smaug
 * @returns {Promise} with result
 * @api public
 */
export default (request, context) => {
  try {
    log.debug('Validating request');
    validateParams(request);
  } catch (err) { // eslint-disable-line brace-style
    return new Promise(resolve => {
      return resolve({statusCode: 400, error: err});
    });
  }

  let i = request.orderId.indexOf(':');
  let orderType = request.orderId.substring(0, i);
  let orderId = request.orderId.substring(i + 1);

  let params = {
    'cancelOrder.orderId': orderId,
    'cancelOrder.orderType': orderType,
    agencyId: IsilUtils.getIdFromIsil(context.get('user.isil', 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)
  };

  let soap = constructSoap(params);

  return context.call('openuserstatus', soap).then(body => {
    body = JSON.parse(body).cancelOrderResponse;

    if (body.cancelOrderStatus[0].cancelOrderError) {
      return {statusCode: 500, error: body.cancelOrderStatus[0].cancelOrderError.$};
    }
    if (body.cancelOrderStatus[0].orderCancelled) {
      return {statusCode: 200, data: {deleted: true, orderId: body.cancelOrderStatus[0].orderId.$}};
    }
    return {statusCode: 500, error: 'Unknown error occured'};
  });
};
<commit_msg>Issue #784: Test log user<commit_after>/**
 * @file
 * cancelOrder transformer.
 *
 * Wraps cancelOrder functionality of openorder backend.
 */
import {log} from '../utils';
import * as IsilUtils from './utils/isil.utils';

/**
 * Validate parameters
 *
 * @param {object} params parameters to validate
 * @throws if validation fails
 */
function validateParams(params) {
  if (!params.orderId) {
    throw ('missing orderId. Needed to cancel order');
  }
}

/**
 * Constructs soap request to perform cancelOrder request
 * @param {object} params Parameters to substitute into soap request
 * @returns soap request string
 */
function constructSoap(params) {

  let soap = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:open="http://oss.dbc.dk/ns/openuserstatus">
   <soapenv:Header/>
   <soapenv:Body>
      <open:cancelOrderRequest>
         <open:agencyId>${params.agencyId}</open:agencyId>
         <open:authentication>
            <open:groupIdAut>${params['authentication.groupIdAut']}</open:groupIdAut>
            <open:passwordAut>${params['authentication.passwordAut']}</open:passwordAut>
            <open:userIdAut>${params['authentication.userIdAut']}</open:userIdAut>
         </open:authentication>
         <open:cancelOrder>
            <open:orderId>${params['cancelOrder.orderId']}</open:orderId>
            <open:orderType>{params['cancelOrder.orderType']}</open:orderType>
         </open:cancelOrder>
         <open:outputType>json</open:outputType>
         <open:userId>${params.userId}</open:userId>
         <open:userPincode>${params.userPincode}</open:userPincode>
      </open:cancelOrderRequest>
   </soapenv:Body>
</soapenv:Envelope>`;

  return soap;
}

/**
 * Default transformer.
 * Wraps the cancelOrder functionality of openorder backend and
 * returns result of call
 *
 * @param {Object} request parameters from the user (no entries from this object is used)
 * @param {Object} context The context object fetched from smaug
 * @returns {Promise} with result
 * @api public
 */
export default (request, context) => {
  try {
    log.debug('Validating request');
    validateParams(request);
  } catch (err) { // eslint-disable-line brace-style
    return new Promise(resolve => {
      return resolve({statusCode: 400, error: err});
    });
  }

  let i = request.orderId.indexOf(':');
  let orderType = request.orderId.substring(0, i);
  let orderId = request.orderId.substring(i + 1);

  console.log('user', context.get('user'));
  let params = {
    'cancelOrder.orderId': orderId,
    'cancelOrder.orderType': orderType,
    agencyId: context.get('user.isil', 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)
  };

  let soap = constructSoap(params);

  return context.call('openuserstatus', soap).then(body => {
    body = JSON.parse(body).cancelOrderResponse;

    if (body.cancelOrderStatus[0].cancelOrderError) {
      return {statusCode: 500, error: body.cancelOrderStatus[0].cancelOrderError.$};
    }
    if (body.cancelOrderStatus[0].orderCancelled) {
      return {statusCode: 200, data: {deleted: true, orderId: body.cancelOrderStatus[0].orderId.$}};
    }
    return {statusCode: 500, error: 'Unknown error occured'};
  });
};

{'fraction_non_alphanumeric': 0.14745541539799914, 'fraction_numerical': 0.005654632448890822, 'mean_word_length': 3.9697406340057637, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '3722787', 'n_tokens_mistral': 2117, 'n_tokens_neox': 2079, 'n_words': 442}