forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleave-organization.js
More file actions
358 lines (337 loc) · 13.9 KB
/
Copy pathleave-organization.js
File metadata and controls
358 lines (337 loc) · 13.9 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import { t } from '@lingui/macro'
import { GraphQLID, GraphQLNonNull } from 'graphql'
import { fromGlobalId, mutationWithClientMutationId } from 'graphql-relay'
import { leaveOrganizationUnion } from '../unions'
export const leaveOrganization = new mutationWithClientMutationId({
name: 'LeaveOrganization',
description: 'This mutation allows users to leave a given organization.',
inputFields: () => ({
orgId: {
type: GraphQLNonNull(GraphQLID),
description: 'Id of the organization the user is looking to leave.',
},
}),
outputFields: () => ({
result: {
type: leaveOrganizationUnion,
description:
'`LeaveOrganizationUnion` resolving to either a `LeaveOrganizationResult` or `AffiliationError`.',
resolve: (payload) => payload,
},
}),
mutateAndGetPayload: async (
args,
{
i18n,
query,
collections,
transaction,
auth: { checkOrgOwner, userRequired, verifiedRequired },
loaders: { loadOrgByKey },
validators: { cleanseInput },
},
) => {
const { id: orgKey } = fromGlobalId(cleanseInput(args.orgId))
const user = await userRequired()
verifiedRequired({ user })
const org = await loadOrgByKey.load(orgKey)
if (typeof org === 'undefined') {
console.warn(
`User ${user._key} attempted to leave undefined organization: ${orgKey}`,
)
return {
_type: 'error',
code: 400,
description: i18n._(t`Unable to leave undefined organization.`),
}
}
// check to see if org owner
const owner = await checkOrgOwner({ orgId: org._id })
// check to see if any other orgs are using this domain
let countCursor
try {
countCursor = await query`
WITH claims, domains, organizations
LET domainIds = (
FOR v, e IN 1..1 ANY ${org._id} claims
RETURN e._to
)
FOR domain IN domains
FILTER domain._id IN domainIds
LET count = LENGTH(
FOR v, e
IN 1..1 ANY domain._id claims
RETURN 1
)
RETURN { count: count }
`
} catch (err) {
console.error(
`Database error occurred while while gathering domainInfo org: ${org._key}, when user: ${user._key} attempted to leave. error: ${err}`,
)
throw new Error(i18n._(t`Unable leave organization. Please try again.`))
}
let domainInfo
try {
domainInfo = await countCursor.next()
} catch (err) {
console.error(
`Cursor error occurred while while gathering domainInfo org: ${org._key}, when user: ${user._key} attempted to leave. error: ${err}`,
)
throw new Error(i18n._(t`Unable leave organization. Please try again.`))
}
// check to see if org has any dmarc summaries
let dmarcSummaryCheckCursor
try {
dmarcSummaryCheckCursor = await query`
WITH domains, ownership, dmarcSummaries, organizations
LET domainIds = (
FOR v, e IN 1..1 ANY ${org._id} ownership
RETURN e._to
)
FOR domain IN domains
FILTER domain._id IN domainIds
RETURN domain
`
} catch (err) {
console.error(
`Database error occurred while checking for dmarc summaries for org: ${org._key}, when user: ${user._key} attempted to leave. error: ${err}`,
)
throw new Error(i18n._(t`Unable leave organization. Please try again.`))
}
// Generate list of collections names
const collectionStrings = []
for (const property in collections) {
collectionStrings.push(property.toString())
}
// Setup Trans action
const trx = await transaction(collectionStrings)
if (owner) {
if (dmarcSummaryCheckCursor.count >= 1) {
try {
await trx.step(
() => query`
WITH ownership, organizations, domains, dmarcSummaries, domainsToDmarcSummaries
LET domainEdges = (
FOR v, e IN 1..1 OUTBOUND ${org._id} ownership
RETURN { edgeKey: e._key, domainId: e._to }
)
FOR domainEdge IN domainEdges
LET dmarcSummaryEdges = (
FOR v, e IN 1..1 OUTBOUND domainEdge.domainId domainsToDmarcSummaries
RETURN { edgeKey: e._key, dmarcSummaryId: e._to }
)
LET removeDmarcSummaryEdges = (
FOR dmarcSummaryEdge IN dmarcSummaryEdges
REMOVE dmarcSummaryEdge.edgeKey IN domainsToDmarcSummaries
)
LET removeDmarcSummary = (
FOR dmarcSummaryEdge IN dmarcSummaryEdges
LET key = PARSE_IDENTIFIER(dmarcSummaryEdge.dmarcSummaryId).key
REMOVE key IN dmarcSummaries
)
RETURN true
`,
)
} catch (err) {
console.error(
`Trx step error occurred while attempting to remove dmarc summaries for org: ${org._key}, when user: ${user._key} attempted to leave. error: ${err}`,
)
throw new Error(
i18n._(t`Unable leave organization. Please try again.`),
)
}
try {
await trx.step(
() => query`
WITH ownership, organizations, domains
LET domainEdges = (
FOR v, e IN 1..1 OUTBOUND ${org._id} ownership
REMOVE e._key IN ownership
)
RETURN true
`,
)
} catch (err) {
console.error(
`Trx step error occurred while attempting to remove ownership for org: ${org._key}, when user: ${user._key} attempted to leave. error: ${err}`,
)
throw new Error(
i18n._(t`Unable leave organization. Please try again.`),
)
}
}
if (domainInfo.count <= 1) {
try {
await Promise.all([
trx.step(
() =>
query`
WITH claims, dkim, domains, domainsDKIM, organizations, dkimToDkimResults, dkimResults
LET domainEdges = (
FOR v, e IN 1..1 OUTBOUND ${org._id} claims
RETURN { edgeKey: e._key, domainId: e._to }
)
FOR domainEdge in domainEdges
LET dkimEdges = (
FOR v, e IN 1..1 OUTBOUND domainEdge.domainId domainsDKIM
RETURN { edgeKey: e._key, dkimId: e._to }
)
FOR dkimEdge IN dkimEdges
LET dkimResultEdges = (
FOR v, e IN 1..1 OUTBOUND dkimEdge.dkimId dkimToDkimResults
RETURN { edgeKey: e._key, dkimResultId: e._to }
)
LET removeDkimResultEdges = (FOR dkimResultEdge IN dkimResultEdges REMOVE dkimResultEdge.edgeKey IN dkimToDkimResults)
LET removeDkimResult = (FOR dkimResultEdge IN dkimResultEdges LET key = PARSE_IDENTIFIER(dkimResultEdge.dkimResultId).key REMOVE key IN dkimResults)
RETURN true
`,
),
trx.step(
() =>
query`
WITH claims, dkim, domains, domainsDKIM, organizations
LET domainEdges = (FOR v, e IN 1..1 OUTBOUND ${org._id} claims RETURN { edgeKey: e._key, domainId: e._to })
FOR domainEdge in domainEdges
LET dkimEdges = (FOR v, e IN 1..1 OUTBOUND domainEdge.domainId domainsDKIM RETURN { edgeKey: e._key, dkimId: e._to })
LET removeDkimEdges = (FOR dkimEdge IN dkimEdges REMOVE dkimEdge.edgeKey IN domainsDKIM)
LET removeDkim = (FOR dkimEdge IN dkimEdges LET key = PARSE_IDENTIFIER(dkimEdge.dkimId).key REMOVE key IN dkim)
RETURN true
`,
),
trx.step(
() =>
query`
WITH claims, dmarc, domains, domainsDMARC, organizations
LET domainEdges = (FOR v, e IN 1..1 OUTBOUND ${org._id} claims RETURN { edgeKey: e._key, domainId: e._to })
FOR domainEdge in domainEdges
LET dmarcEdges = (FOR v, e IN 1..1 OUTBOUND domainEdge.domainId domainsDMARC RETURN { edgeKey: e._key, dmarcId: e._to })
LET removeDmarcEdges = (FOR dmarcEdge IN dmarcEdges REMOVE dmarcEdge.edgeKey IN domainsDMARC)
LET removeDmarc = (FOR dmarcEdge IN dmarcEdges LET key = PARSE_IDENTIFIER(dmarcEdge.dmarcId).key REMOVE key IN dmarc)
RETURN true
`,
),
trx.step(
() =>
query`
WITH claims, domains, domainsSPF, organizations, spf
LET domainEdges = (FOR v, e IN 1..1 OUTBOUND ${org._id} claims RETURN { edgeKey: e._key, domainId: e._to })
FOR domainEdge in domainEdges
LET spfEdges = (FOR v, e IN 1..1 OUTBOUND domainEdge.domainId domainsSPF RETURN { edgeKey: e._key, spfId: e._to })
LET removeSpfEdges = (FOR spfEdge IN spfEdges REMOVE spfEdge.edgeKey IN domainsSPF)
LET removeSpf = (FOR spfEdge IN spfEdges LET key = PARSE_IDENTIFIER(spfEdge.spfId).key REMOVE key IN spf)
RETURN true
`,
),
trx.step(
() =>
query`
WITH claims, domains, domainsHTTPS, https, organizations
LET domainEdges = (FOR v, e IN 1..1 OUTBOUND ${org._id} claims RETURN { edgeKey: e._key, domainId: e._to })
FOR domainEdge in domainEdges
LET httpsEdges = (FOR v, e IN 1..1 OUTBOUND domainEdge.domainId domainsHTTPS RETURN { edgeKey: e._key, httpsId: e._to })
LET removeHttpsEdges = (FOR httpsEdge IN httpsEdges REMOVE httpsEdge.edgeKey IN domainsHTTPS)
LET removeHttps = (FOR httpsEdge IN httpsEdges LET key = PARSE_IDENTIFIER(httpsEdge.httpsId).key REMOVE key IN https)
RETURN true
`,
),
trx.step(
() =>
query`
WITH claims, domains, domainsSSL, organizations, ssl
LET domainEdges = (FOR v, e IN 1..1 OUTBOUND ${org._id} claims RETURN { edgeKey: e._key, domainId: e._to })
FOR domainEdge in domainEdges
LET sslEdges = (FOR v, e IN 1..1 OUTBOUND domainEdge.domainId domainsSSL RETURN { edgeKey: e._key, sslId: e._to})
LET removeSslEdges = (FOR sslEdge IN sslEdges REMOVE sslEdge.edgeKey IN domainsSSL)
LET removeSsl = (FOR sslEdge IN sslEdges LET key = PARSE_IDENTIFIER(sslEdge.sslId).key REMOVE key IN ssl)
RETURN true
`,
),
])
} catch (err) {
console.error(
`Trx step error occurred while attempting to remove scan results for org: ${org._key}, when user: ${user._key} attempted to leave. error: ${err}`,
)
throw new Error(
i18n._(t`Unable leave organization. Please try again.`),
)
}
try {
await trx.step(
() =>
query`
WITH claims, domains, organizations
LET domainEdges = (FOR v, e IN 1..1 OUTBOUND ${org._id} claims RETURN { edgeKey: e._key, domainId: e._to })
LET removeDomainEdges = (FOR domainEdge in domainEdges REMOVE domainEdge.edgeKey IN claims)
LET removeDomain = (FOR domainEdge in domainEdges LET key = PARSE_IDENTIFIER(domainEdge.domainId).key REMOVE key IN domains)
RETURN true
`,
)
} catch (err) {
console.error(
`Trx step error occurred while attempting to remove domains for org: ${org._key}, when user: ${user._key} attempted to leave. error: ${err}`,
)
throw new Error(
i18n._(t`Unable leave organization. Please try again.`),
)
}
}
try {
await Promise.all([
trx.step(
() =>
query`
WITH affiliations, organizations, users
LET userEdges = (FOR v, e IN 1..1 OUTBOUND ${org._id} affiliations RETURN { edgeKey: e._key, userKey: e._to })
LET removeUserEdges = (FOR userEdge IN userEdges REMOVE userEdge.edgeKey IN affiliations)
RETURN true
`,
),
trx.step(
() =>
query`
WITH organizations
REMOVE ${org._key} IN organizations
`,
),
])
} catch (err) {
console.error(
`Trx step error occurred while attempting to remove affiliations, and the org for org: ${org._key}, when user: ${user._key} attempted to leave. error: ${err}`,
)
throw new Error(i18n._(t`Unable leave organization. Please try again.`))
}
}
if (!owner) {
try {
await trx.step(
() =>
query`
WITH affiliations, organizations, users
FOR v, e IN 1..1 OUTBOUND ${org._id} affiliations
FILTER e._to == ${user._id}
REMOVE { _key: e._key } IN affiliations
`,
)
} catch (err) {
console.error(
`Trx step error occurred when removing user: ${user._key} affiliation with org: ${org._key} err: ${err}`,
)
throw new Error(i18n._(t`Unable leave organization. Please try again.`))
}
}
try {
await trx.commit()
} catch (err) {
console.error(
`Trx commit error occurred when user: ${user._key} attempted to leave org: ${org._key}. error: ${err}`,
)
throw new Error(i18n._(t`Unable leave organization. Please try again.`))
}
console.info(`User: ${user._key} successfully left org: ${org.slug}.`)
return {
_type: 'regular',
status: i18n._(t`Successfully left organization: ${org.slug}`),
}
},
})