From 5a7f24b372e54c8ab6fb4451d2c9cc072cc3f5b8 Mon Sep 17 00:00:00 2001 From: elit04 <elit04@fu-berlin.de> Date: Thu, 13 Jan 2022 09:08:02 -0500 Subject: [PATCH] create log entry controller --- server/src/controllers/auth.controllers.ts | 52 +++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/server/src/controllers/auth.controllers.ts b/server/src/controllers/auth.controllers.ts index e62ff91..d96f5a7 100644 --- a/server/src/controllers/auth.controllers.ts +++ b/server/src/controllers/auth.controllers.ts @@ -2,8 +2,11 @@ import bcrypt from "bcrypt"; import { Request, Response } from "express"; import jwt from "jsonwebtoken"; import Employee from "../db/models/Employee"; +import CheckIn from "../db/models/CheckIn"; +import Boat from "../db/models/Boat"; import envVars from "../config"; +//log in const authLoginController = async (req: Request, res: Response) => { try { const { email, password } = req.body; @@ -38,4 +41,51 @@ const authLoginController = async (req: Request, res: Response) => { return res.status(500).json({ success: false, error: "serverError" }); } }; -export default authLoginController; + +//create log entry +const createLogEntry = async (req: Request, res: Response) => { + + try{ + + const logEntry = req.body; + + const boatid = Boat.findByPk(logEntry.boatID); + + //get status of boat + const bootAvailability = (await boatid).status; + + + //if boat is not available => it's locked + if(!(bootAvailability === true)) { + return res.status(409).json({ success: false, error: "BoatLocked" }); + }; + + + const newLogEntry = await CheckIn.create(logEntry); + + return res.status(201).json({ + success: true, + account: { + startTime: newLogEntry.startTime, + estimatedEndTime: newLogEntry.estimatedEndTime, + email: newLogEntry.email, + firstName: newLogEntry.firstName, + lastName: newLogEntry.lastName, + additionalClients: newLogEntry.additionalClients, + boatID: newLogEntry.boatID, + }, + }); + } catch (error) { + console.error(error.message); + return res.status(500).json({ success: false, error: "serverError" }); + } +}; + + +const authControllers = { + authLoginController, + createLogEntry, +}; + + +export default authControllers; -- GitLab