Skip to content
Snippets Groups Projects
Commit 615ba434 authored by Leander Tolksdorf's avatar Leander Tolksdorf
Browse files

send cookie with checkin

parent 9739a16c
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,8 @@ 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) => {
try {
......@@ -13,7 +15,7 @@ export const checkInController = async (req: Request, res: Response) => {
destination,
email,
persons,
responsible
responsible,
}: {
sport: string;
boatName: string;
......@@ -25,16 +27,26 @@ export const checkInController = async (req: Request, res: Response) => {
responsible: string;
} = 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" });
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" });
} 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" });
if (!sportObj)
return res.status(404).json({ success: false, error: "sportIdNotFound" });
const newLogEntry = await CheckIn.create({
SportId: sport,
BoatId: boatName,
......@@ -47,28 +59,40 @@ export const checkInController = async (req: Request, res: Response) => {
bookingType: "default",
returned: false,
note: null,
date: new Date()
date: new Date(),
});
const payload = {
id: newLogEntry.id,
};
const token = jwt.sign(payload, envVars.JWT_SECRET);
//return result after checking all possible error-cases
return res.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
.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" });
......@@ -76,37 +100,43 @@ export const checkInController = async (req: Request, res: Response) => {
};
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" });
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" });
if (!checkin) {
return res
.status(404)
.json({ success: false, error: "checkInIdNotFound" });
}
// TODO integrate booking Type
// TODO On annotation send Mail
const {
note,
bookingType
bookingType,
}: {
note: string;
bookingType: string;
} = req.body;
const updatedCheckin = await CheckIn.update({
returned: true,
note
}, {
where: {
id: req.params.id,
const updatedCheckin = await CheckIn.update(
{
returned: true,
note,
},
returning: true,
});
{
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" });
......@@ -125,14 +155,35 @@ export const getCheckInController = async (req: Request, res: Response) => {
}
};
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,
const updatedCheckin = await CheckIn.update(
{
returned: true,
},
returning: true,
});
}
\ No newline at end of file
{
where: {
returned: false,
},
returning: true,
}
);
};
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