From 5df96530676a8d185c1872df22c3223f0140190b Mon Sep 17 00:00:00 2001
From: Hanen Alrwasheda <alrwasheda@mi.fu-berlin.de>
Date: Sun, 16 Jan 2022 16:51:15 +0100
Subject: [PATCH] PATCH-Methods: returning 200 + empty response when providing
 no input at all

---
 .../src/controllers/accounts.controllers.ts   |  9 +++++
 .../boatControllers.controllers.ts            | 13 ++++--
 .../boatTypeControllers.controllers.ts        | 13 ++++--
 .../userControllers.controllers.ts            | 40 +++++++++++++------
 4 files changed, 56 insertions(+), 19 deletions(-)

diff --git a/server/src/controllers/accounts.controllers.ts b/server/src/controllers/accounts.controllers.ts
index b2f7772..6c3b8d3 100644
--- a/server/src/controllers/accounts.controllers.ts
+++ b/server/src/controllers/accounts.controllers.ts
@@ -119,6 +119,15 @@ const updateAccount = async (req: Request, res: Response) => {
 
     //take what ever in req.body is, and pass it to update()
     const input = req.body;
+
+    //return 200 with empty response if no data was given
+    if (Object.keys(input).length === 0) {
+      return res
+        .status(200)
+        .json({ success: true, result: {}, message: "noInputFound" });
+    }
+
+    //get given id
     const givenId = req.params.id;
 
     //check if given ID exists in DB
diff --git a/server/src/controllers/boatControllers.controllers.ts b/server/src/controllers/boatControllers.controllers.ts
index bc3f21b..5c4567b 100644
--- a/server/src/controllers/boatControllers.controllers.ts
+++ b/server/src/controllers/boatControllers.controllers.ts
@@ -120,6 +120,16 @@ const updateBoatById = async (req: Request, res: Response) => {
         .json({ success: false, error: "MustBeCoordinator" });
     }
 
+    //get new input
+    const input = req.body;
+
+    //return 200 with empty response if no data was given
+    if (Object.keys(input).length === 0) {
+      return res
+        .status(200)
+        .json({ success: true, result: {}, message: "noInputFound" });
+    }
+
     //get id
     const givenId = req.params.id;
 
@@ -130,9 +140,6 @@ const updateBoatById = async (req: Request, res: Response) => {
       return res.status(404).json({ success: false, error: "boatIdNotFound" });
     }
 
-    //get new input
-    const input = req.body;
-
     //try to update
     const updatedBoat = await Boat.update(input, {
       where: {
diff --git a/server/src/controllers/boatTypeControllers.controllers.ts b/server/src/controllers/boatTypeControllers.controllers.ts
index 37f6aa2..5b207fa 100644
--- a/server/src/controllers/boatTypeControllers.controllers.ts
+++ b/server/src/controllers/boatTypeControllers.controllers.ts
@@ -112,6 +112,16 @@ const updateBoatTypeById = async (req: Request, res: Response) => {
         .json({ success: false, error: "MustBeCoordinator" });
     }
 
+    //get new input
+    const input = req.body;
+
+    //return 200 with empty response if no data was given
+    if (Object.keys(input).length === 0) {
+      return res
+        .status(200)
+        .json({ success: true, result: {}, message: "noInputFound" });
+    }
+
     //get id
     const givenId = req.params.id;
 
@@ -124,9 +134,6 @@ const updateBoatTypeById = async (req: Request, res: Response) => {
         .json({ success: false, error: "boatTypeIdNotFound" });
     }
 
-    //get new input
-    const input = req.body;
-
     //try to update
     const updatedBoatType = await BoatType.update(input, {
       where: {
diff --git a/server/src/controllers/userControllers.controllers.ts b/server/src/controllers/userControllers.controllers.ts
index 5052179..cea3237 100644
--- a/server/src/controllers/userControllers.controllers.ts
+++ b/server/src/controllers/userControllers.controllers.ts
@@ -12,18 +12,16 @@ const showCurrentUserController = async (req: Request, res: Response) => {
       },
     });
 
-    res
-      .status(200)
-      .json({
-        success: true,
-        result: {
-          id: currentUserData.id,
-          first_name: currentUserData.first_name,
-          last_name: currentUserData.last_name,
-          email: currentUserData.email,
-          role: currentUserData.role,
-        },
-      });
+    res.status(200).json({
+      success: true,
+      result: {
+        id: currentUserData.id,
+        first_name: currentUserData.first_name,
+        last_name: currentUserData.last_name,
+        email: currentUserData.email,
+        role: currentUserData.role,
+      },
+    });
   } catch (error) {
     console.log("server error: ", error.message);
     res.status(500).json({ success: false, error: "serverError!" });
@@ -43,6 +41,13 @@ const updateCurrentUser = async (req: Request, res: Response) => {
     //take what ever in req.body is, and pass it to update()
     const input = req.body; //Data are saved in the DB in camelCase
 
+    //return 200 with empty response if no data was given
+    if (Object.keys(input).length === 0) {
+      return res
+        .status(200)
+        .json({ success: true, result: {}, message: "noInputFound" });
+    }
+
     if (input.password !== undefined) {
       input.password = await bcrypt.hash(input.password, 10);
     }
@@ -61,7 +66,16 @@ const updateCurrentUser = async (req: Request, res: Response) => {
     const updatedEmployee = EmployeeDataAfterUpdate[1][0];
     delete updatedEmployee.password;
 
-    return res.status(200).json({ success: true, result: updatedEmployee });
+    return res.status(200).json({
+      success: true,
+      result: {
+        id: updatedEmployee.id,
+        first_name: updatedEmployee.first_name,
+        last_name: updatedEmployee.last_name,
+        email: updatedEmployee.email,
+        role: updatedEmployee.role,
+      },
+    });
   } catch (error) {
     console.error("server error: ", error.message);
     return res.status(500).json({ success: false, error: "serverError" });
-- 
GitLab