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/businesses/get.cfm
John Mizerek 6b66d2cef8 Fix normalized DB column names across all API files
Sweep of 26 API files to use prefixed column names matching the
database schema (e.g. BusinessID not ID, BusinessName not Name,
BusinessDeliveryFlatFee not DeliveryFlatFee, ServicePointName not Name).

Files fixed: auth, beacons, businesses, menu, orders, setup, stripe,
tasks, and workers endpoints.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-31 16:56:41 -08:00

160 lines
5.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>
/**
* Get Business Details
* POST: { BusinessID: int }
* Returns: { OK: true, BUSINESS: {...} } or { OK: false, ERROR: string }
*/
response = { "OK": false };
try {
// Get request data
requestBody = toString(getHttpRequestData().content);
if (len(requestBody) == 0) {
response["ERROR"] = "Request body is required";
writeOutput(serializeJSON(response));
abort;
}
requestData = deserializeJSON(requestBody);
businessID = val(requestData.BusinessID ?: 0);
if (businessID == 0) {
response["ERROR"] = "BusinessID is required";
writeOutput(serializeJSON(response));
abort;
}
// Get business details
q = queryExecute("
SELECT
BusinessID,
BusinessName,
BusinessPhone,
BusinessStripeAccountID,
BusinessStripeOnboardingComplete,
BusinessIsHiring,
BusinessHeaderImageExtension,
BusinessTaxRate,
BusinessBrandColor
FROM Businesses
WHERE BusinessID = :businessID
", { businessID: businessID }, { datasource: "payfrit" });
if (q.recordCount == 0) {
response["ERROR"] = "Business not found";
writeOutput(serializeJSON(response));
abort;
}
// Get address from Addresses table (either linked via AddressBusinessID or via Businesses.BusinessAddressID)
qAddr = queryExecute("
SELECT a.AddressLine1, a.AddressLine2, a.AddressCity, a.AddressZIPCode, s.Abbreviation
FROM Addresses a
LEFT JOIN tt_States s ON s.ID = a.AddressStateID
WHERE (a.AddressBusinessID = :businessID OR a.AddressID = (SELECT BusinessAddressID FROM Businesses WHERE BusinessID = :businessID))
AND a.AddressIsDeleted = 0
LIMIT 1
", { businessID: businessID }, { datasource: "payfrit" });
addressStr = "";
addressLine1 = "";
addressCity = "";
addressState = "";
addressZip = "";
if (qAddr.recordCount > 0) {
addressLine1 = qAddr.AddressLine1;
addressCity = qAddr.AddressCity;
addressState = qAddr.Abbreviation;
addressZip = qAddr.AddressZIPCode;
addressParts = [];
if (len(qAddr.AddressLine1)) arrayAppend(addressParts, qAddr.AddressLine1);
if (len(qAddr.AddressLine2)) arrayAppend(addressParts, qAddr.AddressLine2);
cityStateZip = [];
if (len(qAddr.AddressCity)) arrayAppend(cityStateZip, qAddr.AddressCity);
if (len(qAddr.Abbreviation)) arrayAppend(cityStateZip, qAddr.Abbreviation);
if (len(qAddr.AddressZIPCode)) arrayAppend(cityStateZip, qAddr.AddressZIPCode);
if (arrayLen(cityStateZip) > 0) arrayAppend(addressParts, arrayToList(cityStateZip, ", "));
addressStr = arrayToList(addressParts, ", ");
}
// Get hours from Hours table
qHours = queryExecute("
SELECT h.HoursDayID, h.HoursOpenTime, h.HoursClosingTime, d.Abbrev
FROM Hours h
JOIN tt_Days d ON d.ID = h.HoursDayID
WHERE h.HoursBusinessID = :businessID
ORDER BY h.HoursDayID
", { businessID: businessID }, { datasource: "payfrit" });
hoursArr = [];
hoursStr = "";
if (qHours.recordCount > 0) {
for (h in qHours) {
arrayAppend(hoursArr, {
"day": h.Abbrev,
"dayId": h.HoursDayID,
"open": timeFormat(h.HoursOpenTime, "h:mm tt"),
"close": timeFormat(h.HoursClosingTime, "h:mm tt")
});
}
// Build readable hours string (group similar days)
hourGroups = {};
for (h in hoursArr) {
key = h.open & "-" & h.close;
if (!structKeyExists(hourGroups, key)) {
hourGroups[key] = [];
}
arrayAppend(hourGroups[key], h.day);
}
hourStrParts = [];
for (key in hourGroups) {
days = hourGroups[key];
arrayAppend(hourStrParts, arrayToList(days, ",") & ": " & key);
}
hoursStr = arrayToList(hourStrParts, ", ");
}
// Build business object
taxRate = isNumeric(q.BusinessTaxRate) ? q.BusinessTaxRate : 0;
business = {
"BusinessID": q.BusinessID,
"Name": q.BusinessName,
"Address": addressStr,
"Line1": addressLine1,
"City": addressCity,
"AddressState": addressState,
"AddressZip": addressZip,
"Phone": q.BusinessPhone,
"Hours": hoursStr,
"HoursDetail": hoursArr,
"StripeConnected": (len(q.BusinessStripeAccountID) > 0 && q.BusinessStripeOnboardingComplete == 1),
"IsHiring": q.BusinessIsHiring == 1,
"TaxRate": taxRate,
"TaxRatePercent": taxRate * 100,
"BrandColor": len(q.BusinessBrandColor) ? (left(q.BusinessBrandColor, 1) == chr(35) ? q.BusinessBrandColor : chr(35) & q.BusinessBrandColor) : ""
};
// Add header image URL if extension exists
if (len(q.BusinessHeaderImageExtension)) {
business["HeaderImageURL"] = "https://biz.payfrit.com/uploads/headers/" & q.BusinessID & "." & q.BusinessHeaderImageExtension;
}
response["OK"] = true;
response["BUSINESS"] = business;
} catch (any e) {
response["ERROR"] = e.message;
}
try{logPerf(0);}catch(any e){}
writeOutput(serializeJSON(response));
</cfscript>