<commit_before>var twilio 			= require('twilio')
var async 			= require("async")

/* check if the application runs on heroku */
var util

if(process.env.DYNO){
	util = require("../util-pg.js")
} else {
	util = require("../util-file.js")
}

module.exports.get = function(req, res) {
	
	res.status(200).json(req.configuration)

}

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

	var client = new twilio.TaskRouterClient(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN, process.env.TWILIO_WORKSPACE_SID)
	var configuration = req.body.configuration

	async.waterfall([

		function(callback){

			module.exports.syncQueues(configuration, function(err, options){	

				if(err){
					callback(err)
				} else {
					callback(null, configuration)
				}

			})

		}, function(configuration, callback){

			var workflowConfiguration = { task_routing: { filters: [] }}

			for (var i = 0; i < configuration.queues.length; i++) {

				var target = { 
					targets: [{ queue:  configuration.queues[i].taskQueueSid, expression: 'task.team == worker.team' }], 
					expression: configuration.queues[i].expression
				}
				workflowConfiguration.task_routing.filters.push(target)	
			}
			
			var workflow = { sid: configuration.twilio.workflowSid,
				friendlyName: 'Omni Channel Demo Workflow',
				assignmentCallbackUrl: req.protocol + '://' + req.hostname + '/api/taskrouter/assignment',
				taskReservationTimeout: '1200',
				configuration: JSON.stringify(workflowConfiguration)
			}

			module.exports.createOrUpdateWorkflow(workflow, function(err, workflow){	
				
				if (err){ 
					callback(err)
				} else {
					configuration.twilio.workflowSid = workflow.sid
					callback(null, configuration)
				}

			})

		}, function(configuration, callback){

			module.exports.createOrUpdateApplication(req, function(err, application){	
				
				if (err){ 
					callback(err)
				} else {
					configuration.twilio.applicationSid = application.sid
					callback(null, configuration)
				}

			})

		}, function(configuration, callback){
<commit_msg>server-side validation for phone number and display possible errors<commit_after>var twilio 			= require('twilio')
var async 			= require("async")

/* check if the application runs on heroku */
var util

if(process.env.DYNO){
	util = require("../util-pg.js")
} else {
	util = require("../util-file.js")
}

module.exports.get = function(req, res) {
	
	res.status(200).json(req.configuration)

}

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

	var client = new twilio.TaskRouterClient(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN, process.env.TWILIO_WORKSPACE_SID)
	var configuration = req.body.configuration

	async.waterfall([

		function(callback){

			module.exports.syncQueues(configuration, function(err, options){	

				if(err){
					callback(err)
				} else {
					callback(null, configuration)
				}

			})

		}, function(configuration, callback){

			var workflowConfiguration = { task_routing: { filters: [] }}

			for (var i = 0; i < configuration.queues.length; i++) {

				var target = { 
					targets: [{ queue:  configuration.queues[i].taskQueueSid, expression: 'task.team == worker.team' }], 
					expression: configuration.queues[i].expression
				}
				workflowConfiguration.task_routing.filters.push(target)	
			}
			
			var workflow = { sid: configuration.twilio.workflowSid,
				friendlyName: 'Twilio Contact Center Workflow',
				assignmentCallbackUrl: req.protocol + '://' + req.hostname + '/api/taskrouter/assignment',
				taskReservationTimeout: '1200',
				configuration: JSON.stringify(workflowConfiguration)
			}

			module.exports.createOrUpdateWorkflow(workflow, function(err, workflow){	
				
				if (err){ 
					callback(err)
				} else {
					configuration.twilio.workflowSid = workflow.sid
					callback(null, configuration)
				}

			})

		}, function(configuration, callback){

			module.exports.createOrUpdateApplication(req, function(err, application){	
				
				if (err){ 
					callback(err)
				} else {
					configuration.twilio.applicationSid = application.sid
					callback(null, configuration)
				}

			})

		}, function(configuration, callback){

{'fraction_non_alphanumeric': 0.12818662818662818, 'fraction_numerical': 0.003848003848003848, 'mean_word_length': 3.6108647450110865, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3057637', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1267, 'n_words': 236}