Skip to content

Commit

Permalink
Fix/validate date (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Henrique authored Oct 3, 2020
1 parent a9fd372 commit ac7d0fe
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ USAGE
OPTIONS
-b, --debug Debug - Exibe mais informações na execução
-c, --company=company (required) [default: ahgora] ID da empresa no sistema de ponto
-d, --date=date [default: 2020-09-27] Data relacionada a consulta de horas no padrão YYYY-MM-DD
-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
-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 @@ -76,7 +76,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.0.2/src/commands/check.ts)_
_See code: [src/commands/check.ts](https://github.com/carloshpds/my-worktime/blob/v1.1.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.0.2",
"version": "1.1.0",
"author": "Carlos Henrique <carloshpds@gmail.com> https://github.com/carloshpds",
"contributors": [
"Matheus Soares <matheusb95@gmail.com> https://github.com/Matheusss",
Expand Down
36 changes: 25 additions & 11 deletions src/commands/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import Ahgora from '../providers/Ahgora'
import WorktimeProvider from '../providers/WorktimeProvider'
import * as ora from 'ora'
import * as chalk from 'chalk'
import { DATE_FORMAT, DATE_REGEXP } from '../utils/dateFormat'
import {CLIError} from '@oclif/errors'

export default class CheckCommand extends Command {
static description = 'Checks your worktime'
Expand All @@ -20,8 +22,8 @@ export default class CheckCommand extends Command {
help: flags.help({char: 'h'}),
user: flags.string({char: 'u', description: 'ID do usuário no sistema de ponto', required: true, env: 'MW_USER'}),
password: flags.string({char: 'p', description: 'Senha do usuário no sistema', required: true, env: 'MW_PASS'}),
system: flags.string({char: 's', description: 'Nome do sistema de ponto', default: 'ahgora'}),
company: flags.string({char: 'c', description: 'ID da empresa no sistema de ponto', required: true, default: 'ahgora'}),
system: flags.string({char: 's', description: 'Nome do sistema de ponto', default: 'ahgora', env: 'MW_SYSTEM'}),
company: flags.string({char: 'c', description: 'ID da empresa no sistema de ponto', required: true, env: 'MW_COMPANY'}),
date: flags.string({char: 'd', description: 'Data relacionada a consulta de horas no padrão YYYY-MM-DD', default: moment().format('YYYY-MM-DD')}),
debug: flags.boolean({char: 'b', description: 'Debug - Exibe mais informações na execução', default: false}),
journeytime: flags.string({char: 'j', description: 'Quantidade de horas a serem trabalhadas por dia', default: '08:00'}),
Expand All @@ -31,19 +33,24 @@ export default class CheckCommand extends Command {
const {flags} = this.parse(CheckCommand)

const options: Partial<WorktimeProviderOptions> = {
userId: flags.user || process.env.WORKTIME_USER,
password: flags.password || process.env.WORKTIME_PASSWORD,
systemId: flags.system || process.env.WORKTIME_SYSTEM,
companyId: flags.company || process.env.WORKTIME_COMPANY,
date: flags.date || process.env.WORKTIME_DATE,
userId: flags.user,
password: flags.password,
systemId: flags.system,
companyId: flags.company,
date: flags.date,
debug: flags.debug,
journeyTime: flags.journeytime || process.env.WORKTIME_JOURNEYTIME,
journeyTime: flags.journeytime,
}

if (!options.userId || !options.password || !options.systemId || !options.companyId) {
this.log('Não foi possível recuperar as credenciais do sistema de ponto')
this.log('Você pode definir as variáveis de ambiente "WORKTIME_USER" e "WORKTIME_PASSWORD"')
this.log('Use worktime -h para informar as credencias via linha de comando.')
this.log('Você pode definir as variáveis de ambiente "MW_USER" e "MW_PASS"')
this.log('Use my-worktime -h para informar as credencias via linha de comando.')
return
}

if(!DATE_REGEXP.test(options.date as string) || !moment().isValid()){
this.error(chalk.red(`Este formato de data é inválido, utilize o padrão ${DATE_FORMAT}`))
return
}

Expand Down Expand Up @@ -100,10 +107,17 @@ export default class CheckCommand extends Command {
return `${markOnConsole}`
})

let workedMinutesUntilNowOnConsole = ClockHelper.humanizeMinutesToClock(worktimeDayResume.workedMinutesUntilNow)

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

console.log('')
console.log(`🔢 Batidas: ${marksToConsole.join(' ')}`)
console.log(`⏸ Horas de pausas: ${ClockHelper.humanizeMinutesToClock(worktimeDayResume.breakMinutes)}`)
console.log(`🆗 Horas registradas: ${ClockHelper.humanizeMinutesToClock(worktimeDayResume.registeredWorkedMinutes)}`)
console.log(`⏺ Horas trabalhadas até este momento: ${ClockHelper.humanizeMinutesToClock(worktimeDayResume.workedMinutesUntilNow)}`)
console.log(`⏺ Horas trabalhadas até este momento: ${workedMinutesUntilNowOnConsole}`)
console.log('')
}
}
4 changes: 4 additions & 0 deletions src/utils/dateFormat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export const DATE_REGEXP = /^\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/

export const DATE_FORMAT = 'YYYY-MM-DD'

0 comments on commit ac7d0fe

Please sign in to comment.