diff --git a/backend/controller/reservation/tableCancel.js b/backend/controller/reservation/tableCancel.js index 279ef86..59819de 100644 --- a/backend/controller/reservation/tableCancel.js +++ b/backend/controller/reservation/tableCancel.js @@ -1,5 +1,6 @@ -import {searchTableAndUpdate} from '../../model/reserveCancelModel.js'; +//import {searchTableAndUpdate} from '../../model/reserveCancelModel.js'; +import {cancelTable} from '../../model/reserveCancelModel.js'; function ReqIsNumber(s) { return parseFloat(s).toString() !== "NaN"; @@ -18,7 +19,19 @@ export async function tableCancel(req, res) { return res.status(400).send({ "error": "invalid phone" }); } - const searchandupdate = await searchTableAndUpdate (phoneNum,restaurantId) + // const searchandupdate = await searchTableAndUpdate (phoneNum,restaurantId) + + // if(searchandupdate){ + + // return res.status(200).json({ + // data: { + // tableId: searchandupdate, + // } + // }); + // } + + + const searchandupdate = await cancelTable(phoneNum,restaurantId) if(searchandupdate){ @@ -29,6 +42,7 @@ export async function tableCancel(req, res) { }); } + return res.status(404).json({ error: 'Phone not found in tableList.', }); diff --git a/backend/model/orderSummaryModel.js b/backend/model/orderSummaryModel.js index d098bb2..ec9a431 100644 --- a/backend/model/orderSummaryModel.js +++ b/backend/model/orderSummaryModel.js @@ -37,21 +37,13 @@ try { export async function getTablesByRestId(restaurantId) { - try { - const [rows] = await pool.query('SELECT id, headcount, vacancy, phone FROM tableList WHERE restaurantId = ?', [restaurantId]); - return rows; - } catch (error) { - throw error; - } + const [rows] = await pool.query('SELECT id, headcount, vacancy, phone FROM tableList WHERE restaurantId = ?', [restaurantId]); + return rows; } export async function getReservationByRestId(restaurantId) { - try { - const [rows] = await pool.query('SELECT id, phone, headcount FROM Reservation WHERE restaurantId = ?', [restaurantId]); - return rows; - } catch (error) { - throw error; - } + const [rows] = await pool.query('SELECT id, phone, headcount FROM Reservation WHERE restaurantId = ?', [restaurantId]); + return rows; } diff --git a/backend/model/reserveCancelModel.js b/backend/model/reserveCancelModel.js index 5f13bda..48a7c06 100644 --- a/backend/model/reserveCancelModel.js +++ b/backend/model/reserveCancelModel.js @@ -67,13 +67,57 @@ export async function searchTableAndUpdate(phone, restaurantId) { +export async function cancelTable(phone, restaurantId) { + const connection = await pool.getConnection(); + try { + await connection.beginTransaction(); + const [tableRows] = await connection.query('SELECT id, headcount FROM tableList WHERE phone = ? AND restaurantId = ?', [phone, restaurantId]); + if (tableRows.length > 0) { + const tableId = tableRows[0].id; + const tableHeadcount = tableRows[0].headcount; + console.log(tableId); + const [orderRows] = await connection.query('SELECT id FROM OrderList WHERE restaurantId = ? AND tableId = ?', [restaurantId, tableId]); - - + if (orderRows.length > 0) { + const orderId = orderRows[0].id; + await connection.query('DELETE FROM OrderList WHERE id = ?', [orderId]); + } + const [reservationRows] = await connection.query('SELECT id, phone FROM Reservation WHERE headcount <= ? AND restaurantId = ? LIMIT 1', [tableHeadcount, restaurantId]); + + if (reservationRows.length > 0) { + const reservationId = reservationRows[0].id; + const reservationPhone = reservationRows[0].phone; + + console.log('tableheadcount',tableHeadcount) + + console.log('res id',reservationRows[0].id) + console.log('res phone',reservationRows[0].phone) + + await connection.query('UPDATE tableList SET phone = ? WHERE id = ?', [reservationPhone, tableId]); + + const [reservationOrderRows] = await connection.query('SELECT id FROM OrderList WHERE reservationId = ?', [reservationId]); + + if (reservationOrderRows.length > 0) { + await connection.query('UPDATE OrderList SET reservationId = null, tableId = ? WHERE reservationId = ?', [tableId, reservationId]); + } + await connection.query('DELETE FROM Reservation WHERE id = ?', [reservationId]); + } else { + await connection.query('UPDATE tableList SET phone = null, vacancy = true WHERE id = ?', [tableId]); + } + + await connection.commit(); + return tableId; + } + await connection.commit(); + return null; + } catch (error) { + await connection.rollback(); + console.error('Error in cancelReservation:', error); + throw error; + } finally { + connection.release(); + } +} - - - -