Skip to content

Commit

Permalink
feat: get list of user by account type (#147)
Browse files Browse the repository at this point in the history
* feat: get list of user by account type

* chore: format using pritter

* feat: formate with prettier

* chore: run prettier

* chore: lint fix in article resolver

* chore: accountType check by undefined
  • Loading branch information
120EE0692 authored Oct 12, 2022
1 parent 0c8d20e commit 6f3843e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
11 changes: 6 additions & 5 deletions server/schema/article/article.resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,18 @@ module.exports = {
);
const _restritedPermission = UserPermission.exists(session, authToken, decodedToken, 'article.read.restricted');

if (_restritedPermission && _unpublishedPermission) return _articles;
if (_restritedPermission && _unpublishedPermission) {
return _articles;
}

const publicArticles = _articles.filter(
({ publishStatus, isInstituteRestricted }) =>
(_restritedPermission || !isInstituteRestricted) && (_unpublishedPermission || publishStatus)
);

return publicArticles.length === _articles.length ?
publicArticles
:
[...publicArticles, APIError('FORBIDDEN', null, { reason: 'One or more article(s) were not found.' })]
return publicArticles.length === _articles.length
? publicArticles
: [...publicArticles, APIError('FORBIDDEN', null, { reason: 'One or more article(s) were not found.' })];
} catch (error) {
throw APIError(null, error);
}
Expand Down
14 changes: 8 additions & 6 deletions server/schema/user/user.datasources.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,14 @@ const create = async (uid, fullName, email, interestedTopics, session, authToken
try {
mdbSession.startTransaction();
const _user = await UserModel.create(
[{
fullName,
email,
interestedTopics,
createdBy: UserSession.valid(session, authToken) ? mid : null,
}],
[
{
fullName,
email,
interestedTopics,
createdBy: UserSession.valid(session, authToken) ? mid : null,
},
],
{ session: mdbSession }
);

Expand Down
4 changes: 4 additions & 0 deletions server/schema/user/user.query.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ module.exports = new GraphQLObjectType({
description: 'Retrieves a list of all users.',
type: new GraphQLList(UserType),
args: {
accountType: {
description: 'Return the list of users by account type',
type: GraphQLInt,
},
limit: {
description: 'The number of results to return',
type: GraphQLInt,
Expand Down
9 changes: 3 additions & 6 deletions server/schema/user/user.resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,17 +452,14 @@ module.exports = {
/** Admin APIs */
listAllUsers: async (
_parent,
{ limit = DEF_LIMIT, offset = DEF_OFFSET },
{ accountType, limit = DEF_LIMIT, offset = DEF_OFFSET },
{ session, authToken, decodedToken, API: { User } },
{ fieldNodes }
) => {
try {
const _fields = getFieldNodes(fieldNodes);

if (
!UserPermission.exists(session, authToken, decodedToken, 'user.list.all') ||
!UserPermission.exists(session, authToken, decodedToken, 'user.read.public')
) {
if (!UserPermission.exists(session, authToken, decodedToken, 'user.list.all')) {
throw APIError('FORBIDDEN', null, {
reason: 'The user does not have the required permissions to perform this action.',
});
Expand All @@ -477,7 +474,7 @@ module.exports = {
});
}

const _users = await User.find({}, limit, offset);
const _users = await User.find(accountType ? { accountType } : {}, limit, offset);

for (const _user of _users) {
User.findByID.prime(_user.id, _user);
Expand Down

0 comments on commit 6f3843e

Please sign in to comment.