UNPKG

service-titan-api

Version:

A module for authenticating and accessing the ServiceTitan API.

63 lines (58 loc) 2.44 kB
// src/appointments/rescheduleAppointment.mjs import axios from "axios"; /** * Reschedules a job appointment via the ServiceTitan API. * * @param {Object} options * @param {ServiceTitanAuth} options.auth - An instance of ServiceTitanAuth. * @param {string} options.appKey - Your ServiceTitan Application Key. * @param {number} options.tenant - Your Tenant ID. * @param {number} options.id - The ID of the appointment to reschedule. * @param {Object} options.body - The reschedule details. * @property {string} [start] - New start date/time (in UTC) for appointment. * @property {string} [end] - New end date/time (in UTC) for appointment. * @property {string} [arrivalWindowStart] - New arrival window start date/time (in UTC) for appointment, if configured. * @property {string} [arrivalWindowEnd] - New arrival window end date/time (in UTC) for appointment, if configured. * @returns {Promise<Object>} Parsed JSON response from the API representing the updated appointment. */ export async function rescheduleAppointments({ auth, appKey, tenant, id, body, }) { if (!auth) throw new Error("An instance of ServiceTitanAuth is required"); if (!appKey) throw new Error("appKey is required for resource API calls"); if (!tenant) throw new Error("tenant is required for API calls"); if (id == null) throw new Error("id is required for API calls"); if (!body || typeof body !== "object") { throw new Error("Request body is required and must be an object"); } const token = await auth.getAccessToken(); const baseUrl = auth.environment === "integration" ? `https://api-integration.servicetitan.io/jpm/v2/tenant/${tenant}/appointments/${id}/reschedule` : `https://api.servicetitan.io/jpm/v2/tenant/${tenant}/appointments/${id}/reschedule`; try { const response = await axios.post(baseUrl, body, { headers: { Authorization: token, "ST-App-Key": appKey, "Content-Type": "application/json", }, }); return response.data; } catch (error) { const status = error.response ? error.response.status : "Unknown status"; const statusText = error.response ? error.response.statusText : "Unknown error"; const errorData = error.response ? JSON.stringify(error.response.data) : error.message; throw new Error( `Error rescheduling appointment: ${status} ${statusText}: ${errorData}` ); } }