From 5442f2f1f39b33a809ba6bca0aa08adb9445de2c Mon Sep 17 00:00:00 2001 From: Theis Gaedigk Date: Fri, 24 Apr 2026 18:44:07 +0200 Subject: [PATCH] new feature: Error code 507 will return if you want to delete a loan that has not been returned --- FrontendV2/src/pages/MyLoansPage.tsx | 8 ++++++++ FrontendV2/src/utils/i18n/locales/de/de.json | 3 ++- FrontendV2/src/utils/i18n/locales/en/en.json | 3 ++- .../routes/app/database/loansMgmt.database.js | 17 +++++++++++++++++ backendV2/routes/app/loanMgmt.route.js | 10 ++++++++++ backendV2/server.js | 2 -- changelog.md | 1 + 7 files changed, 40 insertions(+), 4 deletions(-) diff --git a/FrontendV2/src/pages/MyLoansPage.tsx b/FrontendV2/src/pages/MyLoansPage.tsx index 8fa5904..ef040de 100644 --- a/FrontendV2/src/pages/MyLoansPage.tsx +++ b/FrontendV2/src/pages/MyLoansPage.tsx @@ -84,6 +84,14 @@ export const MyLoansPage = () => { }); if (!res.ok) { + if (res.status === 507) { + setMsgStatus("error"); + setMsgTitle(t("error")); + setMsgDescription(t("error-deleting-loan-507")); + setIsMsg(true); + return; + } + setMsgStatus("error"); setMsgTitle(t("error")); setMsgDescription(t("error-deleting-loan")); diff --git a/FrontendV2/src/utils/i18n/locales/de/de.json b/FrontendV2/src/utils/i18n/locales/de/de.json index b02e852..353b16d 100644 --- a/FrontendV2/src/utils/i18n/locales/de/de.json +++ b/FrontendV2/src/utils/i18n/locales/de/de.json @@ -93,5 +93,6 @@ "try-naas": "Klick mich", "naas-error": "Fehler mit no-as-a-service", "naas-error-desc": "Ein Fehler ist beim Kommunizieren mit no-as-a-service aufgetreten.", - "naas-header": "Eine gute Möglichkeit, nein zu sagen..." + "naas-header": "Eine gute Möglichkeit, nein zu sagen...", + "error-deleting-loan-507": "Die Ausleihe kann nicht gelöscht werden, da sie noch nicht zurückgegeben wurde." } \ No newline at end of file diff --git a/FrontendV2/src/utils/i18n/locales/en/en.json b/FrontendV2/src/utils/i18n/locales/en/en.json index d093b1c..8dc1059 100644 --- a/FrontendV2/src/utils/i18n/locales/en/en.json +++ b/FrontendV2/src/utils/i18n/locales/en/en.json @@ -93,5 +93,6 @@ "try-naas": "Click me", "naas-error": "Error with no-as-a-service", "naas-error-desc": "An error occurred while communicating with no-as-a-service.", - "naas-header": "A good way to say no..." + "naas-header": "A good way to say no...", + "error-deleting-loan-507": "The loan cannot be deleted because it has not been returned yet." } \ No newline at end of file diff --git a/backendV2/routes/app/database/loansMgmt.database.js b/backendV2/routes/app/database/loansMgmt.database.js index 3fdc67e..aa9a261 100644 --- a/backendV2/routes/app/database/loansMgmt.database.js +++ b/backendV2/routes/app/database/loansMgmt.database.js @@ -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], diff --git a/backendV2/routes/app/loanMgmt.route.js b/backendV2/routes/app/loanMgmt.route.js index 7e0c0ce..2a57f60 100644 --- a/backendV2/routes/app/loanMgmt.route.js +++ b/backendV2/routes/app/loanMgmt.route.js @@ -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" }); } }); diff --git a/backendV2/server.js b/backendV2/server.js index a8d2006..61855d8 100644 --- a/backendV2/server.js +++ b/backendV2/server.js @@ -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" })); diff --git a/changelog.md b/changelog.md index 54c0245..878a0db 100644 --- a/changelog.md +++ b/changelog.md @@ -13,6 +13,7 @@ This update provides some new features for the design. It also contains some imp ## Improvements - I have the error logging for the API route wehre you can take loans improved. +- If you try to delete a loan that has not been returned yet, you will get an 507 error code. ## Fixed bugs