1
0
Fork 0

Update worker with better name management and event compare

This commit is contained in:
Ethanell 2020-12-12 21:52:55 +01:00
parent 22f17d88f2
commit 9940b11cf4
2 changed files with 44 additions and 46 deletions

View file

@ -2,12 +2,14 @@ const https = require("https");
const config = require("worker_threads").workerData; const config = require("worker_threads").workerData;
const models = require("../models"); const models = require("../models");
const reg_event = /(?:(?:BEGIN:VEVENT\nDTSTAMP:(?:[A-Z0-9]*?)\nDTSTART:([A-Z0-9]*?)\nDTEND:([A-Z0-9]*?)\nSUMMARY: {0,}([a-zéèàA-Z0-9-. \, \\/ô]*?)\nLOCATION:([a-zA-Zéèà0-9-. \,\\]*?)\nDESCRIPTION:(?:\\n){0,}(LP(?:[ a-zA-Z0-9]*))\\n((?:(?:[A-Z]*) (?:[A-Z]*)(?: (?:[A-Z]*)){0,}\\n){0,})(?:.*?)\nEND:VEVENT)|(?:BEGIN:VEVENT\nDTSTAMP:(?:[A-Z0-9]*?)\nDTSTART:([A-Z0-9]*?)\nDTEND:([A-Z0-9]*?)\nSUMMARY: {0,}((?:S(?:[A-Z0-9-]*)|M(?:[A-Z0-9-]*)(?:\/M(?:[A-Z0-9-]*)){0,}|Conférence)[a-zéèàA-Z0-9-. \, \\/]*?)\nLOCATION:([a-zA-Zéèà0-9-. \,\\]*?)\nDESCRIPTION:(?:\\n){0,}((?:(?:G[0-9]S[0-9]|S[0-9]|ASPE)\\n){0,})((?:(?:[A-Z]*) (?:[A-Z]*)(?: (?:[A-Z]*)){0,}\\n){0,})(?:.*?)\nEND:VEVENT))/gs; const reg_event = /(?:(?:BEGIN:VEVENT\nDTSTAMP:(?:[A-Z0-9]*?)\nDTSTART:([A-Z0-9]*?)\nDTEND:([A-Z0-9]*?)\nSUMMARY: {0,}([a-zéèàA-Z0-9-. \, \\/ô]*?)\nLOCATION:([a-zA-Zéèà0-9-. \,\\]*?)\nDESCRIPTION:(?:\\n){0,}((?:(?:LP(?:[ a-zA-Z0-9\\]*?))\\n){1,})((?:(?:[A-Z]*) (?:[A-Z]*)(?: (?:[A-Z]*)){0,}\\n){0,})(?:.*?)\nEND:VEVENT)|(?:BEGIN:VEVENT\nDTSTAMP:(?:[A-Z0-9]*?)\nDTSTART:([A-Z0-9]*?)\nDTEND:([A-Z0-9]*?)\nSUMMARY: {0,}((?:S(?:[A-Z0-9-]*)|M(?:[A-Z0-9-]*)(?:\/M(?:[A-Z0-9-]*)){0,}|Conférence)[a-zéèàA-Z0-9-. \, \\/]*?)\nLOCATION:([a-zA-Zéèà0-9-. \,\\]*?)\nDESCRIPTION:(?:\\n){0,}((?:(?:G[0-9]S[0-9]|S[0-9]|ASPE)\\n){0,})((?:(?:[A-Z]*) (?:[A-Z]*)(?: (?:[A-Z]*)){0,}\\n){0,})(?:.*?)\nEND:VEVENT))/gs;
const reg_location = /((?:[SH0-9][0-9]{2})|(?:(?:Préfa |Amphi)[0-9]))/g; const reg_location = /((?:[SH0-9][0-9]{2})|(?:(?:Préfa |Amphi)[0-9]))/g;
const reg_teachers = /^(?:((?:[a-zA-Z]*[ -]{0,}){0,}) ([a-zA-Z]*))$/m; const reg_teachers = /^(?:((?:[a-zA-Z]*[ -]{0,}){0,}) ([a-zA-Z]*))$/m;
const reg_date = /([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})Z/; const reg_date = /([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})Z/;
const reg_classe = /(?:(LP)[ -]{0,}(.*)|(?:(G[0-9])(S[0-9])))/; const reg_group = /(?:(LP)[ -]{0,}(.*)|(?:(G[0-9])(S[0-9])))/;
const base_url = config["edt"]; const base_url = config["edt"];
const capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
function fetchEvents(days = 1, TS_Start = new Date()) { function fetchEvents(days = 1, TS_Start = new Date()) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -32,8 +34,8 @@ function fetchEvents(days = 1, TS_Start = new Date()) {
reg_event.lastIndex++; reg_event.lastIndex++;
} }
let event = []; let event = [];
let promotion = []; let semester = [];
let classe = []; let group = [];
/* /*
m = [ m = [
FullMatch, FullMatch,
@ -47,21 +49,30 @@ function fetchEvents(days = 1, TS_Start = new Date()) {
*/ */
if (m[1] !== undefined) { if (m[1] !== undefined) {
// LPXXXX // LPXXXX
classe.push(m[5]);
} else { } else {
m.splice(1, 6); m.splice(1, 6);
// GXSX | SX | ASPE // GXSX | SX | ASPE
let csplit = m[5].split("\\n");
csplit.pop();
csplit.forEach(e => {
if (e === "ASPE") promotion.push(e);
if (/^S[0-9]$/.test(e)) promotion.push(e);
if (/^G[0-9]S[0-9]$/.test(e)) classe.push(e);
});
} }
let csplit = m[5].split("\\n");
csplit.pop();
csplit.forEach(e => {
if (e === "ASPE") {
semester.push(e);
}else if(/^S[0-9]$/.test(e)) {
semester.push(e);
}else{
let groupSplit = reg_group.exec(e);
if (groupSplit[1] === 'LP') {
group.push([groupSplit[2], groupSplit[1]]);
} else {
group.push([groupSplit[3], groupSplit[4]]);
}
}
});
event["title"] = m[3]; event["title"] = m[3];
event["promotion"] = promotion; event["semesters"] = semester;
event["class"] = classe; event["groups"] = group;
/* /*
Date Date
*/ */
@ -83,7 +94,7 @@ function fetchEvents(days = 1, TS_Start = new Date()) {
/* /*
Teachers Teachers
*/ */
event['teachers'] = [] event['teachers'] = [];
let fullTeachers = m[6].split('\\n'); let fullTeachers = m[6].split('\\n');
fullTeachers.forEach(e => { fullTeachers.forEach(e => {
if(e !== ""){ if(e !== ""){
@ -128,11 +139,11 @@ function compare(a, b) {
} }
function compareGroups(list1, list2) { function compareGroups(list1, list2) {
return compare(list1, list2.map(g => [g.number, g.Semester.name, g.Semester.year])); return compare(list1.map(g => g[0] + " " + g[1]), list2.map(g => g.number + " " + g.Semester.name));
} }
function compareTeachers(list1, list2) { function compareTeachers(list1, list2) {
return compare(list1, list2.map(t => t.lastName.toUpperCase() + " " + t.firstName.toUpperCase())); return compare(list1.map(t => t[0].toUpperCase() + " " + t[1].toUpperCase()), list2.map(t => t.lastName.toUpperCase() + " " + t.firstName.toUpperCase()));
} }
function compareSemesters(list1, list2) { function compareSemesters(list1, list2) {
@ -147,13 +158,14 @@ async function updateDatabase() {
include: {model: models.Semester, required: true} include: {model: models.Semester, required: true}
}, models.User, models.Semester] }, models.User, models.Semester]
})) { })) {
if (!events.find(e => (e.title === event.name && e.startDate.getTime() === event.startDate.getTime() && let ev = events.find(e => (e.title === event.name && e.startDate.getTime() === event.startDate.getTime() &&
e.endDate.getTime() === event.endDate.getTime() && e.locations.join(", ") === event.locations && e.endDate.getTime() === event.endDate.getTime() && e.locations.join(", ") === event.locations &&
compareGroups(e.class, event.Groups) && compareTeachers(e.teachers, event.Users) && compareGroups(e.groups, event.Groups) && compareTeachers(e.teachers, event.Users) &&
compareSemesters(e.semesters, event.Semesters)))) compareSemesters(e.semesters, event.Semesters)));
if (!ev)
await event.destroy(); await event.destroy();
else else
delete events[events.indexOf(event)]; events = events.filter(e => e !== ev);
} }
for (let event of events) { for (let event of events) {
@ -166,12 +178,12 @@ async function updateDatabase() {
let teachers = []; let teachers = [];
for (let teacher of event.teachers) { for (let teacher of event.teachers) {
let t = await models.User.findOne({where: {permissions: 2, lastName: teacher[0], firstName: teacher[1]}}); let t = await models.User.findOne({where: {permissions: 2, lastName: teacher[0].toUpperCase(), firstName: capitalizeFirstLetter(teacher[1])}});
if (!t) if (!t)
t = await models.User.create({ t = await models.User.create({
email: teacher[1].toLowerCase().replace(" ", "-") + "." + teacher[0].toLowerCase().replace(" ", "-") + "@univ-lyon1.fr", email: teacher[1].toLowerCase().replaceAll(" ", "-") + "." + teacher[0].toLowerCase().replaceAll(" ", "-") + "@univ-lyon1.fr",
firstName: teacher[1], firstName: capitalizeFirstLetter(teacher[1]),
lastName: teacher[0], lastName: teacher[0].toUpperCase(),
permissions: 2, permissions: 2,
passwordHash: Math.round((Math.pow(36, 12 + 1) - Math.random() * Math.pow(36, 12))).toString(36).slice(1) passwordHash: Math.round((Math.pow(36, 12 + 1) - Math.random() * Math.pow(36, 12))).toString(36).slice(1)
}); });
@ -180,7 +192,7 @@ async function updateDatabase() {
await e.addUsers(teachers); await e.addUsers(teachers);
let semesters = []; let semesters = [];
for (let semester of event.promotion) { for (let semester of event.semesters) {
let s = await models.Semester.findOne({where: {name: semester, year: event.startDate.getFullYear()}}); let s = await models.Semester.findOne({where: {name: semester, year: event.startDate.getFullYear()}});
if (!s) if (!s)
s = await models.Semester.create({ s = await models.Semester.create({
@ -192,32 +204,18 @@ async function updateDatabase() {
await e.addSemesters(semesters); await e.addSemesters(semesters);
let groups = []; let groups = [];
for (let group of event.class) { for (let group of event.groups) {
let rGroup = reg_classe.exec(group); let s = await models.Semester.findOne({where: {name: group[1], year: event.startDate.getFullYear()}});
let semesterName;
let groupName;
if (rGroup[1] !== undefined) {
// LP
semesterName = 'LP';
groupName = rGroup[2];
} else {
// GXSX
semesterName = rGroup[4];
groupName = rGroup[3];
}
let s = await models.Semester.findOne({where: {name: semesterName, year: event.startDate.getFullYear()}});
if (!s) if (!s)
s = await models.Semester.create({ s = await models.Semester.create({
name: semesterName, name: group[1],
year: event.startDate.getFullYear() year: event.startDate.getFullYear()
}); });
let g = await models.Group.findOne({where: {number: groupName, SemesterId: s.id}}); let g = await models.Group.findOne({where: {number: group[0], SemesterId: s.id}});
if (!g) if (!g)
g = await models.Group.create({ g = await models.Group.create({
number: groupName, number: group[0],
SemesterId: s.id SemesterId: s.id
}); });
groups.push(g); groups.push(g);

View file

@ -17,7 +17,7 @@ const mailClient = new SMTPClient(process.env.NODE_ENV === "test" ? {} : require
/** /**
* Render all Sass to css * Render all Sass to css
*/ */
css = sass.renderSync({ let css = sass.renderSync({
file: "sass/style.sass", file: "sass/style.sass",
includePaths: ["sass/"], includePaths: ["sass/"],
outputStyle: "compressed" outputStyle: "compressed"