Skip to content
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
9b05952
Move permisisons to own file
brylie Jun 13, 2016
d682d07
Move allow rules; create initial deny rules
brylie Jun 13, 2016
c0b961c
Add averageRating field
brylie Jun 13, 2016
1a664ac
Add bookmarkCount field
brylie Jun 13, 2016
5e4262f
Don't allow averageRating or bookmarkCount to be set by user
brylie Jun 13, 2016
40ee643
Add getAverageRating and getBookmarkCount helpers
brylie Jun 13, 2016
437cc3b
Move helpers to own file
brylie Jun 13, 2016
84c874e
Cleanup
brylie Jun 13, 2016
e567f88
Move helpers; add setAverageRating helper
brylie Jun 13, 2016
26ff894
averageRating and bookmarkCount optional
brylie Jun 13, 2016
7139fad
Ensure apiRating value exists
brylie Jun 13, 2016
6449750
Add setAllApiBackendAverageRatings method
brylie Jun 13, 2016
e0072eb
Include averageRating field
brylie Jun 13, 2016
cd089ae
Pre-populate averageRating star value
brylie Jun 13, 2016
a72f5d9
Add setBookmarkCount method; refactor to 'bookmark'
brylie Jun 13, 2016
9827184
Add setAllApiBackendBookmarkCounts method
brylie Jun 13, 2016
6731c73
Comment and verbose variable name
brylie Jun 13, 2016
bfc8cff
Add popularity column
brylie Jun 13, 2016
cfd9350
Use object shorthand
brylie Jun 13, 2016
0bb19fb
Unset bookmark count if no bookmarks
brylie Jun 13, 2016
7e1d0cc
Add setApiBackendBookmarkCount method
brylie Jun 13, 2016
90bd6ae
Call setApiBackendBookmarkCount method
brylie Jun 13, 2016
16dc998
Add setApiBackendAverageRating method
brylie Jun 13, 2016
16f2543
Call setApiBackendAverageRating method
brylie Jun 13, 2016
cad5453
Cleanup console.log
brylie Jun 14, 2016
efb183b
Merge branch 'develop' of github.com:apinf/api-umbrella-dashboard int…
Jun 14, 2016
a2f52f2
add migration
Jun 14, 2016
a51e66d
Remove console.log
Jun 14, 2016
d49e8f4
Remove bookmarkCount and averageRating migration
brylie Jun 15, 2016
da38c73
Set bookmarkCounts and averageRatings on startup
brylie Jun 15, 2016
d1b98d9
Remove console.log
brylie Jun 15, 2016
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
132 changes: 16 additions & 116 deletions apis/collection/backend.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import ss from 'simple-statistics';

ApiBackends = new Mongo.Collection('apiBackends');

// RegEx constants
Expand Down Expand Up @@ -653,49 +651,28 @@ Schemas.ApiBackendsSchema = new SimpleSchema({
type: "hidden",
label: false
}
}
});

ApiBackends.attachSchema(Schemas.ApiBackendsSchema);

ApiBackends.allow({
insert: function () {
return true;
},
update: function (userId, apiBackendDoc) {
// Save ID of API Backend
const apiBackendId = apiBackendDoc._id;
// Get API backend with ID
const apiBackend = ApiBackends.findOne(apiBackendId);
// Check if current user can edit API Backend
let currentUserCanEdit = apiBackend.currentUserCanEdit();

if (currentUserCanEdit) {
// User is allowed to perform action
return true;
} else {
// User is not allowded to perform action
return false;
},
averageRating: {
type: Number,
decimal: true,
optional: true,
autoform: {
type: "hidden",
label: false
}
},
remove: function (userId, apiBackendDoc) {
// Save ID of API Backend
const apiBackendId = apiBackendDoc._id;
// Get API backend with ID
const apiBackend = ApiBackends.findOne(apiBackendId);
// Check if current user can edit API Backend
let currentUserCanEdit = apiBackend.currentUserCanEdit();

if (currentUserCanEdit) {
// User is allowed to perform action
return true;
} else {
// User is not allowded to perform action
return false;
bookmarkCount: {
type: Number,
optional: true,
autoform: {
type: "hidden",
label: false
}
}
});

ApiBackends.attachSchema(Schemas.ApiBackendsSchema);

SimpleSchema.messages({
// RegEx messages
regEx: [
Expand All @@ -705,80 +682,3 @@ SimpleSchema.messages({
// update password form
"updatePassword_passwordsMismatch": "Passwords do not match"
});

ApiBackends.helpers({
'getRating': function () {
// Get API Backend ID
apiBackendId = this._id;

// Check if user is logged in
if (Meteor.userId()) {
// Check if user has rated API Backend
var userRating = ApiBackendRatings.findOne({
userId: Meteor.userId(),
apiBackendId: apiBackendId
});

if (userRating) {
return userRating.rating;
}
}

// Otherwise, get average rating

// Fetch all ratings
var apiBackendRatings = ApiBackendRatings.find({
apiBackendId: apiBackendId
}).fetch();

// If ratings exist
if (apiBackendRatings) {
// Create array containing only rating values
var apiBackendRatingsArray = _.map(apiBackendRatings, function (rating) {
// get only the rating value; omit User ID and API Backend ID fields
return rating.rating;
});

// Get the average (mean) value for API Backend ratings
var apiBackendRatingsAverage = ss.mean(apiBackendRatingsArray);

return apiBackendRatingsAverage;
}
},
currentUserCanEdit: function() {
// Get current userId
var userId = Meteor.userId();

// Check that user is logged in
if( userId ) {
// Check if user is API manager
var isManager = _.contains(this.managerIds, userId);

if (isManager) {
return true;
}

// Check if user is administrator
var isAdmin = Roles.userIsInRole(userId, ['admin']);

if (isAdmin) {
return true;
}
} else {
// User is not logged in
return false;
}
},
currentUserIsManager: function () {
// Get current User ID
var userId = Meteor.userId();

// Get Manager IDs array from API Backend document
var managerIds = this.managerIds;

// Check if User ID is in Manager IDs array
var isManager = _.contains(managerIds, userId);

return isManager;
}
});
115 changes: 115 additions & 0 deletions apis/collection/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import ss from 'simple-statistics';

ApiBackends.helpers({
getAverageRating () {
console.log("getting average rating");
// Fetch all ratings
var apiBackendRatings = ApiBackendRatings.find({
apiBackendId: this._id
}).fetch();

// If ratings exist
if (apiBackendRatings) {
// Create array containing only rating values
var apiBackendRatingsArray = _.map(apiBackendRatings, function (rating) {
// get only the rating value; omit User ID and API Backend ID fields
return rating.rating;
});

// Get the average (mean) value for API Backend ratings
var apiBackendRatingsAverage = ss.mean(apiBackendRatingsArray);

return apiBackendRatingsAverage;
}
},
setAverageRating () {
// get average rating value
const averageRating = this.getAverageRating();

// Check if average rating calculation succeeds
if (averageRating) {
// Update the API Backend with average rating value
ApiBackends.update(this._id, {$set: {averageRating}});
}
},
getBookmarkCount () {
// Get API Backend ID
const apiBackendId = this._id;

// Get count of API Bookmarks where API Backend ID is in API Backend IDs array
const apiBookmarkCount = ApiBookmarks.find({apiIds: apiBackendId}).count();

return apiBookmarkCount;
},
setBookmarkCount () {
// get average rating value
const bookmarkCount = this.getBookmarkCount();

// Check if average rating calculation succeeds
if (bookmarkCount) {
// Update the API Backend with average rating value
ApiBackends.update(this._id, {$set: {bookmarkCount}});
} else {
ApiBackends.update(this._id, {$unset: {bookmarkCount: ""}})
}
},
getRating () {
// Get API Backend ID
const apiBackendId = this._id;

// Get current user ID
const userId = Meteor.userId();

// Check if user is logged in
if (Meteor.userId()) {
// Check if user has rated API Backend
var userRating = ApiBackendRatings.findOne({
apiBackendId,
userId
});

if (userRating) {
return userRating.rating;
}
}

// Otherwise, get average rating
return this.averageRating;
},
currentUserCanEdit () {
// Get current userId
var userId = Meteor.userId();

// Check that user is logged in
if( userId ) {
// Check if user is API manager
var isManager = _.contains(this.managerIds, userId);

if (isManager) {
return true;
}

// Check if user is administrator
var isAdmin = Roles.userIsInRole(userId, ['admin']);

if (isAdmin) {
return true;
}
} else {
// User is not logged in
return false;
}
},
currentUserIsManager () {
// Get current User ID
var userId = Meteor.userId();

// Get Manager IDs array from API Backend document
var managerIds = this.managerIds;

// Check if User ID is in Manager IDs array
var isManager = _.contains(managerIds, userId);

return isManager;
}
});
52 changes: 52 additions & 0 deletions apis/collection/permissions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
ApiBackends.allow({
insert: function () {
return true;
},
update: function (userId, apiBackendDoc) {
// Save ID of API Backend
const apiBackendId = apiBackendDoc._id;
// Get API backend with ID
const apiBackend = ApiBackends.findOne(apiBackendId);
// Check if current user can edit API Backend
let currentUserCanEdit = apiBackend.currentUserCanEdit();

if (currentUserCanEdit) {
// User is allowed to perform action
return true;
} else {
// User is not allowded to perform action
return false;
}
},
remove: function (userId, apiBackendDoc) {
// Save ID of API Backend
const apiBackendId = apiBackendDoc._id;
// Get API backend with ID
const apiBackend = ApiBackends.findOne(apiBackendId);
// Check if current user can edit API Backend
let currentUserCanEdit = apiBackend.currentUserCanEdit();

if (currentUserCanEdit) {
// User is allowed to perform action
return true;
} else {
// User is not allowded to perform action
return false;
}
}
});

ApiBackends.deny({
insert () {
// Don't allow user to set average rating or bookmark count fields
if (_.contains(fields, "averageRating") || _.contains(fields, "bookmarkCount")) {
return true;
}
},
update () {
// Don't allow user to set average rating or bookmark count fields
if (_.contains(fields, "averageRating") || _.contains(fields, "bookmarkCount")) {
return true;
}
}
});
20 changes: 20 additions & 0 deletions apis/server/methods/bookmarks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Meteor.methods({
setAllApiBackendBookmarkCounts () {
// Get all API Backends
const apiBackends = ApiBackends.find().fetch();

// Update the average rating value for each API Backend
apiBackends.forEach(function (apiBackend) {
// Set average rating value for current API Backend
apiBackend.setBookmarkCount();
});
},
setApiBackendBookmarkCount (apiBackendId) {
console.log("Setting api bookmark count")
// Get API Backend
const apiBackend = ApiBackends.findOne(apiBackendId);

// Update the API Backend bookmark count
apiBackend.setBookmarkCount();
}
});
20 changes: 20 additions & 0 deletions apis/server/methods/ratings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Meteor.methods({
setAllApiBackendAverageRatings () {
// Get all API Backends
const apiBackends = ApiBackends.find().fetch();

// Update the average rating value for each API Backend
apiBackends.forEach(function (apiBackend) {
// Set average rating value for current API Backend
apiBackend.setAverageRating();
});
},
setApiBackendAverageRating (apiBackendId) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brylie method name here could be something like set ApiBackendBookmarkCount

Copy link
Copy Markdown
Contributor Author

@brylie brylie Jun 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which method? The setApiBackendAverageRating is related to the averageRating field. Hence, this method is in the ratings.js file.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brylie Ok, I confused this because console.log says about bookmarks. Could you also cleanup/fix console.logs?

console.log("Setting api bookmark count")
// Get API Backend
const apiBackend = ApiBackends.findOne(apiBackendId);

// Update the API Backend bookmark count
apiBackend.setAverageRating();
}
});
10 changes: 6 additions & 4 deletions bookmarks/server/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Meteor.methods({

// object to store currentUserId and the Api ID
var userBookmarks = {userId: currentUserId, apiIds: apiBackendIds};

// If possible, get the bookmarks for current user.
var existingUserBookmarks = ApiBookmarks.findOne({userId: currentUserId});

// Check if user has existing bookmarks
if (existingUserBookmarks) {
// Get an array of bookmark IDs
Expand All @@ -31,14 +31,16 @@ Meteor.methods({
}

// Updating current user apiBookmarks
ApiBookmarks.update({userId: currentUserId},{$set: {apiIds: apiIds} });
ApiBookmarks.update({userId: currentUserId},{$set: { apiIds } });

} else {
// Insert bookmark to database
ApiBookmarks.insert(userBookmarks);
}

// Update the API Backend bookmark count
Meteor.call("setApiBackendBookmarkCount", backendId);

return apiIds;
}
});

Loading