|
| 1 | +/* eslint-disable no-unused-expressions */ |
| 2 | +import Client from '../client.js' |
| 3 | +import { defaultWorkspaceId, getProjectByName, getProjectById, appName, displayTimeEntry, parseTime } from '../utils.js' |
| 4 | +import dayjs from 'dayjs' |
| 5 | +import debugClient from 'debug' |
| 6 | +import utc from 'dayjs/plugin/utc.js' |
| 7 | +import timezone from 'dayjs/plugin/timezone.js' |
| 8 | +dayjs.extend(utc) |
| 9 | +dayjs.extend(timezone) |
| 10 | + |
| 11 | +const debug = debugClient('toggl-cli-add'); |
| 12 | + |
| 13 | +export const command = 'add [startTime] [endTime] [description]' |
| 14 | +export const desc = 'Create a time entry. Time must be parsable by dayjs, e.g. 4:50PM or \'12:00 AM\'.' |
| 15 | + |
| 16 | +export const builder = { |
| 17 | + d: { alias: ['description'], describe: 'Time entry name', type: 'string:', demandOption: true}, |
| 18 | + p: { alias: ['projectId', 'project'], describe: 'The case insensitive project name or project id.', type: 'string', demandOption: false }, |
| 19 | + s: { alias: ['start', 'startTime'], describe: 'The start time for the task, e.g. 13:00 12:45AM.', type: 'string', demandOption: false }, |
| 20 | + e: { alias: ['end', 'endTime'], describe: 'The end time for the task, e.g. 13:00 12:45AM.', type: 'string', demandOption: false } |
| 21 | +} |
| 22 | + |
| 23 | +export const handler = async function (argv) { |
| 24 | + const client = await Client() |
| 25 | + const params = {} |
| 26 | + |
| 27 | + params.workspace_id = +defaultWorkspaceId |
| 28 | + let project |
| 29 | + if (argv.projectId) { |
| 30 | + if (isNaN(argv.projectId)) { |
| 31 | + project = await getProjectByName(params.workspace_id, argv.projectId) |
| 32 | + } else { |
| 33 | + project = await getProjectById(params.workspace_id, argv.projectId) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + let startTime, endTime |
| 38 | + if (dayjs(argv.startTime).isValid()) { |
| 39 | + startTime = argv.startTime |
| 40 | + } else { |
| 41 | + // Parse the time and set it based upon the current time |
| 42 | + startTime = parseTime(argv.startTime) |
| 43 | + } |
| 44 | + |
| 45 | + if (dayjs(argv.endTime).isValid()) { |
| 46 | + endTime = argv.endTime |
| 47 | + } else { |
| 48 | + // Parse the time and set it based upon the current time |
| 49 | + endTime = parseTime(argv.endTime) |
| 50 | + } |
| 51 | + |
| 52 | + params.created_with = appName |
| 53 | + project ? params.project_id = +project.id : undefined |
| 54 | + startTime ? params.start = startTime.toISOString() : undefined |
| 55 | + endTime ? params.stop = endTime.toISOString() : undefined |
| 56 | + if (startTime || endTime) { |
| 57 | + const startTimeUnix = startTime.unix() |
| 58 | + const endTimeUnix = endTime.unix() |
| 59 | + let duration = endTimeUnix - startTimeUnix |
| 60 | + duration = endTime ? duration : startTimeUnix * -1 |
| 61 | + params.duration = duration |
| 62 | + } |
| 63 | + argv.description ? params.description = argv.description : undefined |
| 64 | + debug(params) |
| 65 | + const timeEntry = await client.timeEntries.create(params) |
| 66 | + await displayTimeEntry(timeEntry) |
| 67 | +} |
| 68 | + |
0 commit comments