ACD Service

Learn how to use Nextiva's ACD service

Overview

The ACDService class manages automatic call distribution (ACD) offers. It handles the offer lifecycle, including accepting, rejecting, and tracking expirations. This service communicates with the backend API, maintains the local state of available offers, and broadcasts relevant events to subscribers.

Public Methods

accept

Accepts an offer with the specified ID.

public accept(offerId: string): Promise<any>

Parameters

  • offerId (string): The unique identifier of the offer to accept

Returns

  • Promise that resolves with the server response

Example

// Accept an incoming offer\
acdService.accept("offer-123")
.then(response => console.log("Offer accepted", response))
.catch(error => console.error("Failed to accept offer", error));

reject

Rejects an offer with specified reason information.

public reject({ offerId, status, statusId }: RejectOptions): Promise<any>

Parameters

  • RejectOptions object:
  • offerId (string): The unique identifier of the offer to reject
  • status (number, optional): A numeric status code representing the rejection reason
  • statusId (string, optional): A string identifier for the rejection status

Returns

  • Promise that resolves with the server response

Example

// Reject an offer with reason  
acdService.reject({
  offerId: "offer-123",
  status: 2, 
  statusId: "agent-unavailable"
})
  .then(response => console.log("Offer rejected", response))
  .catch(error => console.error("Failed to reject offer", error));

expired

Marks an offer as expired with optional status information.

public expired({ offerId, status, statusId }: RejectOptions): Promise<any>

Parameters

  • RejectOptions object:
  • offerId (string): The unique identifier of the offer to mark as expired
  • status (number, optional): A numeric status code representing the expiration reason
  • statusId (string, optional): A string identifier for the expiration status

Returns

  • Promise that resolves with the server response

Example

// Mark an offer as expired
acdService.expired({
  offerId: "offer-123",
  status: 3,
  statusId: "timeout"
})
  .then(response => console.log("Offer marked as expired", response))
  .catch(error => console.error("Failed to mark offer as expired", error));

Private Methods

handleOfferNotification

Sets up a subscription to InternalEvents.OfferNotification events. When a new offer notification is received:

  1. Logs the notification 2. Adds the offer to the state using addOffer() 3. Emits an ExternalEvents.OffersChangedNotification event with the updated list of offers
private handleOfferNotification(): void

handleOfferRemovedNotification

Sets up a subscription to multiple offer removal events.

private handleOfferRemovedNotification(): void
  • InternalEvents.OfferAcceptedNotification
  • InternalEvents.OfferExpiredNotification
  • InternalEvents.OfferRejectedNotification
  • InternalEvents.OfferTerminatedByHangupNotification

When any of these events are received:

    1. Logs the notification with its specific event name 2. Removes the corresponding offer using removeOffer() 3. Emits an ExternalEvents.OffersChangedNotification event with the updated list of offers

addOffer

Adds a new offer to the state.

private addOffer(offer: Offer): Offer\[]

Parameters

  • offer (Offer): The offer object to add

Returns

  • The updated array of offers

removeOffer

Removes an offer from the state by its ID.

private removeOffer(offerId: string): Offer\[]

Parameters

  • offerId (string): The ID of the offer to remove

Returns

  • The updated array of offers (with the specified offer removed)

Types

Offer

export interface Offer \\{  
  \_id: string;
  createdAt: number;
  modifiedAt: number;
  objectType: string;
  offerId: string;
  tenantId: string;
  users: string[];
  workitem: Workitem;
}

RejectOptions

export interface RejectOptions \\{  
  offerId: string;
  status?: number;
  statusId?: string;
}

Event Flow

  1. When the service receives an internal OfferNotification event, it:
    • Adds the offer to the local state
      • Broadcasts an OffersChangedNotification to external subscribers 2. When the service receives any offer removal event (accepted, expired, rejected, terminated), it:
        • Removes the offer from the local state
          • Broadcasts an OffersChangedNotification to external subscribers

Usage Example

// Initialize SDK (assuming container setup is handled elsewhere)\
const sdk = initializeSDK();


// Listen for offer changes\
sdk.events.on(ExternalEvents.OffersChangedNotification, (data) => \{\
console.log("Current offers:", data.offers);
// Update UI to show available offers
});

// Accept an offer\
function acceptOffer(offerId) \{\
sdk.acd.accept(offerId)
.then(() => console.log("Offer accepted successfully"))
.catch(error => console.error("Failed to accept offer", error));
}

// Reject an offer\
function rejectOffer(offerId) \{\
sdk.acd.reject(\{
offerId,
status: 1,
statusId: "agent-busy"
})
.then(() => console.log("Offer rejected successfully"))
.catch(error => console.error("Failed to reject offer", error));
}