method/editBooking.js

/**
 * @module method/editBooking
 *
 * @requires method/getUserToken
 */

const functions = require('firebase-functions');
const axios = require('axios');
const {simplybook} = functions.config();

const {has} = require('lodash');

const getUserToken = require('./getUserToken');

/**
 * Edit an existing booking record
 *
 * @param  {number} bookingID - Booking ID of the booking record.
 * @param  {number} eventID - ID of the service used for the booking record.
 * @param  {number} performerID - ID of the service provider used for the booking record.
 * @param  {number} clientID - ID of the client the booking record was under.
 * @param  {string} date - The new date of the booking record, e.g. '2020-03-06'.
 * @param  {string} startTime - The new booking start time of the booking record, e.g. '15:30:00'.
 *
 * @return {object} An object containing data related to the booking record.
 */
const editBooking = async (bookingID, eventID, performerID, clientID, date, startTime) => {
  return axios({
    method: 'post',
    url: `${simplybook.url}/admin`,
    headers: {
      'X-Company-Login': simplybook.company,
      'X-User-Token': await getUserToken(),
    },
    data: {
      method: 'editBook',
      params: [
        bookingID,
        eventID,
        performerID,
        clientID,
        date,
        startTime,
        null,
        null,
        0,
      ],
      id: 0,
    }
  }).then(({data}) => {
    // Handle error
    if (has(data, 'error')) {
      throw new Error(data.error.message);
    }

    // Return token
    return data.result;
  }).catch((error) => {
    throw new functions.https.HttpsError(
      'unimplemented',
      error.message,
    );
  });
};

module.exports = editBooking;