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"; ...@@ -2,6 +2,8 @@ import { Request, Response } from "express";
import CheckIn from "../db/models/CheckIn"; import CheckIn from "../db/models/CheckIn";
import Boat from "../db/models/Boat"; import Boat from "../db/models/Boat";
import Sport from "../db/models/Sport"; import Sport from "../db/models/Sport";
import jwt from "jsonwebtoken";
import envVars from "../config";
export const checkInController = async (req: Request, res: Response) => { export const checkInController = async (req: Request, res: Response) => {
try { try {
...@@ -13,7 +15,7 @@ export const checkInController = async (req: Request, res: Response) => { ...@@ -13,7 +15,7 @@ export const checkInController = async (req: Request, res: Response) => {
destination, destination,
email, email,
persons, persons,
responsible responsible,
}: { }: {
sport: string; sport: string;
boatName: string; boatName: string;
...@@ -25,16 +27,26 @@ export const checkInController = async (req: Request, res: Response) => { ...@@ -25,16 +27,26 @@ export const checkInController = async (req: Request, res: Response) => {
responsible: string; responsible: string;
} = req.body; } = req.body;
const boat = await Boat.findByPk(boatName); const boat = await Boat.findByPk(boatName);
if(boat) { if (boat) {
if (boat.status != 0 || (await CheckIn.findAll({ where: { BoatId: boatName, returned: false } })).length > 0) { if (
return res.status(400).json({ success: false, error: "Boat not Availible" }); 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
else return res.status(404).json({ success: false, error: "boatIdNotFound" }); return res.status(404).json({ success: false, error: "boatIdNotFound" });
const sportObj = await Sport.findByPk(sport); 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({ const newLogEntry = await CheckIn.create({
SportId: sport, SportId: sport,
BoatId: boatName, BoatId: boatName,
...@@ -47,28 +59,40 @@ export const checkInController = async (req: Request, res: Response) => { ...@@ -47,28 +59,40 @@ export const checkInController = async (req: Request, res: Response) => {
bookingType: "default", bookingType: "default",
returned: false, returned: false,
note: null, 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 result after checking all possible error-cases
return res.status(201).json({ return res
success: true, .cookie("checkin_token", token, {
result: { secure: process.env.NODE_ENV === "production",
id: newLogEntry.id, path: "/",
sport, })
boatName, .status(201)
startTime, .json({
estimatedEndTime, success: true,
destination, result: {
responsible, id: newLogEntry.id,
email, sport,
persons, boatName,
bookingType: "default", startTime,
returned: false, estimatedEndTime,
note: null, destination,
date: new Date() responsible,
}, email,
}); persons,
bookingType: "default",
returned: false,
note: null,
date: new Date(),
},
});
} catch (error) { } catch (error) {
console.log(error); console.log(error);
return res.status(500).json({ success: false, error: "serverError" }); return res.status(500).json({ success: false, error: "serverError" });
...@@ -76,37 +100,43 @@ export const checkInController = async (req: Request, res: Response) => { ...@@ -76,37 +100,43 @@ export const checkInController = async (req: Request, res: Response) => {
}; };
export const checkOutController = async (req: Request, res: Response) => { export const checkOutController = async (req: Request, res: Response) => {
try { try {
if (!req.params.id || req.params.id == 'undefined'){ if (!req.params.id || req.params.id == "undefined") {
return res.status(404).json({ success: false, error: "checkInIdNotFound" }); return res
.status(404)
.json({ success: false, error: "checkInIdNotFound" });
} }
const checkin = await CheckIn.findByPk(req.params.id); const checkin = await CheckIn.findByPk(req.params.id);
if(!checkin) { if (!checkin) {
return res.status(404).json({ success: false, error: "checkInIdNotFound" }); return res
.status(404)
.json({ success: false, error: "checkInIdNotFound" });
} }
// TODO integrate booking Type // TODO integrate booking Type
// TODO On annotation send Mail // TODO On annotation send Mail
const { const {
note, note,
bookingType bookingType,
}: { }: {
note: string; note: string;
bookingType: string; bookingType: string;
} = req.body; } = req.body;
const updatedCheckin = await CheckIn.update({ const updatedCheckin = await CheckIn.update(
returned: true, {
note returned: true,
}, { note,
where: {
id: req.params.id,
}, },
returning: true, {
}); where: {
id: req.params.id,
},
returning: true,
}
);
return res.status(200).json({ return res.status(200).json({
success: true, success: true,
result: updatedCheckin, result: updatedCheckin,
}); });
} catch (error) { } catch (error) {
console.error(error.message); console.error(error.message);
return res.status(500).json({ success: false, error: "serverError" }); return res.status(500).json({ success: false, error: "serverError" });
...@@ -125,14 +155,35 @@ export const getCheckInController = async (req: Request, res: Response) => { ...@@ -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) => { export const resetCheckIns = async (req?: Request, res?: Response) => {
const updatedCheckin = await CheckIn.update({ const updatedCheckin = await CheckIn.update(
returned: true, {
}, { returned: true,
where: {
returned: false,
}, },
returning: true, {
}); where: {
} returned: false,
\ No newline at end of file },
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