Newer
Older
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";
export const checkInController = async (req: Request, res: Response) => {
const {
sport,
boatName,
startTime,
estimatedEndTime,
destination,
email,
persons,
}: {
sport: string;
boatName: string;
startTime: Date;
estimatedEndTime: Date;
destination: string;
email: string;
persons: string[];
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" });
const newLogEntry = await CheckIn.create({
SportId: sport,
BoatId: boatName,
startTime,
estimatedEndTime,
destination,
email,
additionalClients: persons,
bookingType: "default",
returned: false,
note: null,
const payload = {
id: newLogEntry.id,
};
const token = jwt.sign(payload, envVars.JWT_SECRET);

alrwasheda
committed
//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(),
},
});
return res.status(500).json({ success: false, error: "serverError" });
}
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" });
}: {
note: string;
bookingType: string;
} = req.body;
const updatedCheckin = await CheckIn.update(
{
returned: true,
note,
{
where: {
id: req.params.id,
},
returning: true,
}
);
return res.status(200).json({
success: true,
result: updatedCheckin,
});
} 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!" });
}
};
export const resetCheckIns = async (req?: Request, res?: Response) => {
const updatedCheckin = await CheckIn.update(
{
returned: true,
{
where: {
returned: false,
},
returning: true,
}
);
};