Compare commits

...

4 Commits

Author SHA1 Message Date
theis.gaedigk 67c704009e refactored loan code creation function 2026-05-13 19:42:54 +02:00
theis.gaedigk 831de8b610 added new loan code function - NOT TESTED YET 2026-05-13 14:04:12 +02:00
theis.gaedigk fd43e84c0c refactored code 2026-05-11 22:40:34 +02:00
theis.gaedigk 1ce8d33b0d pulled naas 2026-05-09 23:32:12 +02:00
2 changed files with 18 additions and 23 deletions
@@ -63,11 +63,9 @@ export const createLoanInDatabase = async (
}; };
} }
const itemNames = itemIds const itemNames = itemIds.map(
.map( (id) => itemsRows.find((r) => Number(r.id) === Number(id)).item_name,
(id) => itemsRows.find((r) => Number(r.id) === Number(id))?.item_name, );
)
.filter(Boolean);
// Build lockers array (unique, only 2-digit numbers from safe_nr) // Build lockers array (unique, only 2-digit numbers from safe_nr)
const lockers = [ const lockers = [
@@ -109,27 +107,24 @@ export const createLoanInDatabase = async (
}; };
} }
// Generate unique loan_code (retry a few times) let index = false;
let loanCode = null; let candidate;
for (let i = 0; i < 6; i++) { let loanCode;
const candidate = Math.floor(100000 + Math.random() * 899999); // 6 digits
const [exists] = await conn.query( // Generates 6-digit loan code
"SELECT 1 FROM loans WHERE loan_code = ? LIMIT 1", do {
candidate = Math.floor(100000 + Math.random() * 899999);
const [rows] = await conn.query(
"SELECT 1 FROM loans where loan_code = ? LIMIT 1",
[candidate], [candidate],
); );
if (exists.length === 0) {
if (rows.length == 0) {
index = true;
loanCode = candidate; loanCode = candidate;
break;
} }
} } while (index === false);
if (!loanCode) {
await conn.rollback();
return {
success: false,
code: "SERVER_ERROR",
message: "Failed to generate unique loan code",
};
}
// Insert loan (now includes lockers) // Insert loan (now includes lockers)
const [insertRes] = await conn.query( const [insertRes] = await conn.query(