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/setDefault.cfm
John Mizerek e30a757c39 Fix missing datasource in addresses/list.cfm and LIKE on INT column in setDefault.cfm
- list.cfm: add missing datasource: "payfrit" parameter to queryExecute
- setDefault.cfm: change AddressTypeID LIKE '%2%' to AddressTypeID = 2 (INT column)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-31 21:05:06 -08:00

95 lines
2.4 KiB
Text

<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/json; charset=utf-8">
<!--- Set an address as the default delivery address --->
<cfscript>
function readJsonBody() {
var raw = getHttpRequestData().content;
if (isNull(raw) || len(trim(toString(raw))) == 0) return {};
try {
var data = deserializeJSON(toString(raw));
return isStruct(data) ? data : {};
} catch (any e) {
return {};
}
}
try {
userId = request.UserID ?: 0;
if (userId <= 0) {
writeOutput(serializeJSON({
"OK": false,
"ERROR": "unauthorized",
"MESSAGE": "Authentication required"
}));
abort;
}
data = readJsonBody();
addressId = val(data.AddressID ?: 0);
if (addressId <= 0) {
writeOutput(serializeJSON({
"OK": false,
"ERROR": "missing_field",
"MESSAGE": "AddressID is required"
}));
abort;
}
// Verify address belongs to user
qCheck = queryExecute("
SELECT ID
FROM Addresses
WHERE ID = :addressId
AND UserID = :userId
AND IsDeleted = 0
", {
addressId: { value: addressId, cfsqltype: "cf_sql_integer" },
userId: { value: userId, cfsqltype: "cf_sql_integer" }
}, { datasource: "payfrit" });
if (qCheck.recordCount == 0) {
writeOutput(serializeJSON({
"OK": false,
"ERROR": "not_found",
"MESSAGE": "Address not found"
}));
abort;
}
// Clear all defaults for this user
queryExecute("
UPDATE Addresses
SET IsDefaultDelivery = 0
WHERE UserID = :userId
AND (BusinessID = 0 OR BusinessID IS NULL)
AND AddressTypeID = 2
", {
userId: { value: userId, cfsqltype: "cf_sql_integer" }
}, { datasource: "payfrit" });
// Set this one as default
queryExecute("
UPDATE Addresses
SET IsDefaultDelivery = 1
WHERE ID = :addressId
", {
addressId: { value: addressId, cfsqltype: "cf_sql_integer" }
}, { datasource: "payfrit" });
writeOutput(serializeJSON({
"OK": true,
"MESSAGE": "Default address updated"
}));
} catch (any e) {
writeOutput(serializeJSON({
"OK": false,
"ERROR": "server_error",
"MESSAGE": e.message
}));
}
</cfscript>