forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-domain.js
More file actions
290 lines (263 loc) · 8.52 KB
/
create-domain.js
File metadata and controls
290 lines (263 loc) · 8.52 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
import { GraphQLNonNull, GraphQLList, GraphQLID } from 'graphql'
import { mutationWithClientMutationId, fromGlobalId } from 'graphql-relay'
import { t } from '@lingui/macro'
import { createDomainUnion } from '../unions'
import { Domain, Selectors } from '../../scalars'
export const createDomain = new mutationWithClientMutationId({
name: 'CreateDomain',
description: 'Mutation used to create a new domain for an organization.',
inputFields: () => ({
orgId: {
type: GraphQLNonNull(GraphQLID),
description:
'The global id of the organization you wish to assign this domain to.',
},
domain: {
type: GraphQLNonNull(Domain),
description: 'Url that you would like to be added to the database.',
},
selectors: {
type: new GraphQLList(Selectors),
description: 'DKIM selector strings corresponding to this domain.',
},
}),
outputFields: () => ({
result: {
type: createDomainUnion,
description:
'`CreateDomainUnion` returning either a `Domain`, or `CreateDomainError` object.',
resolve: (payload) => payload,
},
}),
mutateAndGetPayload: async (
args,
{
i18n,
request,
query,
collections,
transaction,
userKey,
auth: { checkPermission, userRequired, verifiedRequired },
loaders: { loadDomainByDomain, loadOrgByKey },
validators: { cleanseInput },
},
) => {
// Get User
const user = await userRequired()
verifiedRequired({ user })
// Cleanse input
const { type: _orgType, id: orgId } = fromGlobalId(cleanseInput(args.orgId))
const domain = cleanseInput(args.domain)
let selectors
if (typeof args.selectors !== 'undefined') {
selectors = args.selectors.map((selector) => cleanseInput(selector))
} else {
selectors = []
}
// Check to see if org exists
const org = await loadOrgByKey.load(orgId)
if (typeof org === 'undefined') {
console.warn(
`User: ${userKey} attempted to create a domain to an organization: ${orgId} that does not exist.`,
)
return {
_type: 'error',
code: 400,
description: i18n._(
t`Unable to create domain in unknown organization.`,
),
}
}
// Check to see if user belongs to org
const permission = await checkPermission({ orgId: org._id })
if (
permission !== 'user' &&
permission !== 'admin' &&
permission !== 'super_admin'
) {
console.warn(
`User: ${userKey} attempted to create a domain in: ${org.slug}, however they do not have permission to do so.`,
)
return {
_type: 'error',
code: 400,
description: i18n._(
t`Permission Denied: Please contact organization user for help with creating domain.`,
),
}
}
const insertDomain = {
domain: domain.toLowerCase(),
lastRan: null,
selectors: selectors,
status: {
dkim: null,
dmarc: null,
https: null,
spf: null,
ssl: null,
},
}
// Check to see if domain already belongs to same org
let checkDomainCursor
try {
checkDomainCursor = await query`
WITH claims, domains, organizations
LET domainIds = (FOR domain IN domains FILTER domain.domain == ${insertDomain.domain} RETURN { id: domain._id })
FOR domainId IN domainIds
LET domainEdges = (FOR v, e IN 1..1 ANY domainId.id claims RETURN { _from: e._from })
FOR domainEdge IN domainEdges
LET org = DOCUMENT(domainEdge._from)
FILTER org._key == ${org._key}
RETURN MERGE({ _id: org._id, _key: org._key, _rev: org._rev }, TRANSLATE(${request.language}, org.orgDetails))
`
} catch (err) {
console.error(
`Database error occurred while running check to see if domain already exists in an org: ${err}`,
)
throw new Error(i18n._(t`Unable to create domain. Please try again.`))
}
let checkOrgDomain
try {
checkOrgDomain = await checkDomainCursor.next()
} catch (err) {
console.error(
`Cursor error occurred while running check to see if domain already exists in an org: ${err}`,
)
throw new Error(i18n._(t`Unable to create domain. Please try again.`))
}
if (typeof checkOrgDomain !== 'undefined') {
console.warn(
`User: ${userKey} attempted to create a domain for: ${org.slug}, however that org already has that domain claimed.`,
)
return {
_type: 'error',
code: 400,
description: i18n._(
t`Unable to create domain, organization has already claimed it.`,
),
}
}
// Check to see if domain already exists in db
const checkDomain = await loadDomainByDomain.load(insertDomain.domain)
// Generate list of collections names
const collectionStrings = []
for (const property in collections) {
collectionStrings.push(property.toString())
}
// Setup Transaction
const trx = await transaction(collectionStrings)
let insertedDomainCursor
if (typeof checkDomain === 'undefined') {
try {
insertedDomainCursor = await trx.step(
() =>
query`
WITH domains
INSERT ${insertDomain} INTO domains
RETURN MERGE(
{
id: NEW._key,
_type: "domain"
},
NEW
)
`,
)
} catch (err) {
console.error(
`Transaction step error occurred for user: ${userKey} when inserting new domain: ${err}`,
)
throw new Error(i18n._(t`Unable to create domain. Please try again.`))
}
let insertedDomain
try {
insertedDomain = await insertedDomainCursor.next()
} catch (err) {
console.error(
`Cursor error occurred for user: ${userKey} after inserting new domain and gathering its domain info: ${err}`,
)
throw new Error(i18n._(t`Unable to create domain. Please try again.`))
}
try {
await trx.step(
() =>
query`
WITH claims, domains, organizations
INSERT {
_from: ${org._id},
_to: ${insertedDomain._id}
} INTO claims
`,
)
} catch (err) {
console.error(
`Transaction step error occurred for user: ${userKey} when inserting new domain edge: ${err}`,
)
throw new Error(i18n._(t`Unable to create domain. Please try again.`))
}
} else {
const { selectors: selectorList, status, lastRan } = checkDomain
selectors.forEach((selector) => {
if (!checkDomain.selectors.includes(selector)) {
selectorList.push(selector)
}
})
insertDomain.selectors = selectorList
insertDomain.status = status
insertDomain.lastRan = lastRan
try {
await trx.step(
() =>
query`
WITH claims, domains, organizations
UPSERT { _key: ${checkDomain._key} }
INSERT ${insertDomain}
UPDATE ${insertDomain}
IN domains
`,
)
} catch (err) {
console.error(
`Transaction step error occurred for user: ${userKey} when inserting domain selectors: ${err}`,
)
throw new Error(i18n._(t`Unable to create domain. Please try again.`))
}
try {
await trx.step(
() =>
query`
WITH claims, domains, organizations
INSERT {
_from: ${org._id},
_to: ${checkDomain._id}
} INTO claims
`,
)
} catch (err) {
console.error(
`Transaction step error occurred for user: ${userKey} when inserting domain edge: ${err}`,
)
throw new Error(i18n._(t`Unable to create domain. Please try again.`))
}
}
try {
await trx.commit()
} catch (err) {
console.error(
`Transaction commit error occurred while user: ${userKey} was creating domain: ${err}`,
)
throw new Error(i18n._(t`Unable to create domain. Please try again.`))
}
// Clear dataloader incase anything was updated or inserted into domain
await loadDomainByDomain.clear(insertDomain.domain)
const returnDomain = await loadDomainByDomain.load(insertDomain.domain)
console.info(
`User: ${userKey} successfully created ${returnDomain.domain} in org: ${org.slug}.`,
)
return {
...returnDomain,
}
},
})