refactored loan code creation function
This commit is contained in:
@@ -108,54 +108,23 @@ export const createLoanInDatabase = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
let index = false;
|
let index = false;
|
||||||
let candidate = Math.floor(100000 + Math.random() * 899999); // 6 digits
|
let candidate;
|
||||||
|
let loanCode;
|
||||||
|
|
||||||
try {
|
// Generates 6-digit loan code
|
||||||
const [exists] = await conn.query(
|
do {
|
||||||
"SELECT 1 FROM loans WHERE loan_code = ? LIMIT 1",
|
candidate = Math.floor(100000 + Math.random() * 899999);
|
||||||
[candidate],
|
|
||||||
);
|
|
||||||
} catch (error) {
|
|
||||||
while ((i = false)) {
|
|
||||||
candidate = Math.floor(100000 + Math.random() * 899999); // 6 digits
|
|
||||||
|
|
||||||
const [exists] = await conn.query(
|
const [rows] = await conn.query(
|
||||||
"SELECT 1 FROM loans WHERE loan_code = ? LIMIT 1",
|
"SELECT 1 FROM loans where loan_code = ? LIMIT 1",
|
||||||
[candidate],
|
[candidate],
|
||||||
);
|
);
|
||||||
|
|
||||||
// sets index to true if the 6 digit code does not exist
|
if (rows.length == 0) {
|
||||||
if (exists.length == 0) {
|
|
||||||
index = true;
|
index = true;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
// Generate unique loan_code (retry a few times)
|
|
||||||
let loanCode = null;
|
|
||||||
for (let i = 0; i < 6; i++) {
|
|
||||||
const candidate = Math.floor(100000 + Math.random() * 899999); // 6 digits
|
|
||||||
const [exists] = await conn.query(
|
|
||||||
"SELECT 1 FROM loans WHERE loan_code = ? LIMIT 1",
|
|
||||||
[candidate],
|
|
||||||
);
|
|
||||||
if (exists.length === 0) {
|
|
||||||
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(
|
||||||
|
|||||||
Reference in New Issue
Block a user