forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplete-tour.js
More file actions
77 lines (70 loc) · 2.53 KB
/
Copy pathcomplete-tour.js
File metadata and controls
77 lines (70 loc) · 2.53 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
import { GraphQLString } from 'graphql'
import { mutationWithClientMutationId } from 'graphql-relay'
import { t } from '@lingui/macro'
import { completeTourUnion } from '../unions'
export const completeTour = new mutationWithClientMutationId({
name: 'CompleteTour',
description:
'This mutation allows users to confirm that they have completed the tour. This mutation will update the user object in the database to reflect that the user has completed the tour.',
inputFields: () => ({
tourId: {
type: GraphQLString,
description: 'The id of the tour that the user is confirming completion of.',
},
}),
outputFields: () => ({
result: {
type: completeTourUnion,
description: '`CompleteTourUnion` returning either a `CompleteTourResult`, or `CompleteTourError` object.',
resolve: (payload) => payload,
},
}),
mutateAndGetPayload: async (
args,
{ i18n, query, auth: { userRequired }, loaders: { loadUserByKey }, validators: { cleanseInput } },
) => {
// Cleanse Input
const tourId = cleanseInput(args.tourId)
// Get user info from DB
const user = await userRequired()
if (!tourId) {
console.warn(`User: ${user._key} did not provide a tour id when attempting to confirm completion of the tour.`)
return {
_type: 'error',
code: 400,
description: i18n._(t`Unable to confirm completion of the tour. Please try again.`),
}
}
// Complete tour
try {
const completeTourCursor = await query`
LET userCompleteTours = FIRST(
FOR user IN users
FILTER user._key == ${user._key}
LIMIT 1
RETURN user.completedTours
)
UPDATE { _key: ${user._key} }
WITH {
completedTours: APPEND(
userCompleteTours[* FILTER CURRENT.tourId != ${tourId}],
{ tourId: ${tourId}, completedAt: DATE_ISO8601(DATE_NOW()) }
)
}
IN users
`
await completeTourCursor.next()
} catch (err) {
console.error(`Database error occurred when user: ${user._key} attempted to complete tour: ${tourId}: ${err}`)
throw new Error(i18n._(t`Unable to confirm completion of the tour. Please try again.`))
}
await loadUserByKey.clear(user._key)
const returnUser = await loadUserByKey.load(user._key)
console.info(`User: ${user._key} has confirmed completion of tour: ${tourId}`)
return {
_type: 'success',
status: i18n._(t`Tour completion confirmed successfully`),
user: returnUser,
}
},
})