Skip to content
Snippets Groups Projects
createLogEntry.controllers.ts 4.65 KiB
Newer Older
elit04's avatar
elit04 committed
import { Request, Response } from "express";
import CheckIn from "../db/models/CheckIn";
import Boat from "../db/models/Boat";
import Sport from "../db/models/Sport";
import jwt from "jsonwebtoken";
import envVars from "../config";
fu1106jv's avatar
fu1106jv committed
export const checkInController = async (req: Request, res: Response) => {
fu1106jv's avatar
fu1106jv committed
    const {
      sport,
      boatName,
      startTime,
      estimatedEndTime,
      destination,
      email,
      persons,
      responsible,
fu1106jv's avatar
fu1106jv committed
    }: {
      sport: string;
      boatName: string;
      startTime: Date;
      estimatedEndTime: Date;
      destination: string;
      email: string;
      persons: string[];
      responsible: string;
fu1106jv's avatar
fu1106jv committed
    } = req.body;
    const boat = await Boat.findByPk(boatName);
    if (boat) {
      if (
        boat.status != 0 ||
        (
          await CheckIn.findAll({
            where: { BoatId: boatName, returned: false },
          })
        ).length > 0
      ) {
        return res
          .status(400)
          .json({ success: false, error: "Boat not Availible" });
    } else
      return res.status(404).json({ success: false, error: "boatIdNotFound" });

    const sportObj = await Sport.findByPk(sport);
    if (!sportObj)
      return res.status(404).json({ success: false, error: "sportIdNotFound" });

fu1106jv's avatar
fu1106jv committed
    const newLogEntry = await CheckIn.create({
      SportId: sport,
      BoatId: boatName,
      startTime,
      estimatedEndTime,
      destination,
      fullNameOfResponsibleClient: responsible,
fu1106jv's avatar
fu1106jv committed
      email,
      additionalClients: persons,
      bookingType: "default",
      returned: false,
      note: null,
      date: new Date(),
fu1106jv's avatar
fu1106jv committed
    });
    const payload = {
      id: newLogEntry.id,
    };

    const token = jwt.sign(payload, envVars.JWT_SECRET);

    //return result after checking all possible error-cases
    return res
      .cookie("checkin_token", token, {
        secure: process.env.NODE_ENV === "production",
        path: "/",
      })
      .status(201)
      .json({
        success: true,
        result: {
          id: newLogEntry.id,
          sport,
          boatName,
          startTime,
          estimatedEndTime,
          destination,
          responsible,
          email,
          persons,
          bookingType: "default",
          returned: false,
          note: null,
          date: new Date(),
        },
      });
  } catch (error) {
    console.log(error);
    return res.status(500).json({ success: false, error: "serverError" });
  }
fu1106jv's avatar
fu1106jv committed
export const checkOutController = async (req: Request, res: Response) => {
  try {
    if (!req.params.id || req.params.id == "undefined") {
      return res
        .status(404)
        .json({ success: false, error: "checkInIdNotFound" });
    }
    const checkin = await CheckIn.findByPk(req.params.id);
    if (!checkin) {
      return res
        .status(404)
        .json({ success: false, error: "checkInIdNotFound" });
fu1106jv's avatar
fu1106jv committed
    // TODO integrate booking Type
    // TODO On annotation send Mail
fu1106jv's avatar
fu1106jv committed
    const {
      note,
      bookingType,
fu1106jv's avatar
fu1106jv committed
    }: {
      note: string;
      bookingType: string;
    } = req.body;
    const updatedCheckin = await CheckIn.update(
      {
        returned: true,
        note,
fu1106jv's avatar
fu1106jv committed
      },
      {
        where: {
          id: req.params.id,
        },
        returning: true,
      }
    );
    return res.status(200).json({
      success: true,
      result: updatedCheckin,
    });
fu1106jv's avatar
fu1106jv committed
  } catch (error) {
    console.error(error.message);
    return res.status(500).json({ success: false, error: "serverError" });
  }
};

export const getCheckInController = async (req: Request, res: Response) => {
  try {
    const checkin = await CheckIn.findByPk(req.params.id);
    if (checkin) {
      return res.status(200).json({ success: true, result: checkin });
    }
    return res.status(404).json({ success: false, error: "checkInIdNotFound" });
  } catch (error) {
    return res.status(500).json({ success: false, error: "serverError" });
  }
};

export const getCurrentCheckInController = async (
  req: Request,
  res: Response
) => {
  try {
    const currentId = res.locals.checkinToken.id; //payload
    const currentCheckIn = await CheckIn.findByPk(currentId);
    if (currentCheckIn) {
      return res.status(200).json({ success: true, result: currentCheckIn.id });
    } else {
      return res
        .status(404)
        .json({ success: false, error: "checkInIdNotFound" });
    }
  } catch (error) {
    res.status(500).json({ success: false, error: "serverError!" });
  }
};
fu1106jv's avatar
fu1106jv committed
export const resetCheckIns = async (req?: Request, res?: Response) => {
  const updatedCheckin = await CheckIn.update(
    {
      returned: true,
fu1106jv's avatar
fu1106jv committed
    },
    {
      where: {
        returned: false,
      },
      returning: true,
    }
  );
};