const twilio 	= require('twilio')

const client = twilio(
	process.env.TWILIO_ACCOUNT_SID,
	process.env.TWILIO_AUTH_TOKEN)

module.exports.delete = function (req, res) {
	let id = req.params.id

	client.taskrouter.workspaces(process.env.TWILIO_WORKSPACE_SID).workers(id).remove()
		.then(worker => {
			res.status(200).end()
		}).catch(error => {
			res.status(500).send(res.convertErrorToJSON(error))
		})

}

module.exports.create = function (req, res) {
	const worker = {
		friendlyName: req.body.friendlyName,
		attributes: req.body.attributes
	}

	client.taskrouter.workspaces(process.env.TWILIO_WORKSPACE_SID).workers.create(worker)
		.then(worker => {
			const payload = {
				sid: worker.sid,
				friendlyName: worker.friendlyName,
				attributes: worker.attributes,
				activityName: worker.activityName
			}

			res.status(200).json(payload)
		}).catch(error => {
			res.status(500).send(res.convertErrorToJSON(error))
		})

}

module.exports.list = function (req, res) {

	client.taskrouter.workspaces(process.env.TWILIO_WORKSPACE_SID).workers.list()
		.then(workers => {
			let payload =[]

			for (let i = 0; i < workers.length; i++) {
				const worker = {
					sid: workers[i].sid,
					friendlyName: workers[i].friendlyName,
					attributes: JSON.parse(workers[i].attributes),
					activityName: workers[i].activityName
				}

				payload.push(worker)
			}

			res.status(200).json(payload)
		}).catch(error => {
			res.status(500).send(res.convertErrorToJSON(error))
		})

}

@@ -50,18 +50,16 @@
  twilio(
-%0A%09
 process.
@@ -74,21 +74,70 @@
 IO_A
-CCOUNT_SID,%0A%09
+PI_KEY_SID, process.env.TWILIO_API_KEY_SECRET, %7B%0A%09accountSid: 
 proc
@@ -156,18 +156,22 @@
 IO_A
-UTH_TOKEN)
+CCOUNT_SID%0A%7D);
 %0A%0Amo
@@ -1131,13 +1131,16 @@
 %3E %7B%0A
+%0A
 %09%09%09
-le
+cons
 t pa
@@ -1150,78 +1150,46 @@
 ad =
-%5B%5D%0A%0A%09%09%09for (let i = 0; i %3C workers.length; i++) %7B%0A%09%09%09%09const worker =
+ workers.map(worker =%3E %7B%0A%09%09%09%09return
  %7B
+ 
 %0A%09%09%09
@@ -1201,20 +1201,16 @@
 : worker
-s%5Bi%5D
 .sid,%0A%09%09
@@ -1232,20 +1232,16 @@
 : worker
-s%5Bi%5D
 .friendl
@@ -1281,20 +1281,16 @@
 e(worker
-s%5Bi%5D
 .attribu
@@ -1324,12 +1324,8 @@
 rker
-s%5Bi%5D
 .act
@@ -1344,38 +1344,13 @@
 %09%09%7D%0A
-%0A
 %09%09%09
-%09payload.push(worker)%0A%09%09%09
 %7D
+)
 %0A%0A%09%09

use api key / refactored worker list 💫