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.
- Loading branch information
Showing
15 changed files
with
408 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ module.exports = { | |
availability: true, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
} | ||
}, | ||
], | ||
{}, | ||
), | ||
|
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.