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/addresses/list.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

99 lines
2.7 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(required struct payload) {
writeOutput(serializeJSON(payload));
abort;
}
function getHeader(name) {
try {
req = getPageContext().getRequest();
val = req.getHeader(arguments.name);
if (!isNull(val)) return trim(val);
} catch (any e) {
k = "HTTP_" & ucase(reReplace(arguments.name, "[^A-Za-z0-9]", "_", "all"));
if (structKeyExists(cgi, k)) return trim(cgi[k]);
}
return "";
}
// Get authenticated user
userId = 0;
if (structKeyExists(request, "UserID") && isNumeric(request.UserID) && request.UserID > 0) {
userId = request.UserID;
} else {
userToken = getHeader("X-User-Token");
if (len(userToken)) {
try {
qTok = queryExecute(
"SELECT UserID FROM UserTokens WHERE Token = ? LIMIT 1",
[{ value = userToken, cfsqltype = "cf_sql_varchar" }],
{ datasource = "payfrit" }
);
if (qTok.recordCount EQ 1) {
userId = qTok.UserID;
}
} catch (any e) {}
}
}
if (userId <= 0) {
apiAbort({ "OK": false, "ERROR": "not_logged_in", "MESSAGE": "Authentication required" });
}
try {
// Get user's delivery addresses
qAddresses = queryExecute("
SELECT
a.AddressID AS ID,
a.AddressIsDefaultDelivery AS IsDefaultDelivery,
a.AddressLine1 AS Line1,
a.AddressLine2 AS Line2,
a.AddressCity AS City,
a.AddressStateID AS StateID,
s.tt_StateAbbreviation as StateAbbreviation,
s.tt_StateName as StateName,
a.AddressZIPCode AS ZIPCode
FROM Addresses a
LEFT JOIN tt_States s ON a.AddressStateID = s.tt_StateID
WHERE a.AddressUserID = :userId
AND (a.AddressBusinessID = 0 OR a.AddressBusinessID IS NULL)
AND a.AddressTypeID = 2
AND a.AddressIsDeleted = 0
ORDER BY a.AddressIsDefaultDelivery DESC, a.AddressID DESC
", {
userId: { value = userId, cfsqltype = "cf_sql_integer" }
});
addresses = [];
for (row in qAddresses) {
arrayAppend(addresses, {
"AddressID": row.ID,
"IsDefault": row.IsDefaultDelivery == 1,
"Line1": row.Line1,
"Line2": row.Line2 ?: "",
"City": row.City,
"StateID": row.StateID,
"StateAbbr": row.StateAbbreviation ?: "",
"ZIPCode": row.ZIPCode,
"DisplayText": row.Line1 & (len(row.Line2) ? ", " & row.Line2 : "") & ", " & row.City & ", " & (row.StateAbbreviation ?: "") & " " & row.ZIPCode
});
}
writeOutput(serializeJSON({
"OK": true,
"ADDRESSES": addresses
}));
} catch (any e) {
apiAbort({
"OK": false,
"ERROR": "server_error",
"MESSAGE": e.message
});
}
</cfscript>