-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
90 lines (79 loc) · 2.82 KB
/
index.js
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
const moment = require('moment')
const inquirer = require('inquirer')
const loader = require('cli-loader')()
const inquirerAutocompletePrompt = require('inquirer-autocomplete-prompt')
const prettyjson = require('prettyjson')
const getTimeCalcQuestions = require('./time-calc-questions')
const utils = require('./lib/utils')
const options = require('./lib/options')
// Parse command line options and instantiate planmill client
const bootstrap = () => {
try {
const opts = options.parse()
if (opts.help) {
console.log(options.usage())
return null
}
return require('./lib/planmill').instance(opts)
} catch (e) {
console.log('ERROR:', e.message)
console.log(options.usage())
return null
}
}
const main = pm => {
inquirer.registerPrompt('autocomplete', inquirerAutocompletePrompt)
// Start the spinner
loader.start()
Promise.all([pm.get.projectsWithTasks(), pm.get.requestingUser()])
.then(([projectTasks, requestingUser]) => {
// Stop the spinner
loader.stop()
// Set some default values
const defaultFinishTime = moment().format('DD.MM.YYYY HH:mm')
const defaultLunchbreak = 0.5
const questions = getTimeCalcQuestions(projectTasks, defaultFinishTime, defaultLunchbreak)
return inquirer.prompt(questions)
.then(answers => {
const timeDiffInMinutes = moment.duration(moment(answers.finish, 'YYYY-MM-DD[T]HH:mm:ss.SSSZZ')
.diff(moment(answers.start, 'YYYY-MM-DD[T]HH:mm:ss.SSSZZ'))).asMinutes()
const timeDiffInDecimalHours = timeDiffInMinutes / 60 - answers.lunchbreak
const amount = utils.roundToClosestQuarter(timeDiffInDecimalHours) * 60
const project = projectTasks.filter(projTask => projTask.project.name === answers.project)[0]
return Promise.resolve({
answers: Object.assign({}, answers, {
amount,
project,
task: project.tasks.filter(task => task.name === answers.task)[0]
}),
requestingUser
})
})
})
.then(({answers, requestingUser}) => {
const timeReportObj = Object.assign({}, answers, {
project: answers.project.project.id,
task: answers.task.id,
person: requestingUser.id
})
delete timeReportObj.lunchbreak
return pm.post.timereport(timeReportObj)
})
.then(data => pm.get.timereportsById(data.id))
.then(data => {
const hours = utils.roundToClosestQuarter(data.amount / 60)
console.log('Successfully inserted following data:')
console.log(prettyjson.render({
'Hours reported': hours
}))
console.log(prettyjson.render(data))
})
.catch(err => {
console.log(err)
})
}
// Attempt to bootstrap application, run main if successful
const pm = bootstrap()
if (pm !== null) {
main(pm)
}