This repository was archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexam.js
More file actions
118 lines (105 loc) · 3.29 KB
/
Copy pathexam.js
File metadata and controls
118 lines (105 loc) · 3.29 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
const db = require('./lib/db');
const crypto = require('./lib/crypto');
const settings = require('./settings');
function getExamInformation(examCode) {
return new Promise(async (resolve, reject) => {
try {
const client = await db.connect();
const doc = await client.db().collection('exams').findOne({
accessCode: examCode
}, {
projection: {
_id: false,
accessCode: true,
title: true,
startTime: true,
endTime: true,
questions: true
}
});
await client.close();
doc.count = doc.questions ? doc.questions.length : 0; // 문제 수
if (doc.questions) {
doc.questions = undefined;
}
resolve(doc);
} catch (err) {
reject(err);
}
});
}
function getQuestions(examCode) {
return new Promise(async (resolve, reject) => {
try {
const client = await db.connect();
const doc = await client.db().collection('exams').findOne({
accessCode: examCode
}, {
projection: {
_id: false,
questions: true
}
});
await client.close();
doc.questions.forEach(e => { // 정답 제외
if (e.answers) { // 주관식
e.answers = undefined;
}
if (e.multipleChoices) { // 객관식
e.multipleChoices.forEach(f => {
f.answers = undefined;
});
}
});
resolve(doc.questions);
} catch (err) {
reject(err);
}
});
}
const envelope = new Map();
function encryptQuestions(questions, userCode) {
return new Promise(async (resolve, reject) => {
try {
const password = await crypto.randomBytes(settings.crypto.length);
const key = await crypto.createKey(password);
const encrypted = await crypto.encrypt(JSON.stringify(questions), key, 'utf8');
envelope.set(userCode, key);
resolve(encrypted);
} catch (err) {
reject(err);
}
});
}
function decryptQuestions(encrypted, userCode) {
return new Promise(async (resolve, reject) => {
try {
const key = envelope.get(userCode);
const decrypted = await crypto.decrypt(encrypted, key, 'utf8');
const questions = JSON.parse(decrypted);
resolve(questions);
} catch (err) {
reject(err);
}
});
}
async function submitAnswers(examCode, userCode, answers) { // 답변 제출
const client = await db.connect();
const doc = await client.db().collection('exams').findOne({
accessCode: examCode
});
await client.db().collection('users').updateOne({
_id: {
$in: doc.users
},
accessCode: userCode
}, {
$set: {
answers
}
});
await client.close();
}
module.exports = {
getExamInformation, getQuestions, envelope, encryptQuestions, decryptQuestions, submitAnswers
};