From 8830303fb4ef89118cc121d0abe7b4c0dcfdecce Mon Sep 17 00:00:00 2001 From: Jonasz Bigda Date: Thu, 12 Nov 2020 16:51:13 +0100 Subject: [PATCH] Trial activation, setRestaurantVisibility, Readme update, image upload fix --- README.md | 16 ++++++++++++++ loaders/mongoose.js | 1 + routes/routeRestaurant.js | 33 ++++++++++++++++++++++++++-- services/databaseServices.js | 42 +++++++++++++++++++++++++++++++----- services/oceanServices.js | 3 ++- 5 files changed, 87 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index cfb201b..329253d 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,22 @@
+- ### **/restaurant/visibility** + + - #### **POST** + + Takes a **restaurantId, visible** parameters and a **JWT token(header)**, and tries to set restaurant visibility. + +
+ +- ### **/restaurant/trial** + + - #### **POST** + + Takes a **restaurantId** parameters and a **JWT token(header)**, and tries to activate trial (if not already used). + +
+ * ### **/img** - #### **POST** diff --git a/loaders/mongoose.js b/loaders/mongoose.js index 8ae93e5..d4b1d85 100644 --- a/loaders/mongoose.js +++ b/loaders/mongoose.js @@ -10,6 +10,7 @@ const loadMongoose = async () => { { useNewUrlParser: true, useUnifiedTopology: true, + useFindAndModify: false }, (err) => { if (err) console.log("Unable to connect :("); diff --git a/routes/routeRestaurant.js b/routes/routeRestaurant.js index e525ef5..4cf5ae4 100644 --- a/routes/routeRestaurant.js +++ b/routes/routeRestaurant.js @@ -9,8 +9,9 @@ const { changeLunchMenu, changeLunchMenuSet, fetchUser, - initializePayment, renewSubscription, + setRestaurantVisibility, + startTrial } = require("../services/databaseServices.js"); const { decodeAndSanitize, @@ -161,6 +162,21 @@ router.post("/delete", async (req, res) => { } }); +// SET RESTAURANT VISIBILITY + +router.post("/visibility", async (req, res) => { + try { + const token = req.headers["x-auth-token"]; + const user = await validateUserToken(token); + await validateRestaurant(req.body.restaurantId); + await verifyRestaurantAccess(req.body.restaurantId, user); + await setRestaurantVisibility(req.body.restaurantId, req.body.visible); + res.send("Widoczność restauracji została zmieniona."); + } catch (error) { + handleError(error, res); + } +}) + // ACTIVATE SUBSCRIPTION router.post("/subscription", async (req, res) => { @@ -174,10 +190,23 @@ router.post("/subscription", async (req, res) => { req.body.type ); */ await renewSubscription(req.body.restaurantId, req.body.type); - res.send(200); + res.sendStatus(200); } catch (error) { handleError(error, res); } }); +// ACTIVATE TRIAL + +router.post("/trial", async (req, res) => { + try { + const token = req.headers["x-auth-token"]; + const user = validateUserToken(token); + await startTrial(req.body.restaurantId, user); + res.send("Trial aktywowany."); + } catch (error) { + handleError(error, res); + } +}) + module.exports = router; diff --git a/services/databaseServices.js b/services/databaseServices.js index 63426de..98b0bad 100644 --- a/services/databaseServices.js +++ b/services/databaseServices.js @@ -121,6 +121,30 @@ async function renewSubscription(restaurantId, monthsToAdd) { return dueDateBasedOnSubscription(restaurant, monthsToAdd); } +async function startTrial(restaurantId, userData) { + const restaurant = await Restaurant.findById(restaurantId).catch((err) => { + throw newError("Nie udało się pobrać restauracji.", 404); + }); + if (!userData.trialUsed || userData.trialUsed === false) { + 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ę aktywować okresu próbnego.", + 500 + ); + }); + } else { + throw newError("Okres próbny został już wykorzystany.", 500); + } +} + async function checkIfCategoryExists(restaurant, category) { const categories = restaurant.categories; if (categories.includes(category)) { @@ -241,6 +265,7 @@ async function fetchRestaurant(id) { const data = await Restaurant.findById(id).catch((e) => { throw newError("Nie udało się pobrać restauracji.", 500); }); + console.log("fetched restaurant"); return data; } @@ -254,11 +279,8 @@ async function fetchMultipleRestaurants(idArray) { } async function fetchAllDishesForRestaurant(restaurant) { - let dishes = []; - for (const dish of restaurant.dishes) { - let res = await fetchDish(dish._id); - if (res !== null) dishes.push(res); - } + const idList = restaurant.dishes; + const dishes = await Dish.find({ '_id': { $in: idList } }); return dishes; } @@ -337,6 +359,14 @@ async function initializePayment(restaurantId, userData, type) { return payment; } +async function setRestaurantVisibility(restaurantId, visible) { + await Restaurant.findByIdAndUpdate(restaurantId, { $set: { hidden: !visible } }).catch( + (e) => { + throw newError("Nie udało się zmienić dania.", 500); + } + ); +} + exports.changeUserPass = changeUserPass; exports.removeDish = removeDish; exports.removeRestaurant = removeRestaurant; @@ -353,3 +383,5 @@ exports.fetchAllDishesForRestaurant = fetchAllDishesForRestaurant; exports.fetchDish = fetchDish; exports.fetchUser = fetchUser; exports.initializePayment = initializePayment; +exports.setRestaurantVisibility = setRestaurantVisibility; +exports.startTrial = startTrial; diff --git a/services/oceanServices.js b/services/oceanServices.js index 63ee879..29bf20e 100644 --- a/services/oceanServices.js +++ b/services/oceanServices.js @@ -18,7 +18,8 @@ async function renameBlob(blobURL) { const key = blobURL.replace(containerURL, ""); s3.copyObject({ CopySource: "menuicdn/" + key, - Bucket: "menuicdn", + Bucket: "menuicdn", + ACL: 'public-read', Key: removePrefix(key) }, (err) => { if (err) {