From ec35494147cdd3a2affefb310776bcfe3e779e53 Mon Sep 17 00:00:00 2001 From: Tonia-SE <64790742+Tonia-SE@users.noreply.github.com> Date: Tue, 16 Mar 2021 19:44:46 +0300 Subject: [PATCH] fix: updte rates on the very first review --- src/handlers/rateCounter.ts | 1 - src/routes/reviews.ts | 25 ++++++++++++++----------- src/routes/sights.ts | 2 ++ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/handlers/rateCounter.ts b/src/handlers/rateCounter.ts index c4fbc88..f1b672e 100644 --- a/src/handlers/rateCounter.ts +++ b/src/handlers/rateCounter.ts @@ -17,4 +17,3 @@ function rateCounter(sightId: string) { } export default rateCounter; - diff --git a/src/routes/reviews.ts b/src/routes/reviews.ts index e2f9b2d..49119e2 100644 --- a/src/routes/reviews.ts +++ b/src/routes/reviews.ts @@ -37,6 +37,8 @@ reviewsRouter.post( user: user, sightId: sightId, rate: +rate, + }).then(() => { + rateCounter(sightId); }); } else { Reviews.findOneAndUpdate( @@ -45,10 +47,10 @@ reviewsRouter.post( rate: +rate, }, {}, - () => { - rateCounter(sightId); - }, - ); + () => {}, + ).then(() => { + rateCounter(sightId); + }); } res.status(200); res.json({ result: 'Rate was updated' }); @@ -76,25 +78,26 @@ reviewsRouter.post( }; const reviews: Array = await Reviews.find(filter); if (!reviews.length) { - Reviews.insertMany({ + await Reviews.insertMany({ user: user, sightId: sightId, rate: +rate, review: review, + }).then(() => { + rateCounter(sightId); }); } else { - console.log('updating'); - Reviews.findOneAndUpdate( + await Reviews.findOneAndUpdate( filter, { rate: +rate, review: review, }, {}, - () => { - rateCounter(sightId); - }, - ); + () => {}, + ).then(() => { + rateCounter(sightId); + }); } res.status(200); res.json({ result: 'Review was updated' }); diff --git a/src/routes/sights.ts b/src/routes/sights.ts index 9e2879a..9ffa265 100644 --- a/src/routes/sights.ts +++ b/src/routes/sights.ts @@ -9,6 +9,8 @@ sightsRouter.get( let filter = {}; if (req.query.countryId) { filter = { countryId: req.query.countryId }; + } else if (req.query.sightId) { + filter = { _id: req.query.sightId }; } const sights = await Sights.find(filter); res.json(sights);