From 609f18291c65a0cebb19a35e4715abe3416b60c8 Mon Sep 17 00:00:00 2001 From: Jonasz Bigda Date: Wed, 9 Dec 2020 16:41:08 +0100 Subject: [PATCH] /img (fixed) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Błędy rzucane przez multer są teraz obsługiwane poprawnie --- routes/routeImg.js | 29 +++++++++++++++-------------- routes/routeRestaurant.js | 2 +- services/databaseServices.js | 5 ++++- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/routes/routeImg.js b/routes/routeImg.js index d46c461..f07b12d 100644 --- a/routes/routeImg.js +++ b/routes/routeImg.js @@ -19,23 +19,24 @@ const uploadStrategy = multer({ // POST -router.post("/", async (req, res) => { - try { - await uploadStrategy(req, res, async (err) => { - if(err){ - if(err.code === "LIMIT_FILE_SIZE"){ - throw newError("error", 413); +router.post("/", (req, res) => { + uploadStrategy(req, res, async (err) => { + if (err) { + if (err.code === "LIMIT_FILE_SIZE") { + handleError(newError("File too big", 413), res); + } else { + handleError(newError("Unknown error...", 500), res); } } else { - const token = req.headers["x-auth-token"]; - validateUserToken(token); - await uploadBlob(req, res); + try { + const token = req.headers["x-auth-token"]; + validateUserToken(token); + await uploadBlob(req, res); + } catch (error) { + handleError(error, res) + } } }) - } catch (error) { - console.log("error: " + error) - handleError(error, res); - } }); -module.exports = router; +module.exports = router; \ No newline at end of file diff --git a/routes/routeRestaurant.js b/routes/routeRestaurant.js index 4cf5ae4..f23c50f 100644 --- a/routes/routeRestaurant.js +++ b/routes/routeRestaurant.js @@ -203,7 +203,7 @@ router.post("/trial", async (req, res) => { const token = req.headers["x-auth-token"]; const user = validateUserToken(token); await startTrial(req.body.restaurantId, user); - res.send("Trial aktywowany."); + res.send("Okres próbny aktywowany."); } catch (error) { handleError(error, res); } diff --git a/services/databaseServices.js b/services/databaseServices.js index 98b0bad..0308296 100644 --- a/services/databaseServices.js +++ b/services/databaseServices.js @@ -126,7 +126,7 @@ async function startTrial(restaurantId, userData) { throw newError("Nie udało się pobrać restauracji.", 404); }); if (!userData.trialUsed || userData.trialUsed === false) { - const dueDate = dueDateBasedOnSubscription(restaurant, monthsToAdd); + const dueDate = dueDateBasedOnSubscription(restaurant, 3); const start = startDate(restaurant); await Restaurant.findByIdAndUpdate(restaurantId, { $set: { @@ -140,6 +140,9 @@ async function startTrial(restaurantId, userData) { 500 ); }); + await User.findByIdAndUpdate(userData.id, { $set: { trialUsed: true } }).catch((e) => { + throw newError("Błąd podczas aktywacji okresu próbnego (user data)") + }) } else { throw newError("Okres próbny został już wykorzystany.", 500); }