forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-phone-number.js
More file actions
84 lines (76 loc) · 2.39 KB
/
Copy pathremove-phone-number.js
File metadata and controls
84 lines (76 loc) · 2.39 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
import { t } from '@lingui/macro'
import { mutationWithClientMutationId } from 'graphql-relay'
import { removePhoneNumberUnion } from '../unions'
export const removePhoneNumber = new mutationWithClientMutationId({
name: 'RemovePhoneNumber',
description:
'This mutation allows for users to remove a phone number from their account.',
outputFields: () => ({
result: {
type: removePhoneNumberUnion,
description:
'`RemovePhoneNumberUnion` returning either a `RemovePhoneNumberResult`, or `RemovePhoneNumberError` object.',
resolve: (payload) => payload,
},
}),
mutateAndGetPayload: async (
_args,
{ i18n, collections, query, transaction, auth: { userRequired } },
) => {
// Get requesting user
const user = await userRequired()
// Set TFA method to backup incase user gets logged out, so they're not locked out of their account
let tfaSendMethod = 'none'
if (user.emailValidated && user.tfaSendMethod !== 'none') {
tfaSendMethod = 'email'
}
// 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.step(
() => query`
WITH users
UPSERT { _key: ${user._key} }
INSERT {
phoneDetails: null,
phoneValidated: false,
tfaSendMethod: ${tfaSendMethod}
}
UPDATE {
phoneDetails: null,
phoneValidated: false,
tfaSendMethod: ${tfaSendMethod}
}
IN users
`,
)
} catch (err) {
console.error(
`Trx step error occurred well removing phone number for user: ${user._key}: ${err}`,
)
throw new Error(
i18n._(t`Unable to remove phone number. Please try again.`),
)
}
try {
await trx.commit()
} catch (err) {
console.error(
`Trx commit error occurred well removing phone number for user: ${user._key}: ${err}`,
)
throw new Error(
i18n._(t`Unable to remove phone number. Please try again.`),
)
}
console.info(`User: ${user._key} successfully removed their phone number.`)
return {
_type: 'result',
status: i18n._(t`Phone number has been successfully removed.`),
}
},
})