UNPKG

@cxco/sdk-webhooks

Version:

DigitalCX Webhook library

92 lines (73 loc) 1.95 kB
"use strict"; const TransactionResponseModel = require('./TransactionResponseModel'); /** * Transaction Webhook: Inbound payload wrapper class */ class TransactionResponse { constructor(data) { this.slots = data.slots.map(slot => { const clear = () => { this.result.resetSlotStatus = this.result.resetSlotStatus.concat([slot.name]); }; return Object.assign({}, slot, { clear }); }); this.success = typeof data.success === 'boolean' ? data.success : true; this.error = data.error || ''; this.sessionId = data.sessionId || ''; this.result = { message: { text: data.okMessage.text }, resetSlotStatus: [], metadata: data.metadata || '' }; } /** * Adds error to the response and marks it unsuccessfull. * Sets `success` to `false` * @param {string} err */ setError(err) { this.success = false; this.error = err; } /** * Converts the TransactionResponse into a TransactionResponseModel object. * * @returns {Object} */ toModel() { return new TransactionResponseModel(this); } /** * Sets the metadata property * @param {object} value */ setMetadata(value) { this.result.metadata = value; } setMessage(value) { this.result.message.text = value; } setResetSlotStatus(resetSlotStatus) { this.result.resetSlotStatus = resetSlotStatus; } getSlotValue(slotName) { const foundSlot = this.slots.find(slot => slot.name.toLowerCase() === slotName.toLowerCase()); if (foundSlot !== undefined) { return foundSlot.value; } return foundSlot; } setData(data) { if (data.message && typeof data.message.text !== 'undefined') { this.setMessage(data.message.text); } if (Array.isArray(data.resetSlotStatus)) { this.setResetSlotStatus(data.resetSlotStatus); } } } module.exports = TransactionResponse;