Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gulp.task('import-csv', async function (cb) {
- const fs = require('fs');
- const generalDao = require('./dao/general-tool-dao');
- const pwdtool = require('./tools/password-hash-tool');
- function endTask(err) {
- require('./db').end();
- cb(err);
- }
- async function readCSV(file) {
- const csvtojson = require("csvtojson");
- return await csvtojson().fromFile(file);
- }
- let argv = require('yargs').argv;
- let std = argv.student;
- let crse = argv.course;
- let crse_prd = argv["course-period"];
- if (!std && !crse && !crse_prd) {
- console.error("No se ha definido que archivo ni a que tabla se importará a la BD");
- endTask();
- }
- if (std) {
- console.log("Importando Estudiantes...")
- if (fs.existsSync(std)) {
- let students = await readCSV(std);
- for(let student of students){
- console.log("Insertando a estudiante: " + student.codigo);
- generalDao.save('student', {
- username: student.username,
- first_name: student.first_name,
- last_name: student.last_name,
- email: student.email,
- institution_id: student.institution_id,
- password: pwdtool.generateHashSync(student.codigo)
- }).then(function (res) {
- console.log(res);
- endTask();
- }).catch(function (err) {
- console.error(err);
- endTask();
- });
- break;
- }
- } else{
- console.error("Archivo de estudiantes no existe o no es válido.");
- endTask();
- }
- }
- if (crse) {
- console.log("Importando Cursos...")
- if (fs.existsSync(crse)) {
- let courses = await readCSV(crse);
- console.log('Courses', courses);
- //TODO
- } else{
- console.error("Archivo de cursos no existe o no es válido.");
- endTask();
- }
- }
- if (crse_prd) {
- console.log("Importando Cursos Periodo...")
- if (fs.existsSync(crse_prd)) {
- let course_periods = await readCSV(crse_prd);
- console.log('Course-Periods', course_periods);
- //TODO
- } else{
- console.error("Archivo de cursos periodo no existe o no es válido.");
- endTask();
- }
- }
- /*generalDao.save('user', {
- username: process.env.ADMIN_USER || 'admin',
- password: pwdtool.generateHashSync(process.env.ADMIN_PASSWORD || 'km2019i'),
- profile_id: 'A',
- state: '10'
- }).then(function (res) {
- console.log(res);
- endTask();
- }).catch(endTask);*/
- endTask();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement