From 6f3843e16afe83e15e4759df170470e5224bc536 Mon Sep 17 00:00:00 2001 From: Aashish <83752052+120EE0692@users.noreply.github.com> Date: Wed, 12 Oct 2022 20:07:51 +0530 Subject: [PATCH] feat: get list of user by account type (#147) * 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 --- server/schema/article/article.resolver.js | 11 ++++++----- server/schema/user/user.datasources.js | 14 ++++++++------ server/schema/user/user.query.js | 4 ++++ server/schema/user/user.resolver.js | 9 +++------ 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/server/schema/article/article.resolver.js b/server/schema/article/article.resolver.js index 8ec572c4..41fe97b9 100644 --- a/server/schema/article/article.resolver.js +++ b/server/schema/article/article.resolver.js @@ -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); } diff --git a/server/schema/user/user.datasources.js b/server/schema/user/user.datasources.js index 14c2bc15..cac85a20 100644 --- a/server/schema/user/user.datasources.js +++ b/server/schema/user/user.datasources.js @@ -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 } ); diff --git a/server/schema/user/user.query.js b/server/schema/user/user.query.js index 560d33af..a45b1e40 100644 --- a/server/schema/user/user.query.js +++ b/server/schema/user/user.query.js @@ -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, diff --git a/server/schema/user/user.resolver.js b/server/schema/user/user.resolver.js index 0c9b3f1f..59e5102d 100644 --- a/server/schema/user/user.resolver.js +++ b/server/schema/user/user.resolver.js @@ -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.', }); @@ -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);