-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
161 lines (150 loc) · 4.52 KB
/
run.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const config = require('./config');
const misc = require('./misc');
const fs = require('fs');
const path = require('path');
const jsdom = require('jsdom');
const {JSDOM} = jsdom;
const fastcsv = require('fast-csv');
var loader = require('csv-load-sync');
const Shell = require('node-powershell');
try {
clearData();
const schoolSections = createSections(config.importFiles[2]);
const studentGroups = enrollUser(config.importFiles[0]);
const teacherGroups = enrollUser(config.importFiles[1]);
const students = createUsers(config.importFiles[3],studentGroups,schoolSections);
const teachers = createUsers(config.importFiles[4],teacherGroups,schoolSections);
writeFile('Groups.csv',schoolSections);
writeFile('Students.csv',students);
writeFile('Teachers.csv',teachers);
runScripts().then(r => {
console.log('Operation finished successful');
});
} catch(e){
console.log('########################');
console.log('#########ERROR##########');
console.log('########################');
console.log(e);
console.log('########################');
console.log('#########ERROR##########');
console.log('########################');
process.exit(1);
}
/**
* Runs powershell script
* @returns {Promise<void>}
*/
async function runScripts(){
const ps = new Shell({
verbose: true,
executionPolicy: 'Bypass',
noProfile: true
});
if (fs.existsSync(path.resolve('export', 'text.txt'))) {
fs.unlinkSync(path.resolve('export', 'text.txt'));
}
await ps.addCommand(path.resolve('scripts', config.runscript + '.ps1') + ' ' + config.username + ' ' + config.password + ' ' + config.domain);
ps.invoke()
.then(output => {
console.log(output);
ps.dispose();
})
.catch(err => {
console.log(err);
ps.dispose();
});
ps.on('end', code => {
});
}
/**
* Write new csv file for powershell script
* @param filename
* @param data
* @returns {Promise<void>}
*/
async function writeFile(filename,data){
const ws = fs.createWriteStream(path.resolve('export', filename));
fastcsv
.write(data, {headers: true})
.pipe(ws);
}
/**
* Get school sections array
* @param filename
* @returns {[]}
*/
function createSections(filename){
var sections = [];
var results = loader(path.resolve('import',filename));
results.forEach(function(e){
sections.push({
'id': e['SIS ID'],
'name': e['Section Name'].toUpperCase(),
'alias': misc.removeDiacritics(e['Section Name']),
})
});
return sections;
}
/**
* Enroll user to school section
* @param filename
* @returns {[]}
*/
function enrollUser(filename){
var groups = [];
var results = loader(path.resolve('import',filename));
results.forEach(function(e){
groups.push({
'group': e['Section SIS ID'],
'user': e['SIS ID'],
})
});
return groups;
}
/**
* Get array of users
* @param filename
* @param groups
* @param sections
* @returns {[]}
*/
function createUsers(filename,groups,sections){
var users = [];
var results = loader(path.resolve('import',filename));
results.forEach(function(e){
var Enroll = '';
groups.filter(element => element.user === e['SIS ID']).forEach(a => {
sections.filter(el => el.id === a.group).forEach(b => {
Enroll += b.alias + '*';
})
})
users.push({
'id': e['SIS ID'],
'name': e['First Name'].toUpperCase(),
'surname': e['Last Name'].toUpperCase(),
'password': e['Password'],
'email': misc.removeDiacritics(e['First Name']).toUpperCase() + '.' + misc.removeDiacritics(e['Last Name']).toUpperCase() + config.domain,
'groups': Enroll.slice(0,-1)
})
});
return users;
}
/**
* Clean export folder and check for csv to import
*/
function clearData(){
if (fs.existsSync(path.resolve('export', 'Student.csv'))) {
fs.unlinkSync(path.resolve('export', 'Student.csv'));
}
if (fs.existsSync(path.resolve('export', 'Groups.csv'))) {
fs.unlinkSync(path.resolve('export', 'Groups.csv'));
}
if (fs.existsSync(path.resolve('export', 'Teachers.csv'))) {
fs.unlinkSync(path.resolve('export', 'Teachers.csv'));
}
config.importFiles.forEach(function (e) {
if (!fs.existsSync(path.resolve('import', e))) {
throw new Error('FILE NOT FOUND: import/' + e);
}
});
}