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
|
const mariadb = require("mariadb");
const bcrypt = require("bcrypt");
const nanoid = require("nanoid");
const fs = require("fs");
class DB {
constructor() {
this.pool = mariadb.createPool({
host: process.env.DBHost,
user: process.env.DBUser,
password: process.env.DBPassword,
database: process.env.DBName,
});
this.init();
}
connect() {
return this.pool.getConnection();
}
init() {
fs.readFile(__dirname + "/tables.sql", "utf8", async (err, data) => {
if (err) throw err;
const queries = data.split(";");
queries.pop();
for (const query of queries) await this.query(query);
console.log("Tables created!");
const res = await this.query("SELECT * FROM users");
if (res.length === 0) this.initValues();
});
}
initValues() {
fs.readFile(__dirname + "/names.csv", "utf8", async (err, data) => {
if (err) throw err;
await this.query("INSERT INTO types (name) VALUES ('pupil'), ('teacher')");
await this.query(
"INSERT INTO class (name) VALUES ('TGM13.1'), ('TGM13.2'), ('TGTM13.1'), ('TGI13.1'), ('TGI13.2'), ('teacher')",
);
const types = ["number", "file", "date", "text", "color"];
await this.query("INSERT INTO profile_input_types (type) VALUES (?), (?), (?), (?), (?)", types);
// User polls
fs.readFile(__dirname + "/poll.txt", "utf8", (err, data) => {
if (err) throw err;
const parts = data.split("--");
parts.forEach((part, i) => {
const questions = part.split("\n");
questions.forEach((question) => {
if (question.length > 0)
this.query("INSERT INTO ranking_questions (question, type_id) VALUE (?,?)", [
question,
i + 1,
]);
});
});
});
// Motto votes
fs.readFile(__dirname + "/mottos.txt", "utf8", (err, data) => {
if (err) throw err;
const mottos = data.split("\n");
mottos.forEach(async (motto) => {
const [name, desc] = motto.split(" - ");
if (motto) await this.query("INSERT INTO mottos (name, description) VALUES (?, ?)", [name, desc]);
});
});
// User profile
fs.readFile(__dirname + "/profile.txt", "utf8", (err, data) => {
if (err) throw err;
const questions = data.split("\n");
questions.forEach((question) => {
if (question) {
const [q, type] = question.split(" - ");
this.query("INSERT INTO profile_questions (question, question_type) VALUE (?, ?)", [
q,
types.indexOf(type) + 1,
]);
}
});
});
const classes = data.split("--");
const userPasswords = {};
console.log("Generating users");
for (const [classIndex, clazz] of classes.entries()) {
const students = clazz.split("\n");
userPasswords[classIndex] = [];
// students.forEach(async (student) => {
for (const student of students) {
// console.log(".");
// Fix undefined
if (student && student.length > 3) {
const [_, surname, name] = student.split(",");
const names = name.split(" ");
const middlename = names.length > 1 && names[1] ? names.slice(1).join(" ") : null;
let username = surname.toLowerCase().slice(0, 6);
if (middlename) username += middlename[0].toLowerCase();
username += names[0].toLowerCase().slice(0, 2);
const pwd = nanoid.nanoid(8);
const password = await bcrypt.hash(pwd, 10);
userPasswords[classIndex].push({ username, pwd });
await this.query(
"INSERT INTO users (username, name, middlename, surname, password, class_id, type_id) VALUE (?,?,?,?,?,?,?)",
[
username,
names[0].replace("\r", ""),
middlename,
surname,
password,
classIndex + 1,
classIndex + 1 === 6 ? 2 : 1,
],
);
}
}
}
fs.writeFile(__dirname + "/users.json", JSON.stringify(userPasswords), (err) => {
if (err) console.error(err);
});
console.log("Initialized users!");
});
}
async query(query, params) {
const conn = await this.connect();
try {
return await conn.query(query, params);
} finally {
conn.release();
}
}
}
module.exports = new DB();
|