server v1.0.8
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
|||||||
removeRestaurant,
|
removeRestaurant,
|
||||||
changeCategory,
|
changeCategory,
|
||||||
changeLunchMenu,
|
changeLunchMenu,
|
||||||
|
changeLunchMenuSet,
|
||||||
fetchUser,
|
fetchUser,
|
||||||
} from "../services/databaseServices.js";
|
} from "../services/databaseServices.js";
|
||||||
import {
|
import {
|
||||||
@@ -103,6 +104,23 @@ router.post("/category", async (req, res) => {
|
|||||||
|
|
||||||
// CHANGE LUNCH MENU
|
// 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) => {
|
router.post("/lunch", async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const token = req.headers["x-auth-token"];
|
const token = req.headers["x-auth-token"];
|
||||||
@@ -111,11 +129,11 @@ router.post("/lunch", async (req, res) => {
|
|||||||
await verifyRestaurantAccess(req.body.restaurantId, user);
|
await verifyRestaurantAccess(req.body.restaurantId, user);
|
||||||
await changeLunchMenu(
|
await changeLunchMenu(
|
||||||
req.body.restaurantId,
|
req.body.restaurantId,
|
||||||
|
req.body.setName,
|
||||||
req.body.dishId,
|
req.body.dishId,
|
||||||
req.body.action
|
req.body.action
|
||||||
);
|
);
|
||||||
const restaurant = await fetchRestaurant(req.body.restaurantId);
|
res.sendStatus(200);
|
||||||
res.send(restaurant);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
handleError(error, res);
|
handleError(error, res);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ export async function createRestaurant(request, oldRestaurant) {
|
|||||||
placesId: request.placesId,
|
placesId: request.placesId,
|
||||||
imgUrl: img,
|
imgUrl: img,
|
||||||
workingHours: request.workingHours,
|
workingHours: request.workingHours,
|
||||||
|
lunchHours: request.lunchHours,
|
||||||
description: sanitizer.sanitize.keepUnicode(request.description),
|
description: sanitizer.sanitize.keepUnicode(request.description),
|
||||||
tags: request.tags,
|
tags: request.tags,
|
||||||
links: request.links,
|
links: request.links,
|
||||||
@@ -84,6 +85,7 @@ export async function createRestaurant(request, oldRestaurant) {
|
|||||||
placesId: request.placesId,
|
placesId: request.placesId,
|
||||||
imgUrl: img,
|
imgUrl: img,
|
||||||
workingHours: request.workingHours,
|
workingHours: request.workingHours,
|
||||||
|
lunchHours: request.lunchHours,
|
||||||
description: sanitizer.sanitize.keepUnicode(request.description),
|
description: sanitizer.sanitize.keepUnicode(request.description),
|
||||||
tags: request.tags,
|
tags: request.tags,
|
||||||
links: request.links,
|
links: request.links,
|
||||||
|
|||||||
@@ -120,10 +120,25 @@ async function checkIfCategoryExists(restaurant, category) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkIfAlreadyInLunchMenu(restaurant, dishId) {
|
async function checkIfSetAlreadyInLunchMenu(restaurant, setName) {
|
||||||
const lunchMenu = restaurant.lunchMenu;
|
const lunchMenu = restaurant.lunchMenu;
|
||||||
if (lunchMenu.includes(dishId)) {
|
for (lunchSet of lunchMenu) {
|
||||||
throw newError("Podane danie jest już w lunch menu", 200);
|
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") {
|
if (action === "add") {
|
||||||
const restaurant = await Restaurant.findById(restaurantId).catch((err) => {
|
const restaurant = await Restaurant.findById(restaurantId).catch((err) => {
|
||||||
throw newError("Nie udało się pobrać restauracji.", 404);
|
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, {
|
await Restaurant.findByIdAndUpdate(restaurantId, {
|
||||||
$push: { lunchMenu: dishId },
|
$push: { lunchMenu: dishId },
|
||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user