-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
129 lines (95 loc) · 3.22 KB
/
index.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
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
import * as puppeteer from "puppeteer";
import * as $ from "cheerio";
import * as dayjs from "dayjs";
import got from "got";
import { slack } from "./slack";
let isFridayStandup = false;
const getRandomInt = (max) => {
return Math.floor(Math.random() * Math.floor(max));
};
const isValidDay = () => {
const today = dayjs().day();
const validDays = [1, 2, 3, 4, 5];
return validDays.includes(today);
};
const getStandupDate = () => {
const today = dayjs().day();
const isMonday = today === 1;
let daysAgo = 1;
if (isMonday) {
isFridayStandup = true;
daysAgo = 3;
}
return dayjs().subtract(daysAgo, "day").format("YYYY/M/D");
};
(async () => {
if (!isValidDay()) return;
const _slack = slack();
const browser = await puppeteer.launch();
const page = await browser.newPage();
const url = "https://id.getharvest.com/harvest/sign_in";
await page.goto(url, { waitUntil: ["networkidle2"] });
const navigationPromise = page.waitForNavigation({
waitUntil: ["networkidle2"],
});
await page.waitForSelector('[name="email"]');
await page.waitForSelector('[name="password"]');
await page.type('[name="email"]', process.env.harvestU);
await page.type('[name="password"]', process.env.harvestP);
await page.click('[type="submit"]');
await navigationPromise;
const standupDate = getStandupDate();
const nextUrl = `https://deviqio.harvestapp.com/time/day/${standupDate}`;
await page.goto(nextUrl, { waitUntil: ["networkidle2"] });
const bodyHTML = await page.evaluate(() => document.body.innerHTML);
const $page = $.load(bodyHTML);
const sows = {};
try {
let message = ``;
const reportedTickets = $page(".day-view-entry-list tbody").find("tr");
reportedTickets.each(function () {
const sow = $page(this).find(".project-client .project").text();
const task = $page(this).find(".task").text();
if (!sows[sow]) {
sows[sow] = {};
}
let ticket: string = "";
$page(this)
.find(".remote-entry-data a p")
.each(function (i) {
const isTicket = i === 0;
const text = $page(this).text();
if (isTicket) {
ticket = `${task} - ${text}`;
sows[sow][ticket] = "";
return;
}
sows[sow][ticket] += `- ${text}\n `;
});
});
const sowMap = new Map(Object.entries(sows));
let whenText = isFridayStandup ? "On Friday" : "Yesterday";
sowMap.forEach((tickets, sow) => {
message += `${whenText}, I worked on: ${sow} \n\n`;
const ticketMap = new Map(Object.entries(tickets));
ticketMap.forEach((notes, ticket) => {
message += `For the ticket: ${ticket} \n\n`;
message += `Here's what i did: \n ${notes} \n\n`;
});
});
if (message === "") return;
message += `hours are in baby!!\n\n`;
const quoteRequest = await got("https://type.fit/api/quotes", {
responseType: "json",
});
if (quoteRequest) {
const quotes: any = quoteRequest.body;
const randomInt = getRandomInt(quotes.length);
const { text, author } = quotes[randomInt];
message += `Quote of the day: ${text} - ${author} \n\n`;
}
await _slack.send(message);
} finally {
process.exit(0);
}
})();