'use strict';

const config = require('../config/lib/twilio');

const client = require('twilio')(config.accountSid, config.authToken);

function useTwilioTestCreds() {
  return config.useTwilioTestCreds === 'true';
}

module.exports.postMessage = function (phone, messageText) {
  const payload = {
    to: useTwilioTestCreds() ? config.toNumber : phone,
    body: messageText,
  };

  // Twilio test credentials don't support messagingServiceSid.
  // @see https://www.twilio.com/docs/api/rest/test-credentials#test-sms-messages-parameters
  if (useTwilioTestCreds()) {
    payload.from = config.fromNumber;
  } else {
    payload.messagingServiceSid = config.messagingServiceSid;
  }

  return client.messages.create(payload);
};

@@ -272,16 +272,61 @@
 Text) %7B%0A
+  const useTestCreds = useTwilioTestCreds();%0A
   const 
@@ -349,30 +349,24 @@
 to: useT
-wilioT
 estCreds
 () ? con
@@ -357,18 +357,16 @@
 estCreds
-()
  ? confi
@@ -581,30 +581,24 @@
 if (useT
-wilioT
 estCreds
 ()) %7B%0A  
@@ -589,18 +589,16 @@
 estCreds
-()
 ) %7B%0A    

use one fn call instead of two.