UNPKG

ringcentral-call-control

Version:

[![Coverage Status](https://coveralls.io/repos/github/ringcentral/ringcentral-call-control-js/badge.svg?branch=master)](https://coveralls.io/github/ringcentral/ringcentral-call-control-js?branch=master) [![NPM Version](https://img.shields.io/npm/v/ringcen

704 lines 30.8 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Session = exports.ReplyWithPattern = exports.PartyStatusCode = exports.Direction = void 0; var events_1 = require("events"); var formatParty_1 = require("./formatParty"); var userAgent_1 = require("./userAgent"); var Direction; (function (Direction) { Direction["inbound"] = "Inbound"; Direction["outbound"] = "Outbound"; })(Direction = exports.Direction || (exports.Direction = {})); var PartyStatusCode; (function (PartyStatusCode) { PartyStatusCode["setup"] = "Setup"; PartyStatusCode["proceeding"] = "Proceeding"; PartyStatusCode["answered"] = "Answered"; PartyStatusCode["disconnected"] = "Disconnected"; PartyStatusCode["gone"] = "Gone"; PartyStatusCode["parked"] = "Parked"; PartyStatusCode["hold"] = "Hold"; PartyStatusCode["voicemail"] = "Voicemail"; PartyStatusCode["faxReceive"] = "FaxReceive"; PartyStatusCode["voicemailScreening"] = "VoiceMailScreening"; })(PartyStatusCode = exports.PartyStatusCode || (exports.PartyStatusCode = {})); var ReplyWithPattern; (function (ReplyWithPattern) { ReplyWithPattern["willCallYouBack"] = "WillCallYouBack"; ReplyWithPattern["callMeBack"] = "CallMeBack"; ReplyWithPattern["onMyWay"] = "OnMyWay"; ReplyWithPattern["onTheOtherLine"] = "OnTheOtherLine"; ReplyWithPattern["willCallYouBackLater"] = "WillCallYouBackLater"; ReplyWithPattern["callMeBackLater"] = "CallMeBackLater"; ReplyWithPattern["inAMeeting"] = "InAMeeting"; ReplyWithPattern["onTheOtherLineNoCall"] = "OnTheOtherLineNoCall"; })(ReplyWithPattern = exports.ReplyWithPattern || (exports.ReplyWithPattern = {})); function objectEqual(obj1, obj2) { var equal = true; if (!obj1 || !obj2) { return false; } Object.keys(obj2).forEach(function (key) { if (obj2[key] === obj1[key]) { return; } equal = false; }); Object.keys(obj1).forEach(function (key) { if (obj1[key] === obj2[key]) { return; } equal = false; }); return equal; } function diffParty(oldParty, updatedParty) { var diffs = []; updatedParty && Object.keys(updatedParty).forEach(function (key) { if (updatedParty[key] === oldParty[key]) { return; } if (typeof updatedParty[key] !== 'object') { diffs.push({ key: key, value: updatedParty[key] }); return; } if (objectEqual(updatedParty[key], oldParty[key])) { return; } diffs.push({ key: key, value: updatedParty[key] }); }); return diffs; } function diffParties(oldParties, updatedParties) { var oldMap = {}; oldParties.forEach(function (p) { oldMap[p.id] = p; }); var diffs = []; updatedParties.forEach(function (updatedParty) { if (!oldMap[updatedParty.id]) { diffs.push({ type: 'new', party: updatedParty }); return; } var oldParty = oldMap[updatedParty.id]; var partyDiffs = diffParty(oldParty, updatedParty); if (partyDiffs.length === 0) { return; } diffs.push({ type: 'update', party: updatedParty, partyDiffs: partyDiffs }); }); return diffs; } var Session = /** @class */ (function (_super) { __extends(Session, _super); function Session(rawData, sdk, accountLevel, userAgent) { var _this = _super.call(this) || this; _this._data = __assign({}, rawData); _this._sdk = sdk; _this._accountLevel = !!accountLevel; _this._userAgent = userAgent; return _this; } Session.prototype.onUpdated = function (data) { var _this = this; var partiesDiff = diffParties(this.parties, data.parties); partiesDiff.forEach(function (diff) { if (diff.type === 'new') { _this._data.parties = [].concat(_this.parties).concat(diff.party); _this.emit('status', { party: diff.party }); return; } if (diff.type === 'update') { var oldPartyIndex = _this.parties.findIndex(function (p) { return p.id === diff.party.id; }); var parties = _this.parties.slice(0); parties[oldPartyIndex] = __assign(__assign({}, parties[oldPartyIndex]), diff.party); _this._data.parties = parties; diff.partyDiffs.forEach(function (partyDiff) { _this.emit(partyDiff.key, { party: diff.party }); }); return; } }); }; Session.prototype.restore = function (data) { this._data = data; }; Object.defineProperty(Session.prototype, "data", { get: function () { return this._data || {}; }, enumerable: false, configurable: true }); Object.defineProperty(Session.prototype, "id", { get: function () { return this.data.id; }, enumerable: false, configurable: true }); Object.defineProperty(Session.prototype, "accountId", { get: function () { return this.data.accountId; }, enumerable: false, configurable: true }); Object.defineProperty(Session.prototype, "creationTime", { get: function () { return this.data.creationTime; }, enumerable: false, configurable: true }); Object.defineProperty(Session.prototype, "extensionId", { get: function () { return this.data.extensionId; }, enumerable: false, configurable: true }); Object.defineProperty(Session.prototype, "origin", { get: function () { return this.data.origin; }, enumerable: false, configurable: true }); Object.defineProperty(Session.prototype, "parties", { get: function () { return this.data.parties || []; }, enumerable: false, configurable: true }); Object.defineProperty(Session.prototype, "serverId", { get: function () { return this.data.serverId; }, enumerable: false, configurable: true }); Object.defineProperty(Session.prototype, "sessionId", { get: function () { return this.data.sessionId; }, enumerable: false, configurable: true }); Object.defineProperty(Session.prototype, "party", { get: function () { var _this = this; var extensionId = this.data.extensionId; var accountId = this.data.accountId; var parties = this.parties.filter(function (p) { if (_this._accountLevel) { return p.accountId === accountId; } return p.extensionId === extensionId; }); if (parties.length === 0) { return; } if (parties.length === 1) { return parties[0]; } var activeParty = parties.find(function (p) { return p.status.code !== PartyStatusCode.disconnected; }); if (activeParty) { return activeParty; } return parties[parties.length - 1]; }, enumerable: false, configurable: true }); Object.defineProperty(Session.prototype, "otherParties", { get: function () { if (!this.party) { return this.parties; } var partyId = this.party.id; return this.parties.filter(function (p) { return p.id !== partyId; }); }, enumerable: false, configurable: true }); Object.defineProperty(Session.prototype, "recordings", { get: function () { var party = this.party; return (party && party.recordings) || []; }, enumerable: false, configurable: true }); Object.defineProperty(Session.prototype, "voiceCallToken", { get: function () { return this.data.voiceCallToken; }, enumerable: false, configurable: true }); Session.prototype.toJSON = function () { return this.data; }; Session.prototype.reload = function () { return __awaiter(this, void 0, void 0, function () { var response, data; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._sdk.platform().get("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id, undefined, this.requestOptions)]; case 1: response = _a.sent(); return [4 /*yield*/, response.json()]; case 2: data = _a.sent(); data.extensionId = this.data.extensionId; data.accountId = this.data.accountId; data.parties = data.parties.map(function (p) { return formatParty_1.formatParty(p); }); this._data = data; return [2 /*return*/]; } }); }); }; Session.prototype.drop = function () { return __awaiter(this, void 0, void 0, function () { var e_1, disconnectedParty; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, this._sdk.platform().delete("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id, undefined, this.requestOptions)]; case 1: _a.sent(); return [3 /*break*/, 3]; case 2: e_1 = _a.sent(); if (e_1 && e_1.response && e_1.response.status === 404) { disconnectedParty = __assign(__assign({}, this.party), { status: __assign(__assign({}, this.party.status), { code: PartyStatusCode.disconnected }) }); this.saveNewPartyData(disconnectedParty); this.emit('status', { party: this.party }); return [2 /*return*/]; } throw e_1; case 3: return [2 /*return*/]; } }); }); }; Session.prototype.saveNewPartyData = function (rawParty) { var newParty = formatParty_1.formatParty(rawParty); var newParties = this._data.parties.filter(function (p) { return p.id !== newParty.id; }); newParties.push(newParty); this._data.parties = newParties; }; Session.prototype.hold = function () { return __awaiter(this, void 0, void 0, function () { var oldParty, response, newParty; return __generator(this, function (_a) { switch (_a.label) { case 0: oldParty = this.party; return [4 /*yield*/, this._sdk.platform().post("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/" + oldParty.id + "/hold", undefined, undefined, this.requestOptions)]; case 1: response = _a.sent(); return [4 /*yield*/, response.json()]; case 2: newParty = _a.sent(); this.saveNewPartyData(newParty); this.emit('status', { party: this.party }); return [2 /*return*/, this.party]; } }); }); }; Session.prototype.unhold = function () { return __awaiter(this, void 0, void 0, function () { var oldParty, response, newParty; return __generator(this, function (_a) { switch (_a.label) { case 0: oldParty = this.party; return [4 /*yield*/, this._sdk.platform().post("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/" + oldParty.id + "/unhold", undefined, undefined, this.requestOptions)]; case 1: response = _a.sent(); return [4 /*yield*/, response.json()]; case 2: newParty = _a.sent(); this.saveNewPartyData(newParty); this.emit('status', { party: this.party }); return [2 /*return*/, this.party]; } }); }); }; Session.prototype.toVoicemail = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._sdk.platform().post("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/" + this.party.id + "/reject", undefined, undefined, this.requestOptions)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; Session.prototype.ignore = function (params) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._sdk.platform().post("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/" + this.party.id + "/ignore", params, undefined, this.requestOptions)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; Session.prototype.answer = function (params) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._sdk.platform().post("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/" + this.party.id + "/answer", params, undefined, this.requestOptions)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }; Session.prototype.reply = function (params) { return __awaiter(this, void 0, void 0, function () { var response, rawParty; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._sdk.platform().post("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/" + this.party.id + "/reply", params, undefined, this.requestOptions)]; case 1: response = _a.sent(); return [4 /*yield*/, response.json()]; case 2: rawParty = _a.sent(); this.saveNewPartyData(rawParty); this.emit('status', { party: this.party }); return [2 /*return*/, this.party]; } }); }); }; Session.prototype.forward = function (params) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._sdk.platform().post("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/" + this.party.id + "/forward", params, undefined, this.requestOptions)]; case 1: response = _a.sent(); return [2 /*return*/, response.json()]; } }); }); }; Session.prototype.transfer = function (params) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._sdk.platform().post("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/" + this.party.id + "/transfer", params, undefined, this.requestOptions)]; case 1: response = _a.sent(); return [2 /*return*/, response.json()]; } }); }); }; Session.prototype.bridge = function (params) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._sdk.platform().post("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/" + this.party.id + "/bridge", params, undefined, this.requestOptions)]; case 1: response = _a.sent(); return [2 /*return*/, response.json()]; } }); }); }; Session.prototype.park = function () { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._sdk.platform().post("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/" + this.party.id + "/park", undefined, undefined, this.requestOptions)]; case 1: response = _a.sent(); return [2 /*return*/, response.json()]; } }); }); }; // async pickup(params: PickUpParams) { // const response = await this._sdk.platform().post( // `/account/~/telephony/sessions/${this._data.id}/parties/${this.party.id}/pickup`, // params, // ); // return response.json(); // } // async transferToVoicemail() { // const result = await this.forward({ voicemail: this._data.extensionId }); // return result; // } Session.prototype.flip = function (params) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._sdk.platform().post("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/" + this.party.id + "/flip", params, undefined, this.requestOptions)]; case 1: response = _a.sent(); return [2 /*return*/, response.json()]; } }); }); }; Session.prototype.updateParty = function (params) { return __awaiter(this, void 0, void 0, function () { var response, rawParty; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._sdk.platform().send({ method: 'PATCH', url: "/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/" + this.party.id, query: undefined, body: params, userAgent: this.requestOptions.userAgent, })]; case 1: response = _a.sent(); return [4 /*yield*/, response.json()]; case 2: rawParty = _a.sent(); this.saveNewPartyData(rawParty); return [2 /*return*/, rawParty]; } }); }); }; Session.prototype.mute = function () { return __awaiter(this, void 0, void 0, function () { var result; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.updateParty({ muted: true })]; case 1: result = _a.sent(); this.emit('muted', { party: result }); return [2 /*return*/, result]; } }); }); }; Session.prototype.unmute = function () { return __awaiter(this, void 0, void 0, function () { var result; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.updateParty({ muted: false })]; case 1: result = _a.sent(); this.emit('muted', { party: result }); return [2 /*return*/, result]; } }); }); }; Session.prototype.createRecord = function () { return __awaiter(this, void 0, void 0, function () { var response, recording, recordings; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._sdk.platform().post("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/" + this.party.id + "/recordings", undefined, undefined, this.requestOptions)]; case 1: response = _a.sent(); return [4 /*yield*/, response.json()]; case 2: recording = _a.sent(); recordings = (this.party.recordings || []).filter(function (r) { return r.id !== recording.id; }); recordings.push(recording); this.party.recordings = recordings; this.emit('recordings', { party: this.party }); return [2 /*return*/, recording]; } }); }); }; Session.prototype.updateRecord = function (params) { return __awaiter(this, void 0, void 0, function () { var response, recording, recordings; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._sdk.platform().send({ method: 'PATCH', url: "/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/" + this.party.id + "/recordings/" + params.id, query: undefined, body: { active: params.active, }, userAgent: this.requestOptions.userAgent, })]; case 1: response = _a.sent(); return [4 /*yield*/, response.json()]; case 2: recording = _a.sent(); recordings = (this.party.recordings || []).filter(function (r) { return r.id !== recording.id; }); recordings.push(recording); this.party.recordings = recordings; this.emit('recordings', { party: this.party }); return [2 /*return*/, recording]; } }); }); }; Session.prototype.pauseRecord = function (recordingId) { return __awaiter(this, void 0, void 0, function () { var result; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.updateRecord({ id: recordingId, active: false })]; case 1: result = _a.sent(); return [2 /*return*/, result]; } }); }); }; Session.prototype.resumeRecord = function (recordingId) { return __awaiter(this, void 0, void 0, function () { var result; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.updateRecord({ id: recordingId, active: true })]; case 1: result = _a.sent(); return [2 /*return*/, result]; } }); }); }; Session.prototype.supervise = function (params) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._sdk.platform().post("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/supervise", params, undefined, this.requestOptions)]; case 1: response = _a.sent(); return [2 /*return*/, response.json()]; } }); }); }; Session.prototype.bringInParty = function (params) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._sdk.platform().post("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/bring-in", params, undefined, this.requestOptions)]; case 1: response = _a.sent(); return [2 /*return*/, response.json()]; } }); }); }; Session.prototype.removeParty = function (partyId, options) { return __awaiter(this, void 0, void 0, function () { var requestOptions; return __generator(this, function (_a) { switch (_a.label) { case 0: requestOptions = __assign({}, this.requestOptions); if (options) { requestOptions.body = options; } return [4 /*yield*/, this._sdk.platform().delete("/restapi/v1.0/account/~/telephony/sessions/" + this._data.id + "/parties/" + partyId, undefined, requestOptions)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }; Object.defineProperty(Session.prototype, "requestOptions", { get: function () { return { userAgent: this._userAgent ? this._userAgent + " " + userAgent_1.USER_AGENT : userAgent_1.USER_AGENT, }; }, enumerable: false, configurable: true }); return Session; }(events_1.EventEmitter)); exports.Session = Session; //# sourceMappingURL=Session.js.map