forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-user-profile.js
More file actions
145 lines (133 loc) · 4.33 KB
/
Copy pathupdate-user-profile.js
File metadata and controls
145 lines (133 loc) · 4.33 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
import { GraphQLString } from 'graphql'
import { mutationWithClientMutationId } from 'graphql-relay'
import { GraphQLEmailAddress } from 'graphql-scalars'
import { t } from '@lingui/macro'
import { LanguageEnums, TfaSendMethodEnum } from '../../enums'
import { updateUserProfileUnion } from '../unions'
export const updateUserProfile = new mutationWithClientMutationId({
name: 'UpdateUserProfile',
description:
'This mutation allows the user to update their user profile to change various details of their current profile.',
inputFields: () => ({
displayName: {
type: GraphQLString,
description: 'The updated display name the user wishes to change to.',
},
userName: {
type: GraphQLEmailAddress,
description: 'The updated user name the user wishes to change to.',
},
preferredLang: {
type: LanguageEnums,
description:
'The updated preferred language the user wishes to change to.',
},
tfaSendMethod: {
type: TfaSendMethodEnum,
description:
'The method in which the user wishes to have their TFA code sent via.',
},
}),
outputFields: () => ({
result: {
type: updateUserProfileUnion,
description:
'`UpdateUserProfileUnion` returning either a `UpdateUserProfileResult`, or `UpdateUserProfileError` object.',
resolve: (payload) => payload,
},
}),
mutateAndGetPayload: async (
args,
{
i18n,
query,
collections,
transaction,
userKey,
auth: { userRequired },
loaders: { loadUserByKey, loadUserByUserName },
validators: { cleanseInput },
},
) => {
// Cleanse Input
const displayName = cleanseInput(args.displayName)
const userName = cleanseInput(args.userName).toLowerCase()
const preferredLang = cleanseInput(args.preferredLang)
const subTfaSendMethod = cleanseInput(args.tfaSendMethod)
// Get user info from DB
const user = await userRequired()
// Check to see if user name is already in use
if (userName !== '') {
const checkUser = await loadUserByUserName.load(userName)
if (typeof checkUser !== 'undefined') {
console.warn(
`User: ${userKey} attempted to update their username, but the username is already in use.`,
)
return {
_type: 'error',
code: 400,
description: i18n._(t`Username not available, please try another.`),
}
}
}
let tfaSendMethod
if (subTfaSendMethod === 'phone' && user.phoneValidated) {
tfaSendMethod = 'phone'
} else if (subTfaSendMethod === 'email' && user.emailValidated) {
tfaSendMethod = 'email'
} else if (
subTfaSendMethod === 'none' ||
typeof user.tfaSendMethod === 'undefined'
) {
tfaSendMethod = 'none'
} else {
tfaSendMethod = user.tfaSendMethod
}
// Create object containing updated data
const updatedUser = {
displayName: displayName || user.displayName,
userName: userName || user.userName,
preferredLang: preferredLang || user.preferredLang,
tfaSendMethod: tfaSendMethod,
}
// 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 ${updatedUser}
UPDATE ${updatedUser}
IN users
`,
)
} catch (err) {
console.error(
`Trx step error ocurred when user: ${user._key} attempted to update their profile: ${err}`,
)
throw new Error(i18n._(t`Unable to update profile. Please try again.`))
}
try {
await trx.commit()
} catch (err) {
console.error(
`Trx commit error ocurred when user: ${user._key} attempted to update their profile: ${err}`,
)
throw new Error(i18n._(t`Unable to update profile. Please try again.`))
}
await loadUserByKey.clear(user._key)
const returnUser = await loadUserByKey.load(userKey)
console.info(`User: ${user._key} successfully updated their profile.`)
return {
_type: 'success',
status: i18n._(t`Profile successfully updated.`),
user: returnUser,
}
},
})