Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User can leave only one review for a single product #10

Merged
merged 1 commit into from
Jun 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions controllers/orderController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const JWT = require("../utilities/jwt");
const OrderFactory = require("../utilities/OrderFactory");
const { verifyAuthorization } = require("../utilities/verifyAuthorization");
const Order = require("../models/orderModel");
const Review = require("../models/reviewModel");
const mongoose = require("mongoose");

module.exports.addOrder = async (req, res) => {
Expand Down Expand Up @@ -32,6 +33,25 @@ module.exports.canReview = async (req, res) => {
});
}
let { uid } = JWT.verify(req.headers.authorization);

// check if review by the logged in user for this product already exists
try {
const reviews = await Review.find({
user: uid,
product: req.params.product_id,
});

if (reviews.length > 0) {
return res.status(200).json({
canReview: false,
});
}
} catch (err) {
return res.status(400).json({
message: "Something went wrong",
});
}

const pipeline = [
{ $unwind: "$products" },
{
Expand Down