From 4fbd0ac6e1018181438b2e14b5d020d31d0d86cc Mon Sep 17 00:00:00 2001 From: 65160270 <65160270@go.buu.ac.th> Date: Sun, 23 Mar 2025 00:22:33 +0700 Subject: [PATCH] update-address --- server.js | 6 ++++- shop-routes/order.js | 32 +++------------------------ views/confirmation.ejs | 49 ----------------------------------------- views/order-details.ejs | 6 ----- 4 files changed, 8 insertions(+), 85 deletions(-) diff --git a/server.js b/server.js index 7d13e10..98e05a2 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 acea568..d934947 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 551a701..4a9c7b0 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 e1cbbd4..45cf64f 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 -- GitLab