 
 #####################################################
 # Creates a new Task
-# Create the new queue objects for the created task #
 #####################################################
 exports.addTask = ->
 

        utils.logAndSetResponse this, 'bad request', 'Cannot queue task as there are transactions with disabled or deleted channels', 'info'
        return

      t = 0
      while t < transactions.tids.length
        transaction = tid: transactions.tids[t]
        transactionsArr.push transaction
        t++
      taskObject.transactions = transactionsArr

      task = new Task(taskObject)
      result = yield Q.ninvoke(task, 'save')

      taskID = result[0]._id
      transactions = taskObject.transactions
      i = 0
      while i < transactions.length
        try
          transactionID = transactions[i].tid
          queue.enqueue 'rerun_transaction', {
            transactionID: transactionID
            taskID: taskID
          }, (e, job) ->
            logger.info 'Enqueued transaction: #{job.data.params.transactionID}'
            return

          # All ok! So set the result
          utils.logAndSetResponse this, 'created', 'Queue item successfully created', 'info'
        catch err
          # Error! So inform the user
          utils.logAndSetResponse this, 'internal server error', "Could not add Queue item via the API: #{err}", 'info'
        i++

      # All ok! So set the result
      utils.logAndSetResponse this, 'created', "User #{this.authenticated.email} created task with id #{task.id}", 'info'

Q = require 'q'
logger = require 'winston'

authorisation = require './authorisation'
authMiddleware = require '../middleware/authorisation'

