Skip to content

Commit

Permalink
馃悰Fixed count.posts respecting co authors (#9830)
Browse files Browse the repository at this point in the history
closes #9822

- Fixed the post count issue for co authors
- Corrected and refactored tests related to users post count
- Consistency fix, because we return all posts where the author is primary or co author for the author page already
  • Loading branch information
naz authored and kirrg001 committed Aug 30, 2018
1 parent 647fb1d commit a0e7160
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
5 changes: 3 additions & 2 deletions core/server/models/plugins/include-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ module.exports = function (Bookshelf) {
}
},
users: {
posts: function addPostCountToTags(model, options) {
posts: function addPostCountToUsers(model, options) {
model.query('columns', 'users.*', function (qb) {
qb.count('posts.id')
.from('posts')
.whereRaw('posts.author_id = users.id')
.join('posts_authors', 'posts.id', 'posts_authors.post_id')
.whereRaw('posts_authors.author_id = users.id')
.as('count__posts');

if (options.context && options.context.public) {
Expand Down
32 changes: 13 additions & 19 deletions core/test/integration/api/api_users_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,25 +184,19 @@ describe('Users API', function () {
should.exist(response.users);
response.users.should.have.length(9);

testUtils.API.checkResponse(response.users[0], 'user', 'count');
testUtils.API.checkResponse(response.users[1], 'user', 'count');
testUtils.API.checkResponse(response.users[2], 'user', 'count');
testUtils.API.checkResponse(response.users[3], 'user', 'count');
testUtils.API.checkResponse(response.users[4], 'user', 'count');
testUtils.API.checkResponse(response.users[5], 'user', 'count');
testUtils.API.checkResponse(response.users[6], 'user', 'count');
testUtils.API.checkResponse(response.users[7], 'user', 'count');
testUtils.API.checkResponse(response.users[8], 'user', 'count');

response.users[0].count.posts.should.eql(0);
response.users[1].count.posts.should.eql(0);
response.users[2].count.posts.should.eql(0);
response.users[3].count.posts.should.eql(0);
response.users[4].count.posts.should.eql(0);
response.users[5].count.posts.should.eql(8);
response.users[6].count.posts.should.eql(0);
response.users[7].count.posts.should.eql(0);
response.users[8].count.posts.should.eql(0);
response.users.map((user) => {
testUtils.API.checkResponse(user, 'user', 'count');
});

response.users.map((user) => {
if (user.email === 'jbloggs@example.com') {
user.count.posts.should.eql(8);
} else if (user.email === 'jbOgendAth@example.com') {
user.count.posts.should.eql(1);
} else {
response.users[1].count.posts.should.eql(0);
}
});

response.meta.pagination.should.have.property('page', 1);
response.meta.pagination.should.have.property('limit', 15);
Expand Down
2 changes: 1 addition & 1 deletion core/test/utils/fixtures/data-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ DataGenerator.forKnex = (function () {
{
id: ObjectId.generate(),
post_id: DataGenerator.Content.posts[3].id,
author_id: DataGenerator.Content.users[2].id,
author_id: _.find(DataGenerator.Content.users, {email: 'jbOgendAth@example.com'}).id,
sort_order: 1
},
{
Expand Down

0 comments on commit a0e7160

Please sign in to comment.