server v1.0.0
This commit is contained in:
@@ -41,7 +41,7 @@ export async function addDishToRestaurant(restaurantId, dishId) {
|
||||
{ _id: restaurantId },
|
||||
{ $push: { dishes: dishId } }
|
||||
).catch((error) => {
|
||||
throw newError("Couldn't add dish to restaurant", 500);
|
||||
throw newError("Nie udało się dodać dania do restauracji", 500);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -49,16 +49,67 @@ export async function addRestaurantToUser(user, restaurant) {
|
||||
await User.findByIdAndUpdate(user.id, {
|
||||
$push: { restaurants: restaurant._id },
|
||||
}).catch((e) => {
|
||||
throw newError("Couldn't add restaurant to user", 500);
|
||||
throw newError("Nie udało się dodać restauracji do użytkownika", 500);
|
||||
});
|
||||
}
|
||||
|
||||
function dueDateBasedOnSubscription(restaurant, monthsToAdd) {
|
||||
let date;
|
||||
if (
|
||||
restaurant.subscriptionActive === false ||
|
||||
!restaurant.subscriptionActive
|
||||
) {
|
||||
date = new Date();
|
||||
date.setMonth(date.getMonth() + monthsToAdd);
|
||||
return date;
|
||||
} else {
|
||||
date = restaurant.subscriptionDue;
|
||||
date.setMonth(date.getMonth() + monthsToAdd);
|
||||
return date;
|
||||
}
|
||||
}
|
||||
|
||||
function startDate(restaurant) {
|
||||
let date;
|
||||
if (
|
||||
restaurant.subscriptionActive === true &&
|
||||
restaurant.subscriptionStarted
|
||||
) {
|
||||
date = restaurant.subscriptionStarted;
|
||||
return date;
|
||||
} else {
|
||||
date = new Date();
|
||||
return date;
|
||||
}
|
||||
}
|
||||
|
||||
export async function renewSubscription(restaurantId, monthsToAdd) {
|
||||
const restaurant = await Restaurant.findById(restaurantId).catch((err) => {
|
||||
throw newError("Nie udało się pobrać restauracji.", 404);
|
||||
});
|
||||
const dueDate = dueDateBasedOnSubscription(restaurant, monthsToAdd);
|
||||
const start = startDate(restaurant);
|
||||
await Restaurant.findByIdAndUpdate(restaurantId, {
|
||||
$set: {
|
||||
subscriptionActive: true,
|
||||
subscriptionDue: dueDate,
|
||||
subscriptionStarted: start,
|
||||
},
|
||||
}).catch((e) => {
|
||||
throw newError(
|
||||
"Nie udało się przedłużyć subskrypcji, spróbuj ponownie.",
|
||||
500
|
||||
);
|
||||
});
|
||||
return dueDateBasedOnSubscription(restaurant, monthsToAdd);
|
||||
}
|
||||
|
||||
export async function fetchRestaurant(id) {
|
||||
let data;
|
||||
await Restaurant.findById(id, (err, result) => {
|
||||
data = result;
|
||||
}).catch((e) => {
|
||||
throw newError("Couldn't fetch restaurant", 500);
|
||||
throw newError("Nie udało się pobrać restauracji.", 500);
|
||||
});
|
||||
return data;
|
||||
}
|
||||
@@ -74,14 +125,14 @@ export async function fetchAllDishesForRestaurant(restaurant) {
|
||||
|
||||
export async function fetchDish(id) {
|
||||
let data = await Dish.findById(id).catch((e) => {
|
||||
throw newError(`Couldn't fetch ${id}`, 404);
|
||||
throw newError(`Nie udało się pobrać ${id}`, 404);
|
||||
});
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function fetchUser(email) {
|
||||
if (!email) throw newError("No input", 204);
|
||||
if (!email) throw newError("Brak danych", 204);
|
||||
const user = await User.findOne({ email: email });
|
||||
if (!user) throw newError("No such user...", 404);
|
||||
if (!user) throw newError("Użytkownik nie istnieje", 404);
|
||||
return user;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user