liveperson-functions-cli
Version:
LivePerson Functions CLI
52 lines • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SDEUtil = void 0;
const LpServices_1 = require("../lp-client/LpServices");
class SDEUtil {
constructor(lpClient) {
this.lpClient = lpClient;
this.brandId = process.env.BRAND_ID;
}
getSDEsFromConv(conversation) {
try {
if (!conversation || !conversation.hasOwnProperty('conversationHistoryRecords')) {
throw new Error('Please provide a valid conversation.');
}
const { conversationHistoryRecords: [{ sdes = { events: [] }, unAuthSdes = { events: [] } }], } = conversation;
return { sdes: this.sortSDEs(sdes), unAuthSdes: this.sortSDEs(unAuthSdes) };
}
catch (error) {
throw new Error(`Error while getting SDEs from conversation: ${error.message}`);
}
}
async addSDEs(sdes, visitorId, sessionId) {
if (!sdes || sdes.length === 0) {
throw new Error('Please provide one or more SDEs');
}
try {
const requestOptions = {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: sdes,
json: true,
};
await this.lpClient(LpServices_1.LpServices.SMT, `/api/account/${this.brandId}/monitoring/visitors/${visitorId}/visits/current/events?v=1&sid=${sessionId}`, requestOptions);
}
catch (error) {
throw new Error(`Error while setting SDEs: ${error.message}`);
}
}
/**
* Will sort the events of the SDES by the server-timestamp. The last event is the most recent one.
* @param sdes the sdes which should be sorted.
*/
sortSDEs(sdes) {
return {
events: sdes.events.sort((event1, event2) => (event1.serverTimeStamp < event2.serverTimeStamp ? -1 : 1)),
};
}
}
exports.SDEUtil = SDEUtil;
//# sourceMappingURL=SDEUtil.js.map