forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-domain.js
More file actions
199 lines (178 loc) · 5.84 KB
/
update-domain.js
File metadata and controls
199 lines (178 loc) · 5.84 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
import { GraphQLID, GraphQLNonNull, GraphQLList } from 'graphql'
import { mutationWithClientMutationId, fromGlobalId } from 'graphql-relay'
import { t } from '@lingui/macro'
import { updateDomainUnion } from '../unions'
import { Domain, Selectors } from '../../scalars'
export const updateDomain = new mutationWithClientMutationId({
name: 'UpdateDomain',
description:
'Mutation allows the modification of domains if domain is updated through out its life-cycle',
inputFields: () => ({
domainId: {
type: GraphQLNonNull(GraphQLID),
description: 'The global id of the domain that is being updated.',
},
orgId: {
type: GraphQLNonNull(GraphQLID),
description:
'The global ID of the organization used for permission checks.',
},
domain: {
type: Domain,
description: 'The new url of the of the old domain.',
},
selectors: {
type: new GraphQLList(Selectors),
description:
'The updated DKIM selector strings corresponding to this domain.',
},
}),
outputFields: () => ({
result: {
type: updateDomainUnion,
description:
'`UpdateDomainUnion` returning either a `Domain`, or `DomainError` object.',
resolve: (payload) => payload,
},
}),
mutateAndGetPayload: async (
args,
{
i18n,
query,
collections,
transaction,
userKey,
auth: { checkPermission, userRequired, verifiedRequired },
validators: { cleanseInput },
loaders: { loadDomainByKey, loadOrgByKey },
},
) => {
// Get User
const user = await userRequired()
verifiedRequired({ user })
const { id: domainId } = fromGlobalId(cleanseInput(args.domainId))
const { id: orgId } = fromGlobalId(cleanseInput(args.orgId))
const updatedDomain = cleanseInput(args.domain)
let selectors
if (typeof args.selectors !== 'undefined') {
selectors = args.selectors.map((selector) => cleanseInput(selector))
} else {
selectors = null
}
// Check to see if domain exists
const domain = await loadDomainByKey.load(domainId)
if (typeof domain === 'undefined') {
console.warn(
`User: ${userKey} attempted to update domain: ${domainId}, however there is no domain associated with that id.`,
)
return {
_type: 'error',
code: 400,
description: i18n._(t`Unable to update unknown domain.`),
}
}
// Check to see if org exists
const org = await loadOrgByKey.load(orgId)
if (typeof org === 'undefined') {
console.warn(
`User: ${userKey} attempted to update domain: ${domainId} for org: ${orgId}, however there is no org associated with that id.`,
)
return {
_type: 'error',
code: 400,
description: i18n._(t`Unable to update domain in an unknown org.`),
}
}
// Check permission
const permission = await checkPermission({ orgId: org._id })
if (
permission !== 'user' &&
permission !== 'admin' &&
permission !== 'super_admin'
) {
console.warn(
`User: ${userKey} attempted to update domain: ${domainId} for org: ${orgId}, however they do not have permission in that org.`,
)
return {
_type: 'error',
code: 403,
description: i18n._(
t`Permission Denied: Please contact organization user for help with updating this domain.`,
),
}
}
// Check to see if org has a claim to this domain
let countCursor
try {
countCursor = await query`
WITH claims, domains, organizations
FOR v, e IN 1..1 ANY ${domain._id} claims
FILTER e._from == ${org._id}
RETURN e
`
} catch (err) {
console.error(
`Database error occurred while user: ${userKey} attempted to update domain: ${domainId}, error: ${err}`,
)
throw new Error(i18n._(t`Unable to update domain. Please try again.`))
}
if (countCursor.count < 1) {
console.warn(
`User: ${userKey} attempted to update domain: ${domainId} for org: ${orgId}, however that org has no claims to that domain.`,
)
return {
_type: 'error',
code: 400,
description: i18n._(
t`Unable to update domain that does not belong to the given organization.`,
),
}
}
// Generate list of collections names
const collectionStrings = []
for (const property in collections) {
collectionStrings.push(property.toString())
}
// Setup Transaction
const trx = await transaction(collectionStrings)
// Update domain
const domainToInsert = {
domain: updatedDomain.toLowerCase() || domain.domain.toLowerCase(),
lastRan: domain.lastRan,
selectors: selectors || domain.selectors,
}
try {
await trx.step(
async () =>
await query`
WITH domains
UPSERT { _key: ${domain._key} }
INSERT ${domainToInsert}
UPDATE ${domainToInsert}
IN domains
`,
)
} catch (err) {
console.error(
`Transaction step error occurred when user: ${userKey} attempted to update domain: ${domainId}, error: ${err}`,
)
throw new Error(i18n._(t`Unable to update domain. Please try again.`))
}
// Commit transaction
try {
await trx.commit()
} catch (err) {
console.error(
`Transaction commit error occurred when user: ${userKey} attempted to update domain: ${domainId}, error: ${err}`,
)
throw new Error(i18n._(t`Unable to update domain. Please try again.`))
}
// Clear dataloader and load updated domain
await loadDomainByKey.clear(domain._key)
const returnDomain = await loadDomainByKey.load(domain._key)
console.info(`User: ${userKey} successfully updated domain: ${domainId}.`)
returnDomain.id = returnDomain._key
return returnDomain
},
})