/*! Copyright (c) 2022, XAPPmedia */ import { CrmResponse, ExternalLead } from "../Crm"; import { DateTime, DateTimeRange } from "../DateTime"; import { BusyDayDescription } from "../Form"; export type DayOfWeek = "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday" | "sunday"; export interface AvailabilityClass { /** * ID for the availability class, typically the slugged name. */ id: string; /** * Human readable name of the availability class */ name: string; /** * Summary of the availability class. This is used to describe the availability class so the AI can match it to user input. It contains high level description as well as examples of input that would match to this class. */ summary: string; /** * Minimum number of days out that can be scheduled for this type. * * This is independent of if the day is available or not. * * Value of 0 is the default if not provided, meaning same day could be available. */ numberOfDaysOut?: number; /** * Maximum number of appointments that can be scheduled per day for this type. * * If not provided, it is assumed to be unlimited. */ appointmentsPerDay?: number; /** * If true, this availability class is only for leads and not for appointments as they typically require more information and followup. */ leadOnly?: boolean; /** * The current day stays available *until* this time, in HH:MM (24-hour) format, wall-clock in the * business timezone. * * For example, "16:00" lets a job in this class still be booked for today up to 4:00 PM; at or after * 4:00 PM, today is no longer offered. Same meaning as BusyDayDescription.currentDayAvailableUntil, * which this overrides for jobs resolved to this class. * * Only meaningful when numberOfDaysOut is 0 or absent (i.e. same-day is possible at all). */ currentDayAvailableUntil?: string; } /** * A deterministic mapping from an FSM/CRM job type to an availability class. * * This is the symmetric counterpart to CrmServiceAvailabilitySettings.delayedJobTypes, which is * also keyed on the FSM job-type id: delayedJobTypes pushes a job type further out, this maps a * job type onto a class (which may pull it in — e.g. same-day for emergency work). */ export interface JobTypeAvailabilityClass { /** * The FSM/CRM job type id, stringified (ServiceTitan job type ids are numbers). */ jobTypeId: string; /** * AvailabilityClass.id */ classId: string; /** * Denormalized for display in Studio. Not used for matching. */ jobTypeName?: string; /** * Provenance: "AI" = suggested by the class suggester, "USER" = confirmed/edited by a human. */ source?: "AI" | "USER"; } /** * Settings for the availability of the CRM service. */ export interface CrmServiceAvailabilitySettings { /** * The days of the week they are available to schedule appointments through the scheduler. */ availableDays?: DayOfWeek[]; /** * These are holidays or any other days specific to the business that they are not available for appointments. */ blockedDays?: DateTime[]; /** * Maximum total number of appointments a day that can be scheduled through the scheduler. */ maxTotalDailyAppointments?: number; /** * Job types, where we only schedule a few days (3) ahead */ delayedJobTypes?: string[]; /** * The customer specific "availability classes" that describes the scheduling strategy for job types. */ availabilityClasses?: AvailabilityClass[]; /** * The default availability class (when the AI cannot figure it out) */ defaultAvailabilityClass?: string; /** * The default busy days for the business. This is used when the FSM/Scheduling backend does not provide busy days */ defaultBusyDays?: BusyDayDescription; /** * Deterministic job type -> class mapping. Beats the LLM's guess. */ jobTypeClasses?: JobTypeAvailabilityClass[]; /** * Forces every booking in this scope into the given class, regardless of job type. * * This is deliberately NOT defaultAvailabilityClass: that field is a *fallback* (it loses to * jobType.class), so it cannot express "force". Used by campaign booking widgets, which carry * their own availability settings. */ forceAvailabilityClass?: string; /** * The business's IANA timezone, for example "America/Chicago". * * Stamped by the platform from the business's location; wall-clock rules such as * BusyDayDescription.afterHoursNextDayBlockedUntil are evaluated in it. */ timezone?: string; } export interface CrmServiceTimeAvailability { /** * The time slot */ range: DateTimeRange; /** * If the time slot is available. */ available: boolean; } export interface CrmServiceDateAvailability { /** * Typically, just the date is used, tz and time are not needed. */ date: DateTime; /** * If the day has any availability for appointments. */ available: boolean; /** * The number of remaining available appointments. */ remainingAppointments?: number; /** * Blocked time slots for the day. * * These are only blocked times during their normal business hours. * * @note - This is only for future consideration. */ blockedTimeSlots?: CrmServiceTimeAvailability[]; /** * Every bookable window that day, open or full, in time order. * * Only set by CRMs offering live slots. */ timeSlots?: CrmServiceTimeAvailability[]; } export interface CrmServiceAvailability { /** * The range */ range: DateTimeRange; /** * Availability for each date in the range. * * If the date is not included in the array, it is assumed to be available. */ unavailabilities: CrmServiceDateAvailability[]; /** * The CRM will not take a scheduled booking now (request-only mode, or calendar * unreachable with fallback "hide"). * * The form should skip date/time selection. */ schedulingDisabled?: boolean; } /** * What the visitor has provided so far, as collected. */ export interface CrmServiceAvailabilityContact { /** * Full name as collected, if any. */ name?: string; phone?: string; email?: string; /** * Free-form street address as collected. */ address?: string; zip?: string; } export interface CrmServiceAvailabilityOptions extends CrmServiceAvailabilitySettings { /** * Job Type to filter availability by. * * This allows to display availability for a specific job type, which can be different. */ jobType?: CrmServiceJobType; /** * What the visitor has provided so far, for CRMs whose availability depends on who or where * the customer is (for example ServiceMinder slotsearch). Optional; CRMs that don't need it ignore it. */ contact?: CrmServiceAvailabilityContact; } export interface CrmServiceJobType { /** * The job type id. */ id: string; /** * The job type name. */ name?: string; class?: string; } export interface CrmService { /** * Send information about a lead to the CRM. * * This can be used to either create a lead or append data to an existing lead. * * The existing lead can be determined by the refId on the ExternalLead information or by attempting to match data such as email or phone number. * * @param externalLead Lead information * @param extras Optional additional metadata to pass to the CRM */ send(externalLead: ExternalLead, extras?: Record): Promise; /** * Updates a lead if the user provides more information after the lead has been sent. * * It leverages the refId on the externalLead, which is originally provided in the CrmResponse to properly * * @deprecated Use send with a refId on the externalLead and call send() again. This will update. * * @param externalLead * @param extras */ update?(externalLead: ExternalLead, extras?: Record): Promise; /** * Returns availability for scheduling an appointment with the business. * * @param range * @param options */ getAvailability(range: DateTimeRange, options?: CrmServiceAvailabilityOptions): Promise; /** * Returns the job type (id) for the free text job description (AI call usually) * * @param message * @param externalLead * @param options Availability settings in scope for this request (e.g. a widget's * forceAvailabilityClass or jobTypeClasses), needed to resolve the job type's class. * * Deliberately typed as the settings-only CrmServiceAvailabilitySettings rather than * CrmServiceAvailabilityOptions: this method *determines* the job type, so the `jobType` member of * the Options type would be meaningless (and misleading) here. */ getJobType?(message: string, externalLead?: ExternalLead, options?: CrmServiceAvailabilitySettings): Promise; /** * Lists the tenant's job types from the FSM. Backs a Studio picker. */ listJobTypes?(): Promise; /** * Returns a suggested job-type -> class map. Does not persist anything. * * @param availabilityClasses */ suggestJobTypeClasses?(availabilityClasses?: AvailabilityClass[]): Promise; } export type CrmServiceProps = CrmServiceAvailabilitySettings; export declare class AbstractCrmService implements CrmService { protected availableDays?: DayOfWeek[]; protected blockedDays?: DateTime[]; protected maxTotalDailyAppointments?: number | undefined; protected delayedJobTypes?: string[]; constructor(props: CrmServiceProps); send(externalLead: ExternalLead, extras?: Record): Promise; getAvailability(range: DateTimeRange, options?: CrmServiceAvailabilityOptions): Promise; getJobType(message: string, externalLead?: ExternalLead, options?: CrmServiceAvailabilitySettings): Promise; update?(externalLead: ExternalLead, extras?: Record): Promise; }