diff --git a/controllers/taskrouter.js b/controllers/taskrouter.js index 4b9b057..9b48186 100755 --- a/controllers/taskrouter.js +++ b/controllers/taskrouter.js @@ -1,43 +1,38 @@ -const twilio = require('twilio') - -const client = twilio( - process.env.TWILIO_ACCOUNT_SID, - process.env.TWILIO_AUTH_TOKEN) - -module.exports.getWorkspace = function (req, res) { - - client.taskrouter.workspaces(process.env.TWILIO_WORKSPACE_SID).fetch() - .then(workspace => { - let payload = { +const twilio = require('twilio'); + +const client = twilio(process.env.TWILIO_API_KEY_SID, process.env.TWILIO_API_KEY_SECRET, { + accountSid: process.env.TWILIO_ACCOUNT_SID +}); + +module.exports.getWorkspace = function(req, res) { + client.taskrouter + .workspaces(process.env.TWILIO_WORKSPACE_SID) + .fetch() + .then((workspace) => { + const payload = { sid: workspace.sid, friendlyName: workspace.friendlyName - } + }; - res.status(200).json(payload) - }).catch(error => { - res.status(500).send(res.convertErrorToJSON(error)) + res.status(200).json(payload); }) - -} - -module.exports.getActivities = function (req, res) { - - client.taskrouter.workspaces(process.env.TWILIO_WORKSPACE_SID).activities.list() - .then(activities => { - let payload =[] - - for (let i = 0; i < activities.length; i++) { - const activity = { - sid: activities[i].sid, - friendlyName: activities[i].friendlyName, - } - - payload.push(activity) - } - - res.status(200).json(payload) - }).catch(error => { - res.status(500).send(res.convertErrorToJSON(error)) + .catch((error) => { + res.status(500).send(res.convertErrorToJSON(error)); + }); +}; + +module.exports.getActivities = function(req, res) { + client.taskrouter + .workspaces(process.env.TWILIO_WORKSPACE_SID) + .activities.list() + .then((activities) => { + const payload = activities.map((activity) => { + return { sid: activity.sid, friendlyName: activity.friendlyName }; + }); + + res.status(200).json(payload); }) - -} \ No newline at end of file + .catch((error) => { + res.status(500).send(res.convertErrorToJSON(error)); + }); +};