From f0bd681ccc501864cb1b95fa367259aaffb3eecf Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Wed, 22 Sep 2021 13:28:56 -0400 Subject: [PATCH] implement actionHook for message verb --- lib/tasks/message.js | 62 ++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/lib/tasks/message.js b/lib/tasks/message.js index 72311060..44d82a51 100644 --- a/lib/tasks/message.js +++ b/lib/tasks/message.js @@ -10,7 +10,7 @@ class TaskMessage extends Task { this.payload = { message_sid: this.data.message_sid || uuidv4(), - provider: this.data.provider, + carrier: this.data.carrier, to: this.data.to, from: this.data.from, text: this.data.text @@ -27,16 +27,18 @@ class TaskMessage extends Task { const {srf, accountSid} = cs; const {res} = cs.callInfo; let payload = this.payload; + const actionParams = {message_sid: this.payload.message_sid}; + await super.exec(cs); try { const {getSmpp, dbHelpers} = srf.locals; const {lookupSmppGateways} = dbHelpers; - this.logger.info(`looking up gateways for account_sid: ${accountSid}`); + this.logger.debug(`looking up gateways for account_sid: ${accountSid}`); const r = await lookupSmppGateways(accountSid); let gw, url, relativeUrl; if (r.length > 0) { - gw = r.find((o) => 1 === o.sg.outbound && (!this.payload.provider || o.vc.name === this.payload.provider)); + gw = r.find((o) => 1 === o.sg.outbound && (!this.payload.carrier || o.vc.name === this.payload.carrier)); } if (gw) { this.logger.info({gw, accountSid}, 'Message:exec - using smpp to send message'); @@ -51,21 +53,34 @@ class TaskMessage extends Task { else { //TMP: smpp only at the moment, need to add http back in /* - this.logger.info({gw, accountSid, provider: this.payload.provider}, + this.logger.info({gw, accountSid, carrier: this.payload.carrier}, 'Message:exec - no smpp gateways found to send message'); relativeUrl = 'v1/outboundSMS'; const sbcAddress = getSBC(); if (sbcAddress) url = `http://${sbcAddress}:3000/`; */ - return res.sendStatus(404); + this.performAction({ + ...actionParams, + message_status: 'no carriers' + }).catch((err) => {}); + if (res) res.sendStatus(404); + return; } if (url) { const post = bent(url, 'POST', 'json', 201, 480); this.logger.info({payload, url}, 'Message:exec sending outbound SMS'); const response = await post(relativeUrl, payload); - if (response.smpp_err_code) { - this.logger.info({response}, 'Error sending SMS'); - if (cs.callInfo.res) { + const {smpp_err_code, carrier, message_id, message} = response; + if (smpp_err_code) { + this.logger.info({response}, 'SMPP error sending SMS'); + this.performAction({ + ...actionParams, + carrier, + carrier_message_id: message_id, + message_status: 'failure', + message_failure_reason: message + }).catch((err) => {}); + if (res) { res.status(480).json({ ...response, sid: cs.callInfo.messageSid @@ -73,25 +88,38 @@ class TaskMessage extends Task { } } else { + const {message_id, carrier} = response; this.logger.info({response}, 'Successfully sent SMS'); - if (cs.callInfo.res) { - this.logger.info('Message:exec sending 200 OK response to HTTP POST from api server'); + this.performAction({ + ...actionParams, + carrier, + carrier_message_id: message_id, + message_status: 'success', + }).catch((err) => {}); + if (res) { res.status(200).json({ sid: cs.callInfo.messageSid, - providerResponse: response + carrierResponse: response }); } } - - // TODO: action Hook } else { - this.logger.info('Message:exec - unable to send SMS as there are no available SMS gateways'); - res.status(422).json({message: 'no configured SMS gateways'}); + this.logger.info('Message:exec - unable to send SMS as SMPP is not configured on the system'); + this.performAction({ + ...actionParams, + message_status: 'smpp configuration error' + }).catch((err) => {}); + if (res) res.status(404).json({message: 'no configured SMS gateways'}); } } catch (err) { - this.logger.error(err, 'TaskMessage:exec - Error sending SMS'); - res.status(422).json({message: 'no configured SMS gateways'}); + this.logger.error(err, 'TaskMessage:exec - unexpected error sending SMS'); + this.performAction({ + ...actionParams, + message_status: 'system error', + message_failure_reason: err.message + }); + if (res) res.status(422).json({message: 'no configured SMS gateways'}); } } }