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/admin/_scripts/createParentBusiness.cfm
John 16a3b7c9a3 Replace queryExecute with queryTimed across all endpoints for perf tracking
Converts 200+ endpoint files to use queryTimed() wrapper which tracks
DB query count and execution time. Restores perf dashboard files that
were accidentally moved to _scripts/. Includes portal UI updates.
2026-02-02 00:28:37 -08:00

119 lines
3.5 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>
/**
* Create Parent Business (Shell)
*
* Creates a minimal business record to serve as a parent for other businesses.
* No menu items, categories, or hours required.
*
* POST body:
* {
* "Name": "Century Casino",
* "UserID": 1,
* "ChildBusinessIDs": [47, 48] // Optional: link existing businesses as children
* }
*
* Returns the new BusinessID
*/
function readJsonBody() {
var raw = getHttpRequestData().content;
if (isNull(raw) || len(trim(raw)) == 0) return {};
try {
var data = deserializeJSON(raw);
return isStruct(data) ? data : {};
} catch (any e) {
return {};
}
}
response = { "OK": false };
try {
data = readJsonBody();
Name = structKeyExists(data, "Name") ? trim(data.Name) : "";
UserID = structKeyExists(data, "UserID") ? val(data.UserID) : 0;
ChildBusinessIDs = structKeyExists(data, "ChildBusinessIDs") && isArray(data.ChildBusinessIDs) ? data.ChildBusinessIDs : [];
if (!len(Name)) {
response["ERROR"] = "missing_name";
response["MESSAGE"] = "Name is required";
writeOutput(serializeJSON(response));
abort;
}
if (UserID <= 0) {
response["ERROR"] = "missing_userid";
response["MESSAGE"] = "UserID is required";
writeOutput(serializeJSON(response));
abort;
}
// Create minimal address record (just a placeholder)
queryTimed("
INSERT INTO Addresses (Line1, UserID, AddressTypeID, AddedOn)
VALUES ('Parent Business - No Physical Location', :userID, 2, NOW())
", {
userID: UserID
}, { datasource = "payfrit" });
qAddr = queryTimed("SELECT LAST_INSERT_ID() as id", {}, { datasource = "payfrit" });
addressId = qAddr.id;
// Create parent business (no menu, no hours, just a shell)
queryTimed("
INSERT INTO Businesses (Name, UserID, AddressID, ParentBusinessID, BusinessDeliveryZipCodes, AddedOn)
VALUES (:name, :userId, :addressId, NULL, '', NOW())
", {
name: Name,
userId: UserID,
addressId: addressId
}, { datasource = "payfrit" });
qBiz = queryTimed("SELECT LAST_INSERT_ID() as id", {}, { datasource = "payfrit" });
newBusinessID = qBiz.id;
// Link address back to business
queryTimed("
UPDATE Addresses SET BusinessID = :bizId WHERE ID = :addrId
", {
bizId: newBusinessID,
addrId: addressId
}, { datasource = "payfrit" });
// Update child businesses if provided
linkedChildren = [];
for (childID in ChildBusinessIDs) {
childID = val(childID);
if (childID > 0) {
queryTimed("
UPDATE Businesses SET ParentBusinessID = :parentId WHERE ID = :childId
", {
parentId: newBusinessID,
childId: childID
}, { datasource = "payfrit" });
arrayAppend(linkedChildren, childID);
}
}
response["OK"] = true;
response["BusinessID"] = newBusinessID;
response["Name"] = Name;
response["MESSAGE"] = "Parent business created";
if (arrayLen(linkedChildren) > 0) {
response["LinkedChildren"] = linkedChildren;
}
} catch (any e) {
response["ERROR"] = "server_error";
response["MESSAGE"] = e.message;
response["DETAIL"] = e.detail;
}
writeOutput(serializeJSON(response));
</cfscript>