Trial activation, setRestaurantVisibility, Readme update, image upload fix

This commit is contained in:
2020-11-12 16:51:13 +01:00
parent cc72671911
commit 8830303fb4
5 changed files with 87 additions and 8 deletions

View File

@@ -166,6 +166,22 @@
<br>
- ### **/restaurant/visibility**
- #### **POST**
Takes a **restaurantId, visible** parameters and a **JWT token(header)**, and tries to set restaurant visibility.
<br>
- ### **/restaurant/trial**
- #### **POST**
Takes a **restaurantId** parameters and a **JWT token(header)**, and tries to activate trial (if not already used).
<br>
* ### **/img**
- #### **POST**

View File

@@ -10,6 +10,7 @@ const loadMongoose = async () => {
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
},
(err) => {
if (err) console.log("Unable to connect :(");

View File

@@ -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;

View File

@@ -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;

View File

@@ -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) {