-
Notifications
You must be signed in to change notification settings - Fork 69
/
submissionController.ts
163 lines (148 loc) · 3.87 KB
/
submissionController.ts
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
import { User } from '.prisma/client'
import { Context } from '../../@types/helpers'
import {
CreateSubmissionMutation,
MutationAcceptSubmissionArgs,
MutationCreateSubmissionArgs,
MutationRejectSubmissionArgs,
QuerySubmissionsArgs,
SubmissionStatus
} from '../../graphql'
import prisma from '../../prisma'
import { hasPassedLesson } from '../../helpers/hasPassedLesson'
import { updateSubmission } from '../../helpers/updateSubmission'
import { sendSubmissionNotification, IdType } from '../../helpers/discordBot'
import { decode } from '../../helpers/encoding'
import * as Sentry from '@sentry/node'
import { CliToken } from '../../@types/user'
export const createSubmission = async (
_parent: void,
args: MutationCreateSubmissionArgs,
ctx: Context
): Promise<CreateSubmissionMutation['createSubmission']> => {
const { req } = ctx
if (!args) throw new Error('Invalid args')
const { challengeId, cliToken, diff, lessonId } = args
let id = req.user!?.id
if (!id && cliToken) {
const decodedCliToken: CliToken = cliToken && decode(cliToken)
const user = await prisma.user.findFirst({
where: { cliToken: decodedCliToken.cliToken }
})
id = user!?.id
Sentry.captureException({
message: `${user?.id}/${user?.username} is using CLI version ${
user?.cliVersion || 'that is lower than 2.2.5'
} - Must be 2.2.5^`
})
}
const previousSubmission = await prisma.submission.findFirst({
where: {
challengeId,
lessonId,
user: { id },
status: SubmissionStatus.Open
}
})
if (previousSubmission) {
await prisma.submission.update({
where: { id: previousSubmission.id },
data: {
status: SubmissionStatus.Overwritten
}
})
}
const submission = await prisma.submission.create({
data: {
challengeId,
lessonId,
userId: id,
diff,
status: SubmissionStatus.Open
},
include: {
challenge: true,
user: {
select: {
discordId: true,
username: true
}
},
lesson: true
}
})
const { challenge, user, lesson } = submission
// Get next lesson
const nextLesson = await prisma.lesson.findFirst({
where: { order: lesson.order + 1 }
})
await sendSubmissionNotification(
user.discordId ? IdType.DISCORD : IdType.C0D3,
user.discordId || user.username,
nextLesson?.id ?? lessonId,
lesson.slug,
challenge.title
)
return submission
}
export const acceptSubmission = async (
_parent: void,
args: MutationAcceptSubmissionArgs,
ctx: Context
) => {
if (!args) throw new Error('Invalid args')
const reviewerId = await getReviewer(ctx.req.user, args.lessonId)
return updateSubmission({
...args,
reviewerId,
status: SubmissionStatus.Passed
})
}
export const rejectSubmission = async (
_parent: void,
args: MutationRejectSubmissionArgs,
ctx: Context
) => {
if (!args) throw new Error('Invalid args')
const reviewerId = await getReviewer(ctx.req.user, args.lessonId)
return updateSubmission({
...args,
reviewerId,
status: SubmissionStatus.NeedMoreWork
})
}
export const submissions = async (
_parent: void,
arg: QuerySubmissionsArgs,
ctx: Context
) => {
const { lessonId } = arg
await getReviewer(ctx.req.user, lessonId)
return prisma.submission.findMany({
where: {
status: SubmissionStatus.Open,
lessonId: lessonId
},
include: {
challenge: true,
user: true,
reviewer: true,
comments: {
include: {
author: true
}
}
}
})
}
export const getReviewer = async (
user: User | null,
lessonId: number
): Promise<number> => {
const reviewerId = user?.id
if (!reviewerId) throw new Error('Invalid user')
if (!(await hasPassedLesson(reviewerId, lessonId))) {
throw new Error('User has not passed this lesson and cannot review.')
}
return reviewerId
}