forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind-user-by-username.js
More file actions
44 lines (40 loc) · 1.16 KB
/
find-user-by-username.js
File metadata and controls
44 lines (40 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { GraphQLEmailAddress } from 'graphql-scalars'
import { t } from '@lingui/macro'
import { GraphQLNonNull } from 'graphql'
import { userSharedType } from '../objects'
export const findUserByUsername = {
type: userSharedType,
args: {
userName: {
type: GraphQLNonNull(GraphQLEmailAddress),
description: 'Email address of user you wish to gather data for.',
},
},
description: 'Query a specific user by user name.',
resolve: async (
_,
args,
{
i18n,
userKey,
auth: { userRequired, checkUserIsAdminForUser },
loaders: { loadUserByUserName },
validators: { cleanseInput },
},
) => {
// Cleanse input
const userName = cleanseInput(args.userName).toLowerCase()
// Get querying user
await userRequired()
const permission = await checkUserIsAdminForUser({ userName })
if (permission) {
// Retrieve user by userName
const user = await loadUserByUserName.load(userName)
user.id = user._key
return user
} else {
console.warn(`User ${userKey} is not permitted to query users.`)
throw new Error(i18n._(t`User could not be queried.`))
}
},
}