forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-user-from-org.js
More file actions
174 lines (159 loc) · 5.56 KB
/
Copy pathremove-user-from-org.js
File metadata and controls
174 lines (159 loc) · 5.56 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
163
164
165
166
167
168
169
170
171
172
173
174
import { GraphQLNonNull, GraphQLID, GraphQLString } from 'graphql'
import { mutationWithClientMutationId, fromGlobalId } from 'graphql-relay'
import { t } from '@lingui/macro'
export const removeUserFromOrg = new mutationWithClientMutationId({
name: 'RemoveUserFromOrg',
description: '',
inputFields: () => ({
userId: {
type: GraphQLNonNull(GraphQLID),
description: '',
},
orgId: {
type: GraphQLNonNull(GraphQLID),
description: '',
},
}),
outputFields: () => ({
status: {
type: GraphQLString,
description: '',
resolve: ({ status }) => status,
},
}),
mutateAndGetPayload: async (
args,
{
i18n,
query,
collections,
transaction,
userKey,
auth: { checkPermission, userRequired },
loaders: { orgLoaderByKey, userLoaderByKey },
validators: { cleanseInput },
},
) => {
// Cleanse Input
const { id: requestedUserKey } = fromGlobalId(cleanseInput(args.userId))
const { id: requestedOrgKey } = fromGlobalId(cleanseInput(args.orgId))
// Get requesting user
await userRequired()
// Get requested org
const requestedOrg = await orgLoaderByKey.load(requestedOrgKey)
if (typeof requestedOrg === 'undefined') {
console.warn(
`User: ${userKey} attempted to remove user: ${requestedUserKey} from org: ${requestedOrgKey}, however no org with that id could be found.`,
)
throw new Error(
i18n._(t`Unable to remove user from organization. Please try again.`),
)
}
// Check requesting users permission
const permission = await checkPermission({ orgId: requestedOrg._id })
if (permission === 'user' || typeof permission === 'undefined') {
console.warn(
`User: ${userKey} attempted to remove user: ${requestedUserKey} from org: ${requestedOrg._key}, however they do not have the permission to remove users.`,
)
throw new Error(
i18n._(t`Unable to remove user from organization. Please try again.`),
)
}
// Get requested user
const requestedUser = await userLoaderByKey.load(requestedUserKey)
if (typeof requestedUser === 'undefined') {
console.warn(
`User: ${userKey} attempted to remove user: ${requestedUserKey} from org: ${requestedOrg._key}, however no user with that id could be found.`,
)
throw new Error(
i18n._(t`Unable to remove user from organization. Please try again.`),
)
}
// Get requested users current permission level
let affiliationCursor
try {
affiliationCursor = await query`
FOR v, e IN 1..1 ANY ${requestedUser._id} affiliations
FILTER e._from == ${requestedOrg._id}
RETURN { _key: e._key, permission: e.permission }
`
} catch (err) {
console.error(
`Database error occurred when user: ${userKey} attempted to check the current permission of user: ${requestedUser._key} to see if they could be removed.`,
)
throw new Error(
i18n._(t`Unable to remove user from organization. Please try again.`),
)
}
if (affiliationCursor.count < 1) {
console.warn(
`User: ${userKey} attempted to remove user: ${requestedUser._key}, but they do not have any affiliations to org: ${requestedOrg._key}.`,
)
throw new Error(
i18n._(t`Unable to remove user from organization. Please try again.`),
)
}
const affiliation = await affiliationCursor.next()
let canRemove
if (
permission === 'super_admin' &&
(affiliation.permission === 'admin' || affiliation.permission === 'user')
) {
canRemove = true
} else if (permission === 'admin' && affiliation.permission === 'user') {
canRemove = true
} else {
canRemove = false
}
if (canRemove) {
// Generate list of collections names
const collectionStrings = []
for (const property in collections) {
collectionStrings.push(property.toString())
}
// Setup Transaction
const trx = await transaction(collectionStrings)
try {
await trx.run(async () => {
await query`
FOR aff IN affiliations
FILTER aff._from == ${requestedOrg._id}
FILTER aff._to == ${requestedUser._id}
REMOVE aff IN affiliations
RETURN true
`
})
} catch (err) {
console.error(
`Transaction run error occurred when user: ${userKey} attempted to remove user: ${requestedUser._key} from org: ${requestedOrg._key}, error: ${err}`,
)
throw new Error(
i18n._(t`Unable to remove user from organization. Please try again.`),
)
}
try {
await trx.commit()
} catch (err) {
console.error(
`Transaction commit error occurred when user: ${userKey} attempted to remove user: ${requestedUser._key} from org: ${requestedOrg._key}, error: ${err}`,
)
throw new Error(
i18n._(t`Unable to remove user from organization. Please try again.`),
)
}
console.info(
`User: ${userKey} successfully removed user: ${requestedUser._key} from org: ${requestedOrg._key}.`,
)
return {
status: i18n._(t`Successfully removed user from organization.`),
}
} else {
console.warn(
`User: ${userKey} attempted to remove user: ${requestedUser._key} from org: ${requestedOrg._key}, but they do not have the right permission.`,
)
throw new Error(
i18n._(t`Unable to remove user from organization. Please try again.`),
)
}
},
})