Skip to content
Snippets Groups Projects
Commit 5e44574f authored by alrwasheda's avatar alrwasheda :speech_balloon:
Browse files

check if num of the other clients + main client = num of total seats of chosen boat

parent 238838db
No related branches found
No related tags found
No related merge requests found
import { Request, Response } from "express"; import { Request, Response } from "express";
import CheckIn from "../db/models/CheckIn"; import CheckIn from "../db/models/CheckIn";
import Boat from "../db/models/Boat"; import Boat from "../db/models/Boat";
import BoatType from "../db/models/BoatType";
//create log entry //create log entry
const createLogEntryController = async (req: Request, res: Response) => { const createLogEntryController = async (req: Request, res: Response) => {
try { try {
//get input
const logEntry = req.body; 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 //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) { if (bootAvailability === false) {
return res.status(409).json({ success: false, error: "BoatLocked" }); 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); const newLogEntry = await CheckIn.create(logEntry);
//return result after checking all possible error-cases
return res.status(201).json({ return res.status(201).json({
success: true, success: true,
result: { result: {
startTime: newLogEntry.startTime, startTime: newLogEntry.startTime,
estimatedEndTime: newLogEntry.estimatedEndTime, estimatedEndTime: newLogEntry.estimatedEndTime,
email: newLogEntry.email, email: newLogEntry.email,
firstName: newLogEntry.firstName, fullNameOfResponsableClient: newLogEntry.fullNameOfResponsableClient,
lastName: newLogEntry.lastName,
additionalClients: newLogEntry.additionalClients, additionalClients: newLogEntry.additionalClients,
boatID: newLogEntry.boatID, boatID: newLogEntry.boatId,
}, },
}); });
} catch (error) { } catch (error) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment