<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">

<cffunction name="apiAbort" access="public" returntype="void" output="true">
  <cfargument name="payload" type="struct" required="true">
  <cfcontent type="application/json; charset=utf-8">
  <cfoutput>#serializeJSON(arguments.payload)#</cfoutput>
  <cfabort>
</cffunction>

<cfset TaskID = val(url.TaskID ?: 0)>

<cfif TaskID LTE 0>
  <cfset apiAbort({ "OK": false, "ERROR": "missing_params", "MESSAGE": "TaskID required in URL" })>
</cfif>

<cftry>
  <cfset qTask = queryTimed("
    SELECT ID, BusinessID, TaskCategoryID, OrderID, TaskTypeID,
           ClaimedByUserID, ClaimedOn, CompletedOn,
           CreatedOn, SourceType, SourceID
    FROM Tasks
    WHERE ID = ?
  ", [ { value = TaskID, cfsqltype = "cf_sql_integer" } ], { datasource = "payfrit" })>

  <cfif qTask.recordCount EQ 0>
    <cfset apiAbort({ "OK": false, "ERROR": "not_found", "MESSAGE": "Task not found" })>
  </cfif>

  <cfset apiAbort({
    "OK": true,
    "TASK": {
      "TaskID": qTask.ID,
      "BusinessID": qTask.BusinessID,
      "TaskCategoryID": qTask.CategoryID,
      "OrderID": qTask.OrderID,
      "TaskTypeID": qTask.TaskTypeID,
      "ClaimedByUserID": qTask.ClaimedByUserID,
      "ClaimedOn": qTask.ClaimedOn,
      "CompletedOn": qTask.CompletedOn,
      "CreatedOn": qTask.CreatedOn,
      "SourceType": qTask.SourceType,
      "SourceID": qTask.SourceID,
      "IS_COMPLETED": NOT (isNull(qTask.CompletedOn) OR len(trim(qTask.CompletedOn)) EQ 0)
    }
  })>

  <cfcatch>
    <cfset apiAbort({
      "OK": false,
      "ERROR": "server_error",
      "MESSAGE": cfcatch.message
    })>
  </cfcatch>
</cftry>
