Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
createLogEntry.controllers.ts 2.44 KiB
import { Request, Response } from "express";
import CheckIn from "../db/models/CheckIn";
import Boat from "../db/models/Boat";

export const checkInController = async (req: Request, res: Response) => {
  try {
    const {
      sport,
      boatName,
      startTime,
      estimatedEndTime,
      destination,
      email,
      persons,
      responsable
    }: {
      sport: string;
      boatName: string;
      startTime: Date;
      estimatedEndTime: Date;
      destination: string;
      email: string;
      persons: string[];
      responsable: string;
    } = req.body;

    // TODO test check status of Boat
    if ((await Boat.findByPk(boatName)).status != 0 || (await CheckIn.findAll({ where: { BoatId: boatName, returned: false } })).length > 0) {
      return res.status(400).json({ success: false, error: "Boat not Availible" });
    }


    const newLogEntry = await CheckIn.create({
      SportId: sport,
      BoatId: boatName,
      startTime,
      estimatedEndTime,
      destination,
      fullNameOfResponsibleClient: responsable,
      email,
      additionalClients: persons,
      bookingType: "default",
      returned: false,
      note: null,
      date: new Date()
    });

    //return result after checking all possible error-cases
    return res.status(201).json({
      success: true,
      result: {
        id: newLogEntry.id,
        sport,
        boatName,
        startTime,
        estimatedEndTime,
        destination,
        responsable,
        email,
        persons,
        bookingType: "default",
        returned: false,
        note: null,
        date: new Date()
      },
    });
  } catch (error) {
    return res.status(500).json({ success: false, error: "serverError" });
  }
};
export const checkOutController = async (req: Request, res: Response) => {
  try {
    // TODO integrate booking Type
    const {
      note,
      bookingType
    }: {
      note: string;
      bookingType: string;
    } = req.body;
    const updatedCheckin = await CheckIn.update({
      returned: true,
      note
    }, {
      where: {
        id: req.params.id,
      },
      returning: true,
    });

  } catch (error) {
    console.error(error.message);
    return res.status(500).json({ success: false, error: "serverError" });
  }
};
export const resetCheckIns = async (req?: Request, res?: Response) => {
  const updatedCheckin = await CheckIn.update({
    returned: true,
  }, {
    where: {
      returned: false,
    },
    returning: true,
  });
}