-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.ts
56 lines (48 loc) · 2.18 KB
/
helpers.ts
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
import { table } from 'table';
import { add, addMinutes, format, formatDuration, intervalToDuration, parse, subMinutes } from "date-fns";
import { dateTimeFormat, referenceDate } from "./const";
import { Preset, TimeEntry } from "./model";
export const createSummary = (timeEntries: TimeEntry[], presets: {[key: string]: Preset}) => {
let totalEnd = new Date(0);
let totalEndBillable = new Date(0);
const tableData = [
['ID', 'Billable', 'Preset', 'Start', 'Pause', 'End', 'Worked hours'],
...timeEntries.map((timeEntry, index) => {
const duration = getDuration(timeEntry.start, timeEntry.end, timeEntry.pausedDuration);
const billable = presets?.[timeEntry.preset]?.billable;
totalEnd = add(totalEnd, duration);
if (billable) {
totalEndBillable = add(totalEndBillable, duration);
}
return [
index+1,
billable ? 'Yes' : 'No',
...Object.values(timeEntry),
formatDuration(duration),
];
}),
['', '', '', '', '', '', `Total: ${formatDuration(intervalToDuration({
start: new Date(0),
end: totalEnd,
}))}`],
['', '', '', '', '', '', `Billable: ${formatDuration(intervalToDuration({
start: new Date(0),
end: totalEndBillable,
}))}`]
];
return table(tableData);
};
export const getDuration = (start: string, end: string, pausedDuration: number) => {
return intervalToDuration({
start: parse(start, dateTimeFormat, referenceDate),
end: subMinutes(parse(end, dateTimeFormat, referenceDate), pausedDuration),
});
};
export const getFormmattedDateTime = (offsetMinutes: number = 0, dateTime?: string) => {
const date = dateTime ? parse(dateTime, 'dd-MM-yyyy HH:mm', referenceDate) : new Date();
return format(addMinutes(date, offsetMinutes), 'dd-MM-yyyy HH:mm')
};
export const getMoneybirdFormattedDateTime = (dateTime: string) => {
const parsedDate = parse(dateTime, 'dd-MM-yyyy HH:mm', referenceDate);
return format(addMinutes(parsedDate, parsedDate.getTimezoneOffset()), "yyyy-MM-dd HH:mm:00 'UTC'");
};