Skip to content

Commit

Permalink
Enhancement/#15/worked time difference (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Henrique authored Oct 14, 2020
1 parent f4b3ca2 commit 236c6af
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ USAGE
OPTIONS
-b, --debug Debug - Exibe mais informações na execução
-c, --company=company (required) ID da empresa no sistema de ponto
-d, --date=date [default: 2020-10-03] Data relacionada a consulta de horas no padrão YYYY-MM-DD
-d, --date=date [default: 2020-10-13] Data relacionada a consulta de horas no padrão YYYY-MM-DD
-h, --help show CLI help
-j, --journeytime=journeytime [default: 08:00] Quantidade de horas a serem trabalhadas por dia
-p, --password=password (required) Senha do usuário no sistema
Expand All @@ -77,7 +77,7 @@ EXAMPLES
$ my-worktime check -u 321 -p 123 -s ahgora -c a22 -j 08:48 -d 2020-09-23
```

_See code: [src/commands/check.ts](https://github.com/carloshpds/my-worktime/blob/v1.1.0/src/commands/check.ts)_
_See code: [src/commands/check.ts](https://github.com/carloshpds/my-worktime/blob/v1.2.0/src/commands/check.ts)_

## `my-worktime help [COMMAND]`

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "my-worktime",
"version": "1.1.0",
"version": "1.2.0",
"author": "Carlos Henrique <carloshpds@gmail.com> https://github.com/carloshpds",
"contributors": [
"Matheus Soares <matheusb95@gmail.com> https://github.com/Matheusss",
Expand Down
21 changes: 16 additions & 5 deletions src/commands/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export default class CheckCommand extends Command {
const worktimeDayResume: WorktimeDayResume = await worktimeProvider.getWorktimeDayResume()

if(worktimeDayResume.marks.length){
loader.succeed(`Dados encontrados, seu horário de saída ideal é ${chalk.black.bgGreen(' ' + worktimeDayResume.shouldLeaveClockTime + ' ')}`)
this.printResult(worktimeDayResume)
loader.succeed(`Dados encontrados, seu horário ideal de saída é ${chalk.black.bgBlueBright(' ' + worktimeDayResume.shouldLeaveClockTime + ' ')}`)
this.printResult(worktimeDayResume, options)
} else {
loader.fail('Não há nenhuma batida para esta data ainda.')
}
Expand All @@ -95,10 +95,10 @@ export default class CheckCommand extends Command {
}
}

printResult(worktimeDayResume: WorktimeDayResume){
printResult(worktimeDayResume: WorktimeDayResume, options: Partial<WorktimeProviderOptions>){
const marksToConsole = worktimeDayResume.marks.map((mark, index) => {
const isLastMark = index === worktimeDayResume.marks.length - 1
let markOnConsole = chalk.green(mark.clock)
let markOnConsole = chalk.blueBright(mark.clock)

if(isLastMark && worktimeDayResume.isMissingPairMark){
markOnConsole = chalk.yellow(mark.clock) + chalk.gray(' Batida ímpar')
Expand All @@ -110,7 +110,18 @@ export default class CheckCommand extends Command {
let workedMinutesUntilNowOnConsole = ClockHelper.humanizeMinutesToClock(worktimeDayResume.workedMinutesUntilNow)

if(worktimeDayResume.isMissingPairMark){
workedMinutesUntilNowOnConsole = chalk.yellow(workedMinutesUntilNowOnConsole)
workedMinutesUntilNowOnConsole = chalk.yellow(workedMinutesUntilNowOnConsole) + ' '
}

if(worktimeDayResume.missingMinutesToCompleteJourney){
const humanizedMissingMinutes = ClockHelper.humanizeMinutesToClock(worktimeDayResume.missingMinutesToCompleteJourney)

workedMinutesUntilNowOnConsole += chalk.gray(` - ${options.journeyTime} = `)
if(worktimeDayResume.missingMinutesToCompleteJourney > 0) {
workedMinutesUntilNowOnConsole += chalk.red(`-${humanizedMissingMinutes} `)
} else {
workedMinutesUntilNowOnConsole += chalk.green(`+${humanizedMissingMinutes} `)
}
}

console.log('')
Expand Down
8 changes: 7 additions & 1 deletion src/providers/WorktimeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,23 @@ export default abstract class WorktimeProvider {
shouldLeaveMarks.pop()
}

const missingMinutesToCompleteJourney = journeyTimeInMinutes - registeredWorkedMinutes
let missingMinutesToCompleteJourney = journeyTimeInMinutes - registeredWorkedMinutes
const minutesFromTheLastMark = ClockHelper.convertClockStringToMinutes(shouldLeaveMarks[shouldLeaveMarks.length - 1].clock)
const breakMinutesToCalculateShouldLeaveClockTime = breakMinutes > 60 && registeredWorkedMinutes > journeyTimeInMinutes && !lastPeriodIsOpen ? breakMinutes - 60 : 0
const shouldLeaveClockTime = ClockHelper.humanizeMinutesToClock((minutesFromTheLastMark + missingMinutesToCompleteJourney) - breakMinutesToCalculateShouldLeaveClockTime)

if(lastPeriodIsOpen){
missingMinutesToCompleteJourney = journeyTimeInMinutes - workedMinutesUntilNow
}

return {
registeredWorkedMinutes,
workedMinutesUntilNow,
isMissingPairMark: lastPeriodIsOpen,
shouldLeaveClockTime,
breakMinutes,
journeyTimeInMinutes,
missingMinutesToCompleteJourney,
marks,
now,
}
Expand All @@ -106,6 +111,7 @@ export default abstract class WorktimeProvider {
isMissingPairMark: false,
registeredWorkedMinutes: 0,
workedMinutesUntilNow: 0,
missingMinutesToCompleteJourney: 0,
breakMinutes: 0,
marks
}
Expand Down
7 changes: 7 additions & 0 deletions src/providers/__tests__/WorktimeProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ describe('WorktimeProvier', () => {
expect(worktimeDayResume.registeredWorkedMinutes).toBe(60 * 8)
expect(worktimeDayResume.workedMinutesUntilNow).toBe(60 * 8)
expect(worktimeDayResume.shouldLeaveClockTime).toBe('18:00')
expect(worktimeDayResume.missingMinutesToCompleteJourney).toBe(0)
expect(worktimeDayResume.isMissingPairMark).toBe(false)
})

Expand All @@ -138,10 +139,12 @@ describe('WorktimeProvier', () => {
const worktimeProvider: WorktimeProvider = new AbstractProvider(defaultOptions)
const worktimeDayResume: WorktimeDayWorkedTime = worktimeProvider.calculateWorkedTimeMinutes(marks, currentMomentDate.format())
const twelveHours = 60 * 12
const fourExtraHours = -(60 * 4)

expect(worktimeDayResume.registeredWorkedMinutes).toBe(twelveHours)
expect(worktimeDayResume.workedMinutesUntilNow).toBe(twelveHours)
expect(worktimeDayResume.shouldLeaveClockTime).toBe('18:00')
expect(worktimeDayResume.missingMinutesToCompleteJourney).toBe(fourExtraHours)
expect(worktimeDayResume.isMissingPairMark).toBe(false)
})

Expand Down Expand Up @@ -345,9 +348,12 @@ describe('WorktimeProvier', () => {
const worktimeDayResume: WorktimeDayWorkedTime = worktimeProvider.calculateWorkedTimeMinutes(marks, currentMomentDate.format())
const elevenHours = 60 * 11
const twelveHoursAndFifthNineMinutes = elevenHours + 119 // 119 = 1h + 59m
const threeExtraHoursAndFifthNineMinutes = -(60 * 3 + 119) // 119 = 1h + 59m

expect(worktimeDayResume.registeredWorkedMinutes).toBe(elevenHours)
expect(worktimeDayResume.workedMinutesUntilNow).toBe(twelveHoursAndFifthNineMinutes)
expect(worktimeDayResume.missingMinutesToCompleteJourney).toBe(threeExtraHoursAndFifthNineMinutes)

})

it('Calulates to odd marks', () => {
Expand All @@ -370,6 +376,7 @@ describe('WorktimeProvier', () => {

expect(worktimeDayResume.registeredWorkedMinutes).toBe(threeHours)
expect(worktimeDayResume.workedMinutesUntilNow).toBe(eightHours)
expect(worktimeDayResume.missingMinutesToCompleteJourney).toBe(0)
})

it('Calulates to values less than an hour', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/providers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ export interface WorktimeDayWorkedTime {
breakMinutes: number;
now: Moment;
marks: WorktimeDayMark[];
missingMinutesToCompleteJourney: number;
}

export interface WorktimeDayResume {
registeredWorkedMinutes: number;
workedMinutesUntilNow: number;
missingMinutesToCompleteJourney: number;
breakMinutes: number;
shouldLeaveClockTime?: string;
isMissingPairMark: boolean;
Expand Down
5 changes: 5 additions & 0 deletions src/utils/ClockHelper/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ describe('Clock Helper', () => {
const clock = ClockHelper.humanizeMinutesToClock(1290)
expect(clock).toBe('21:30')
})

it('Humanizes negative numbers', () => {
const clock = ClockHelper.humanizeMinutesToClock(-1290)
expect(clock).toBe('21:30')
})
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/utils/ClockHelper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ClockHelper {
}

humanizeMinutesToClock(minutes: number | string, options = {separator: ':'}): string {
const minutesNumber = typeof minutes === 'number' ? minutes : parseInt(minutes, 10)
const minutesNumber = typeof minutes === 'number' ? Math.abs(minutes) : Math.abs(parseInt(minutes, 10))
const realHoursNumbers = Math.floor(minutesNumber / 60)
const realMinutes = minutesNumber % 60

Expand Down

0 comments on commit 236c6af

Please sign in to comment.