diff options
| -rw-r--r-- | database.js | 3 | ||||
| -rw-r--r-- | parseExceptions.js | 16 | ||||
| -rw-r--r-- | server.js | 41 | ||||
| -rw-r--r-- | update.js | 18 | 
4 files changed, 51 insertions, 27 deletions
diff --git a/database.js b/database.js index d8e2d92..1f5b68f 100644 --- a/database.js +++ b/database.js @@ -44,8 +44,7 @@ class Database  		{  			this.pool.query(query, (err, res, fields) =>  			{ -				if (err) -					reject(err) +				if (err) reject(err);  				resolve(res);  			});  		}) diff --git a/parseExceptions.js b/parseExceptions.js index 42a1c50..fd129f2 100644 --- a/parseExceptions.js +++ b/parseExceptions.js @@ -8,25 +8,29 @@ function parseLine(line) {  	let dateStrings = datePart.split('-');  	if (dateStrings.length === 1)  		dateStrings.push(dateStrings[0]); -	assert.equal(dateStrings.length, 2, `päivämääräväli ${datePart} on virheellinen (ks. rivi ${line})`); +	assert.equal(dateStrings.length, 2, `päivämääräväli ”${datePart}” on virheellinen (ks. rivi ”${line}”)`);  	let d1   = dateStrings[0].split('.');  	const d2 = dateStrings[1].split('.');  	assert.equal(d2.length, 3, `päivämäärästä ”${dateStrings[1]}”  puuttui päivä, kuukausi tai vuosi tai se on muutoin virheellinen (ks. rivi ”${line}”)`); +	if (d1[d1.length - 1].length === 0) { +		d1.pop(); +	}  	while (d1.length < 3) {  		d1.push(d2[d1.length])  	}  	for (let i = 0; i < 3; i++) { -		d1[i] = +d1[i]; -		d2[i] = +d2[i]; +		d1[i] = parseInt(d1[i]); +		d2[i] = parseInt(d2[i]);  		let opts = ['päivä', 'kuukausi', 'vuosi']; -		assert.ok(!isNaN(d1[i]), `syötetty ${opts[i]} ei koostunut pelkistä numeroista (ks. rivi ${line})`); -		assert.ok(!isNaN(d2[i]), `syötetty ${opts[i]} ei koostunut pelkistä numeroista (ks. rivi ${line})`); +		assert.ok(!isNaN(d1[i]), `syötetty ”${opts[i]}“ ei koostunut pelkistä numeroista tai numeroita ei ollut tarpeeksi (ks. rivi ”${line}”)`); +		assert.ok(!isNaN(d2[i]), `syötetty ”${opts[i]}” ei koostunut pelkistä numeroista tai numeroita ei ollut tarpeeksi (ks. rivi ”${line}”)`);  	}  	const start = new Date(d1[2], d1[1] - 1, d1[0]);  	const end = new Date(d2[2], d2[1] - 1, d2[0]); +	assert(start <= end, `Ilmoituksen ensimmäisen päivämäärän ”${dateStrings[0]}” tulee olla ennen toista päivämäärää ”${dateStrings[1]}” (ks. rivi ”${line}”)`);  	let [header, message = ''] = rest.split('|', 2); -	assert.equal(header === undefined, false, 'otsikko täytyy antaa (ks. rivi ${line})'); +	assert((header !== undefined) && (header.length !== 0), `otsikko täytyy antaa (ks. rivi ”${line}”)`);  	header = header.trimEnd();  	message = message.trimStart(); @@ -82,9 +82,9 @@ async function init()  				let exceptions = "";  				let suppliedPassword = "";  				try { -					shifts = decodeURIComponent(q.get("shifts")).replaceAll("+", " ").replaceAll('\r\n', '\n'); -					classes = decodeURIComponent(q.get("classes")).replaceAll("+", " ").replaceAll('\r\n', '\n'); -					exceptions = decodeURIComponent(q.get('exceptions').replaceAll("+", ' ')).replaceAll('\r\n', '\n'); +					shifts = decodeURIComponent(q.get("shifts")).replaceAll('\r\n', '\n'); +					classes = decodeURIComponent(q.get("classes")).replaceAll('\r\n', '\n'); +					exceptions = decodeURIComponent(q.get('exceptions')).replaceAll('\r\n', '\n');  					suppliedPassword = decodeURIComponent(q.get('password'));  				} catch {  					console.log("Malformed url, presumably"); @@ -109,7 +109,7 @@ async function init()  					'sha512',  				);  				suppliedPassHash = suppliedPassHash.toString('hex'); -				console.log(suppliedPassHash); +				//console.log(suppliedPassHash); // will be used to retrieve the password, because there's no way to register  				let passHashes = await open.file(PASSPATH);  				passHashes = passHashes.toString('utf-8').split("\n");  				let match = false; @@ -126,17 +126,17 @@ async function init()  					return;  				} -				let shiftfile = await fs.open(`${SHIFTPATH}.tmp`, "w"); -				await shiftfile.write(shifts); -				shiftfile.close(); +				let tmpshiftfile = await fs.open(`${SHIFTPATH}.tmp`, "w"); +				await tmpshiftfile.write(shifts); +				tmpshiftfile.close(); -				let classfile = await fs.open(`${CLASSPATH}.tmp`, "w"); -				await classfile.write(classes) -				classfile.close(); +				let tmpclassfile = await fs.open(`${CLASSPATH}.tmp`, "w"); +				await tmpclassfile.write(classes) +				tmpclassfile.close(); -				let exceptionfile = await fs.open(`${EXCEPTIONPATH}.tmp`, "w"); -				await exceptionfile.write(exceptions); -				exceptionfile.close(); +				let tmpexceptionfile = await fs.open(`${EXCEPTIONPATH}.tmp`, "w"); +				await tmpexceptionfile.write(exceptions); +				tmpexceptionfile.close();  				try {  					await updateDB.update(SQLDB, `${SHIFTPATH}.tmp`, `${CLASSPATH}.tmp`, `${EXCEPTIONPATH}.tmp`); @@ -151,8 +151,16 @@ async function init()  				} catch(e) {  					console.log(e);  					res.writeHead(400); -					const cont = await buildCustomMessage("500: Virhe palvelimella", "Ruokailuvuorojen päivitysprosessi palautti virheen. Jos ruokailuvuorot on kirjoitettu jotenkin eri lailla kuin edellisissä viesteissä, selittynee virhe sillä. Jos pienellä muokkauksella et saa päivitystä toimimaan, ota yhteys kehittäjään."); +					let header = 'Päivityksessä tapahtui virhe'; +					let message = e.message; +					let s = e.message.split(': '); +					if (s.length === 2) { +						header = s[0]; +						message = s[1]; +					} +					const cont = await buildCustomMessage(header, message);  					res.end(cont); +					await updateDB.update(SQLDB, SHIFTPATH, CLASSPATH, EXCEPTIONPATH);  					return;  				}  			}); @@ -337,6 +345,7 @@ async function buildMain(args)  	// Show message if the normal schedule isn't in place  	const exceptionInfo = await SQLDB.query("SELECT * FROM exceptions"); +	let messages = '';  	for(let week = 0; week < exceptionInfo.length; week++)  	{  		// get the date of the requested day @@ -352,10 +361,10 @@ async function buildMain(args)  			new Date(exceptionInfo[week].end)  		))  		{ -			const message = `<div class="shift-result float-block"><h2>${exceptionInfo[week].header}</h2>${(exceptionInfo[week].message.length > 0) ? '<br>' : ''}${exceptionInfo[week].message}</div>`; -			data_string = strFuncs.replaceElement(data_string, "div id=\"shift-result\" class=\"float-block\"", message); +			messages = `<div class="shift-result float-block"><h2>${exceptionInfo[week].header}</h2>${(exceptionInfo[week].message.length > 0) ? '<br>' : ''}${exceptionInfo[week].message}</div>\n<br>\n` + messages;  		}  	} +	data_string = strFuncs.replaceElement(data_string, "div id=\"shift-result\" class=\"float-block\"", messages);  	// get the example input  	res["example-input"] = await DBPARSE.randomIndex(day, SQLDB); @@ -14,9 +14,21 @@ async function buildDB(dbconnection, shiftPath, classPath, exceptionPath) {  	let classes = await openFile(classPath);  	classes = classes.toString('utf-8'); -	await parseClasses(dbconnection, classes); -        await parse.build(shiftCont, dbconnection); -        await updateExceptions(exceptions, dbconnection); +	try { +		await parseClasses(dbconnection, classes); +	} catch (e) { +		throw new Error(`Virhe kurssitarjottimen osassa: ${e.message}`, { cause: e }); +	} +	try { +		await parse.build(shiftCont, dbconnection); +	} catch (e) { +		throw new Error(`Virhe ruokailuvuoroissa: ${e.message}`, { cause: e }); +	} +	try { +		await updateExceptions(exceptions, dbconnection); +	} catch (e) { +		throw new Error(`Virhe ilmoituksissa: ${e.message}`, { cause: e }); +	}  	return 0;  }  | 
