Skip to content

Commit

Permalink
Fixed customerInfoUpdate, storeStatistics and caluclate rating on app…
Browse files Browse the repository at this point in the history
…roved reivews, not when posting a review. Maybe some other things, i don't recall what i have done anymore:)
  • Loading branch information
frikkha committed Jan 8, 2020
1 parent ee4bc3a commit b354e1c
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 31 deletions.
18 changes: 11 additions & 7 deletions middleware/updateRatingMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const MenuItem = require('../models/menuItem.js');
const ItemReview = require('../models/itemReview.js');
const MenuReview = require('../models/menuReview.js');

const Sequelize = require('sequelize');
const Op = Sequelize.Op;

// Input is:
// List of menuItems that have a new review, or have had a review deleted.
// The menu that is modified.
Expand All @@ -14,17 +17,16 @@ module.exports = async (menuItems, menu) => {
menuItems = await Promise.all(menuItems);

menuItems = await menuItems.map(async mi => {
// TODO: This should only return ItemReviews with status "Approved"
// [Op.and]: [{MI_ID: mi.menuItemID}, {Status: 'Approved'}]

//This should only return ItemReviews with status "Approved"
const reviews = await ItemReview.findAll({
attributes: ['ItemRating'],
where: {
MI_ID: mi.menuItemID
[Op.and]: [{MI_ID: mi.menuItemID}, {Status: 'Approved'}]
}
});
let rating = await averageRating(reviews, 'ItemRating');
//Update Rating-attribute for MenuItem.
//Update Rating-attribute for MenuItem
await MenuItem.update({
Rating: rating
},
Expand All @@ -35,7 +37,7 @@ module.exports = async (menuItems, menu) => {
});
return reviews;
});
console.log('Finished with updating modified Items.');
//Finished with updating modified Items

let menuItemRatings = await MenuItem.findAll({
attributes: ['Rating'],
Expand All @@ -52,15 +54,17 @@ module.exports = async (menuItems, menu) => {
}

let menuItemsRating = await averageRating(mir, 'Rating');


// Find the new ratings of the Menu
// TODO: This should only return approved reviews.
// [Op.and]: [{Menu_ID: menu.Menu_ID}, {Status: 'Approved'}]
let menuReviews = await MenuReview.findAll({
attributes: ['ServiceRating', 'QualityRating'],
where: {
Menu_ID: menu.Menu_ID
[Op.and]: [{Menu_ID: menu.Menu_ID}, {Status: 'Approved'}]
}
});

let serviceRating = averageRating(menuReviews, 'ServiceRating');
let qualityRating = averageRating(menuReviews, 'QualityRating');
let menuRating = await computeMenuRating(qualityRating, serviceRating, menuItemsRating, menuReviews.length, menuItems.length);
Expand Down
3 changes: 3 additions & 0 deletions models/itemReview.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const ItemReview = db.define('ItemReview', {
},
MenuReview_ID: {
type: Sequelize.INTEGER
},
Status: {
type: Sequelize.STRING
}
},
{
Expand Down
5 changes: 0 additions & 5 deletions routes/createReview.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ router.post('/menu/:menuID', auth, isCustomer, async (req, res) => {
MenuReview_ID: menuReviewID
})
}
// TODO: Delete this!!! Move to ownerApproveReview
// Update ratings for Menu and MenuItems that are affected of this adding.

const addedToMenu = {Menu_ID: menuID};
await updateRating(itemReviews, addedToMenu);

res.status(201).send('Successful operation');
}catch (error) {
Expand Down
9 changes: 7 additions & 2 deletions routes/manageCustomerInformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const isCustomer = require('../middleware/checkIfCustomerMiddleware.js');
const Customer = require('../models/customer.js');

//These are needed for checking the the nationality of a customer.
const {getNames} = require('country-list');
const {getNames, getCode} = require('country-list');
nationalities = getNames();


Expand Down Expand Up @@ -87,12 +87,17 @@ router.put('/', auth, isCustomer, async (req, res) => {
Username: req.username
}
});
let nationalityName = customerModified[0].dataValues.Nationality;
let nationalityCode = getCode(nationalityName);

res.status(200).send({
email: customerModified[0].dataValues.Email,
gender: customerModified[0].dataValues.Gender,
ageRange: customerModified[0].dataValues.AgeRange,
nationality: customerModified[0].dataValues.Nationality
nationality: {
nationalityName,
nationalityCode
}
});
} catch (error) {
console.log(error);
Expand Down
2 changes: 0 additions & 2 deletions routes/manageCustomerReviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ router.get('/', auth, isCustomer , async (req, res) => {
// Delete a specific menuReview of a customer, with all its associated itemReviews
router.delete('/:reviewID', auth, isCustomer, async (req, res) => {
try {
//TODO: Validate input
//check if the review with given reviewID exist
const reviewFound = await MenuReview.findOne({
where: {
Review_ID: req.params.reviewID
Expand Down
46 changes: 33 additions & 13 deletions routes/pendingReviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const updateRating = require('../middleware/updateRatingMiddleware.js');
const Restaurant = require('../models/restaurant.js');
const Menu = require('../models/menu.js');
const MenuReview = require('../models/menuReview.js');
const ItemReview = require('../models/itemReview.js');
const MenuItem = require('../models/menuItem.js');


Expand All @@ -26,7 +27,7 @@ router.get('/', auth, isOwner, findRestaurant, async (req, res) => {
// console.log('In GET /api/user/owner/restaurant/review')

try {
const username=req.username;
const username = req.username;

const reviews = await MenuReview.findAll({
where: {
Expand Down Expand Up @@ -58,9 +59,9 @@ router.get('/', auth, isOwner, findRestaurant, async (req, res) => {
//Approve or disapprove pending review
router.post('/:reviewID', auth, isOwner, findRestaurant, async (req, res) => {
try {
const username=req.username;
const reviewID=req.params.reviewID;
const status=req.body.isApproved? 'Approved' : 'Disapproved';
const username = req.username;
const reviewID = req.params.reviewID;
const status = req.body.isApproved? 'Approved' : 'Disapproved';

//check if the review is regarding to logged in owner's restaurant
const ownerReviewCheck= await MenuReview.findOne({
Expand All @@ -78,8 +79,8 @@ router.post('/:reviewID', auth, isOwner, findRestaurant, async (req, res) => {

if(!ownerReviewCheck) res.status(404).send('Review with given reviewID not found');

const reviewRestaurantOwner=ownerReviewCheck.Menu.Restaurant.Owner;
if(reviewRestaurantOwner!=username) res.status(403).send('Forbidden request');
const reviewRestaurantOwner = ownerReviewCheck.Menu.Restaurant.Owner;
if(reviewRestaurantOwner != username) res.status(403).send('Forbidden request');

await MenuReview.update({
Status: status},
Expand All @@ -88,13 +89,32 @@ router.post('/:reviewID', auth, isOwner, findRestaurant, async (req, res) => {
Review_ID: reviewID
}
});
/*

let itemReviews = await ItemReview.findAll({
attributes: ['MI_ID', 'Review_ID'],
where: {
MenuReview_ID: ownerReviewCheck.Review_ID
}
});
console.log('itemreviews[0]: ' + itemReviews[0]);
for (let i = 0; i < itemReviews.length; i++) {
await ItemReview.update({
Status: status
},
{
where: {
Review_ID: itemReviews[i].Review_ID
}
})
}
itemReviews = await itemReviews.map(async r => {
return {menuItemID: r.MI_ID}
});

if (status === APPROVED) {
// const menuID = {Menu_ID: ownerReviewCheck.Menu_ID}
// Also find every itemReview associated to the current menuReview
// await updateRating(itemReviews, menuID)
const menuID = {Menu_ID: ownerReviewCheck.Menu_ID}
await updateRating(itemReviews, menuID)
}
*/

//get pending reviews
const reviews = await MenuReview.findAll({
Expand All @@ -113,14 +133,14 @@ router.post('/:reviewID', auth, isOwner, findRestaurant, async (req, res) => {
model: MenuItem
}]
}]
})
});

//build response
let response = formatReviews(reviews);

res.status(201).send(response);
}catch (error) {
res.status(500).send('Internal server error');
res.status(500).send(error);
}
});

Expand Down
45 changes: 44 additions & 1 deletion routes/searchByMenuItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,29 @@ const MenuItem = require('../models/menuItem.js');
const Restaurant = require('../models/restaurants.js');
const TaggedMenu = require('../models/taggedMenu.js');
const Tag = require('../models/tag.js');
const Customer = require('../models/customer.js');
const Search = require('../models/search.js');


const jwt = require('jsonwebtoken');
const config = require('config');

const Sequelize = require('sequelize');
const Op = Sequelize.Op;

router.get('/', async (req, res) => {
try {
const token = req.header('x-auth-token');
let username = null;
if (token) {
username = jwt.verify(token, config.get('jwtPrivateKey')).username;
}
const searchWord = req.query.menuItemName;
let matchingItems;
matchingItems = await MenuItem.findAll({
attributes: ['Name', 'Menu_ID'],
where: {
Name: {[Op.iLike]: '%'+ req.query.menuItemName + '%'}
Name: {[Op.iLike]: '%'+ searchWord+ '%'}
}
});
if (matchingItems.length < 1) {
Expand Down Expand Up @@ -63,15 +75,46 @@ router.get('/', async (req, res) => {
};
return menuInfo;
});
// Update the statistics
await storeSearch(username, searchWord);

const result = await Promise.all(promises);



res.status(200).send(result);
} catch (error) {
res.status(500).send('Internal server error.');
}
});

const storeSearch = async (username, searchWord) => {
const searchedFor = await Search.findOne({
where: {
[Op.and]: [{SearchedWord: searchWord}, {Username: username}]
}
});

if (searchedFor == null) {
const search = await Search.create({
Username: username,
SearchedWord: searchWord,
NumberOfSearches: 1
})
} else {
let count = searchedFor.NumberOfSearches + 1;
const search = await Search.update ({
NumberOfSearches: count
},
{
where: {
[Op.and]: [{SearchedWord: searchWord}, {Username: username}]
}
}
)
}
};

const formatTags = (arr) => {
for (let i = 0; i < arr.length; i++){
arr[i] = {
Expand Down
2 changes: 1 addition & 1 deletion validationSchemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const schemas = {
}),
//Change the password of the owner
changeOwnerPassword: Joi.object().keys({
oldPassword: Joi.strin().required(),
oldPassword: Joi.string().required(),
newPassword: Joi.string().min(5).required(),
})

Expand Down

0 comments on commit b354e1c

Please sign in to comment.