- HUD now displays "Payfrit Tasks - <BusinessName>" by fetching from getBusiness API - Fixed portal Task HUD button to link to /hud/index.html instead of /hud/ Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
137 lines
4.6 KiB
Text
137 lines
4.6 KiB
Text
<cfsetting showdebugoutput="false">
|
|
<cfsetting enablecfoutputonly="true">
|
|
|
|
<cfcontent type="application/json; charset=utf-8" reset="true">
|
|
<cfheader name="Cache-Control" value="no-store">
|
|
|
|
<cfscript>
|
|
function apiAbort(obj){
|
|
writeOutput(serializeJSON(obj));
|
|
abort;
|
|
}
|
|
|
|
function readJsonBody(){
|
|
raw = toString(getHttpRequestData().content);
|
|
if (isNull(raw) || len(trim(raw)) EQ 0){
|
|
apiAbort({OK=false,ERROR="missing_body"});
|
|
}
|
|
try {
|
|
parsed = deserializeJSON(raw);
|
|
} catch(any e){
|
|
apiAbort({OK=false,ERROR="bad_json",MESSAGE="Invalid JSON body"});
|
|
}
|
|
if (!isStruct(parsed)){
|
|
apiAbort({OK=false,ERROR="bad_json",MESSAGE="JSON must be an object"});
|
|
}
|
|
return parsed;
|
|
}
|
|
|
|
function normStr(v){
|
|
if (isNull(v)) return "";
|
|
return trim(toString(v));
|
|
}
|
|
|
|
/* ---------- AUTH CONTEXT ---------- */
|
|
if (!structKeyExists(request,"BusinessID") || !isNumeric(request.BusinessID) || request.BusinessID LTE 0){
|
|
apiAbort({OK=false,ERROR="no_business_selected"});
|
|
}
|
|
|
|
/* ---------- INPUT ---------- */
|
|
data = readJsonBody();
|
|
|
|
if (!structKeyExists(data,"BeaconID") || !isNumeric(data.BeaconID) || int(data.BeaconID) LTE 0){
|
|
apiAbort({OK=false,ERROR="missing_BeaconID"});
|
|
}
|
|
if (!structKeyExists(data,"ServicePointID") || !isNumeric(data.ServicePointID) || int(data.ServicePointID) LTE 0){
|
|
apiAbort({OK=false,ERROR="missing_ServicePointID"});
|
|
}
|
|
|
|
BeaconID = int(data.BeaconID);
|
|
ServicePointID = int(data.ServicePointID);
|
|
Notes = "";
|
|
if (structKeyExists(data,"Notes")){
|
|
Notes = left(normStr(data.Notes), 255);
|
|
}
|
|
</cfscript>
|
|
|
|
<!--- Validate Beacon belongs to Business OR to Business's parent --->
|
|
<cfquery name="qBiz" datasource="payfrit">
|
|
SELECT BusinessID, BusinessParentBusinessID
|
|
FROM Businesses
|
|
WHERE BusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
|
|
LIMIT 1
|
|
</cfquery>
|
|
|
|
<cfquery name="qB" datasource="payfrit">
|
|
SELECT BeaconID
|
|
FROM Beacons
|
|
WHERE BeaconID = <cfqueryparam cfsqltype="cf_sql_integer" value="#BeaconID#">
|
|
AND (
|
|
BeaconBusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
|
|
<cfif qBiz.recordCount GT 0 AND len(trim(qBiz.BusinessParentBusinessID)) GT 0 AND isNumeric(qBiz.BusinessParentBusinessID) AND qBiz.BusinessParentBusinessID GT 0>
|
|
OR BeaconBusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#qBiz.BusinessParentBusinessID#">
|
|
</cfif>
|
|
)
|
|
LIMIT 1
|
|
</cfquery>
|
|
<cfif qB.recordCount EQ 0>
|
|
<cfoutput>#serializeJSON({OK=false,ERROR="beacon_not_found_for_business"})#</cfoutput>
|
|
<cfabort>
|
|
</cfif>
|
|
|
|
<!--- Validate ServicePoint belongs to Business --->
|
|
<cfquery name="qS" 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 qS.recordCount EQ 0>
|
|
<cfoutput>#serializeJSON({OK=false,ERROR="servicepoint_not_found_for_business"})#</cfoutput>
|
|
<cfabort>
|
|
</cfif>
|
|
|
|
<!--- Check if THIS BUSINESS already has this exact beacon+servicepoint combo --->
|
|
<!--- (Multiple businesses CAN share the same beacon, but one business shouldn't duplicate) --->
|
|
<cfquery name="qDuplicate" datasource="payfrit">
|
|
SELECT lt_Beacon_Businesses_ServicePointID
|
|
FROM lt_Beacon_Businesses_ServicePoints
|
|
WHERE BusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">
|
|
AND BeaconID = <cfqueryparam cfsqltype="cf_sql_integer" value="#BeaconID#">
|
|
AND ServicePointID = <cfqueryparam cfsqltype="cf_sql_integer" value="#ServicePointID#">
|
|
LIMIT 1
|
|
</cfquery>
|
|
<cfif qDuplicate.recordCount GT 0>
|
|
<cfoutput>#serializeJSON({OK=false,ERROR="assignment_already_exists"})#</cfoutput>
|
|
<cfabort>
|
|
</cfif>
|
|
|
|
<!--- INSERT --->
|
|
<cfquery datasource="payfrit">
|
|
INSERT INTO lt_Beacon_Businesses_ServicePoints
|
|
(BusinessID, BeaconID, ServicePointID,
|
|
lt_Beacon_Businesses_ServicePointAssignedByUserID,
|
|
lt_Beacon_Businesses_ServicePointNotes)
|
|
VALUES
|
|
(
|
|
<cfqueryparam cfsqltype="cf_sql_integer" value="#request.BusinessID#">,
|
|
<cfqueryparam cfsqltype="cf_sql_integer" value="#BeaconID#">,
|
|
<cfqueryparam cfsqltype="cf_sql_integer" value="#ServicePointID#">,
|
|
<cfqueryparam cfsqltype="cf_sql_integer" value="1">,
|
|
<cfqueryparam cfsqltype="cf_sql_varchar" value="#Notes#" null="#(len(Notes) EQ 0)#">
|
|
)
|
|
</cfquery>
|
|
|
|
<cfquery name="qID" datasource="payfrit">
|
|
SELECT LAST_INSERT_ID() AS NewID
|
|
</cfquery>
|
|
|
|
<cfoutput>#serializeJSON({
|
|
"OK"=true,
|
|
"ACTION"="inserted",
|
|
"lt_Beacon_Businesses_ServicePointID"=qID.NewID,
|
|
"BeaconID"=BeaconID,
|
|
"ServicePointID"=ServicePointID,
|
|
"BusinessID"=(request.BusinessID & "")
|
|
})#</cfoutput>
|