forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify-organization.js
More file actions
162 lines (145 loc) · 4.54 KB
/
verify-organization.js
File metadata and controls
162 lines (145 loc) · 4.54 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { t } from '@lingui/macro'
import { GraphQLNonNull, GraphQLID } from 'graphql'
import { mutationWithClientMutationId, fromGlobalId } from 'graphql-relay'
import { verifyOrganizationUnion } from '../unions'
export const verifyOrganization = new mutationWithClientMutationId({
name: 'VerifyOrganization',
description: 'Mutation allows the verification of an organization.',
inputFields: () => ({
orgId: {
type: GraphQLNonNull(GraphQLID),
description: 'The global id of the organization to be verified.',
},
}),
outputFields: () => ({
result: {
type: verifyOrganizationUnion,
description:
'`VerifyOrganizationUnion` returning either an `OrganizationResult`, or `OrganizationError` object.',
resolve: (payload) => payload,
},
}),
mutateAndGetPayload: async (
args,
{
i18n,
query,
collections,
transaction,
userKey,
auth: { checkPermission, userRequired, verifiedRequired },
loaders: { loadOrgByKey },
validators: { cleanseInput },
},
) => {
// Ensure that user is required
const user = await userRequired()
verifiedRequired({ user })
const { id: orgKey } = fromGlobalId(cleanseInput(args.orgId))
// Check to see if org exists
const currentOrg = await loadOrgByKey.load(orgKey)
if (typeof currentOrg === 'undefined') {
console.warn(
`User: ${userKey} attempted to verify organization: ${orgKey}, however no organizations is associated with that id.`,
)
return {
_type: 'error',
code: 400,
description: i18n._(t`Unable to verify unknown organization.`),
}
}
// Check to see if use has permission
const permission = await checkPermission({ orgId: currentOrg._id })
if (permission !== 'super_admin') {
console.warn(
`User: ${userKey} attempted to verify organization: ${orgKey}, however they do not have the correct permission level. Permission: ${permission}`,
)
return {
_type: 'error',
code: 403,
description: i18n._(
t`Permission Denied: Please contact super admin for help with verifying this organization.`,
),
}
}
// Check to see if org is already verified
if (currentOrg.verified) {
console.warn(
`User: ${userKey} attempted to verify organization: ${orgKey}, however the organization has already been verified.`,
)
return {
_type: 'error',
code: 400,
description: i18n._(t`Organization has already been verified.`),
}
}
// Set org to verified
currentOrg.verified = true
// Generate list of collections names
const collectionStrings = []
for (const property in collections) {
collectionStrings.push(property.toString())
}
// Setup Trans action
const trx = await transaction(collectionStrings)
// Upsert new org details
try {
await trx.step(
() =>
query`
WITH organizations
UPSERT { _key: ${orgKey} }
INSERT ${currentOrg}
UPDATE ${currentOrg}
IN organizations
`,
)
} catch (err) {
console.error(
`Transaction error occurred while upserting verified org: ${orgKey}, err: ${err}`,
)
throw new Error(
i18n._(t`Unable to verify organization. Please try again.`),
)
}
// Set all affiliation owner fields to false
try {
await trx.step(
() => query`
WITH affiliations, organizations, users
FOR v, e IN 1..1 OUTBOUND ${currentOrg._id} affiliations
UPSERT { _key: e._key }
INSERT { owner: false }
UPDATE { owner: false }
IN affiliations
RETURN e
`,
)
} catch (err) {
console.error(
`Trx step error occurred when clearing owners for org: ${orgKey}: ${err}`,
)
throw new Error(
i18n._(t`Unable to verify organization. Please try again.`),
)
}
try {
await trx.commit()
} catch (err) {
console.error(
`Transaction error occurred while committing newly verified org: ${orgKey}, err: ${err}`,
)
throw new Error(
i18n._(t`Unable to verify organization. Please try again.`),
)
}
console.info(`User: ${userKey}, successfully verified org: ${orgKey}.`)
return {
_type: 'result',
status: i18n._(
t`Successfully verified organization: ${currentOrg.slug}.`,
),
organization: currentOrg,
}
},
})