All files / lib/bookings index.js

100% Statements 15/15
100% Branches 7/7
100% Functions 5/5
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 482x     20x 2x       18x         5x   5x     20x 20x   5x 5x           5x       1x         1x     1x           2x  
const validate = require('./schema');
 
function parseDate(params, key) {
  if (params[key] && params[key].relative) {
    return params[key].relative === 'before'
      ? `<${params[key].date}`
      : `>${params[key].date}`;
  }
  return params[key];
}
 
function Bookings(rq) {
  async function list(params = {}) {
    validate('list', params);
 
    const parsedDates = [
      'start_date', 'end_date', 'created_date', 'last_modified',
    ].reduce((p, k) => {
      p[k] = parseDate(params, k); // eslint-disable-line no-param-reassign
      return p;
    }, {});
    const qs = { ...params, ...parsedDates };
    const res = await rq({
      method: 'GET',
      route: 'booking',
      qs,
    });
 
    return Object.values(res['booking/index']);
  }
 
  async function retrieve(bookingId) {
    const res = await rq({
      method: 'GET',
      route: `booking/${bookingId}`,
    });
 
    return res.booking;
  }
 
  return {
    list,
    retrieve,
  };
}
 
module.exports = Bookings;