-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add time entry, include start with create #165
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
093c78b
Add time entry, incl start with create
benthayer bbf2bb4
Clean up code
benthayer fca55f5
Fixed bug displaying time of current time entry
benthayer 0956c93
minor tweak to debug
beauraines 2548f1f
works with both positional and option specified values
beauraines 3f9cd50
Removes unused variables and import
beauraines File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* eslint-disable no-unused-expressions */ | ||
import Client from '../client.js' | ||
import { defaultWorkspaceId, getProjectByName, getProjectById, appName, displayTimeEntry, parseTime } from '../utils.js' | ||
import dayjs from 'dayjs' | ||
import debugClient from 'debug' | ||
import utc from 'dayjs/plugin/utc.js' | ||
import timezone from 'dayjs/plugin/timezone.js' | ||
dayjs.extend(utc) | ||
dayjs.extend(timezone) | ||
|
||
const debug = debugClient('toggl-cli-add'); | ||
|
||
export const command = 'add [startTime] [endTime] [description]' | ||
export const desc = 'Create a time entry. Time must be parsable by dayjs, e.g. 4:50PM or \'12:00 AM\'.' | ||
|
||
export const builder = { | ||
d: { alias: ['description'], describe: 'Time entry name', type: 'string:', demandOption: true}, | ||
p: { alias: ['projectId', 'project'], describe: 'The case insensitive project name or project id.', type: 'string', demandOption: false }, | ||
s: { alias: ['start', 'startTime'], describe: 'The start time for the task, e.g. 13:00 12:45AM.', type: 'string', demandOption: false }, | ||
e: { alias: ['end', 'endTime'], describe: 'The end time for the task, e.g. 13:00 12:45AM.', type: 'string', demandOption: false } | ||
} | ||
|
||
export const handler = async function (argv) { | ||
const client = await Client() | ||
const params = {} | ||
|
||
params.workspace_id = +defaultWorkspaceId | ||
let project | ||
if (argv.projectId) { | ||
if (isNaN(argv.projectId)) { | ||
project = await getProjectByName(params.workspace_id, argv.projectId) | ||
} else { | ||
project = await getProjectById(params.workspace_id, argv.projectId) | ||
} | ||
} | ||
|
||
let startTime, endTime | ||
if (dayjs(argv.startTime).isValid()) { | ||
startTime = argv.startTime | ||
} else { | ||
// Parse the time and set it based upon the current time | ||
startTime = parseTime(argv.startTime) | ||
} | ||
|
||
if (dayjs(argv.endTime).isValid()) { | ||
endTime = argv.endTime | ||
} else { | ||
// Parse the time and set it based upon the current time | ||
endTime = parseTime(argv.endTime) | ||
} | ||
|
||
params.created_with = appName | ||
project ? params.project_id = +project.id : undefined | ||
startTime ? params.start = startTime.toISOString() : undefined | ||
endTime ? params.stop = endTime.toISOString() : undefined | ||
if (startTime || endTime) { | ||
const startTimeUnix = startTime.unix() | ||
const endTimeUnix = endTime.unix() | ||
let duration = endTimeUnix - startTimeUnix | ||
duration = endTime ? duration : startTimeUnix * -1 | ||
params.duration = duration | ||
} | ||
argv.description ? params.description = argv.description : undefined | ||
debug(params) | ||
const timeEntry = await client.timeEntries.create(params) | ||
await displayTimeEntry(timeEntry) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,7 @@ export const createTimeEntry = async function (params) { | |
description: params.description, | ||
workspace_id: +params.workspaceId, | ||
project_id: +params.projectId, | ||
start: dayjs().toISOString(), | ||
start: params.start ? params.start : dayjs().toISOString(), | ||
duration: -1 * dayjs().unix(), | ||
created_with: appName, | ||
at: dayjs().toISOString() | ||
|
@@ -113,11 +113,15 @@ export const displayTimeEntry = async function (timeEntry) { | |
console.info(`${chalk.blueBright(timeEntry.description ? timeEntry.description : 'no description')} ${chalk.yellow('#'+timeEntry.id)}`) | ||
console.info(`Billable: ${chalk.gray(timeEntry.billable)}`) | ||
|
||
// TODO this should be abstracted for reuse | ||
const startTime = dayjs.unix(timeEntry.duration * -1) | ||
const duration = dayjs().diff(startTime, 's') | ||
const durationFormatted = dayjs.duration(duration * 1000).format('H[h] m[m]') | ||
let stopTime; | ||
if (timeEntry.stop == null) { | ||
stopTime = dayjs() | ||
} else { | ||
stopTime = dayjs(timeEntry.stop) | ||
} | ||
|
||
const duration = stopTime.diff(dayjs(timeEntry.start)) | ||
const durationFormatted = dayjs.duration(duration).format('H[h] m[m]') | ||
console.info(`Duration: ${chalk.green(durationFormatted)}`) | ||
|
||
const projects = await getProjects(timeEntry.wid) | ||
|
@@ -138,3 +142,23 @@ export const displayTimeEntry = async function (timeEntry) { | |
console.info(`Workspace: ${workspace.name} (#${timeEntry.wid})`) | ||
} | ||
} | ||
|
||
/** | ||
* Parses a timelike string into a dayjs object of the current date and that time | ||
* @param {string} timeString timelike string e.g. 4:50PM '12:00 AM' etc. | ||
* @returns {object} dayjs object | ||
*/ | ||
export function parseTime (timeString) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for moving this here, this really should have been available for reuse. |
||
let h, m | ||
// Assumes time in format 4:50 PM | ||
const time = timeString.split(':', 2) | ||
h = time[0] | ||
m = time[1].match(/[0-9]+/)[0] | ||
if (timeString.match(/PM/i) && h <= 12) { | ||
// + in front of string converts to a number, cool! | ||
h = +h + 12 | ||
} else if (h == 12) { | ||
h = 0 | ||
} | ||
return dayjs().hour(h).minute(m).second(0).millisecond(0) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a
createTimeEntry()
in the utils, but it doesn't support creating a time entry with an endTime. I'll open a new issue to extend that function and use it here too. I see that you started with theedit
command which does its own interaction with the client. I'm not very good about repeating myself or abstracting methods.