Skip to content

Commit

Permalink
Filter for tasks when fetching running timers
Browse files Browse the repository at this point in the history
  • Loading branch information
eXpl0it3r committed Nov 21, 2024
1 parent 2766aac commit 75d607a
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions Clockify/ClockifyContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,10 @@ public async Task<TimeEntryDtoImpl> GetRunningTimerAsync()
{
return null;
}

if (string.IsNullOrEmpty(_projectName))
if (string.IsNullOrEmpty(_projectName) && string.IsNullOrEmpty(_timerName))
{
return string.IsNullOrEmpty(_timerName)
? timeEntries.Data.FirstOrDefault()
: timeEntries.Data.FirstOrDefault(t => t.Description == _timerName);
return timeEntries.Data.FirstOrDefault(t => string.IsNullOrEmpty(_timerName) || t.Description == _timerName);
}

var project = await FindMatchingProjectAsync(workspace.Id);
Expand All @@ -135,10 +133,12 @@ public async Task<TimeEntryDtoImpl> GetRunningTimerAsync()
{
return null;
}

var task = await FindMatchingTaskAsync(workspace.Id, project.Id, _taskName);

return string.IsNullOrEmpty(_timerName)
? timeEntries.Data.FirstOrDefault(t => t.ProjectId == project.Id)
: timeEntries.Data.FirstOrDefault(t => t.ProjectId == project.Id && t.Description == _timerName);
return timeEntries.Data.FirstOrDefault(t => t.ProjectId == project.Id
&& (string.IsNullOrEmpty(_timerName) || t.Description == _timerName)
&& (string.IsNullOrEmpty(_taskName) || string.IsNullOrEmpty(task) || t.TaskId == task));
}

public async Task UpdateSettings(PluginSettings settings)
Expand Down Expand Up @@ -258,18 +258,25 @@ private async Task<ProjectDtoImpl> FindMatchingProjectAsync(string workspaceId)
return project.Single();
}

private async Task<string> FindOrCreateTaskAsync(string workspaceId, string projectId, string taskName)
private async Task<string> FindMatchingTaskAsync(string workspaceId, string projectId, string taskName)
{
var taskResponse = await _clockifyClient.FindAllTasksAsync(workspaceId, projectId, name: taskName, pageSize: 5000);

if (!taskResponse.IsSuccessful || taskResponse.Data == null)
if (!taskResponse.IsSuccessful || taskResponse.Data == null || !taskResponse.Data.Any())
{
return null;
}

if (taskResponse.Data.Any())
return taskResponse.Data.First().Id;
}

private async Task<string> FindOrCreateTaskAsync(string workspaceId, string projectId, string taskName)
{
var task = await FindMatchingTaskAsync(workspaceId, projectId, taskName);

if (!string.IsNullOrEmpty(task))
{
return taskResponse.Data.First().Id;
return task;
}

var taskRequest = new TaskRequest
Expand Down

0 comments on commit 75d607a

Please sign in to comment.