Add station CRUD endpoints and wire into portal UI
New endpoints: save.cfm (combined add/update) and delete.cfm (soft-delete with item unassignment). Registered in Application.cfm. Portal station-assignment page now uses real API calls for add, edit, and delete instead of client-side-only prompt(). Fixed key naming mismatch (StationName/StationColor → Name/Color) so the page works with real API data. Removed hardcoded demo fallbacks. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
94b5bbbce1
commit
f5ff9cdfeb
4 changed files with 271 additions and 31 deletions
|
|
@ -309,6 +309,8 @@ if (len(request._api_path)) {
|
|||
|
||||
// Stations endpoints
|
||||
if (findNoCase("/api/stations/list.cfm", request._api_path)) request._api_isPublic = true;
|
||||
if (findNoCase("/api/stations/save.cfm", request._api_path)) request._api_isPublic = true;
|
||||
if (findNoCase("/api/stations/delete.cfm", request._api_path)) request._api_isPublic = true;
|
||||
|
||||
// Ratings endpoints (setup + token-based submission)
|
||||
if (findNoCase("/api/ratings/setup.cfm", request._api_path)) request._api_isPublic = true;
|
||||
|
|
|
|||
72
api/stations/delete.cfm
Normal file
72
api/stations/delete.cfm
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
<cfsetting showdebugoutput="false">
|
||||
<cfsetting enablecfoutputonly="true">
|
||||
|
||||
<cffunction name="readJsonBody" access="public" returntype="struct" output="false">
|
||||
<cfset var raw = getHttpRequestData().content>
|
||||
<cfif isNull(raw) OR len(trim(raw)) EQ 0>
|
||||
<cfreturn {}>
|
||||
</cfif>
|
||||
<cftry>
|
||||
<cfset var data = deserializeJSON(raw)>
|
||||
<cfif isStruct(data)>
|
||||
<cfreturn data>
|
||||
<cfelse>
|
||||
<cfreturn {}>
|
||||
</cfif>
|
||||
<cfcatch>
|
||||
<cfreturn {}>
|
||||
</cfcatch>
|
||||
</cftry>
|
||||
</cffunction>
|
||||
|
||||
<cffunction name="apiAbort" access="public" returntype="void" output="true">
|
||||
<cfargument name="payload" type="struct" required="true">
|
||||
<cfcontent type="application/json; charset=utf-8">
|
||||
<cfoutput>#serializeJSON(arguments.payload)#</cfoutput>
|
||||
<cfabort>
|
||||
</cffunction>
|
||||
|
||||
<cfset data = readJsonBody()>
|
||||
<cfset BusinessID = val( structKeyExists(data,"BusinessID") ? data.BusinessID : 0 )>
|
||||
<cfset StationID = val( structKeyExists(data,"StationID") ? data.StationID : 0 )>
|
||||
|
||||
<cfif BusinessID LTE 0>
|
||||
<cfset apiAbort({ "OK": false, "ERROR": "missing_businessid", "MESSAGE": "BusinessID is required." })>
|
||||
</cfif>
|
||||
|
||||
<cfif StationID LTE 0>
|
||||
<cfset apiAbort({ "OK": false, "ERROR": "missing_stationid", "MESSAGE": "StationID is required." })>
|
||||
</cfif>
|
||||
|
||||
<cftry>
|
||||
<!--- Soft-delete the station --->
|
||||
<cfset queryTimed("
|
||||
UPDATE Stations SET IsActive = 0 WHERE ID = ? AND BusinessID = ?
|
||||
", [
|
||||
{ value = StationID, cfsqltype = "cf_sql_integer" },
|
||||
{ value = BusinessID, cfsqltype = "cf_sql_integer" }
|
||||
], { datasource = "payfrit" })>
|
||||
|
||||
<!--- Unassign any items that were on this station --->
|
||||
<cfset queryTimed("
|
||||
UPDATE Items SET StationID = 0 WHERE StationID = ? AND BusinessID = ?
|
||||
", [
|
||||
{ value = StationID, cfsqltype = "cf_sql_integer" },
|
||||
{ value = BusinessID, cfsqltype = "cf_sql_integer" }
|
||||
], { datasource = "payfrit" })>
|
||||
|
||||
<cfset apiAbort({
|
||||
"OK": true,
|
||||
"ERROR": "",
|
||||
"StationID": StationID
|
||||
})>
|
||||
|
||||
<cfcatch>
|
||||
<cfset apiAbort({
|
||||
"OK": false,
|
||||
"ERROR": "server_error",
|
||||
"MESSAGE": "Failed to delete station",
|
||||
"DETAIL": cfcatch.message
|
||||
})>
|
||||
</cfcatch>
|
||||
</cftry>
|
||||
99
api/stations/save.cfm
Normal file
99
api/stations/save.cfm
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
<cfsetting showdebugoutput="false">
|
||||
<cfsetting enablecfoutputonly="true">
|
||||
|
||||
<cffunction name="readJsonBody" access="public" returntype="struct" output="false">
|
||||
<cfset var raw = getHttpRequestData().content>
|
||||
<cfif isNull(raw) OR len(trim(raw)) EQ 0>
|
||||
<cfreturn {}>
|
||||
</cfif>
|
||||
<cftry>
|
||||
<cfset var data = deserializeJSON(raw)>
|
||||
<cfif isStruct(data)>
|
||||
<cfreturn data>
|
||||
<cfelse>
|
||||
<cfreturn {}>
|
||||
</cfif>
|
||||
<cfcatch>
|
||||
<cfreturn {}>
|
||||
</cfcatch>
|
||||
</cftry>
|
||||
</cffunction>
|
||||
|
||||
<cffunction name="apiAbort" access="public" returntype="void" output="true">
|
||||
<cfargument name="payload" type="struct" required="true">
|
||||
<cfcontent type="application/json; charset=utf-8">
|
||||
<cfoutput>#serializeJSON(arguments.payload)#</cfoutput>
|
||||
<cfabort>
|
||||
</cffunction>
|
||||
|
||||
<cfset data = readJsonBody()>
|
||||
<cfset BusinessID = val( structKeyExists(data,"BusinessID") ? data.BusinessID : 0 )>
|
||||
<cfset StationID = val( structKeyExists(data,"StationID") ? data.StationID : 0 )>
|
||||
<cfset Name = trim( structKeyExists(data,"Name") ? data.Name : "" )>
|
||||
<cfset Color = trim( structKeyExists(data,"Color") ? data.Color : "#666666" )>
|
||||
|
||||
<cfif BusinessID LTE 0>
|
||||
<cfset apiAbort({ "OK": false, "ERROR": "missing_businessid", "MESSAGE": "BusinessID is required." })>
|
||||
</cfif>
|
||||
|
||||
<cfif len(Name) EQ 0>
|
||||
<cfset apiAbort({ "OK": false, "ERROR": "missing_name", "MESSAGE": "Station name is required." })>
|
||||
</cfif>
|
||||
|
||||
<cftry>
|
||||
<cfif StationID GT 0>
|
||||
<!--- Update existing station --->
|
||||
<cfset queryTimed("
|
||||
UPDATE Stations SET Name = ?, Color = ? WHERE ID = ? AND BusinessID = ?
|
||||
", [
|
||||
{ value = Name, cfsqltype = "cf_sql_varchar" },
|
||||
{ value = Color, cfsqltype = "cf_sql_varchar" },
|
||||
{ value = StationID, cfsqltype = "cf_sql_integer" },
|
||||
{ value = BusinessID, cfsqltype = "cf_sql_integer" }
|
||||
], { datasource = "payfrit" })>
|
||||
<cfelse>
|
||||
<!--- Insert new station with auto-incrementing SortOrder --->
|
||||
<cfset queryTimed("
|
||||
INSERT INTO Stations (BusinessID, Name, Color, SortOrder, IsActive, AddedOn)
|
||||
VALUES (?, ?, ?,
|
||||
(SELECT COALESCE(MAX(s2.SortOrder), 0) + 1 FROM Stations s2 WHERE s2.BusinessID = ?),
|
||||
1, NOW())
|
||||
", [
|
||||
{ value = BusinessID, cfsqltype = "cf_sql_integer" },
|
||||
{ value = Name, cfsqltype = "cf_sql_varchar" },
|
||||
{ value = Color, cfsqltype = "cf_sql_varchar" },
|
||||
{ value = BusinessID, cfsqltype = "cf_sql_integer" }
|
||||
], { datasource = "payfrit", result = "insertResult" })>
|
||||
<cfset StationID = insertResult.generatedKey>
|
||||
</cfif>
|
||||
|
||||
<!--- Query back the saved station --->
|
||||
<cfset q = queryTimed("
|
||||
SELECT ID, BusinessID, Name, Color, SortOrder
|
||||
FROM Stations WHERE ID = ?
|
||||
", [ { value = StationID, cfsqltype = "cf_sql_integer" } ], { datasource = "payfrit" })>
|
||||
|
||||
<cfif q.recordCount EQ 0>
|
||||
<cfset apiAbort({ "OK": false, "ERROR": "not_found", "MESSAGE": "Station not found after save." })>
|
||||
</cfif>
|
||||
|
||||
<cfset apiAbort({
|
||||
"OK": true,
|
||||
"ERROR": "",
|
||||
"STATION": {
|
||||
"StationID": q.ID,
|
||||
"Name": q.Name,
|
||||
"Color": q.Color,
|
||||
"SortOrder": q.SortOrder
|
||||
}
|
||||
})>
|
||||
|
||||
<cfcatch>
|
||||
<cfset apiAbort({
|
||||
"OK": false,
|
||||
"ERROR": "server_error",
|
||||
"MESSAGE": "Failed to save station",
|
||||
"DETAIL": cfcatch.message
|
||||
})>
|
||||
</cfcatch>
|
||||
</cftry>
|
||||
|
|
@ -480,13 +480,8 @@
|
|||
}
|
||||
} catch (err) {
|
||||
console.error('[StationAssignment] Stations error:', err);
|
||||
// Demo stations
|
||||
this.stations = [
|
||||
{ StationID: 1, StationName: 'Grill', StationColor: '#FF5722' },
|
||||
{ StationID: 2, StationName: 'Fry', StationColor: '#FFC107' },
|
||||
{ StationID: 3, StationName: 'Drinks', StationColor: '#2196F3' },
|
||||
{ StationID: 4, StationName: 'Expo', StationColor: '#4CAF50' }
|
||||
];
|
||||
this.stations = [];
|
||||
this.toast('Failed to load stations', 'error');
|
||||
}
|
||||
this.renderStations();
|
||||
},
|
||||
|
|
@ -519,15 +514,8 @@
|
|||
}
|
||||
} catch (err) {
|
||||
console.error('[StationAssignment] Items error:', err);
|
||||
// Demo items
|
||||
this.items = [
|
||||
{ ItemID: 1, ItemName: 'Cheeseburger', ItemPrice: 8.99, ItemCategoryName: 'Burgers' },
|
||||
{ ItemID: 2, ItemName: 'Double-Double', ItemPrice: 10.99, ItemCategoryName: 'Burgers' },
|
||||
{ ItemID: 3, ItemName: 'French Fries', ItemPrice: 3.99, ItemCategoryName: 'Sides' },
|
||||
{ ItemID: 4, ItemName: 'Onion Rings', ItemPrice: 4.99, ItemCategoryName: 'Sides' },
|
||||
{ ItemID: 5, ItemName: 'Chocolate Shake', ItemPrice: 5.99, ItemCategoryName: 'Drinks' },
|
||||
{ ItemID: 6, ItemName: 'Lemonade', ItemPrice: 2.99, ItemCategoryName: 'Drinks' }
|
||||
];
|
||||
this.items = [];
|
||||
this.toast('Failed to load items', 'error');
|
||||
}
|
||||
this.renderItems();
|
||||
this.updateStationCounts();
|
||||
|
|
@ -596,7 +584,7 @@
|
|||
<div class="item-name">${this.escapeHtml(item.ItemName)}</div>
|
||||
<div class="item-price">$${(item.ItemPrice || 0).toFixed(2)}</div>
|
||||
</div>
|
||||
${station ? `<span class="item-station-badge" style="background: ${station.StationColor}">${this.escapeHtml(station.StationName)}</span>` : ''}
|
||||
${station ? `<span class="item-station-badge" style="background: ${station.Color}">${this.escapeHtml(station.Name)}</span>` : ''}
|
||||
</div>
|
||||
`;
|
||||
});
|
||||
|
|
@ -628,13 +616,24 @@
|
|||
<div class="station-header"
|
||||
ondragover="StationAssignment.onDragOver(event)"
|
||||
ondrop="StationAssignment.onDrop(event, ${station.StationID})">
|
||||
<div class="station-color" style="background: ${station.StationColor}">
|
||||
${station.StationName.charAt(0)}
|
||||
<div class="station-color" style="background: ${station.Color}">
|
||||
${station.Name.charAt(0)}
|
||||
</div>
|
||||
<div class="station-info">
|
||||
<div class="station-name">${this.escapeHtml(station.StationName)}</div>
|
||||
<div class="station-name">${this.escapeHtml(station.Name)}</div>
|
||||
<div class="station-count">${itemsInStation.length} items</div>
|
||||
</div>
|
||||
<button class="remove-btn" onclick="event.stopPropagation(); StationAssignment.editStation(${station.StationID})" title="Edit station">
|
||||
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
||||
<path d="M11 4H4a2 2 0 00-2 2v14a2 2 0 002 2h14a2 2 0 002-2v-7"/>
|
||||
<path d="M18.5 2.5a2.121 2.121 0 013 3L12 15l-4 1 1-4 9.5-9.5z"/>
|
||||
</svg>
|
||||
</button>
|
||||
<button class="remove-btn" onclick="event.stopPropagation(); StationAssignment.deleteStation(${station.StationID})" title="Delete station">
|
||||
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
||||
<path d="M3 6h18M19 6v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6m3 0V4a2 2 0 012-2h4a2 2 0 012 2v2"/>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
<div class="station-items ${itemsInStation.length === 0 ? 'empty' : ''}"
|
||||
ondragover="StationAssignment.onDragOver(event)"
|
||||
|
|
@ -781,7 +780,7 @@
|
|||
this.renderStations();
|
||||
|
||||
if (station && assignedCount > 0) {
|
||||
this.toast(`${assignedCount} items assigned to ${station.StationName}`, 'success');
|
||||
this.toast(`${assignedCount} items assigned to ${station.Name}`, 'success');
|
||||
}
|
||||
},
|
||||
|
||||
|
|
@ -793,7 +792,7 @@
|
|||
const item = this.items.find(i => i.ItemID === itemId);
|
||||
const station = this.stations.find(s => s.StationID === stationId);
|
||||
if (item && station) {
|
||||
this.toast(`${item.ItemName} assigned to ${station.StationName}`, 'success');
|
||||
this.toast(`${item.ItemName} assigned to ${station.Name}`, 'success');
|
||||
}
|
||||
},
|
||||
|
||||
|
|
@ -835,20 +834,88 @@
|
|||
}
|
||||
},
|
||||
|
||||
addStation() {
|
||||
async addStation() {
|
||||
const name = prompt('Station name:');
|
||||
if (!name) return;
|
||||
if (!name || !name.trim()) return;
|
||||
|
||||
const color = '#' + Math.floor(Math.random()*16777215).toString(16).padStart(6, '0');
|
||||
|
||||
this.stations.push({
|
||||
StationID: Date.now(),
|
||||
StationName: name,
|
||||
StationColor: color
|
||||
});
|
||||
try {
|
||||
const response = await fetch(`${this.config.apiBaseUrl}/stations/save.cfm`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ BusinessID: this.config.businessId, Name: name.trim(), Color: color })
|
||||
});
|
||||
const data = await response.json();
|
||||
if (data.OK && data.STATION) {
|
||||
this.stations.push(data.STATION);
|
||||
this.renderStations();
|
||||
this.toast(`Station "${name.trim()}" added`, 'success');
|
||||
} else {
|
||||
this.toast(data.MESSAGE || 'Failed to add station', 'error');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[StationAssignment] Add station error:', err);
|
||||
this.toast('Failed to add station', 'error');
|
||||
}
|
||||
},
|
||||
|
||||
this.renderStations();
|
||||
this.toast(`Station "${name}" added`, 'success');
|
||||
async editStation(stationId) {
|
||||
const station = this.stations.find(s => s.StationID === stationId);
|
||||
if (!station) return;
|
||||
|
||||
const name = prompt('Station name:', station.Name);
|
||||
if (name === null || !name.trim()) return;
|
||||
|
||||
try {
|
||||
const response = await fetch(`${this.config.apiBaseUrl}/stations/save.cfm`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ BusinessID: this.config.businessId, StationID: stationId, Name: name.trim(), Color: station.Color })
|
||||
});
|
||||
const data = await response.json();
|
||||
if (data.OK && data.STATION) {
|
||||
Object.assign(station, data.STATION);
|
||||
this.renderStations();
|
||||
this.renderItems();
|
||||
this.toast(`Station updated`, 'success');
|
||||
} else {
|
||||
this.toast(data.MESSAGE || 'Failed to update station', 'error');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[StationAssignment] Edit station error:', err);
|
||||
this.toast('Failed to update station', 'error');
|
||||
}
|
||||
},
|
||||
|
||||
async deleteStation(stationId) {
|
||||
const station = this.stations.find(s => s.StationID === stationId);
|
||||
if (!station) return;
|
||||
if (!confirm(`Delete station "${station.Name}"? Items assigned to it will become unassigned.`)) return;
|
||||
|
||||
try {
|
||||
const response = await fetch(`${this.config.apiBaseUrl}/stations/delete.cfm`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ BusinessID: this.config.businessId, StationID: stationId })
|
||||
});
|
||||
const data = await response.json();
|
||||
if (data.OK) {
|
||||
this.stations = this.stations.filter(s => s.StationID !== stationId);
|
||||
// Remove assignments for deleted station
|
||||
Object.keys(this.assignments).forEach(itemId => {
|
||||
if (this.assignments[itemId] === stationId) delete this.assignments[itemId];
|
||||
});
|
||||
this.renderStations();
|
||||
this.renderItems();
|
||||
this.toast(`Station "${station.Name}" deleted`, 'success');
|
||||
} else {
|
||||
this.toast(data.MESSAGE || 'Failed to delete station', 'error');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[StationAssignment] Delete station error:', err);
|
||||
this.toast('Failed to delete station', 'error');
|
||||
}
|
||||
},
|
||||
|
||||
toast(message, type = 'info') {
|
||||
|
|
|
|||
Reference in a new issue