server/util/parsers.js

/**
 * removeFakeId
 * removes fake ids
 * @param item
 * @returns {*}
 */
function removeFakeId(item) {
  const updated = item;
  if (String(updated.id || '').indexOf('_fake') !== -1) {
    delete updated.id;
  }
  return updated;
}

/**
 * parseProduct
 * prepare product to validation
 * @param context
 * @returns {*}
 */
function parseProduct(context) {
  const hook = context;
  Object.assign(hook.data, {
    attributes: (hook.data.attributes || [])
      .map(attribute => Object.assign(removeFakeId(attribute), {
        values: (attribute.values || []).map(removeFakeId),
    })),
    options: (hook.data.options || []).map(option => Object.assign(removeFakeId(option), {
        'option-value': (option['option-value'] || []).map((item) => {
          let updated = item;
          updated = removeFakeId(updated);
          delete updated.description;
          return updated;
        }),
      })),
    images: (hook.data.images || []).map(removeFakeId),
    discounts: (hook.data.discounts || []).map(removeFakeId),
    reward: (hook.data.reward || []).map(removeFakeId),
    specials: (hook.data.specials || []).map(removeFakeId),
  });
  return hook;
}

/**
 * parseReturn
 * prepare return to validation
 * @param context
 * @returns {*}
 */
function parseReturn(context) {
  const hook = context;
  Object.assign(hook.data, {
    histories: (hook.data.histories || []).map(removeFakeId).map((el) => {
      const history = el;
      delete history.status;
      return history;
    }),
  });
  return hook;
}

/**
 * parseCustomer
 * prepare customer to validation
 * @param context
 * @returns {*}
 */
function parseCustomer(context) {
  const hook = context;
  Object.assign(hook.data, {
    addresses: (hook.data.addresses || []).map(removeFakeId).map((el) => {
      const address = el;
      delete address.country;
      delete address.zone;
      return address;
    }),
    histories: (hook.data.histories || []).map(removeFakeId),
    transactions: (hook.data.transactions || []).map(removeFakeId),
    rewards: (hook.data.rewards || []).map(removeFakeId),
  });
  return hook;
}

module.exports = {
  parseProduct,
  parseReturn,
  parseCustomer,
};