-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse-swipe.js
61 lines (55 loc) · 1.5 KB
/
parse-swipe.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
var db = require('./models');
/**
* Extract UIN from i-card track 2 data.
*
* Handles:
* - 16-digit card numbers
* - Raw track 2 data
* - Raw UINs
*
* Returns null if swipe data is invalid.
*/
var extractUIN = function(swipeData) {
var re = /(?:6397(6\d{8})\d{3}|(^6\d{8}$))/;
var result = re.exec(swipeData);
// result === null: invalid data
// result[1]: got UIN from (string containing) 16-digit card number
// result[2]: got UIN from raw UIN
var uin = result ? result[1] || result[2] : null;
return uin;
};
/**
* Search imported course roster for student with given NetID, and return the
* student's UIN if found.
*/
var getUINfromNetID = function(swipeData, courseId, callback) {
db.Student.find({
where: {courseId: courseId, netid: swipeData}
}).then(function(student) {
if (student) {
callback(student.uin);
} else {
callback(null);
}
});
};
/**
* Get UIN from swipe data or NetID and course ID.
*
* Syntax: parseSwipe(swipeData [, courseId ], callback)
* If courseId is omitted, does not run NetID check.
*/
var parseSwipe = function(swipeData, courseId, callback) {
if (typeof courseId === 'function') {
callback = courseId;
callback(extractUIN(swipeData));
} else {
var uin = extractUIN(swipeData);
if (uin) {
callback(uin);
} else {
getUINfromNetID(swipeData, courseId, callback);
}
}
};
module.exports = parseSwipe;