Skip to content

Commit 0196fda

Browse files
committed
auto add student
1 parent 624da82 commit 0196fda

File tree

5 files changed

+106
-3
lines changed

5 files changed

+106
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
.idea
33
config/local.json
4-
key.json
4+
key.json
5+
src/uploads/file

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
"express": "^4.16.4",
2727
"jsonwebtoken": "^8.5.0",
2828
"morgan": "^1.9.1",
29+
"multer": "^1.4.1",
2930
"mysql2": "^1.6.5",
3031
"nodemon": "^1.18.10",
31-
"sequelize": "^4.42.0"
32+
"sequelize": "^4.42.0",
33+
"xls-to-json-lc": "^0.3.4",
34+
"xlsx-to-json-lc": "^0.5.0"
3235
}
3336
}

src/controller/user.controller.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ async function register(req, res) {
1313
email,
1414
name
1515
} = req.body;
16-
console.log(req.body);
1716
try {
1817
if(!user_name || !password || !email || !name){
1918
throw new Error("Bạn vui lòng điền đủ theo mẫu.")

src/router/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ const api = require('express').Router();
22
const user_router = require('./user');
33
const team_router = require('./team');
44
const course_router = require('./course');
5+
const Tool = require('../utils/addStudentsToDatabase');
56

67
api.use('/users', user_router);
78
api.use('/teams', team_router);
89
api.use('/courses', course_router);
10+
api.post('/students', Tool.autoAdd);
911

1012
module.exports = api;

src/utils/addStudentsToDatabase.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
const multer = require('multer');
2+
const xlstojson = require("xls-to-json-lc");
3+
const xlsxtojson = require("xlsx-to-json-lc");
4+
const bcrypt = require('bcrypt');
5+
const response = require('./response');
6+
const db = require('../database');
7+
8+
const storage = multer.diskStorage({
9+
destination: function (req, file, cb) {
10+
cb(null, './src/uploads/')
11+
},
12+
filename: function (req, file, cb) {
13+
cb(null, file.fieldname)
14+
}
15+
});
16+
17+
const upload = multer({
18+
storage: storage,
19+
fileFilter: function (req, file, callback) {
20+
if (file.originalname.split('.')[file.originalname.split('.').length - 1] !== "xlsx" && file.originalname.split('.')[file.originalname.split('.').length - 1] !== "xls") {
21+
return callback(new Error('Wrong extension type'));
22+
}
23+
callback(null, true);
24+
}
25+
}).single('file');
26+
27+
28+
module.exports.autoAdd = async (req, res) => {
29+
let exceltojson;
30+
try{
31+
upload(req, res, async function (err) {
32+
if (err) {
33+
throw new Error(err);
34+
}
35+
if (!req.file) {
36+
throw new Error("No file choose");
37+
}
38+
if (req.file.originalname.split('.')[req.file.originalname.split('.').length - 1] === 'xlsx') {
39+
exceltojson = xlsxtojson;
40+
} else {
41+
exceltojson = xlstojson;
42+
}
43+
await exceltojson({
44+
input: req.file.path,
45+
output: null,
46+
}, async function (err, jsonFile) {
47+
if (err) {
48+
throw new Error("Something went wrong");
49+
}
50+
const {team_id} = req.body;
51+
let i=0;
52+
let team = await db.TeamModel.findOne({
53+
where: {
54+
id: team_id
55+
}
56+
});
57+
if(!team){
58+
throw new Error("Team không tồn tại.")
59+
}
60+
for(i=0; i < jsonFile.length; i++){
61+
if(jsonFile[i].email && jsonFile[i].phone){
62+
let salt = await bcrypt.genSalt(10);
63+
let hashPassword = await bcrypt.hash(jsonFile[i].phone, salt);
64+
let user = await db.UserModel.findOrCreate({
65+
where: {
66+
user_name: jsonFile[i].email
67+
},
68+
defaults: {
69+
user_name: jsonFile[i].email,
70+
email: jsonFile[i].email,
71+
name: jsonFile[i].name,
72+
password: hashPassword,
73+
role_id: 3,
74+
create_time: Date.now(),
75+
avatar: "/img/avatar.png"
76+
}
77+
});
78+
await db.TeamUserModel.findOrCreate({
79+
where: {
80+
user_id: user[0].dataValues.id,
81+
team_id: team_id
82+
},
83+
defaults: {
84+
user_id: user[0].dataValues.id,
85+
team_id: team_id
86+
}
87+
});
88+
}
89+
}
90+
return res.json(response.success({}));
91+
});
92+
})
93+
}
94+
catch(err){
95+
console.log("Error: ", err.message);
96+
return res.json(response.fail(err.message));
97+
}
98+
};

0 commit comments

Comments
 (0)