-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-period.js
63 lines (47 loc) · 1.98 KB
/
generate-period.js
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
'use strict';
const stringify = require('csv-stringify');
const _ = require('lodash');
const fs = require('fs');
const join = require('path').join;
const ONE_DAY = 26 * 60 * 60 * 1000;
const SUNDAY = 0;
const SATURDAY = 6;
const generateDays = (start, end) =>
generateAllDaysForPeriod(start, end).filter(isValidDate);
const isValidDate = date => {
const dayOfTheWeek = date.getDay();
return dayOfTheWeek !== SUNDAY && dayOfTheWeek !== SATURDAY;
};
const generateAllDaysForPeriod = (start, end) => {
const days = [];
let currentDay = start;
while (currentDay <= end) {
console.log(currentDay, end);
days.push(currentDay);
currentDay = new Date(currentDay.getTime() + ONE_DAY);
currentDay.setHours(0);
currentDay.setMinutes(0);
currentDay.setSeconds(0);
}
return days;
};
const getStartAndEndDate = (pivot) => pivot.getDate() <= 15
? {start: cloneInDate(pivot, 1), end: cloneInDate(pivot, 15)}
: {start: cloneInDate(pivot, 16), end: lastOfTheMonth(pivot)};
const cloneInDate = (fullDate, dayOfTheMonth) => new Date(fullDate.getFullYear(), fullDate.getMonth(), dayOfTheMonth);
const lastOfTheMonth = (fullDate) => new Date(fullDate.getFullYear(), fullDate.getMonth() + 1, 0);
const formatDate = date => `${pad(date.getMonth() + 1)}-${pad(date.getDate())}`;
const pad = n => _.padStart(n, 2, '0');
const generateEntries = dates => _.flatMap(dates, date => {
const baseEntry = {ticket: 6252, date: formatDate(date), comment: '', activity: 'Development'};
return [hours(baseEntry, 4), hours(baseEntry, 2), hours(baseEntry, 2)]
});
const hours = (src, h) => Object.assign({hours: h}, src);
const period = getStartAndEndDate(new Date());
const validDates = generateDays(period.start, period.end);
const entries = generateEntries(validDates);
const stringifier = stringify({quotedEmpty: true, header: true});
const stream = stringifier
.pipe(fs.createWriteStream(join(__dirname + '/entries.csv')));
entries.forEach(entry => stringifier.write(entry));
stringifier.end();