Skip to content

Commit

Permalink
1.0.0-beta.6 (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielseco authored Apr 8, 2020
1 parent a489fc3 commit 22744c6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Removed
### BREAKING CHANGES

## [1.0.0-beta.6] - 2020-04-08
### Fixed
- Wait for the xhr request to complete

## [1.0.0-beta.5] - 2020-04-06
### Fixed
- Fixed problem with dates
Expand Down
11 changes: 10 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "holded-bot",
"version": "1.0.0-beta.5",
"version": "1.0.0-beta.6",
"description": "Holded bot so you can fill your time from terminal",
"author": "Gabriel García Seco <[email protected]>",
"keywords": [
Expand Down Expand Up @@ -28,6 +28,7 @@
"devDependencies": {
"@types/jest": "^25.2.1",
"@types/luxon": "^1.22.0",
"@types/puppeteer": "^2.0.1",
"@typescript-eslint/eslint-plugin": "^2.25.0",
"@typescript-eslint/parser": "^2.25.0",
"coveralls": "^3.0.11",
Expand Down
28 changes: 21 additions & 7 deletions src/holded.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import puppeteer from 'puppeteer';
import { DateTime } from 'luxon';
import {
login,
startWithUrl,
addTimeButton,
fillInputs,
editLastTimeline
editLastTimeline,
waitForResponse
} from './puppetter';

interface AccountArgs {
Expand All @@ -18,7 +18,7 @@ interface CliArgs {
time?: string;
}

export function getTimes(time?: string) {
export function getTimes(time?: string): { time: string; timeEnd: string } {
const FORMAT_TIME = 'HH:mm';
const outputTime = time || DateTime.local().toFormat(FORMAT_TIME);
const timeEnd = DateTime.fromISO(outputTime)
Expand All @@ -35,10 +35,10 @@ export async function startWork(
account: AccountArgs,
routes,
args: CliArgs
): Promise<any> {
): Promise<void> {
try {
const url = routes.LOGIN;
const { page, browser } = await startWithUrl(puppeteer, url, args);
const { page, browser } = await startWithUrl(url, args);
const dayOfTheWeek = DateTime.local().weekday;
const { time, timeEnd } = getTimes(args.time);

Expand All @@ -58,6 +58,11 @@ export async function startWork(
timeEnd
});

await waitForResponse(
page,
'https://app.holded.com/tp/timetracking/entries/save'
);

await browser.close();

console.log(`Start work at ${time}`);
Expand All @@ -66,9 +71,13 @@ export async function startWork(
}
}

export async function stopWork(account: AccountArgs, routes, args: CliArgs) {
export async function stopWork(
account: AccountArgs,
routes,
args: CliArgs
): Promise<void> {
const url = routes.LOGIN;
const { page, browser } = await startWithUrl(puppeteer, url, args);
const { page, browser } = await startWithUrl(url, args);
const { time } = getTimes(args.time);
const date = DateTime.local().toFormat('dd-MM-yyyy');

Expand All @@ -83,6 +92,11 @@ export async function stopWork(account: AccountArgs, routes, args: CliArgs) {

await editLastTimeline(page, { date, time });

await waitForResponse(
page,
'https://app.holded.com/tp/timetracking/entries/save'
);

await browser.close();

console.log(`Stopped work at ${time}`);
Expand Down
30 changes: 23 additions & 7 deletions src/puppetter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export async function login(page, form): Promise<void> {
import { Page, Browser } from 'puppeteer';
import puppeteer from 'puppeteer';

export async function login(page: Page, form): Promise<void> {
try {
await page.type('#tpemail', form.username);
await page.type('#tppassword', form.password);
Expand All @@ -10,10 +13,9 @@ export async function login(page, form): Promise<void> {
}

export async function startWithUrl(
puppeteer,
url: string,
options: { debug?: boolean }
) {
): Promise<{ page: Page; browser: Browser }> {
try {
const browser = await puppeteer.launch({
headless: !options.debug
Expand All @@ -31,7 +33,10 @@ export async function startWithUrl(
}
}

export async function addTimeButton(page, dayOfTheWeek: number): Promise<void> {
export async function addTimeButton(
page: Page,
dayOfTheWeek: number
): Promise<void> {
try {
const links = await page.$$('div.timeline-additem');
const link = links[dayOfTheWeek - 1];
Expand All @@ -41,7 +46,7 @@ export async function addTimeButton(page, dayOfTheWeek: number): Promise<void> {
}
}

async function fillInput(page, options): Promise<void> {
async function fillInput(page: Page, options): Promise<void> {
try {
await page.evaluate(
(selector, time) => {
Expand All @@ -61,7 +66,7 @@ async function fillInput(page, options): Promise<void> {
}
}

export async function fillInputs(page, options): Promise<void> {
export async function fillInputs(page: Page, options): Promise<void> {
try {
await fillInput(page, {
selector: 'timerange-inputstart',
Expand All @@ -79,7 +84,7 @@ export async function fillInputs(page, options): Promise<void> {
}
}

export async function editLastTimeline(page, options): Promise<void> {
export async function editLastTimeline(page: Page, options): Promise<void> {
try {
await page.evaluate(date => {
const selector = `td[data-date="${date}"] .timeline-item`;
Expand All @@ -98,3 +103,14 @@ export async function editLastTimeline(page, options): Promise<void> {
console.log('error editing last timeline');
}
}

export const waitForResponse = (page: Page, url: string): Promise<unknown> => {
return new Promise(resolve => {
page.on('response', function callback(response) {
if (response.url() === url) {
resolve(response);
page.removeListener('response', callback);
}
});
});
};

0 comments on commit 22744c6

Please sign in to comment.