summaryrefslogtreecommitdiff
path: root/timetrack-migrate.js
blob: c94b1b58eecbcb803181b2f4a5264d10b3b21da2 (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
45
46
47
48
49
50
51
52
#!/usr/bin/env node

const fs = require("fs");

// accounts: {"user": hash (String)}
const origaccounts_obj = JSON.parse(fs.readFileSync("persist/timetrack/7a90e38a211ece1c346928e7d1f3e968"));
const origaccounts = origaccounts_obj.value;

// events: {"user": [{id: Int,sheet: String,text: String,date: Date,type: String}]}
//   type: "in" / "out"
const origevents_obj = JSON.parse(fs.readFileSync("persist/timetrack/16908b0605f2645dfcb4c3a8d248cef3"));
const origevents = origevents_obj.value;

const destfname = "persist/timetrack2/7a90e38a211ece1c346928e7d1f3e968";

const accounts = {};
for (let user in origaccounts) {
	accounts[user] = {
		list: [],
		current: null,
		pwhash: origaccounts[user],
	};

	let current = null;
	for (let ev of origevents[user]) {
		if (ev.type == "in" && current == null) {
			current = ev;
		} else if (ev.type == "out" && current != null) {
			if (current.sheet != ev.sheet) {
				console.error("Inconsistent in/out pairs (sheet for id " + ev.id + ")");
				process.exit(1);
			}

			const text = current.text.length > ev.text.length ? current.text : ev.text;

			accounts[user].list.push({
				id: current.id,  // arbitrarily choose check-in id
				sheet: current.sheet,
				text: text,
				indate: current.date,
				outdate: ev.date,
			});

			current = null;
		} else {
			console.error("Inconsistent in/out pairs at id=" + ev.id);
			process.exit(1);
		}
	}
}

fs.writeFileSync(destfname, JSON.stringify({key:"accounts",value:accounts}));