This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(rate): a user can rate a facility
- add the rateFacility controller - add the totalRate and averageRate attribute to the facility table - add the ratings table [Maintains #170947578]
- Loading branch information
Showing
17 changed files
with
402 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Ratings', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.STRING | ||
}, | ||
rating: { | ||
type: Sequelize.INTEGER | ||
}, | ||
userId: { | ||
type: Sequelize.STRING, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
references:{ | ||
model: 'Users', | ||
key: 'id' | ||
} | ||
}, | ||
facilityId: { | ||
type: Sequelize.STRING, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
references:{ | ||
model: 'Facilities', | ||
key: 'id' | ||
} | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('Ratings'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Ratings = sequelize.define('Ratings', { | ||
userId: DataTypes.STRING, | ||
facilityId: DataTypes.STRING, | ||
rating: DataTypes.INTEGER | ||
}, {}); | ||
Ratings.associate = (models) => { | ||
Ratings.belongsTo(models.User, { | ||
foreignKey: 'userId', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
}); | ||
Ratings.belongsTo(models.Facilities, { | ||
foreignKey: 'facilityId', | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
}); | ||
}; | ||
return Ratings; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module.exports = { | ||
up: (queryInterface) => queryInterface.bulkInsert( | ||
'Bookings', [ | ||
{ | ||
id: 'd1d6c6d3-92b3-4000-9f8e-70981c49dc6e', | ||
roomId: '47d21452-ea54-4f2a-a1df-2c167c34cdd2', | ||
facilityId: '5be72db7-5510-4a50-9f15-e23f103116d5', | ||
checkin: '2018-01-11', | ||
checkout: '2019-10-10', | ||
requestId: '51e74db7-5510-4f50-9f15-e23710331ld5', | ||
bookedBy: 'jdev@andela.com', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
} | ||
], | ||
{}, | ||
), | ||
|
||
down: (queryInterface) => queryInterface.bulkDelete('Rooms', null, {}), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Sequelize } from 'sequelize'; | ||
import uuid from 'uuid/v4'; | ||
import db from '../models'; | ||
import stringHelper from '../utils/stringHelper'; | ||
|
||
/** | ||
* @description facility services | ||
* @class FacilitiesService | ||
*/ | ||
export default class FacilityService { | ||
/** | ||
* @param {Object} facilityId | ||
* @param {Object} user | ||
* @param {Object} rating | ||
* @returns {Object} updatedFacility | ||
*/ | ||
static async rateFacility(facilityId, user, rating) { | ||
const todayDate = new Date(); | ||
const facility = await db.Facilities.findOne({ where: { id: facilityId } }); | ||
if (!facility) return stringHelper.facilityNotFound; | ||
const booking = await db.Bookings.findOne({ | ||
where: { | ||
bookedBy: user.email, | ||
facilityId, | ||
checkin: { | ||
[Sequelize.Op.lte]: todayDate | ||
} | ||
} | ||
}); | ||
if (!booking) return stringHelper.notVisitedFacility; | ||
const ratingUser = await db.Ratings.findOne({ where: { facilityId, userId: user.id } }); | ||
if (!ratingUser) { | ||
await db.Ratings.create({ | ||
id: uuid(), | ||
userId: user.id, | ||
facilityId, | ||
rating: rating * 1, | ||
}); | ||
const sum = await db.Ratings.aggregate('rating', 'sum', { where: { facilityId } }); | ||
const average = ((await db.Ratings.aggregate('rating', 'avg', { where: { facilityId }, dataType: 'float' })) * 1).toFixed(2); | ||
const updatedFacility = await facility.update({ | ||
totalRating: sum, averageRating: average | ||
}, { | ||
returning: true, plain: true | ||
}); | ||
return updatedFacility; | ||
} | ||
await ratingUser.update({ rating: rating * 1 }, { returning: true, plain: true }); | ||
const sum = await db.Ratings.aggregate('rating', 'sum', { where: { facilityId } }); | ||
const average = ((await db.Ratings.aggregate('rating', 'avg', { where: { facilityId }, dataType: 'float' })) * 1).toFixed(2); | ||
const updatedFacility = await facility.update({ | ||
totalRating: sum, averageRating: average | ||
}, { | ||
returning: true, plain: true | ||
}); | ||
return updatedFacility; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.