This repository has been archived on 2026-03-21. You can view files and clone it, but cannot push or open issues or pull requests.
payfrit-biz/api/stations/save.cfm
John Mizerek f5ff9cdfeb Add station CRUD endpoints and wire into portal UI
New endpoints: save.cfm (combined add/update) and delete.cfm
(soft-delete with item unassignment). Registered in Application.cfm.

Portal station-assignment page now uses real API calls for add,
edit, and delete instead of client-side-only prompt(). Fixed key
naming mismatch (StationName/StationColor → Name/Color) so the
page works with real API data. Removed hardcoded demo fallbacks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-02 16:26:45 -08:00

99 lines
3.2 KiB
Text

<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cffunction name="readJsonBody" access="public" returntype="struct" output="false">
<cfset var raw = getHttpRequestData().content>
<cfif isNull(raw) OR len(trim(raw)) EQ 0>
<cfreturn {}>
</cfif>
<cftry>
<cfset var data = deserializeJSON(raw)>
<cfif isStruct(data)>
<cfreturn data>
<cfelse>
<cfreturn {}>
</cfif>
<cfcatch>
<cfreturn {}>
</cfcatch>
</cftry>
</cffunction>
<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 data = readJsonBody()>
<cfset BusinessID = val( structKeyExists(data,"BusinessID") ? data.BusinessID : 0 )>
<cfset StationID = val( structKeyExists(data,"StationID") ? data.StationID : 0 )>
<cfset Name = trim( structKeyExists(data,"Name") ? data.Name : "" )>
<cfset Color = trim( structKeyExists(data,"Color") ? data.Color : "#666666" )>
<cfif BusinessID LTE 0>
<cfset apiAbort({ "OK": false, "ERROR": "missing_businessid", "MESSAGE": "BusinessID is required." })>
</cfif>
<cfif len(Name) EQ 0>
<cfset apiAbort({ "OK": false, "ERROR": "missing_name", "MESSAGE": "Station name is required." })>
</cfif>
<cftry>
<cfif StationID GT 0>
<!--- Update existing station --->
<cfset queryTimed("
UPDATE Stations SET Name = ?, Color = ? WHERE ID = ? AND BusinessID = ?
", [
{ value = Name, cfsqltype = "cf_sql_varchar" },
{ value = Color, cfsqltype = "cf_sql_varchar" },
{ value = StationID, cfsqltype = "cf_sql_integer" },
{ value = BusinessID, cfsqltype = "cf_sql_integer" }
], { datasource = "payfrit" })>
<cfelse>
<!--- Insert new station with auto-incrementing SortOrder --->
<cfset queryTimed("
INSERT INTO Stations (BusinessID, Name, Color, SortOrder, IsActive, AddedOn)
VALUES (?, ?, ?,
(SELECT COALESCE(MAX(s2.SortOrder), 0) + 1 FROM Stations s2 WHERE s2.BusinessID = ?),
1, NOW())
", [
{ value = BusinessID, cfsqltype = "cf_sql_integer" },
{ value = Name, cfsqltype = "cf_sql_varchar" },
{ value = Color, cfsqltype = "cf_sql_varchar" },
{ value = BusinessID, cfsqltype = "cf_sql_integer" }
], { datasource = "payfrit", result = "insertResult" })>
<cfset StationID = insertResult.generatedKey>
</cfif>
<!--- Query back the saved station --->
<cfset q = queryTimed("
SELECT ID, BusinessID, Name, Color, SortOrder
FROM Stations WHERE ID = ?
", [ { value = StationID, cfsqltype = "cf_sql_integer" } ], { datasource = "payfrit" })>
<cfif q.recordCount EQ 0>
<cfset apiAbort({ "OK": false, "ERROR": "not_found", "MESSAGE": "Station not found after save." })>
</cfif>
<cfset apiAbort({
"OK": true,
"ERROR": "",
"STATION": {
"StationID": q.ID,
"Name": q.Name,
"Color": q.Color,
"SortOrder": q.SortOrder
}
})>
<cfcatch>
<cfset apiAbort({
"OK": false,
"ERROR": "server_error",
"MESSAGE": "Failed to save station",
"DETAIL": cfcatch.message
})>
</cfcatch>
</cftry>