Skip to content

Commit

Permalink
Bug fixes and minor adjustments.
Browse files Browse the repository at this point in the history
  • Loading branch information
pgebert committed Aug 22, 2023
1 parent faba97c commit 05af897
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 280 deletions.
440 changes: 176 additions & 264 deletions package-lock.json

Large diffs are not rendered by default.

40 changes: 28 additions & 12 deletions src/bcsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,19 @@ export class BcsClient implements BcsClientInterface {
}

private async insertPresence(time: Time) {
await this.page.evaluate(time => {

const timeInput = document.querySelector(`td[name="attandenceDuration"] > span > input:nth-child(3)`) as HTMLInputElement;
timeInput.value = time.minutes.toString();
// 60 minutes of break
const minutesOfBreak = 60;

// clear fields so type can work as expected
await this.clearPresenceInputFields()

// Insert break
await this.page.type('tr:nth-child(2) >td[name="attandenceDuration"] > span > input:nth-child(3)', minutesOfBreak.toString());

// Insert presence
await this.page.type('tr:nth-child(1) >td[name="attandenceDuration"] > span > input:nth-child(3)', (time.minutes + minutesOfBreak).toString());

}, time);
}

private async fetchTasks(): Promise<Task[]> {
Expand Down Expand Up @@ -222,16 +229,15 @@ export class BcsClient implements BcsClientInterface {

private async resetPresence() {

await this.page.evaluate(() => {
// 60 minutes of break
const minutesOfBreak = 60;

const timeInput = document.querySelector(`td[name="attandenceDuration"] > span`);
const hourInput = timeInput.querySelector(`input:nth-child(1)`) as HTMLInputElement;
const minuteInput = timeInput.querySelector(`input:nth-child(3)`) as HTMLInputElement;

hourInput.value = '0';
minuteInput.value = '0';
});
// clear fields so type can work as expected
await this.clearPresenceInputFields()

// reset presence and break values
await this.page.type('tr:nth-child(1) >td[name="attandenceDuration"] > span > input:nth-child(3)', minutesOfBreak.toString());
await this.page.type('tr:nth-child(2) >td[name="attandenceDuration"] > span > input:nth-child(3)', minutesOfBreak.toString());
}

private async selectDate(date: Date) {
Expand Down Expand Up @@ -260,4 +266,14 @@ export class BcsClient implements BcsClientInterface {
this.page.waitForNavigation()
]);
}

private async clearPresenceInputFields() {
await Promise.all([
this.page.$eval('tr:nth-child(1) > td[name="attandenceDuration"] > span > input:nth-child(1)', el => el.value = '0'),
this.page.$eval('tr:nth-child(1) > td[name="attandenceDuration"] > span > input:nth-child(3)', el => el.value = '0'),
this.page.$eval('tr:nth-child(2) > td[name="attandenceDuration"] > span > input:nth-child(1)', el => el.value = '0'),
this.page.$eval('tr:nth-child(2) > td[name="attandenceDuration"] > span > input:nth-child(3)', el => el.value = '0'),
])

}
}
7 changes: 7 additions & 0 deletions src/commands/addCommand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Task} from "../task";
import {Time} from "../time";
import {BcsClient} from "../bcsClient";
import {TimeoutError} from "puppeteer";

const {prompt} = require('enquirer');

Expand Down Expand Up @@ -93,5 +94,11 @@ export const handleAddCommand = async () => {
await bcsClient.reset(targetDate)
await bcsClient.add(targetDate, tasks).then(() => console.log("Saved new tasks"))
}
}).catch(e => {
console.log("Oh snap - something went wrong! 😕");

if (e instanceof TimeoutError) {
console.log("Received timeout error!");
}
});
}
7 changes: 7 additions & 0 deletions src/commands/checkCommand.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import {BcsClient} from "../bcsClient";
import {TimeoutError} from "puppeteer";

export const handleCheckCommand = async () => {

const bcsClient = await BcsClient.getInstance();
await bcsClient.check().then((result) => {
console.log(`Your time balance for the current month is ${result}`)
}).catch(e => {
console.log("Oh snap - something went wrong! 😕");

if (e instanceof TimeoutError) {
console.log("Received timeout error!");
}
});
}
9 changes: 8 additions & 1 deletion src/commands/getCommand.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {BcsClient} from "../bcsClient";
import {TimeoutError} from "puppeteer";

const {prompt} = require('enquirer');

const validateDateInput = (input: string): boolean => /\d{1,2}.\d{1,2}.\d{4}/i.test(input);
const dateFromString = (value: string): Date => new Date(value.split(".").reverse().join("-"));

export const handleGetCommand = async () => {

await prompt([
{
type: 'input',
Expand All @@ -32,5 +33,11 @@ export const handleGetCommand = async () => {
} else {
console.log("No time recordings for this date!");
}
}).catch(e => {
console.log("Oh snap - something went wrong! 😕");

if (e instanceof TimeoutError) {
console.log("Received timeout error!");
}
});
}
7 changes: 5 additions & 2 deletions src/commands/mappingCommand.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {readFromFile, writeToFile} from "../utils/fileUtils";
import * as path from "path";

const {Input} = require('enquirer');

const mappingFilePath = path.resolve(__dirname, 'mapping.json')

interface MappingItem {
source: string;
regex: string;
Expand All @@ -11,7 +14,7 @@ interface MappingItem {

export const handleMappingCommand = async () => {

const mappingFile = readFromFile('mapping.json') || '[]';
const mappingFile = readFromFile(mappingFilePath) || '[]';
const mapping = JSON.parse(mappingFile);

const validateTaskToProjectMapping = (input: string): string | boolean => {
Expand Down Expand Up @@ -59,7 +62,7 @@ export const handleMappingCommand = async () => {
.then(input => {
const mapping = input.split("\n").filter(m => m).map(m => JSON.parse(m));
// console.log('ANSWER', mapping);
writeToFile('mapping.json', JSON.stringify(mapping));
writeToFile(mappingFilePath, JSON.stringify(mapping));
})
.catch(console.log);

Expand Down
7 changes: 7 additions & 0 deletions src/commands/resetCommand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {BcsClient} from "../bcsClient";
import {TimeoutError} from "puppeteer";

const {prompt} = require('enquirer');

Expand Down Expand Up @@ -30,5 +31,11 @@ export const handleResetCommand = async () => {
await bcsClient.reset(date);
console.log("Finished reset of all time recordings for this date!");
}
}).catch(e => {
console.log("Oh snap - something went wrong! 😕");

if (e instanceof TimeoutError) {
console.log("Received timeout error!");
}
});
}
File renamed without changes.
6 changes: 5 additions & 1 deletion src/task.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {Time} from "./time";
import {readFromFile} from "./utils/fileUtils";

import * as path from "path";

const mappingFilePath = path.resolve(__dirname, 'mapping.json')

interface ITask {
ticket: string
description: string
Expand Down Expand Up @@ -37,7 +41,7 @@ export class Task {
// {regex: /sm-/i, value: '1686218840677', source: 'ticket'},
// ];

const mappingFile = readFromFile('mapping.json') || '[]';
const mappingFile = readFromFile(mappingFilePath) || '[]';
const mapping = JSON.parse(mappingFile);

const patterns: IPattern[] = mapping.map(m => ({
Expand Down

0 comments on commit 05af897

Please sign in to comment.