From 5e44574fa7b87ffa04678686495b6f11e567112d Mon Sep 17 00:00:00 2001
From: Hanen Alrwasheda <alrwasheda@mi.fu-berlin.de>
Date: Sun, 16 Jan 2022 06:19:48 +0100
Subject: [PATCH] check if num of the other clients + main client = num of
 total seats of chosen boat

---
 .../createLogEntryControllers.controllers.ts  | 35 +++++++++++++++----
 1 file changed, 29 insertions(+), 6 deletions(-)

diff --git a/server/src/controllers/createLogEntryControllers.controllers.ts b/server/src/controllers/createLogEntryControllers.controllers.ts
index 0314d4f..493496a 100644
--- a/server/src/controllers/createLogEntryControllers.controllers.ts
+++ b/server/src/controllers/createLogEntryControllers.controllers.ts
@@ -1,33 +1,56 @@
 import { Request, Response } from "express";
 import CheckIn from "../db/models/CheckIn";
 import Boat from "../db/models/Boat";
+import BoatType from "../db/models/BoatType";
 
 //create log entry
 const createLogEntryController = async (req: Request, res: Response) => {
   try {
+
+    //get input
     const logEntry = req.body;
-    const boatid = await Boat.findByPk(logEntry.boatID);
+
+    //find related boat using given boatId
+    const chosenBoat = await Boat.findByPk(logEntry.boatId);
+
+    //if boat not found
+    if (!chosenBoat) {
+      return res
+        .status(404)
+        .json({ success: false, error: "givenBoatIdNotFound" });
+    }
 
     //get status of boat
-    const bootAvailability = boatid.status;
+    const bootAvailability = chosenBoat.status;
 
-    //if boat is not available => it's locked
+    //if boat status is false => is not available => boat locked
     if (bootAvailability === false) {
       return res.status(409).json({ success: false, error: "BoatLocked" });
     }
 
+    //check if number of additional clients == number of seats - 1 (responsable client)
+    const numOfAdditionalClients = logEntry.additionalClients.length;
+    const boatTypeOfChosenBoat = await BoatType.findByPk(chosenBoat.boattype); //number of seats of any boat can be found in its boattype
+    if (numOfAdditionalClients !== boatTypeOfChosenBoat.seats - 1) { //minus responsable client
+      return res.status(404).json({
+        success: false,
+        error: "numOfAllGivenClientsDoesNotMatchNumOfChosenBoatSeats",
+      });
+    }
+
+    //create entry
     const newLogEntry = await CheckIn.create(logEntry);
 
+    //return result after checking all possible error-cases
     return res.status(201).json({
       success: true,
       result: {
         startTime: newLogEntry.startTime,
         estimatedEndTime: newLogEntry.estimatedEndTime,
         email: newLogEntry.email,
-        firstName: newLogEntry.firstName,
-        lastName: newLogEntry.lastName,
+        fullNameOfResponsableClient: newLogEntry.fullNameOfResponsableClient,
         additionalClients: newLogEntry.additionalClients,
-        boatID: newLogEntry.boatID,
+        boatID: newLogEntry.boatId,
       },
     });
   } catch (error) {
-- 
GitLab