This repository was archived by the owner on May 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.js
More file actions
140 lines (103 loc) · 3.89 KB
/
backup.js
File metadata and controls
140 lines (103 loc) · 3.89 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { Temporal } from '@js-temporal/polyfill';
// helper
const TZ = 'Europe/Stockholm';
function formatPersonalShiftTemporal(shift) {
const s = shift.sharedShift ?? shift.draftShift;
if (!s?.startDateTime || !s?.endDateTime) return null;
// Parse the UTC timestamps as Instants
const startInstant = Temporal.Instant.from(s.startDateTime);
const endInstant = Temporal.Instant.from(s.endDateTime);
// Convert to Stockholm time
const startZoned = startInstant.toZonedDateTimeISO(TZ);
const endZoned = endInstant.toZonedDateTimeISO(TZ);
const date = startZoned.toPlainDate().toString(); // "2026-02-24"
const startTime = startZoned.toPlainTime().toString({ smallestUnit: 'minute' });
const endTime = endZoned.toPlainTime().toString({ smallestUnit: 'minute' });
// Duration math
const duration = endInstant.since(startInstant).round({
largestUnit: 'hours',
smallestUnit: 'minutes',
});
const hours = duration.hours;
const minutes = duration.minutes;
const timeSummary = minutes === 0 ? `${hours}h` : `${hours}h ${minutes}m`;
return {
shift: s.notes ?? '',
date,
timeSummary,
start: startTime,
end: endTime,
};
}
/* The `formatPersonalShiftText` function takes a shift object as input and formats it into a text
representation. */
function formatPersonalShiftText(shift) {
const f = formatPersonalShiftTemporal(shift);
if (!f) return null;
return [
`Shift: ${f.shift}`,
`Date: ${f.date}`,
`TimeSummary: ${f.timeSummary}`,
`Start: ${f.start}`,
`End: ${f.end}`,
].join('\n');
}
function formatPersonalShifts(shifts) {
if (!Array.isArray(shifts)) return [];
return shifts.map(formatPersonalShiftText).filter(Boolean);
}
function shiftMatchesPeriod(shift, { year, month }) {
const s = shift.sharedShift ?? shift.draftShift;
if (!s?.startDateTime) return false;
const start = Temporal.Instant.from(s.startDateTime).toZonedDateTimeISO(TZ);
if (year && start.year !== year) return false;
if (month && start.month !== month) return false;
return true;
}
function filterShiftsByPeriod(shifts, { year, month } = {}) {
if (!Array.isArray(shifts)) return [];
return shifts.filter((shift) => shiftMatchesPeriod(shift, { year, month }));
}
function currentMonthFilter() {
const now = Temporal.Now.zonedDateTimeISO(TZ);
return { year: now.year, month: now.month };
}
function sumFromFormattedStrings(formatted) {
let totalMinutes = 0;
for (const block of formatted) {
// block contains e.g. "TimeSummary: 4h 30m"
const match = String(block).match(/TimeSummary:\s*([^\n]+)/);
if (!match) continue;
const summary = match[1]; // "4h 30m" or "5h"
const h = summary.match(/(\d+)\s*h/);
const m = summary.match(/(\d+)\s*m/);
if (h) totalMinutes += parseInt(h[1], 10) * 60;
if (m) totalMinutes += parseInt(m[1], 10);
}
const hours = Math.floor(totalMinutes / 60);
const minutes = totalMinutes % 60;
return minutes === 0 ? `${hours}h` : `${hours}h ${minutes}m`;
}
function parseHoursToDecimal(timeString) {
const h = timeString.match(/(\d+)\s*h/);
const m = timeString.match(/(\d+)\s*m/);
const hours = h ? parseInt(h[1], 10) : 0;
const minutes = m ? parseInt(m[1], 10) : 0;
return hours + minutes / 60;
}
const formattedData = formatPersonalShifts(filteredData);
log.trace('formattedData:', formattedData);
// Pretty print
log.info(formattedData.join('\n\n'));
const thisMonthFormattedData = filterShiftsByPeriod(filteredData, currentMonthFilter());
const thisMonthFormattedShifts = formatPersonalShifts(thisMonthFormattedData);
log.info(thisMonthFormattedShifts.join('\n\n'));
console.log(thisMonthFormattedShifts.join('\n\n'));
const totalHoursStr = sumFromFormattedStrings(thisMonthFormattedShifts);
console.log(totalHoursStr);
const totalHours = parseHoursToDecimal(totalHoursStr);
const pretax = totalHours * 140;
const pretax_sem = pretax * 1.12;
const posttax = pretax_sem * 0.7;
log.info('pre-tax:', pretax_sem.toFixed(0) + 'kr');
log.info('post-tax:', posttax.toFixed(0) + 'kr');