-
Notifications
You must be signed in to change notification settings - Fork 1
/
import-roster.js
38 lines (32 loc) · 1.03 KB
/
import-roster.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
var fs = require('fs');
var cheerio = require('cheerio');
var db = require('./models');
var _ = require('lodash');
/**
* Import roster in the format of My.CS Portal's "Export to Excel".
*/
var importRoster = function(roster, courseId) {
$ = cheerio.load(roster);
var students = $('tbody tr').map(function() {
var row = $(this).children('td');
return {
courseId: courseId,
uin: row.eq(1).text(),
netid: row.eq(0).text(),
firstName: row.eq(5).text(),
lastName: row.eq(4).text()
};
});
students = _.uniqBy(students, 'uin');
// Don't delete existing records if no students were found in case user
// uploaded the wrong file.
if (students.length === 0) return [0, null];
return db.Student.destroy({
where: {courseId: courseId}
}).then(function() {
return db.Student.bulkCreate(students);
}).then(function(result) {
return [result.length, result[0].updatedAt];
});
};
module.exports = importRoster;