new feature: Error code 507 will return if you want to delete a loan that has not been returned
This commit is contained in:
@@ -234,6 +234,23 @@ export const getBorrowableItemsFromDatabase = async (
|
||||
};
|
||||
|
||||
export const SETdeleteLoanFromDatabase = async (loanId) => {
|
||||
const [checkIfdatesReturned] = await pool.query(
|
||||
"SELECT take_date, returned_date FROM loans WHERE id = ? AND deleted = 0",
|
||||
[loanId],
|
||||
);
|
||||
|
||||
if (checkIfdatesReturned.length === 0) {
|
||||
return { success: false, code: "LOAN_NOT_FOUND" };
|
||||
}
|
||||
|
||||
const { take_date, returned_date } = checkIfdatesReturned[0];
|
||||
const bothNull = take_date === null && returned_date === null;
|
||||
const bothSet = take_date !== null && returned_date !== null;
|
||||
|
||||
if (!(bothNull || bothSet)) {
|
||||
return { success: false, code: "LOAN_NOT_RETURNED" };
|
||||
}
|
||||
|
||||
const [result] = await pool.query(
|
||||
"UPDATE loans SET deleted = 1 WHERE id = ?;",
|
||||
[loanId],
|
||||
|
||||
@@ -134,6 +134,16 @@ router.delete("/delete-loan/:id", authenticate, async (req, res) => {
|
||||
if (result.success) {
|
||||
res.status(200).json({ message: "Loan deleted successfully" });
|
||||
} else {
|
||||
if (result.code === "LOAN_NOT_FOUND") {
|
||||
res.status(404).json({ message: "Loan not found" });
|
||||
}
|
||||
|
||||
if (result.code === "LOAN_NOT_RETURNED") {
|
||||
res.status(507).json({
|
||||
message: "Cannot delete loan that has not been returned",
|
||||
});
|
||||
}
|
||||
|
||||
res.status(500).json({ message: "Failed to delete loan" });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -23,8 +23,6 @@ const app = express();
|
||||
const port = 8004;
|
||||
const naasURL = process.env.NAAS_URL;
|
||||
|
||||
console.log(naasURL);
|
||||
|
||||
app.use(cors());
|
||||
// Body-Parser VOR den Routen registrieren
|
||||
app.use(express.json({ limit: "10mb" }));
|
||||
|
||||
Reference in New Issue
Block a user