From 3c7a1a876a2339181c5ca127f661554a29bd4179 Mon Sep 17 00:00:00 2001 From: noahnghg <157671712+noahnghg@users.noreply.github.com> Date: Sun, 20 Apr 2025 23:42:22 -0600 Subject: [PATCH] fix duplicate products --- backend/controllers/transaction.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/backend/controllers/transaction.js b/backend/controllers/transaction.js index 63e59a0..0753975 100644 --- a/backend/controllers/transaction.js +++ b/backend/controllers/transaction.js @@ -7,7 +7,23 @@ exports.createTransaction = async (req, res) => { const { userID, productID, date, paymentStatus } = req.body; try { + // Check if the transaction already exists for the same user and product + const [existingTransaction] = await db.execute( + `SELECT TransactionID FROM Transaction WHERE UserID = ? AND ProductID = ?`, + [userID, productID] + ); + + if (existingTransaction.length > 0) { + return res.status(400).json({ + success: false, + message: "Transaction already exists for this user and product", + }); + } + + // Format the date const formattedDate = new Date(date).toISOString().slice(0, 19).replace("T", " "); + + // Insert the new transaction const [result] = await db.execute( `INSERT INTO Transaction (UserID, ProductID, Date, PaymentStatus) VALUES (?, ?, ?, ?)`,