Extract business hours from Toast schedule data

Parses upcomingSchedules from ROOT_QUERY.restaurantV2.schedule
and formats as "Mon 7:30am-6:30pm, Tue 7:30am-6:30pm" text string
that the setup wizard's parseHoursString() can consume.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
John Mizerek 2026-03-03 10:24:45 -08:00
parent 4351978c10
commit 2b441e166e

View file

@ -1082,6 +1082,55 @@
<cfif structKeyExists(restaurant, "brandColor") AND NOT isNull(restaurant.brandColor)>
<cfset toastBusiness["brandColor"] = replace(restaurant.brandColor, "##", "")>
</cfif>
<!--- Extract business hours from schedule --->
<cfif structKeyExists(restaurant, "schedule") AND isStruct(restaurant.schedule)>
<cfset sched = restaurant.schedule>
<cfif structKeyExists(sched, "upcomingSchedules") AND isArray(sched.upcomingSchedules) AND arrayLen(sched.upcomingSchedules) GT 0>
<!--- Use the first schedule (usually TAKE_OUT) --->
<cfset upcoming = sched.upcomingSchedules[1]>
<cfif structKeyExists(upcoming, "dailySchedules") AND isArray(upcoming.dailySchedules)>
<!--- Build a 7-day hours map: dayOfWeek -> {open, close} --->
<cfset dayHours = {}>
<cfloop array="#upcoming.dailySchedules#" index="ds">
<cfif structKeyExists(ds, "date") AND structKeyExists(ds, "servicePeriods") AND isArray(ds.servicePeriods) AND arrayLen(ds.servicePeriods) GT 0>
<cfset dsDate = parseDateTime(ds.date)>
<cfset dow = dayOfWeek(dsDate)><!--- 1=Sun, 2=Mon, ..., 7=Sat --->
<cfset sp = ds.servicePeriods[1]>
<cfif structKeyExists(sp, "startTime") AND structKeyExists(sp, "endTime")>
<cfset dayHours[dow] = { "open": left(sp.startTime, 5), "close": left(sp.endTime, 5) }>
</cfif>
</cfif>
</cfloop>
<!--- Format as text string: "Mon 7:30am-6:30pm, Tue 7:30am-6:30pm" --->
<cfset dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]>
<cfset hoursParts = []>
<!--- Reorder to Mon-Sun (2,3,4,5,6,7,1) --->
<cfset dayOrder = [2, 3, 4, 5, 6, 7, 1]>
<cfloop array="#dayOrder#" index="dIdx">
<cfif structKeyExists(dayHours, dIdx)>
<cfset dh = dayHours[dIdx]>
<!--- Convert 24h to 12h format --->
<cfset openH = val(listFirst(dh.open, ":"))>
<cfset openM = val(listLast(dh.open, ":"))>
<cfset closeH = val(listFirst(dh.close, ":"))>
<cfset closeM = val(listLast(dh.close, ":"))>
<cfset openAmPm = openH GTE 12 ? "pm" : "am">
<cfset closeAmPm = closeH GTE 12 ? "pm" : "am">
<cfif openH GT 12><cfset openH = openH - 12></cfif>
<cfif openH EQ 0><cfset openH = 12></cfif>
<cfif closeH GT 12><cfset closeH = closeH - 12></cfif>
<cfif closeH EQ 0><cfset closeH = 12></cfif>
<cfset openStr = openH & (openM GT 0 ? ":" & numberFormat(openM, "00") : "") & openAmPm>
<cfset closeStr = closeH & (closeM GT 0 ? ":" & numberFormat(closeM, "00") : "") & closeAmPm>
<cfset arrayAppend(hoursParts, dayNames[dIdx] & " " & openStr & "-" & closeStr)>
</cfif>
</cfloop>
<cfif arrayLen(hoursParts) GT 0>
<cfset toastBusiness["hours"] = arrayToList(hoursParts, ", ")>
</cfif>
</cfif>
</cfif>
</cfif>
</cfif>
</cfloop>
</cfif>