From 3eee1fd8e09ffc56076d3c1b85078e83dd13484f Mon Sep 17 00:00:00 2001 From: Anantaya Yaowarit <65160130@go.buu.ac.th> Date: Sun, 23 Mar 2025 22:51:14 +0700 Subject: [PATCH] add showAddTreatmentForm --- controllers/DentistController.js | 83 +++++++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 11 deletions(-) diff --git a/controllers/DentistController.js b/controllers/DentistController.js index 755d249..68e43d7 100644 --- a/controllers/DentistController.js +++ b/controllers/DentistController.js @@ -15,6 +15,8 @@ class DentistController { res.status(500).send("Error fetching queue details"); } } + + //แสดงประวัติการรักษา static async showTreatmentHistory(req, res) { @@ -49,30 +51,84 @@ class DentistController { } } - // แสดงหน้าฟอร์มเพิ่มประวัติการรักษา + + static async showAddTreatmentForm(req, res) { const queuedetail_id = req.params.queuedetail_id; - + + // ตรวจสอบว่า queuedetail_id ถูกต้องหรือไม่ + if (!queuedetail_id) { + console.error("queuedetail_id is missing or invalid."); + return res.status(400).send("Invalid queuedetail_id."); + } + try { // ดึงข้อมูลจาก queuedetail โดยใช้ queuedetail_id const queueDetail = await QueueDetail.getQueueDetailById(queuedetail_id); + if (!queueDetail) { return res.status(404).send('ไม่พบข้อมูลการรักษา'); } - - // ส่งข้อมูลที่ดึงมาให้หน้า EJS - res.render('addtreatmenthis', { - queuedetail_id: queueDetail.queuedetail_id, - treatment_date: queueDetail.treatment_date, - patient_name: queueDetail.patient_name, - treatment_name: queueDetail.treatment_name, - }); + + // ใช้ patient_id ที่ได้จาก queueDetail + const patientId = queueDetail.patient_id; + if (!patientId) { + console.error("Patient ID is missing."); + return res.status(400).send("Patient ID is missing."); + } + + // ตรวจสอบว่า `patient_id` นี้มีการจองใหม่ที่ยังไม่ได้บันทึกการรักษาหรือไม่ + const treatmentHistory = await TreatmentHistory.getTreatmentHistory(patientId); + + // ถ้าการบันทึกของ queuedetail_id อยู่ใน treatmentHistory + if (treatmentHistory && treatmentHistory.length > 0) { + // ตรวจสอบการจองล่าสุดจาก patient_id + const newQueueDetail = await QueueDetail.getQueueDetailsByPatientId(patientId); + + if (!newQueueDetail) { + // ถ้าไม่มีการจองใหม่ + return res.render('addtreatmenthis', { + message: 'ไม่พบการจองใหม่สำหรับผู้ป่วยนี้', + patient_id: patientId, + }); + } + + // เปรียบเทียบว่า newQueueDetail ถูกบันทึกใน treatmentHistory หรือไม่ + const isAlreadyBooked = treatmentHistory.some(th => + th.queuedetail_id === newQueueDetail.queuedetail_id + ); + + if (isAlreadyBooked) { + // ถ้าการจองนี้ถูกบันทึกแล้วใน `treatmenthistory` + return res.render('addtreatmenthis', { + message: 'ไม่พบการจองใหม่สำหรับผู้ป่วยนี้', + patient_id: patientId, + }); + } + + // ถ้ามีการจองใหม่ที่ยังไม่ได้บันทึก ให้แสดงข้อมูลการจอง + res.render('addtreatmenthis', { + queuedetail_id: newQueueDetail.queuedetail_id, + treatment_date: newQueueDetail.treatment_date, + patient_name: newQueueDetail.patient_name, + treatment_name: newQueueDetail.treatment_name, + }); + } else { + // หากไม่มีการจองใหม่ที่ยังไม่ได้บันทึกการรักษา + return res.render('addtreatmenthis', { + message: 'ไม่พบการจองใหม่สำหรับผู้ป่วยนี้', + patient_id: patientId, + }); + } + } catch (error) { console.error("Error fetching queue details:", error.message); res.status(500).send("Error fetching queue details"); } } + + // เพิ่มประวัติการรักษาใหม่ static async addTreatmentHistory(req, res) { const { queuedetail_id, dentist_name, tm_details, followUpdate } = req.body; @@ -88,6 +144,8 @@ class DentistController { } } + + //แสดงฟอร์มแก้ไขข้อมูล static async showEditTreatmentForm(req, res) { const treatmenthistoryId = req.params.id; @@ -107,6 +165,7 @@ class DentistController { } + //อัปเดตข้อมูล static async updateTreatmentHistory(req, res) { const { treatmenthistory_id, dentist_name, tm_details, followUpdate } = req.body; @@ -129,6 +188,8 @@ class DentistController { } } + + // ลบประวัติการรักษา static async deleteTreatmentHistory(req, res) { const treatmenthistoryId = req.params.id; @@ -151,4 +212,4 @@ class DentistController { } -module.exports = DentistController; \ No newline at end of file +module.exports = DentistController; -- GitLab