diff --git a/server.js b/server.js index 7d13e109e67d1b1d27a23fd7cfafc210b98a0608..98e05a26d727ae5dbe70f7243323e68d46337ba4 100644 --- a/server.js +++ b/server.js @@ -8,7 +8,11 @@ require("dotenv").config(); const app = express(); // ประกาศ app ที่นี่ const MySQLStore = require('express-mysql-session')(session); -const sessionStore = new MySQLStore({}, pool); +const sessionStore = new MySQLStore({ + clearExpired: true, + checkExpirationInterval: 900000, // 15 นาที + expiration: 86400000 // 24 ชั่วโมง +}, pool); // Middleware isLoggedIn const isLoggedIn = (req, res, next) => { diff --git a/shop-routes/order.js b/shop-routes/order.js index acea568dad1dcc79aa3570f1ec8f582325ae496b..d934947a8c86b0875b9d4b222063b3fe5db5f52c 100644 --- a/shop-routes/order.js +++ b/shop-routes/order.js @@ -200,34 +200,8 @@ router.post('/create', isAuthenticated, async (req, res) => { } }); - router.get('/confirmation', async (req, res) => { - try { - if (!req.session.user || !req.session.user.id) { - return res.status(400).send("กรุณา Login ก่อน"); - } - - console.log("🔹 Fetching last order for User ID:", req.session.user.id); - - const [orderResults] = await pool.query( - `SELECT id, shipping_address - FROM orders - WHERE user_id = ? - ORDER BY created_at DESC - LIMIT 1`, - [req.session.user.id] - ); - - if (orderResults.length === 0) { - console.log("ไม่พบคำสั่งซื้อ"); - return res.status(404).send("ไม่พบคำสั่งซื้อ"); - } - - console.log("Order found:", orderResults[0]); - res.render('confirmation', { order: orderResults[0] }); // ✅ ส่ง `order` ไปที่ View - } catch (error) { - console.error("Error fetching order confirmation:", error); - res.status(500).send("เกิดข้อผิดพลาด"); - } - }); +router.get('/confirmation', (req, res) => { + res.render('confirmation'); +}); module.exports = router; \ No newline at end of file diff --git a/views/confirmation.ejs b/views/confirmation.ejs index 551a70166b7aabb789dfc965e7e92c5e1753809a..4a9c7b0f7ea21da7411d1cf5b3bd54b17c0a9ed9 100644 --- a/views/confirmation.ejs +++ b/views/confirmation.ejs @@ -5,53 +5,4 @@ <p>Your order has been received and is being processed.</p> <a href="/" class="continue-shopping" >Continue Shopping</a> </div> -<div class="info-section"> - <h3>Shipping Address</h3> - <p> - <span id="shipping-address"><%= order.shipping_address %></span> - <input type="text" id="new-address" value="<%= order.shipping_address %>" style="display:none;"> - </p> - <button id="edit-btn">แก้ไขที่อยู่</button> - <button id="save-btn" style="display:none;">บันทึก</button> -</div> -<script> - // Debug Log: ตรวจสอบว่า order ถูกส่งมาหรือไม่ - console.log("Order ID:", "<%= order ? order.id : 'Not Found' %>"); - console.log("Shipping Address:", "<%= order ? order.shipping_address : 'Not Found' %>"); - - const orderId = "<%= order ? order.id : '' %>"; - if (!orderId) { - alert("เกิดข้อผิดพลาด: ไม่พบหมายเลขคำสั่งซื้อ"); - } - - document.getElementById("edit-btn").addEventListener("click", function() { - document.getElementById("new-address").style.display = "inline"; - document.getElementById("save-btn").style.display = "inline"; - document.getElementById("edit-btn").style.display = "none"; // ซ่อนปุ่มแก้ไข - }); - - document.getElementById("save-btn").addEventListener("click", async function() { - const newAddress = document.getElementById("new-address").value.trim(); - if (!newAddress) { - alert("กรุณากรอกที่อยู่ใหม่"); - return; - } - - const response = await fetch(`/order/detail/${orderId}`, { // แก้ให้ตรง API - method: "PUT", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ shipping_address: newAddress }) - }); - - if (response.ok) { - document.getElementById("shipping-address").innerText = newAddress; - document.getElementById("new-address").style.display = "none"; - document.getElementById("save-btn").style.display = "none"; - document.getElementById("edit-btn").style.display = "inline"; - alert("อัปเดตที่อยู่สำเร็จ!"); - } else { - alert("เกิดข้อผิดพลาด!"); - } - }); -</script> <%- include('partials/footer') %> \ No newline at end of file diff --git a/views/order-details.ejs b/views/order-details.ejs index e1cbbd44287519faca68fcab937069d2370b5bfd..45cf64ffcbda6a0a3e8f6c7687d62b3933553497 100644 --- a/views/order-details.ejs +++ b/views/order-details.ejs @@ -42,10 +42,4 @@ <a href="/" class="continue-shopping">Continue Shopping</a> </div> </div> -<script> - const orderId = "<%= order ? order.id : '' %>"; - if (!orderId) { - alert("เกิดข้อผิดพลาด: ไม่พบหมายเลขคำสั่งซื้อ"); - } -</script> <%- include('partials/footer') %> \ No newline at end of file