From a24b2697b0ac0e1ab79f4526b1885b95acb3aa37 Mon Sep 17 00:00:00 2001 From: Theis Gaedigk Date: Wed, 3 Sep 2025 14:33:54 +0200 Subject: [PATCH] enhance loan handling by updating item states on take and return --- backend/services/database.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/backend/services/database.js b/backend/services/database.js index 967d9db..3e02ce9 100644 --- a/backend/services/database.js +++ b/backend/services/database.js @@ -294,24 +294,44 @@ export const createLoanInDatabase = async ( // These functions are only temporary, and will be deleted when the full bin is set up. export const onTake = async (loanId) => { + const [items] = await pool.query( + "SELECT loaned_items_id FROM loans WHERE id = ?", + [loanId] + ); + + const [setItemStates] = await pool.query( + "UPDATE items SET inSafe = 0 WHERE id IN (?)", + [items.map((item) => item.loaned_items_id)] + ); + const [result] = await pool.query( "UPDATE loans SET take_date = NOW() WHERE id = ?", [loanId] ); - if (result.affectedRows > 0) { + if (result.affectedRows > 0 && setItemStates.affectedRows > 0) { return { success: true }; } return { success: false }; }; export const onReturn = async (loanId) => { + const [items] = await pool.query( + "SELECT loaned_items_id FROM loans WHERE id = ?", + [loanId] + ); + + const [setItemStates] = await pool.query( + "UPDATE items SET inSafe = 1 WHERE id IN (?)", + [items.map((item) => item.loaned_items_id)] + ); + const [result] = await pool.query( "UPDATE loans SET returned_date = NOW() WHERE id = ?", [loanId] ); - if (result.affectedRows > 0) { + if (result.affectedRows > 0 && setItemStates.affectedRows > 0) { return { success: true }; } return { success: false };