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 dc9db32b58 Add API performance profiling, caching, and query optimizations
- Add queryTimed() wrapper and logPerf() for per-endpoint timing metrics
- Add api_perf_log table flush mechanism with background thread batching
- Add application-scope cache (appCacheGet/Put/Invalidate) with TTL
- Cache businesses/get (5m), addresses/states (24h), menu/items (2m)
- Fix N+1 queries in orders/history, orders/listForKDS (batch fetch)
- Fix correlated subquery in orders/getDetail (LEFT JOIN)
- Combine 4 queries into 1 in portal/stats (subselects)
- Optimize getForBuilder tree building with pre-indexed parent lookup
- Add cache invalidation in update, saveBrandColor, updateHours, saveFromBuilder
- New admin/perf.cfm dashboard (localhost-protected)
- Instrument top 10 endpoints with queryTimed + logPerf

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 20:41:27 -08:00

176 lines
5.9 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;
}
// Check cache (5 minute TTL)
cached = appCacheGet("biz_" & businessID, 300);
if (!isNull(cached)) {
logPerf();
writeOutput(cached);
abort;
}
// Get business details
q = queryTimed("
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 = queryTimed("
SELECT a.AddressLine1, a.AddressLine2, a.AddressCity, a.AddressZIPCode, s.tt_StateAbbreviation
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.tt_StateAbbreviation;
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.tt_StateAbbreviation)) arrayAppend(cityStateZip, qAddr.tt_StateAbbreviation);
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 = queryTimed("
SELECT h.HoursDayID, h.HoursOpenTime, h.HoursClosingTime, d.tt_DayAbbrev
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.tt_DayAbbrev,
"dayId": h.HoursDayID,
"open": timeFormat(h.HoursOpenTime, "h:mm tt"),
"close": timeFormat(h.HoursClosingTime, "h:mm tt")
});
}
// Build readable hours string (group similar days)
// Mon-Thu: 11am-10pm, Fri-Sat: 11am-11pm, Sun: 11am-10pm
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,
"BusinessName": q.BusinessName,
"BusinessAddress": addressStr,
"AddressLine1": addressLine1,
"AddressCity": addressCity,
"AddressState": addressState,
"AddressZip": addressZip,
"BusinessPhone": q.BusinessPhone,
"BusinessHours": hoursStr,
"BusinessHoursDetail": 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;
// Cache successful response
jsonResponse = serializeJSON(response);
appCachePut("biz_" & businessID, jsonResponse);
logPerf();
writeOutput(jsonResponse);
abort;
} catch (any e) {
response["ERROR"] = e.message;
}
logPerf();
writeOutput(serializeJSON(response));
</cfscript>