diff --git a/models/bxml/verbs/Refer.ts b/models/bxml/verbs/Refer.ts new file mode 100644 index 0000000..71ec247 --- /dev/null +++ b/models/bxml/verbs/Refer.ts @@ -0,0 +1,38 @@ +import { NestableVerb } from '../NestableVerb'; +import { SipUri } from './SipUri'; + +export interface ReferAttributes { + referCompleteUrl?: string; + referCompleteMethod?: string; + tag?: string; +} + +/** + * @export + * @class Refer + * @extends {NestableVerb} + * Represents a Refer BXML verb. + * NOTE: On success the call is terminated — the remote SIP endpoint redirects away from Bandwidth. + * Recovery BXML in referCompleteUrl only makes sense for failure handling. + */ +export class Refer extends NestableVerb { + attributes: ReferAttributes; + + /** + * Creates an instance of Refer + * @param {ReferAttributes} attributes The attributes to add to the element + * @param {SipUri} sipUri The SipUri child element (required for a valid Refer) + */ + constructor(attributes?: ReferAttributes, sipUri?: SipUri) { + super('Refer', undefined, attributes, sipUri ? [sipUri] : undefined); + } + + /** + * Set the SipUri for this Refer verb + * @param {SipUri} sipUri The SipUri to refer to + */ + setSipUri(sipUri: SipUri): void { + this.nestedVerbs = [sipUri]; + } +} + diff --git a/models/bxml/verbs/index.ts b/models/bxml/verbs/index.ts index 4dd67a4..ceda920 100644 --- a/models/bxml/verbs/index.ts +++ b/models/bxml/verbs/index.ts @@ -12,6 +12,7 @@ export * from './PhoneNumber'; export * from './PlayAudio'; export * from './Record'; export * from './Redirect'; +export * from './Refer'; export * from './ResumeRecording'; export * from './Ring'; export * from './SendDtmf'; diff --git a/tests/unit/models/bxml/verbs/Refer.test.ts b/tests/unit/models/bxml/verbs/Refer.test.ts new file mode 100644 index 0000000..1dbef21 --- /dev/null +++ b/tests/unit/models/bxml/verbs/Refer.test.ts @@ -0,0 +1,43 @@ +import { Refer, ReferAttributes } from '../../../../../models/bxml/verbs/Refer'; +import { SipUri } from '../../../../../models/bxml/verbs/SipUri'; + +describe('Refer', () => { + test('should generate Refer XML with SipUri and all attributes', () => { + const attributes: ReferAttributes = { + referCompleteUrl: 'https://example.com/handleRefer', + referCompleteMethod: 'POST', + tag: 'my-tag', + }; + const sipUri = new SipUri('sip:alice@atlanta.example.com'); + const refer = new Refer(attributes, sipUri); + + const xml = refer.toBxml(); + expect(xml).toContain('sip:alice@atlanta.example.com'); + expect(xml).toContain(''); + }); + + test('should generate Refer XML with no attributes', () => { + const sipUri = new SipUri('sip:bob@biloxi.example.com'); + const refer = new Refer(undefined, sipUri); + + const xml = refer.toBxml(); + expect(xml).toContain(''); + expect(xml).toContain('sip:bob@biloxi.example.com'); + }); + + test('setSipUri should replace the nested SipUri', () => { + const sipUri1 = new SipUri('sip:alice@atlanta.example.com'); + const sipUri2 = new SipUri('sip:bob@biloxi.example.com'); + const refer = new Refer({}, sipUri1); + + refer.setSipUri(sipUri2); + const xml = refer.toBxml(); + expect(xml).not.toContain('alice'); + expect(xml).toContain('sip:bob@biloxi.example.com'); + }); +}); +