diff --git a/controllers/bookingController.js b/controllers/bookingController.js index 9502830f6c8aea49f1140bf1c1c2e063490f19c7..01ad1f7a77271c6feee8d4cea4be74da0d630fa5 100644 --- a/controllers/bookingController.js +++ b/controllers/bookingController.js @@ -119,15 +119,22 @@ exports.bookingSummary = async (req, res) => { if (!appointmentId) { return res.status(400).send("ไม่พบรหัสการจอง"); } - // ดึงข้อมูลการจอง - const appointment = await bookingModel.getAppointmentById(appointmentId); - // ดึงข้อมูลผู้ใช้ + + // พยายามดึงข้อมูลการจองจากตารางหลัก + let appointment = await bookingModel.getAppointmentById(appointmentId); + + // ถ้าไม่พบ ลองดึงจากตารางประวัติ (appointment_history) + if (!appointment) { + appointment = await bookingModel.getAppointmentFromHistoryById(appointmentId); + if (!appointment) { + throw new Error("ไม่พบการจอง"); + } + } + const user = await bookingModel.getUserById(appointment.user_id); - // ดึงข้อมูลบริการ const service = await bookingModel.getServiceById(appointment.service_id); - // ดึงข้อมูล booking slot const slot = await bookingModel.getBookingSlotById(appointment.booking_slot_id); - // Render หน้า bookingSummary.ejs พร้อมส่งข้อมูลที่ต้องการ + res.render('customer/bookingSummary', { appointment, user, service, slot }); } catch (error) { console.error(error); @@ -135,6 +142,7 @@ exports.bookingSummary = async (req, res) => { } }; + exports.getAllAppointmentPage = async (req, res) => { try { const user_id = req.session.userId; @@ -171,7 +179,7 @@ exports.cancelAppointment = async (req, res) => { if (paymentStatus === 'Pending') { // ย้ายข้อมูลไป appointment_history พร้อม `payment_status` await bookingModel.addToHistory(appointmentId); - res.send('ยกเลิกการจองสำเร็จ และย้ายไปยังประวัติการนัดหมาย'); + res.redirect('/customer/allAppointments'); } else { res.status(400).send('ไม่สามารถยกเลิกการจองได้ เนื่องจากการชำระเงินเสร็จสมบูรณ์'); } diff --git a/views/customer/bookingSummary.ejs b/views/customer/bookingSummary.ejs index 76d58c89e17af7a6d632d01f8534694776c59da8..e4898c48043042d72c357462e866c809b662a3a0 100644 --- a/views/customer/bookingSummary.ejs +++ b/views/customer/bookingSummary.ejs @@ -89,8 +89,8 @@ <p><strong>ชื่อผู้จอง:</strong> <%= user.first_name %> <%= user.last_name %></p> <p><strong>บริการ:</strong> <%= service.service_name %></p> <p><strong>วันที่เข้ารับบริการ:</strong> <%= new Date(slot.date).toLocaleDateString('th-TH') %></p> <!-- Display only date --> - <p><strong>สถานะการจอง:</strong> <%= appointment.appointment_status %></p> <!-- Display appointment status --> <p><strong>เวลาเข้ารับบริการ:</strong> <%= slot.start_time %></p> + <p><strong>สถานะการจอง:</strong> <%= appointment.appointment_status %></p> <!-- Display appointment status --> <p class="alert-info">กรุณามาก่อนเวลา 10 นาที</p> </div> <div class="actions">