forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind-organization-by-slug.js
More file actions
64 lines (55 loc) · 1.67 KB
/
Copy pathfind-organization-by-slug.js
File metadata and controls
64 lines (55 loc) · 1.67 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { GraphQLNonNull } from 'graphql'
import { t } from '@lingui/macro'
import { Slug } from '../../scalars'
const { organizationType } = require('../objects')
export const findOrganizationBySlug = {
type: organizationType,
description:
'Select all information on a selected organization that a user has access to.',
args: {
orgSlug: {
type: GraphQLNonNull(Slug),
description:
'The slugified organization name you want to retrieve data for.',
},
},
resolve: async (
_,
args,
{
i18n,
auth: { checkPermission, userRequired, verifiedRequired },
loaders: { loadOrgBySlug },
validators: { cleanseInput },
},
) => {
// Get User
const user = await userRequired()
verifiedRequired({ user })
// Cleanse input
const orgSlug = cleanseInput(args.orgSlug)
// Retrieve organization by slug
const org = await loadOrgBySlug.load(orgSlug)
if (typeof org === 'undefined') {
console.warn(`User ${user._key} could not retrieve organization.`)
throw new Error(
i18n._(t`No organization with the provided slug could be found.`),
)
}
// Check user permission for organization access
const permission = await checkPermission({ orgId: org._id })
if (!['super_admin', 'admin', 'user'].includes(permission)) {
console.warn(`User ${user._key} could not retrieve organization.`)
throw new Error(
i18n._(
t`Permission Denied: Could not retrieve specified organization.`,
),
)
}
console.info(
`User ${user._key} successfully retrieved organization ${org._key}.`,
)
org.id = org._key
return org
},
}