<!--- Cron: process EVV visit automation jobs (10427) --->
<cfparam name="url.Agency_ID" default="0">
<cfparam name="url.notify" default="1">
<cfparam name="url.force_poll" default="0">
<cfparam name="url.json" default="0">
<cfparam name="request.send_errors_to" default="velmurugan@myhomecarebiz.com">
<cfif NOT structKeyExists(application, "enckey") OR NOT len(trim(application.enckey))>
	<cfset application.enckey = "54kczhzgHTj4a8n+SRZPyg==">
</cfif>
<cfif NOT structKeyExists(application, "DEV")>
	<cfif CGI.SERVER_NAME EQ "127.0.0.1" OR CGI.SERVER_NAME EQ "localhost"
		OR CGI.SERVER_NAME EQ "52.23.186.30" OR CGI.SERVER_NAME EQ "dev.myhomecarebiz.com"
		OR CGI.SERVER_NAME EQ "98.87.35.139" OR CGI.SERVER_NAME EQ "devsecure.myhomecarebiz.com"
		OR CGI.SERVER_NAME EQ "54.175.136.41" OR CGI.SERVER_NAME EQ "stagesecure.myhomecarebiz.com">
		<cfset application.DEV = true>
	<cfelse>
		<cfset application.DEV = false>
	</cfif>
</cfif>
<cfset Request.prefix_db_lookup = "hhapowerpath">

<cfset evvAuto = createObject("component", "components.evv.EVVVisitAutomation").init()>
<cfset summary = {
	"success" = false,
	"transmitted" = 0,
	"polled" = 0,
	"completed" = 0,
	"failed" = 0,
	"jobsFound" = 0,
	"errors" = [],
	"details" = [],
	"ranAt" = now()
}>
<cfset fatalError = structNew()>

<cftry>
	<cfset summary = evvAuto.processPendingJobs(agencyID = val(url.Agency_ID), forcePoll = (url.force_poll EQ 1))>
	<cfcatch type="any">
		<cfset fatalError = {
			"message" = cfcatch.message,
			"detail" = structKeyExists(cfcatch, "detail") ? cfcatch.detail : ""
		}>
		<cfset summary.success = false>
		<cfset arrayAppend(summary.errors, "Fatal cron error: #cfcatch.message#")>
	</cfcatch>
</cftry>

<cfif url.notify EQ 1>
	<cfset evvAuto.sendCronRunEmail(summary = summary, fatalError = fatalError)>
</cfif>
<cfif url.json EQ 1>
	<cfcontent type="application/json">
	<cfoutput>#serializeJSON(summary)#</cfoutput>
	<cfabort>
</cfif>
