payfrit-works/api/servicepoints/save.cfm
John Mizerek d4e0ae1162 Add branding features: header upload and brand color picker
- Add uploadHeader.cfm API for 1200px header images
- Add saveBrandColor.cfm API for hex color storage
- Add Branding section to menu builder sidebar
- Fix header upload path and permissions
- Various beacon and service point API improvements

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-18 12:14:24 -08:00

146 lines
5.3 KiB
Text

<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/json; charset=utf-8" reset="true">
<cfheader name="Cache-Control" value="no-store">
<cftry>
<cfscript>
function apiAbort(obj) {
writeOutput(serializeJSON(obj));
abort;
}
function readJsonBody() {
raw = toString(getHttpRequestData().content);
if (isNull(raw) || len(trim(raw)) EQ 0) return {};
try {
parsed = deserializeJSON(raw);
} catch(any e) {
apiAbort({ OK=false, ERROR="bad_json", MESSAGE="Invalid JSON body" });
}
if (!isStruct(parsed)) return {};
return parsed;
}
function normStr(v) {
if (isNull(v)) return "";
return trim(toString(v));
}
data = readJsonBody();
if (!structKeyExists(request, "BusinessID") || !isNumeric(request.BusinessID) || request.BusinessID LTE 0) {
apiAbort({ OK=false, ERROR="no_business_selected" });
}
if (!structKeyExists(data, "ServicePointName") || len(normStr(data.ServicePointName)) EQ 0) {
apiAbort({ OK=false, ERROR="missing_name", MESSAGE="ServicePointName is required" });
}
servicePointId = 0;
if (structKeyExists(data, "ServicePointID") && isNumeric(data.ServicePointID) && int(data.ServicePointID) GT 0) {
servicePointId = int(data.ServicePointID);
}
spName = normStr(data.ServicePointName);
spCode = structKeyExists(data, "ServicePointCode") ? normStr(data.ServicePointCode) : "";
spTypeID = structKeyExists(data, "ServicePointTypeID") && isNumeric(data.ServicePointTypeID) ? int(data.ServicePointTypeID) : 1;
sortOrder = structKeyExists(data, "ServicePointSortOrder") && isNumeric(data.ServicePointSortOrder) ? int(data.ServicePointSortOrder) : 0;
isActive = 1;
if (structKeyExists(data, "IsActive")) {
if (isBoolean(data.IsActive)) isActive = (data.IsActive ? 1 : 0);
else if (isNumeric(data.IsActive)) isActive = int(data.IsActive);
else if (isSimpleValue(data.IsActive)) isActive = (lcase(trim(toString(data.IsActive))) EQ "true" ? 1 : 0);
}
</cfscript>
<cfif servicePointId GT 0>
<!--- Update, scoped to this business --->
<cfquery datasource="payfrit">
UPDATE ServicePoints
SET
ServicePointName = <cfqueryparam cfsqltype="cf_sql_varchar" value="#spName#">,
ServicePointCode = <cfqueryparam cfsqltype="cf_sql_varchar" value="#spCode#" null="#(len(spCode) EQ 0)#">,
ServicePointTypeID = <cfqueryparam cfsqltype="cf_sql_integer" value="#spTypeID#">,
ServicePointIsActive = <cfqueryparam cfsqltype="cf_sql_tinyint" value="#isActive#">,
ServicePointSortOrder = <cfqueryparam cfsqltype="cf_sql_integer" value="#sortOrder#">
WHERE ServicePointID = <cfqueryparam cfsqltype="cf_sql_integer" value="#servicePointId#">
AND ServicePointBusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
</cfquery>
<!--- confirm it exists/belongs to business --->
<cfquery name="qCheck" datasource="payfrit">
SELECT ServicePointID
FROM ServicePoints
WHERE ServicePointID = <cfqueryparam cfsqltype="cf_sql_integer" value="#servicePointId#">
AND ServicePointBusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
LIMIT 1
</cfquery>
<cfif qCheck.recordCount EQ 0>
<cfoutput>#serializeJSON({ OK=false, ERROR="not_found" })#</cfoutput>
<cfabort>
</cfif>
<cfelse>
<!--- Insert --->
<cfquery datasource="payfrit">
INSERT INTO ServicePoints (
ServicePointBusinessID,
ServicePointName,
ServicePointCode,
ServicePointTypeID,
ServicePointIsActive,
ServicePointSortOrder
) VALUES (
<cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#spName#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#spCode#" null="#(len(spCode) EQ 0)#">,
<cfqueryparam cfsqltype="cf_sql_integer" value="#spTypeID#">,
<cfqueryparam cfsqltype="cf_sql_tinyint" value="#isActive#">,
<cfqueryparam cfsqltype="cf_sql_integer" value="#sortOrder#">
)
</cfquery>
<cfquery name="qId" datasource="payfrit">
SELECT LAST_INSERT_ID() AS ServicePointID
</cfquery>
<cfset servicePointId = qId.ServicePointID>
</cfif>
<!--- Return saved row --->
<cfquery name="qOut" datasource="payfrit">
SELECT
ServicePointID,
ServicePointBusinessID,
ServicePointName,
ServicePointCode,
ServicePointTypeID,
ServicePointIsActive,
ServicePointSortOrder
FROM ServicePoints
WHERE ServicePointID = <cfqueryparam cfsqltype="cf_sql_integer" value="#servicePointId#">
AND ServicePointBusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
LIMIT 1
</cfquery>
<cfset servicePoint = {
"ServicePointID" = qOut.ServicePointID,
"BusinessID" = qOut.ServicePointBusinessID,
"ServicePointName" = qOut.ServicePointName,
"ServicePointCode" = qOut.ServicePointCode,
"ServicePointTypeID"= qOut.ServicePointTypeID,
"IsActive" = qOut.ServicePointIsActive,
"ServicePointSortOrder" = qOut.ServicePointSortOrder
}>
<cfoutput>#serializeJSON({ OK=true, ERROR="", SERVICEPOINT=servicePoint })#</cfoutput>
<cfcatch type="any">
<cfheader statuscode="200" statustext="OK">
<cfcontent type="application/json; charset=utf-8" reset="true">
<cfoutput>#serializeJSON({ OK=false, ERROR="server_error", MESSAGE=cfcatch.message, DETAIL=cfcatch.detail })#</cfoutput>
</cfcatch>
</cftry>