# In Progress Work Timer

This workflow lets your development team track the time spent working on an issue and log work items automatically. The behavior is similar to the [Stopwatch-style Work Timer](Workflow-Standalone-Work-Timer.html). Instead of using the value for an independent Timer field to start and stop the timer, this workflow uses default values for the State field.

Use this workflow when you want to track time spent working on an issue by measuring the amount of time an issue spends in the In Progress state.

> **Note: Synchronization with Time Tracking**
> Workflow timers are synchronized with the global time tracking settings for your server. Timers only record time on workdays and do not exceed the defined working period per day.
>
>
>
> The timer pauses automatically as soon as the tracked time reaches the maximum working period for a single day. If the timer isn't stopped (manually or programmatically) before the start of the next working day, it resumes tracking from 09:00. The timer is also paused automatically at the start of any non-working day.
>
>
>
> The dates and times are based on the local time zone for the current user.
>
>
>
> For best results, we recommend that you always stop the timer when you finish your work activity. If you find that a workflow has recorded an inaccurate amount of spent time, update the work item manually.

| Name | @jetbrains/youtrack-workflow-work-timer |
| --- | --- |
| Previous Title | In Progress Work Timer |
| Auto-attached | no |
| Modules |    [Start timer when issue is in progress](#start-timer) (on-change rule)       [Stop timer when issue state is updated](#stop-timer) (on-change rule)     |

Procedure: To enable this workflow:

1. Enable and configure time tracking for your project. For instructions, see [Enable and Configure Time Tracking](https://www.jetbrains.com.cn/en-us/help/youtrack/cloud/Enable-and-Configure-Time-Tracking.html).

2. Add a field that stores a `date and time` type with the name Timer time to your project.

3. Attach the In Progress Work Timer workflow to your project.

For this workflow to function as written, your project must use a State field that includes the In Progress value. If you use different fields and values to track the status of issues in your project, modify the workflow accordingly.

## Use Case

When a developer changes the state of an issue to In Progress, the workflow enters the current time in the Timer time field. When the developer changes the issue state from In Progress to another state, a work item with the time spent between the Timer time and the current time is added to issue on behalf of the user who updated the issue.

## Modules

This workflow includes two modules.

### Start timer when issue is in progress

When the state of an issue is changed to In Progress, this rule sets the value of the  Timer time  to the current time.

```JAVASCRIPT
const entities = require('@jetbrains/youtrack-scripting-api/entities');
const workflow = require('@jetbrains/youtrack-scripting-api/workflow');

exports.rule = entities.Issue.onChange({
    title: 'Start timer when issue is in progress',
    guard: (ctx) => {
        return ctx.issue.fields.becomes(ctx.State, ctx.State.InProgress);
    },
    action: (ctx) => {
        ctx.issue.fields.TimerTime = Date.now();
        workflow.message('The timer is started.');
    },
    requirements: {
        TimerTime: {
            type: entities.Field.dateTimeType,
            name: 'Timer time'
        },
        State: {
            type: entities.State.fieldType,
            InProgress: {
                name: 'In Progress'
            }
        }
    }
});
```

### Stop timer when issue state is updated

When the developer changes the issue state from In Progress to another state, this rule calculates the time spent working on the issue and adds a work item to the issue. The work item includes a message that is defined by this rule and the calculated time spent.

```JAVASCRIPT
const entities = require('@jetbrains/youtrack-scripting-api/entities');
const workflow = require('@jetbrains/youtrack-scripting-api/workflow');

exports.rule = entities.Issue.onChange({
    title: 'Stop timer when issue state is updated',
    guard: (ctx) => {
        const issueFields = ctx.issue.fields;
        return issueFields.was(ctx.State, ctx.State.InProgress) && issueFields.TimerTime;
    },
    action: (ctx) => {
        const issue = ctx.issue;
        let duration = issue.project.intervalToWorkingMinutes(issue.fields.TimerTime, Date.now());
        if (duration) {
            const newWorkItem = {
                description: 'The work item automatically added by the timer.',
                date: Date.now(),
                author: ctx.currentUser,
                duration: duration
            };
            issue.addWorkItem(newWorkItem);
            workflow.message('Work time added');
        }
    },
    requirements: {
        TimerTime: {
            type: entities.Field.dateTimeType,
            name: 'Timer time'
        },
        State: {
            type: entities.State.fieldType,
            InProgress: {
                name: 'In Progress'
            }
        }
    }
});
```

> **Warning: Localized Messages**
> The original workflow script provided by YouTrack uses the `workflow.i18n` function. This is an internal function that is only meant to be used to reference localized message strings. To ensure that you can copy and customize this workflow code, we have removed this function from the sample. If you are editing the default workflow and want to customize any message text, we recommend that you remove these functions from your code as well.
>
>
>
> To learn more about localized messages in workflows, see [Localized Workflow Messages](Workflow-Localization.html).

