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> <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** * ### **/img**
- #### **POST** - #### **POST**

View File

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

View File

@@ -9,8 +9,9 @@ const {
changeLunchMenu, changeLunchMenu,
changeLunchMenuSet, changeLunchMenuSet,
fetchUser, fetchUser,
initializePayment,
renewSubscription, renewSubscription,
setRestaurantVisibility,
startTrial
} = require("../services/databaseServices.js"); } = require("../services/databaseServices.js");
const { const {
decodeAndSanitize, 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 // ACTIVATE SUBSCRIPTION
router.post("/subscription", async (req, res) => { router.post("/subscription", async (req, res) => {
@@ -174,10 +190,23 @@ router.post("/subscription", async (req, res) => {
req.body.type req.body.type
); */ ); */
await renewSubscription(req.body.restaurantId, req.body.type); await renewSubscription(req.body.restaurantId, req.body.type);
res.send(200); res.sendStatus(200);
} catch (error) { } catch (error) {
handleError(error, res); 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; module.exports = router;

View File

@@ -121,6 +121,30 @@ async function renewSubscription(restaurantId, monthsToAdd) {
return dueDateBasedOnSubscription(restaurant, 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) { async function checkIfCategoryExists(restaurant, category) {
const categories = restaurant.categories; const categories = restaurant.categories;
if (categories.includes(category)) { if (categories.includes(category)) {
@@ -241,6 +265,7 @@ async function fetchRestaurant(id) {
const data = await Restaurant.findById(id).catch((e) => { const data = await Restaurant.findById(id).catch((e) => {
throw newError("Nie udało się pobrać restauracji.", 500); throw newError("Nie udało się pobrać restauracji.", 500);
}); });
console.log("fetched restaurant");
return data; return data;
} }
@@ -254,11 +279,8 @@ async function fetchMultipleRestaurants(idArray) {
} }
async function fetchAllDishesForRestaurant(restaurant) { async function fetchAllDishesForRestaurant(restaurant) {
let dishes = []; const idList = restaurant.dishes;
for (const dish of restaurant.dishes) { const dishes = await Dish.find({ '_id': { $in: idList } });
let res = await fetchDish(dish._id);
if (res !== null) dishes.push(res);
}
return dishes; return dishes;
} }
@@ -337,6 +359,14 @@ async function initializePayment(restaurantId, userData, type) {
return payment; 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.changeUserPass = changeUserPass;
exports.removeDish = removeDish; exports.removeDish = removeDish;
exports.removeRestaurant = removeRestaurant; exports.removeRestaurant = removeRestaurant;
@@ -353,3 +383,5 @@ exports.fetchAllDishesForRestaurant = fetchAllDishesForRestaurant;
exports.fetchDish = fetchDish; exports.fetchDish = fetchDish;
exports.fetchUser = fetchUser; exports.fetchUser = fetchUser;
exports.initializePayment = initializePayment; exports.initializePayment = initializePayment;
exports.setRestaurantVisibility = setRestaurantVisibility;
exports.startTrial = startTrial;

View File

@@ -19,6 +19,7 @@ async function renameBlob(blobURL) {
s3.copyObject({ s3.copyObject({
CopySource: "menuicdn/" + key, CopySource: "menuicdn/" + key,
Bucket: "menuicdn", Bucket: "menuicdn",
ACL: 'public-read',
Key: removePrefix(key) Key: removePrefix(key)
}, (err) => { }, (err) => {
if (err) { if (err) {