aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Krönner2020-10-01 16:49:45 +0200
committerGitHub2020-10-01 16:49:45 +0200
commit590723afb4178e070a372d6e6054fe4d8549a4b0 (patch)
tree85485162adf07781b8eb0c19b37a9f34ec4e7320
parentce6a00fa25bd12774fa86ee384e840565ecf965b (diff)
parentc0469923b81368d087746a0789fec416ecdec43c (diff)
Merge pull request #1 from marvinborner/users
Users
-rw-r--r--.gitignore1
-rw-r--r--db.js60
-rw-r--r--package.json4
3 files changed, 62 insertions, 3 deletions
diff --git a/.gitignore b/.gitignore
index deae9b5..acf8e9a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@
*lock*
node_*
*.env
+*.csv
diff --git a/db.js b/db.js
index 244e9a6..cd76e0a 100644
--- a/db.js
+++ b/db.js
@@ -1,4 +1,6 @@
const mariadb = require("mariadb");
+const bcrypt = require("bcrypt");
+const nanoid = require("nanoid");
const fs = require("fs");
class DB {
@@ -10,6 +12,9 @@ class DB {
database: process.env.DBName,
});
this.init();
+ this.query("SELECT * FROM users").then((res) => {
+ if (res.length === 0) this.initValues();
+ });
}
connect() {
@@ -21,10 +26,61 @@ class DB {
if (err) throw err;
const queries = data.split(";");
queries.pop();
- const conn = await this.connect();
- for (const query of queries) await conn.query(query);
+ for (const query of queries) await this.query(query);
console.log("Tables created!");
});
+
+ fs.readFile(__dirname + "/names.csv", "utf8", (err, data) => {
+ if (err) throw err;
+ const classes = data.split("--");
+ classes.forEach((clazz, classIndex) => {
+ const students = clazz.split("\n");
+ students.forEach(async (student) => {
+ // 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, 12);
+ await this.query(
+ "INSERT INTO users (username, name, middlename, surname, password, class_id, type_id) VALUE (?,?,?,?,?,?,?)",
+ [username, names[0], middlename, surname, password, classIndex + 1, 2]
+ );
+ }
+ });
+ });
+ });
+ }
+
+ initValues() {
+ fs.readFile(__dirname + "/names.csv", "utf8", (err, data) => {
+ if (err) throw err;
+ const classes = data.split("--");
+ classes.forEach((clazz, classIndex) => {
+ const students = clazz.split("\n");
+ students.forEach(async (student) => {
+ // 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, 12);
+ await this.query(
+ "INSERT INTO users (username, name, middlename, surname, password, class_id, type_id) VALUE (?,?,?,?,?,?,?)",
+ [username, names[0], middlename, surname, password, classIndex + 1, 2]
+ );
+ }
+ });
+ });
+ });
}
async query(query, params) {
diff --git a/package.json b/package.json
index b6d8bdd..a7ad9d3 100644
--- a/package.json
+++ b/package.json
@@ -6,9 +6,11 @@
"author": "LarsVomMars <lars@kroenner.eu>",
"license": "MIT",
"dependencies": {
+ "bcrypt": "^5.0.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-rate-limit": "^5.1.3",
- "mariadb": "^2.4.2"
+ "mariadb": "^2.4.2",
+ "nanoid": "^3.1.12"
}
}