forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclose-account.js
More file actions
234 lines (213 loc) · 6.83 KB
/
Copy pathclose-account.js
File metadata and controls
234 lines (213 loc) · 6.83 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
import { t } from '@lingui/macro'
import { GraphQLID } from 'graphql'
import { fromGlobalId, mutationWithClientMutationId } from 'graphql-relay'
import { logActivity } from '../../audit-logs/mutations/log-activity'
import { closeAccountUnion } from '../unions'
export const closeAccountSelf = new mutationWithClientMutationId({
name: 'CloseAccountSelf',
description: `This mutation allows a user to close their account.`,
outputFields: () => ({
result: {
type: closeAccountUnion,
description: '`CloseAccountUnion` returning either a `CloseAccountResult`, or `CloseAccountError` object.',
resolve: (payload) => payload,
},
}),
mutateAndGetPayload: async (
args,
{ i18n, query, collections, transaction, request: { ip }, auth: { userRequired }, validators: { cleanseInput } },
) => {
let submittedUserId
if (args?.userId) {
submittedUserId = fromGlobalId(cleanseInput(args.userId)).id
}
const user = await userRequired()
const userId = user._id
const targetUserName = user.userName
// Setup Trans action
const trx = await transaction(collections)
try {
await trx.step(
() => query`
WITH affiliations, organizations, users
FOR v, e IN 1..1 INBOUND ${userId} affiliations
REMOVE { _key: e._key } IN affiliations
OPTIONS { waitForSync: true }
`,
)
} catch (err) {
console.error(
`Trx step error occurred when removing users remaining affiliations when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
try {
await trx.step(
() => query`
WITH users
REMOVE PARSE_IDENTIFIER(${userId}).key
IN users OPTIONS { waitForSync: true }
`,
)
} catch (err) {
console.error(
`Trx step error occurred when removing user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
try {
await trx.commit()
} catch (err) {
console.error(`Trx commit error occurred when user: ${user._key} attempted to close account: ${userId}: ${err}`)
await trx.abort()
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
console.info(`User: ${user._key} successfully closed user: ${userId} account.`)
await logActivity({
transaction,
collections,
query,
initiatedBy: {
id: user._key,
userName: user.userName,
role: submittedUserId ? 'SUPER_ADMIN' : '',
ipAddress: ip,
},
action: 'delete',
target: {
resource: targetUserName, // name of resource being acted upon
resourceType: 'user', // user, org, domain
},
})
return {
_type: 'regular',
status: i18n._(t`Successfully closed account.`),
}
},
})
export const closeAccountOther = new mutationWithClientMutationId({
name: 'CloseAccountOther',
description: `This mutation allows a super admin to close another user's account.`,
inputFields: () => ({
userId: {
type: GraphQLID,
description: 'The user id of a user you want to close the account of.',
},
}),
outputFields: () => ({
result: {
type: closeAccountUnion,
description: '`CloseAccountUnion` returning either a `CloseAccountResult`, or `CloseAccountError` object.',
resolve: (payload) => payload,
},
}),
mutateAndGetPayload: async (
args,
{
i18n,
query,
collections,
transaction,
request: { ip },
auth: { checkSuperAdmin, userRequired },
loaders: { loadUserByKey },
validators: { cleanseInput },
},
) => {
let submittedUserId
if (args?.userId) {
submittedUserId = fromGlobalId(cleanseInput(args.userId)).id
}
const user = await userRequired()
let userId = ''
let targetUserName = ''
const permission = await checkSuperAdmin()
if (!permission) {
console.warn(
`User: ${user._key} attempted to close user: ${submittedUserId} account, but requesting user is not a super admin.`,
)
return {
_type: 'error',
code: 400,
description: i18n._(t`Permission error: Unable to close other user's account.`),
}
}
const checkUser = await loadUserByKey.load(submittedUserId)
if (typeof checkUser === 'undefined') {
console.warn(
`User: ${user._key} attempted to close user: ${submittedUserId} account, but requested user is undefined.`,
)
return {
_type: 'error',
code: 400,
description: i18n._(t`Unable to close account of an undefined user.`),
}
}
userId = checkUser._id
targetUserName = checkUser.userName
// Setup Trans action
const trx = await transaction(collections)
try {
await trx.step(
() => query`
WITH affiliations, organizations, users
FOR v, e IN 1..1 INBOUND ${userId} affiliations
REMOVE { _key: e._key } IN affiliations
OPTIONS { waitForSync: true }
`,
)
} catch (err) {
console.error(
`Trx step error occurred when removing users remaining affiliations when user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
try {
await trx.step(
() => query`
WITH users
REMOVE PARSE_IDENTIFIER(${userId}).key
IN users OPTIONS { waitForSync: true }
`,
)
} catch (err) {
console.error(
`Trx step error occurred when removing user: ${user._key} attempted to close account: ${userId}: ${err}`,
)
await trx.abort()
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
try {
await trx.commit()
} catch (err) {
console.error(`Trx commit error occurred when user: ${user._key} attempted to close account: ${userId}: ${err}`)
await trx.abort()
throw new Error(i18n._(t`Unable to close account. Please try again.`))
}
console.info(`User: ${user._key} successfully closed user: ${userId} account.`)
await logActivity({
transaction,
collections,
query,
initiatedBy: {
id: user._key,
userName: user.userName,
role: submittedUserId ? 'SUPER_ADMIN' : '',
ipAddress: ip,
},
action: 'delete',
target: {
resource: targetUserName, // name of resource being acted upon
resourceType: 'user', // user, org, domain
},
})
return {
_type: 'regular',
status: i18n._(t`Successfully closed account.`),
user: checkUser,
}
},
})