Cleanup
This commit is contained in:
@@ -1,15 +1,11 @@
|
||||
const Restaurant = require("../models/restaurant.js");
|
||||
const Dish = require("../models/dish.js");
|
||||
const User = require("../models/users.js");
|
||||
const Report = require("../models/reports.js")
|
||||
const Report = require("../models/reports.js");
|
||||
const { deleteImage } = require("./oceanServices.js");
|
||||
const { newError } = require("./services.js");
|
||||
|
||||
async function changeUserPass(userId, newPass) {
|
||||
User.findByIdAndUpdate(userId, { $set: { password: newPass } }).catch((e) => {
|
||||
throw newError("Zmiana hasła nie powiodła się.", 500);
|
||||
});
|
||||
}
|
||||
// REMOVE DISH
|
||||
|
||||
async function removeDish(dishId) {
|
||||
const deletedDoc = await Dish.findByIdAndDelete(dishId).catch((e) => {
|
||||
@@ -23,6 +19,8 @@ async function removeDish(dishId) {
|
||||
});
|
||||
}
|
||||
|
||||
// REMOVE RESTAURANT
|
||||
|
||||
async function removeRestaurant(restaurantId, userId) {
|
||||
const deletedDoc = await Restaurant.findByIdAndDelete(restaurantId).catch(
|
||||
(e) => {
|
||||
@@ -46,6 +44,8 @@ async function removeRestaurant(restaurantId, userId) {
|
||||
});
|
||||
}
|
||||
|
||||
// ADD DISH TO RESTAURANT
|
||||
|
||||
async function addDishToRestaurant(restaurantId, dishId) {
|
||||
await Restaurant.updateOne(
|
||||
{ _id: restaurantId },
|
||||
@@ -55,6 +55,8 @@ async function addDishToRestaurant(restaurantId, dishId) {
|
||||
});
|
||||
}
|
||||
|
||||
// ADD RESTAURANT
|
||||
|
||||
async function addRestaurantToUser(user, restaurant) {
|
||||
await User.findByIdAndUpdate(user.id, {
|
||||
$push: { restaurants: restaurant._id },
|
||||
@@ -63,6 +65,8 @@ async function addRestaurantToUser(user, restaurant) {
|
||||
});
|
||||
}
|
||||
|
||||
// CHECK IF CATEGORY EXISTS
|
||||
|
||||
async function checkIfCategoryExists(restaurant, category) {
|
||||
const categories = restaurant.categories;
|
||||
if (categories.includes(category)) {
|
||||
@@ -70,27 +74,7 @@ async function checkIfCategoryExists(restaurant, category) {
|
||||
}
|
||||
}
|
||||
|
||||
async function checkIfSetAlreadyInLunchMenu(restaurant, setName) {
|
||||
const lunchMenu = restaurant.lunchMenu;
|
||||
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 (const lunchSet of lunchMenu) {
|
||||
if (lunchSet.lunchSetName === setName) {
|
||||
const dishes = lunchSet.lunchSetDishes;
|
||||
if (dishes.includes(dishId)) {
|
||||
throw newError("Danie jest już w podanym zestawie", 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// ADD OR REMOVE CATEGORY
|
||||
|
||||
async function changeCategory(restaurantId, categoryName, action) {
|
||||
if (action === "add") {
|
||||
@@ -114,6 +98,8 @@ async function changeCategory(restaurantId, categoryName, action) {
|
||||
}
|
||||
}
|
||||
|
||||
// SET DISH VISIBILITY
|
||||
|
||||
async function setDishVisibility(dishId, visible) {
|
||||
await Dish.findByIdAndUpdate(dishId, { $set: { hidden: !visible } }).catch(
|
||||
(e) => {
|
||||
@@ -122,99 +108,7 @@ async function setDishVisibility(dishId, visible) {
|
||||
);
|
||||
}
|
||||
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
|
||||
function appendDishToLunchSet(lunchMenu, setName, dishId, quantity) {
|
||||
const result = lunchMenu.map((lunchSet) => {
|
||||
if (lunchSet.lunchSetName === setName) {
|
||||
let updatedSet = lunchSet;
|
||||
let dishToAdd = {
|
||||
dishId: dishId,
|
||||
quantity: quantity
|
||||
}
|
||||
updatedSet.lunchSetDishes.push(dishToAdd);
|
||||
return updatedSet;
|
||||
} else {
|
||||
return lunchSet;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
function removeDishFromLunchSet(lunchMenu, setName, dishId) {
|
||||
console.log("remove called")
|
||||
const result = lunchMenu.map((lunchSet) => {
|
||||
if (lunchSet.lunchSetName === setName) {
|
||||
let updatedSet = lunchSet;
|
||||
const index = updatedSet.lunchSetDishes.findIndex((dish) => {
|
||||
return dish.dishId.toString() === dishId.toString();
|
||||
});
|
||||
if (index > -1) {
|
||||
updatedSet.lunchSetDishes.splice(index, 1);
|
||||
}
|
||||
return updatedSet;
|
||||
} else {
|
||||
return lunchSet;
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
async function changeLunchMenu(restaurantId, setName, dishId, quantity, 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);
|
||||
const updatedLunchMenu = appendDishToLunchSet(
|
||||
restaurant.lunchMenu,
|
||||
setName,
|
||||
dishId,
|
||||
quantity
|
||||
);
|
||||
await Restaurant.findByIdAndUpdate(restaurantId, {
|
||||
$set: { lunchMenu: updatedLunchMenu },
|
||||
}).catch((e) => {
|
||||
throw newError("Nie udało się dodać dania do lunch menu.", 500);
|
||||
});
|
||||
} else if (action === "delete") {
|
||||
const restaurant = await Restaurant.findById(restaurantId).catch((err) => {
|
||||
throw newError("Nie udało się pobrać restauracji.", 404);
|
||||
});
|
||||
const updatedLunchMenu = removeDishFromLunchSet(
|
||||
restaurant.lunchMenu,
|
||||
setName,
|
||||
dishId
|
||||
);
|
||||
await Restaurant.findByIdAndUpdate(restaurantId, {
|
||||
$set: { lunchMenu: updatedLunchMenu },
|
||||
}).catch((e) => {
|
||||
throw newError("Nie udało się usunąć dania.", 500);
|
||||
});
|
||||
} else {
|
||||
throw newError("Nie sprecyzowano akcji", 500);
|
||||
}
|
||||
}
|
||||
// FETCH RESTAURANT
|
||||
|
||||
async function fetchRestaurant(id) {
|
||||
const data = await Restaurant.findById(id).catch((e) => {
|
||||
@@ -223,17 +117,23 @@ async function fetchRestaurant(id) {
|
||||
return data;
|
||||
}
|
||||
|
||||
// FETCH MULTIPLE RESTAURANTS
|
||||
|
||||
async function fetchMultipleRestaurants(idArray) {
|
||||
let data = await Restaurant.find().where('_id').in(idArray).exec();
|
||||
let data = await Restaurant.find().where("_id").in(idArray).exec();
|
||||
return data;
|
||||
}
|
||||
|
||||
// FETCH DISHES FOR RESTAURANT
|
||||
|
||||
async function fetchAllDishesForRestaurant(restaurant) {
|
||||
const idList = restaurant.dishes;
|
||||
const dishes = await Dish.find({ '_id': { $in: idList } });
|
||||
const dishes = await Dish.find({ _id: { $in: idList } });
|
||||
return dishes;
|
||||
}
|
||||
|
||||
// FETCH SINGLE DISH
|
||||
|
||||
async function fetchDish(id) {
|
||||
let data = await Dish.findById(id).catch((e) => {
|
||||
throw newError(`Nie udało się pobrać ${id}`, 404);
|
||||
@@ -241,6 +141,8 @@ async function fetchDish(id) {
|
||||
return data;
|
||||
}
|
||||
|
||||
// FETCH USER BY EMAIL
|
||||
|
||||
async function fetchUser(email) {
|
||||
if (!email) throw newError("Brak danych", 204);
|
||||
const user = await User.findOne({ email: email });
|
||||
@@ -248,35 +150,42 @@ async function fetchUser(email) {
|
||||
return user;
|
||||
}
|
||||
|
||||
// SET RESTAURANT VISIBLE
|
||||
|
||||
async function setRestaurantVisibility(restaurantId, visible) {
|
||||
await Restaurant.findByIdAndUpdate(restaurantId, { $set: { hidden: !visible } }).catch(
|
||||
(e) => {
|
||||
throw newError("Nie udało się zmienić dania.", 500);
|
||||
}
|
||||
);
|
||||
await Restaurant.findByIdAndUpdate(restaurantId, {
|
||||
$set: { hidden: !visible },
|
||||
}).catch((e) => {
|
||||
throw newError("Nie udało się zmienić dania.", 500);
|
||||
});
|
||||
}
|
||||
|
||||
async function fetchAllAdminData(){
|
||||
const restaurants = await Restaurant.find({}, "_id name city adress subscriptionActive subscriptionDue phone dishes");
|
||||
// FETCH ALL DATA
|
||||
|
||||
async function fetchAllAdminData() {
|
||||
const restaurants = await Restaurant.find(
|
||||
{},
|
||||
"_id name city adress subscriptionActive subscriptionDue phone dishes"
|
||||
);
|
||||
const reports = await Report.find({});
|
||||
const users = await User.find({}, "_id email firstname lastname login billing isRestaurant restaurants trialUsed photos");
|
||||
const users = await User.find(
|
||||
{},
|
||||
"_id email firstname lastname login billing isRestaurant restaurants trialUsed photos"
|
||||
);
|
||||
const result = {
|
||||
restaurants: restaurants,
|
||||
reports: reports,
|
||||
users: users
|
||||
}
|
||||
users: users,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.changeUserPass = changeUserPass;
|
||||
exports.removeDish = removeDish;
|
||||
exports.removeRestaurant = removeRestaurant;
|
||||
exports.addDishToRestaurant = addDishToRestaurant;
|
||||
exports.addRestaurantToUser = addRestaurantToUser;
|
||||
exports.changeCategory = changeCategory;
|
||||
exports.setDishVisibility = setDishVisibility;
|
||||
exports.changeLunchMenuSet = changeLunchMenuSet;
|
||||
exports.changeLunchMenu = changeLunchMenu;
|
||||
exports.fetchRestaurant = fetchRestaurant;
|
||||
exports.fetchMultipleRestaurants = fetchMultipleRestaurants;
|
||||
exports.fetchAllDishesForRestaurant = fetchAllDishesForRestaurant;
|
||||
|
||||
Reference in New Issue
Block a user