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 e92362f773 Fix prefixed column names for tt_States, tt_Days, tt_OrderTypes, ServicePoints, Users, Addresses, Hours tables
All lookup/reference tables use prefixed column names (tt_StateID, tt_StateAbbreviation,
tt_DayID, tt_DayAbbrev, tt_OrderTypeID, tt_OrderTypeName, ServicePointID, ServicePointName,
UserID, UserFirstName, UserLastName, AddressID, AddressLine1, etc). Updated all affected
queries to use correct column names with aliases to maintain API compatibility.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-31 17:38:33 -08:00

160 lines
5.5 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.tt_StateAbbreviation AS Abbreviation
FROM Addresses a
LEFT JOIN tt_States s ON s.tt_StateID = 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.tt_DayAbbrev AS Abbrev
FROM Hours h
JOIN tt_Days d ON d.tt_DayID = 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>