-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
96 lines (82 loc) · 2.63 KB
/
server.js
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
// @ts-check
import _ from 'lodash';
// eslint-disable-next-line
import Ajv from 'ajv';
const userTokens = ['some-token'];
const latestCourseVersions = {
'hexlet-course-source-ci': {
basics: 'release',
'first-program': 'release',
'not-started': 'release',
},
};
const memberCourseVersions = {
'hexlet-course-source-ci': {
basics: 'release',
'first-program': 'release',
},
};
const checkSchema = {
type: 'object',
properties: {
state: { enum: ['fail', 'success'] },
testData: {
type: 'object',
properties: {
passed: { type: 'boolean' },
output: { type: 'string', minLength: 1 },
exception: { type: 'object', nullable: true },
},
required: ['passed', 'output', 'exception'],
additionalProperties: false,
},
lintData: {
type: 'object',
properties: {
passed: { type: 'boolean' },
output: { type: 'string', minLength: 1 },
exception: { type: 'object', nullable: true },
},
required: ['passed', 'output', 'exception'],
additionalProperties: false,
},
sourceContents: { type: 'object' },
testContents: { type: 'object' },
},
required: ['state', 'testData', 'lintData', 'sourceContents', 'testContents'],
additionalProperties: false,
};
// eslint-disable-next-line
export default async (fastify, _options) => {
fastify
.post('/api_internal/courses/:courseSlug/lessons/:lessonSlug/assignment/check/validate', async (req, reply) => {
const { courseSlug, lessonSlug } = req.params;
const token = req.headers['x-auth-key'];
if (!userTokens.includes(token)) {
reply.code(401);
return { message: 'Invalid token passed.' };
}
const memberCourseVersion = _.get(memberCourseVersions, [courseSlug, lessonSlug]);
const latestCourseVersion = _.get(latestCourseVersions, [courseSlug, lessonSlug]);
if (!memberCourseVersion && !latestCourseVersion) {
reply.code(404);
return { message: 'Assignment not found.' };
}
if (!memberCourseVersion) {
reply.code(422);
return { message: 'You haven\'t started your assignment yet' };
}
return { version: memberCourseVersion };
})
.post('/api_internal/courses/:courseSlug/lessons/:lessonSlug/assignment/check', async (req, reply) => {
const { check } = req.body;
const ajv = new Ajv();
const validate = ajv.compile(checkSchema);
if (!validate(check)) {
reply.code(422);
return { message: 'Invalid check schema. Please report to support.' };
}
reply.code(201);
return { message: 'OK' };
});
};