aboutsummaryrefslogtreecommitdiff
path: root/update.js
blob: ceedac61908cccd1fff79e2b5ce67b4f6af9cc7d (plain)
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
const openFile = require("./Functions/open.js").file;
const parseClasses = require("./parseClasses.js").classes;
const parse = require("./dbparse.js");
const updateExceptions = require('./parseExceptions.js').updateExceptions;

// Run this if you want to build the database from text files
async function buildDB(dbconnection, shiftPath, classPath, exceptionPath) {
	let shiftCont = await openFile(shiftPath);
	shiftCont = shiftCont.toString("utf-8");

	let exceptions = await openFile(exceptionPath);
	exceptions = exceptions.toString("utf-8");

	let classes = await openFile(classPath);
	classes = classes.toString('utf-8');

	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;
}

exports.update = buildDB;
// Example call:
/*
const openFile = require("./Functions/open.js").file;
const database  = require("./database.js");
const dbcredentials = await openFile("../dblogin.txt");
const DB = new database.Database(JSON.parse(dbcredentials));
await updateDB.update(DB, "./shifts.txt", "./Kurssitarjottimet/2016Classes.txt", "./Kurssitarjottimet/NewClasses.txt");
*/