From 831de8b6101a63fbb33d71b20d0aa85abe1fa6e2 Mon Sep 17 00:00:00 2001 From: Theis Gaedigk Date: Wed, 13 May 2026 14:04:12 +0200 Subject: [PATCH] added new loan code function - NOT TESTED YET --- .../routes/app/database/loansMgmt.database.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/backendV2/routes/app/database/loansMgmt.database.js b/backendV2/routes/app/database/loansMgmt.database.js index 34b8c72..1b48d41 100644 --- a/backendV2/routes/app/database/loansMgmt.database.js +++ b/backendV2/routes/app/database/loansMgmt.database.js @@ -107,6 +107,32 @@ export const createLoanInDatabase = async ( }; } + let index = false; + let candidate = Math.floor(100000 + Math.random() * 899999); // 6 digits + + try { + const [exists] = await conn.query( + "SELECT 1 FROM loans WHERE loan_code = ? LIMIT 1", + [candidate], + ); + } catch (error) { + while ((i = false)) { + candidate = Math.floor(100000 + Math.random() * 899999); // 6 digits + + const [exists] = await conn.query( + "SELECT 1 FROM loans WHERE loan_code = ? LIMIT 1", + [candidate], + ); + + // sets index to true if the 6 digit code does not exist + if (exists.length == 0) { + index = true; + } + } + } + + /* + // Generate unique loan_code (retry a few times) let loanCode = null; for (let i = 0; i < 6; i++) { @@ -128,6 +154,8 @@ export const createLoanInDatabase = async ( message: "Failed to generate unique loan code", }; } + + */ // Insert loan (now includes lockers) const [insertRes] = await conn.query(