- list.cfm: Use GROUP BY to show unique addresses only, removed BusinessID filter, simplified aggregation for better MySQL compat - delete.cfm: Delete ALL addresses matching the same address content (Line1, Line2, City, State, ZIP) to keep data clean when user deletes a deduplicated address Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
103 lines
3.1 KiB
Text
103 lines
3.1 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 with GROUP BY to show unique addresses only
|
|
qAddresses = queryExecute("
|
|
SELECT
|
|
MIN(a.AddressID) as AddressID,
|
|
MAX(a.AddressLabel) as AddressLabel,
|
|
MAX(a.AddressIsDefaultDelivery) as AddressIsDefaultDelivery,
|
|
a.AddressLine1,
|
|
a.AddressLine2,
|
|
a.AddressCity,
|
|
a.AddressStateID,
|
|
MAX(s.tt_StateAbbreviation) as StateAbbreviation,
|
|
MAX(s.tt_StateName) as StateName,
|
|
a.AddressZIPCode
|
|
FROM Addresses a
|
|
LEFT JOIN tt_States s ON a.AddressStateID = s.tt_StateID
|
|
WHERE a.AddressUserID = :userId
|
|
AND a.AddressTypeID LIKE '%2%'
|
|
AND a.AddressIsDeleted = 0
|
|
GROUP BY a.AddressLine1, a.AddressLine2, a.AddressCity, a.AddressStateID, a.AddressZIPCode
|
|
ORDER BY MAX(a.AddressIsDefaultDelivery) DESC, MIN(a.AddressID) DESC
|
|
", {
|
|
userId: { value = userId, cfsqltype = "cf_sql_integer" }
|
|
});
|
|
|
|
addresses = [];
|
|
for (row in qAddresses) {
|
|
arrayAppend(addresses, {
|
|
"AddressID": row.AddressID,
|
|
"Label": len(row.AddressLabel) ? row.AddressLabel : "Address",
|
|
"IsDefault": row.AddressIsDefaultDelivery == 1,
|
|
"Line1": row.AddressLine1,
|
|
"Line2": row.AddressLine2 ?: "",
|
|
"City": row.AddressCity,
|
|
"StateID": row.AddressStateID,
|
|
"StateAbbr": row.StateAbbreviation ?: "",
|
|
"StateName": row.StateName ?: "",
|
|
"ZIPCode": row.AddressZIPCode,
|
|
"DisplayText": row.AddressLine1 & (len(row.AddressLine2) ? ", " & row.AddressLine2 : "") & ", " & row.AddressCity & ", " & (row.StateAbbreviation ?: "") & " " & row.AddressZIPCode
|
|
});
|
|
}
|
|
|
|
writeOutput(serializeJSON({
|
|
"OK": true,
|
|
"ADDRESSES": addresses
|
|
}));
|
|
|
|
} catch (any e) {
|
|
apiAbort({
|
|
"OK": false,
|
|
"ERROR": "server_error",
|
|
"MESSAGE": e.message,
|
|
"LINE": e.tagContext[1].line ?: 0
|
|
});
|
|
}
|
|
</cfscript>
|