137 lines
4.4 KiB
Text
137 lines
4.4 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>
|
|
/*
|
|
FILE: C:\lucee\tomcat\webapps\ROOT\biz.payfrit.com\api\servicepoints\list.cfm
|
|
|
|
MVP:
|
|
- PUBLIC (no login required)
|
|
- Reads BusinessID from JSON body
|
|
- Optional: onlyActive (default true)
|
|
- Returns hump-case JSON:
|
|
OK, ERROR, BusinessID, COUNT, ServicePoints
|
|
*/
|
|
|
|
function jsonString(val) {
|
|
// Lucee-safe string escaping:
|
|
// serializeJSON("abc") -> "\"abc\"" ; strip surrounding quotes
|
|
s = serializeJSON(toString(val));
|
|
return mid(s, 2, len(s) - 2);
|
|
}
|
|
|
|
function apiAbort(obj) {
|
|
writeOutput('{');
|
|
writeOutput('"OK":false');
|
|
|
|
if (structKeyExists(obj, "ERROR")) {
|
|
writeOutput(',"ERROR":"' & jsonString(obj.ERROR) & '"');
|
|
}
|
|
if (structKeyExists(obj, "MESSAGE")) {
|
|
writeOutput(',"MESSAGE":"' & jsonString(obj.MESSAGE) & '"');
|
|
}
|
|
if (structKeyExists(obj, "DETAIL")) {
|
|
writeOutput(',"DETAIL":"' & jsonString(obj.DETAIL) & '"');
|
|
}
|
|
|
|
writeOutput('}');
|
|
abort;
|
|
}
|
|
|
|
function readJsonBody() {
|
|
raw = toString(getHttpRequestData().content);
|
|
if (isNull(raw) || len(trim(raw)) EQ 0) return {};
|
|
|
|
try {
|
|
parsed = deserializeJSON(raw);
|
|
} catch(any e) {
|
|
apiAbort({ ERROR="bad_json", MESSAGE="Invalid JSON body" });
|
|
}
|
|
|
|
if (!isStruct(parsed)) return {};
|
|
return parsed;
|
|
}
|
|
|
|
data = readJsonBody();
|
|
|
|
// Require BusinessID in JSON body for public MVP endpoint
|
|
if (!structKeyExists(data, "BusinessID") || !isNumeric(data.BusinessID) || int(data.BusinessID) LTE 0) {
|
|
apiAbort({ ERROR="missing_businessid", MESSAGE="Body must include numeric BusinessID" });
|
|
}
|
|
BusinessID = int(data.BusinessID);
|
|
|
|
// Default: only active service points unless onlyActive is explicitly false/0
|
|
onlyActive = true;
|
|
if (structKeyExists(data, "onlyActive")) {
|
|
if (isBoolean(data.onlyActive)) {
|
|
onlyActive = data.onlyActive;
|
|
} else if (isNumeric(data.onlyActive)) {
|
|
onlyActive = (int(data.onlyActive) EQ 1);
|
|
} else if (isSimpleValue(data.onlyActive)) {
|
|
onlyActive = (lcase(trim(toString(data.onlyActive))) EQ "true");
|
|
}
|
|
}
|
|
|
|
// Datasource (use existing app config)
|
|
dsn = "";
|
|
if (structKeyExists(application, "datasource") && len(trim(toString(application.datasource)))) {
|
|
dsn = trim(toString(application.datasource));
|
|
} else if (structKeyExists(application, "dsn") && len(trim(toString(application.dsn)))) {
|
|
dsn = trim(toString(application.dsn));
|
|
}
|
|
if (!len(dsn)) {
|
|
apiAbort({ ERROR="missing_datasource", MESSAGE="application.datasource is not set" });
|
|
}
|
|
</cfscript>
|
|
|
|
<cfquery name="q" datasource="#dsn#">
|
|
SELECT
|
|
ServicePointID,
|
|
ServicePointBusinessID,
|
|
ServicePointName,
|
|
ServicePointTypeID,
|
|
ServicePointCode,
|
|
ServicePointDescription,
|
|
ServicePointSortOrder,
|
|
ServicePointIsActive,
|
|
ServicePointCreatedAt,
|
|
ServicePointUpdatedAt
|
|
FROM ServicePoints
|
|
WHERE ServicePointBusinessID = <cfqueryparam cfsqltype="cf_sql_integer" value="#BusinessID#">
|
|
<cfif onlyActive>
|
|
AND ServicePointIsActive = 1
|
|
</cfif>
|
|
ORDER BY ServicePointSortOrder, ServicePointName, ServicePointID
|
|
</cfquery>
|
|
|
|
<cfscript>
|
|
// Manual JSON output for exact key casing
|
|
writeOutput('{');
|
|
writeOutput('"OK":true');
|
|
writeOutput(',"ERROR":""');
|
|
writeOutput(',"BusinessID":' & BusinessID);
|
|
writeOutput(',"COUNT":' & q.recordCount);
|
|
writeOutput(',"ServicePoints":[');
|
|
|
|
for (i = 1; i LTE q.recordCount; i = i + 1) {
|
|
if (i GT 1) writeOutput(',');
|
|
|
|
writeOutput('{');
|
|
writeOutput('"ServicePointID":' & q.ServicePointID[i]);
|
|
writeOutput(',"BusinessID":' & q.ServicePointBusinessID[i]);
|
|
writeOutput(',"ServicePointName":"' & jsonString(q.ServicePointName[i]) & '"');
|
|
writeOutput(',"ServicePointTypeID":' & q.ServicePointTypeID[i]);
|
|
writeOutput(',"ServicePointCode":' & (isNull(q.ServicePointCode[i]) ? 'null' : '"' & jsonString(q.ServicePointCode[i]) & '"'));
|
|
writeOutput(',"ServicePointDescription":' & (isNull(q.ServicePointDescription[i]) ? 'null' : '"' & jsonString(q.ServicePointDescription[i]) & '"'));
|
|
writeOutput(',"ServicePointSortOrder":' & q.ServicePointSortOrder[i]);
|
|
writeOutput(',"ServicePointIsActive":' & q.ServicePointIsActive[i]);
|
|
writeOutput(',"ServicePointCreatedAt":"' & jsonString(q.ServicePointCreatedAt[i] & "") & '"');
|
|
writeOutput(',"ServicePointUpdatedAt":"' & jsonString(q.ServicePointUpdatedAt[i] & "") & '"');
|
|
writeOutput('}');
|
|
}
|
|
|
|
writeOutput(']}');
|
|
</cfscript>
|