service-titan-api
Version:
A module for authenticating and accessing the ServiceTitan API.
53 lines (48 loc) • 2.12 kB
JavaScript
// src/appointmentAssignments/unassignTechnicians.mjs
import axios from "axios";
/**
* Unassigns a list of technicians from an 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 {Object} options.body - The request body containing:
* @property {number} jobAppointmentId - Id of the appointment to unassign from.
* @property {number[]} technicianIds - Array of technician IDs to unassign.
* @returns {Promise<Object>} Parsed JSON response from the API representing the updated appointment.
*/
export async function unassignAppointments({ auth, appKey, tenant, 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 (!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/dispatch/v2/tenant/${tenant}/appointment-assignments/unassign-technicians`
: `https://api.servicetitan.io/dispatch/v2/tenant/${tenant}/appointment-assignments/unassign-technicians`;
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 unassigning technicians: ${status} ${statusText}: ${errorData}`
);
}
}