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/add.cfm
John Mizerek 1210249f54 Normalize database column and table names across entire codebase
Update all SQL queries, query result references, and ColdFusion code to match
the renamed database schema. Tables use plural CamelCase, PKs are all `ID`,
column prefixes stripped (e.g. BusinessName→Name, UserFirstName→FirstName).

Key changes:
- Strip table-name prefixes from all column references (Businesses, Users,
  Addresses, Hours, Menus, Categories, Items, Stations, Orders,
  OrderLineItems, Tasks, TaskCategories, TaskRatings, QuickTaskTemplates,
  ScheduledTaskDefinitions, ChatMessages, Beacons, ServicePoints, Employees,
  VisitorTrackings, ApiPerfLogs, tt_States, tt_Days, tt_AddressTypes,
  tt_OrderTypes, tt_TaskTypes)
- Rename PK references from {TableName}ID to ID in all queries
- Rewrite 7 admin beacon files to use ServicePoints.BeaconID instead of
  dropped lt_Beacon_Businesses_ServicePoints link table
- Rewrite beacon assignment files (list, save, delete) for new schema
- Fix FK references incorrectly changed to ID (OrderLineItems.OrderID,
  Categories.MenuID, Tasks.CategoryID, ServicePoints.BeaconID)
- Update Addresses: AddressLat→Latitude, AddressLng→Longitude
- Update Users: UserPassword→Password, UserIsEmailVerified→IsEmailVerified,
  UserIsActive→IsActive, UserBalance→Balance, etc.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-30 15:39:12 -08:00

152 lines
4.7 KiB
Text

<cfsetting showdebugoutput="false">
<cfsetting enablecfoutputonly="true">
<cfcontent type="application/json; charset=utf-8">
<!--- Add a new delivery address for the authenticated user --->
<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 {
// Get authenticated user ID from request context (set by Application.cfm)
userId = request.UserID ?: 0;
if (userId <= 0) {
writeOutput(serializeJSON({
"OK": false,
"ERROR": "unauthorized",
"MESSAGE": "Authentication required"
}));
abort;
}
data = readJsonBody();
// Required fields
line1 = trim(data.Line1 ?: "");
city = trim(data.City ?: "");
stateId = val(data.StateID ?: 0);
zipCode = trim(data.ZIPCode ?: "");
// Optional fields
line2 = trim(data.Line2 ?: "");
setAsDefault = (data.SetAsDefault ?: false) == true;
// Hardcoded to delivery address type
typeId = 2;
// Validation
if (len(line1) == 0 || len(city) == 0 || stateId <= 0 || len(zipCode) == 0) {
writeOutput(serializeJSON({
"OK": false,
"ERROR": "missing_fields",
"MESSAGE": "Line1, City, StateID, and ZIPCode are required"
}));
abort;
}
// If setting as default, clear other defaults first (for same type)
if (setAsDefault) {
queryExecute("
UPDATE Addresses
SET AddressIsDefaultDelivery = 0
WHERE UserID = :userId
AND (BusinessID = 0 OR BusinessID IS NULL)
AND AddressTypeID = :typeId
", {
userId: { value: userId, cfsqltype: "cf_sql_integer" },
typeId: { value: typeId, cfsqltype: "cf_sql_integer" }
}, { datasource: "payfrit" });
}
// Get next AddressID
qNext = queryExecute("SELECT IFNULL(MAX(AddressID), 0) + 1 AS NextID FROM Addresses", {}, { datasource: "payfrit" });
newAddressId = qNext.NextID;
// Insert new address
queryExecute("
INSERT INTO Addresses (
AddressID,
UserID,
BusinessID,
AddressTypeID,
AddressLabel,
AddressIsDefaultDelivery,
Line1,
Line2,
City,
StateID,
ZIPCode,
IsDeleted,
AddedOn
) VALUES (
:addressId,
:userId,
0,
:typeId,
:label,
:isDefault,
:line1,
:line2,
:city,
:stateId,
:zipCode,
0,
:addedOn
)
", {
addressId: { value: newAddressId, cfsqltype: "cf_sql_integer" },
userId: { value: userId, cfsqltype: "cf_sql_integer" },
typeId: { value: typeId, cfsqltype: "cf_sql_integer" },
label: { value: label, cfsqltype: "cf_sql_varchar" },
isDefault: { value: setAsDefault ? 1 : 0, cfsqltype: "cf_sql_integer" },
line1: { value: line1, cfsqltype: "cf_sql_varchar" },
line2: { value: line2, cfsqltype: "cf_sql_varchar" },
city: { value: city, cfsqltype: "cf_sql_varchar" },
stateId: { value: stateId, cfsqltype: "cf_sql_integer" },
zipCode: { value: zipCode, cfsqltype: "cf_sql_varchar" },
addedOn: { value: now(), cfsqltype: "cf_sql_timestamp" }
}, { datasource: "payfrit" });
// Get state info for response
qState = queryExecute("SELECT Abbreviation as StateAbbreviation, Name as StateName FROM tt_States WHERE ID = :stateId", {
stateId: { value: stateId, cfsqltype: "cf_sql_integer" }
}, { datasource: "payfrit" });
stateAbbr = qState.recordCount ? qState.StateAbbreviation : "";
stateName = qState.recordCount ? qState.StateName : "";
writeOutput(serializeJSON({
"OK": true,
"ADDRESS": {
"AddressID": newAddressId,
"TypeID": typeId,
"Label": len(label) ? label : "Address",
"IsDefault": setAsDefault,
"Line1": line1,
"Line2": line2,
"City": city,
"StateID": stateId,
"StateAbbr": stateAbbr,
"StateName": stateName,
"ZIPCode": zipCode,
"DisplayText": line1 & (len(line2) ? ", " & line2 : "") & ", " & city & ", " & stateAbbr & " " & zipCode
}
}));
} catch (any e) {
writeOutput(serializeJSON({
"OK": false,
"ERROR": "server_error",
"MESSAGE": e.message
}));
}
</cfscript>