server v1.0.8

This commit is contained in:
2020-10-03 18:54:59 +02:00
parent 63c2fb88e0
commit 3dbbe4b1ba
3 changed files with 64 additions and 7 deletions

View File

@@ -7,6 +7,7 @@ import {
removeRestaurant,
changeCategory,
changeLunchMenu,
changeLunchMenuSet,
fetchUser,
} from "../services/databaseServices.js";
import {
@@ -103,6 +104,23 @@ router.post("/category", async (req, res) => {
// CHANGE LUNCH MENU
router.post("/lunchSet", async (req, res) => {
try {
const token = req.headers["x-auth-token"];
const user = validateUserToken(token);
await validateRestaurant(req.body.restaurantId);
await verifyRestaurantAccess(req.body.restaurantId, user);
await changeLunchMenuSet(
req.body.restaurantId,
req.body.action,
req.body.set
);
res.sendStatus(200);
} catch (error) {
handleError(error, res);
}
});
router.post("/lunch", async (req, res) => {
try {
const token = req.headers["x-auth-token"];
@@ -111,11 +129,11 @@ router.post("/lunch", async (req, res) => {
await verifyRestaurantAccess(req.body.restaurantId, user);
await changeLunchMenu(
req.body.restaurantId,
req.body.setName,
req.body.dishId,
req.body.action
);
const restaurant = await fetchRestaurant(req.body.restaurantId);
res.send(restaurant);
res.sendStatus(200);
} catch (error) {
handleError(error, res);
}

View File

@@ -63,6 +63,7 @@ export async function createRestaurant(request, oldRestaurant) {
placesId: request.placesId,
imgUrl: img,
workingHours: request.workingHours,
lunchHours: request.lunchHours,
description: sanitizer.sanitize.keepUnicode(request.description),
tags: request.tags,
links: request.links,
@@ -84,6 +85,7 @@ export async function createRestaurant(request, oldRestaurant) {
placesId: request.placesId,
imgUrl: img,
workingHours: request.workingHours,
lunchHours: request.lunchHours,
description: sanitizer.sanitize.keepUnicode(request.description),
tags: request.tags,
links: request.links,

View File

@@ -120,10 +120,25 @@ async function checkIfCategoryExists(restaurant, category) {
}
}
async function checkIfAlreadyInLunchMenu(restaurant, dishId) {
async function checkIfSetAlreadyInLunchMenu(restaurant, setName) {
const lunchMenu = restaurant.lunchMenu;
if (lunchMenu.includes(dishId)) {
throw newError("Podane danie jest już w lunch menu", 200);
for (lunchSet of lunchMenu) {
if (lunchSet.lunchSetName === setName) {
throw newError("Nazwa zestawu jest zajęta", 409);
}
return;
}
}
async function checkIfAlreadyInSet(restaurant, setName, dishId) {
const lunchMenu = restaurant.lunchMenu;
for (lunchSet of lunchMenu) {
if (lunchSet.lunchSetName === setName) {
const dishes = lunchSet.lunchSetDishes;
if (dishes.includes(dishId)) {
throw newError("Danie jest już w podanym zestawie", 500);
}
}
}
}
@@ -157,12 +172,34 @@ export async function setDishVisibility(dishId, visible) {
);
}
export async function changeLunchMenu(restaurantId, dishId, action) {
export async function changeLunchMenuSet(restaurantId, action, lunchSet) {
if (action === "add") {
const restaurant = await Restaurant.findById(restaurantId).catch((err) => {
throw newError("Nie udało się pobrać restauracji.", 404);
});
await checkIfAlreadyInLunchMenu(restaurant, dishId);
await checkIfSetAlreadyInLunchMenu(restaurant, lunchSet.lunchSetName);
await Restaurant.findByIdAndUpdate(restaurantId, {
$push: { lunchMenu: lunchSet },
}).catch((e) => {
throw newError("Nie udało się dodać zestawu do lunch menu.", 500);
});
} else if (action === "delete") {
await Restaurant.findByIdAndUpdate(restaurantId, {
$pull: { lunchMenu: lunchSet },
}).catch((e) => {
throw newError("Nie udało się usunąć zestawu.", 500);
});
} else {
throw newError("Nie sprecyzowano akcji", 500);
}
}
export async function changeLunchMenu(restaurantId, setName, dishId, action) {
if (action === "add") {
const restaurant = await Restaurant.findById(restaurantId).catch((err) => {
throw newError("Nie udało się pobrać restauracji.", 404);
});
await checkIfAlreadyInSet(restaurant, setName, dishId);
await Restaurant.findByIdAndUpdate(restaurantId, {
$push: { lunchMenu: dishId },
}).catch((e) => {