Skip to content

Commit 130dcc9

Browse files
committed
Fix client name and tags handling
1 parent 8761aa9 commit 130dcc9

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

Clockify/ClockifyService.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,15 @@ public async Task<TimeEntryWithRatesDtoV1> GetRunningTimerAsync()
8585
logger.LogError($"Fetching running timer failed, no project in workspace matching {_settings.ProjectName}");
8686
return null;
8787
}
88+
89+
var entry = timeEntries?.FirstOrDefault(t => t.ProjectId == _project.Id);
90+
logger.LogInfo($"{string.Join(',', entry?.TagIds?.ToString())}");
8891

8992
return timeEntries?.FirstOrDefault(t => t.ProjectId == _project.Id
9093
&& (string.IsNullOrEmpty(_settings.TimerName) || t.Description == _settings.TimerName)
9194
&& (string.IsNullOrEmpty(_settings.TaskName) || string.IsNullOrEmpty(_task?.Id) || t.TaskId == _task.Id)
92-
&& ((t.TagIds is null && _tags is null) || t.TagIds is not null && _tags is not null && t.TagIds.OrderBy(s => s, StringComparer.InvariantCulture)
93-
.SequenceEqual(_tags.OrderBy(s => s, StringComparer.InvariantCulture)))
95+
&& ((t.TagIds is null && _tags is null) || (t.TagIds is not null && _tags is not null && t.TagIds.OrderBy(s => s, StringComparer.InvariantCulture)
96+
.SequenceEqual(_tags.OrderBy(s => s, StringComparer.InvariantCulture))))
9497
&& t.Billable == _settings.Billable);
9598
}
9699
catch (Exception exception) when (exception is ApiException or HttpRequestException)
@@ -108,6 +111,9 @@ public async Task UpdateSettingsAsync(PluginSettings settings)
108111

109112
var cacheInvalidationRequired = SettingsValidator.HasChanged(_settings, settings);
110113

114+
logger.LogInfo($"{settings.ApiKey}/{settings.WorkspaceName}/{settings.TaskName}/{settings.Billable}/{settings.ProjectName}/{settings.ClientName}/{settings.Tags}");
115+
logger.LogInfo($"{_settings.ApiKey}/{_settings.WorkspaceName}/{_settings.TaskName}/{_settings.Billable}/{_settings.ProjectName}/{_settings.ClientName}/{_settings.Tags}");
116+
111117
// Do we need to recreate the client?
112118
if (!IsValid || _settings.ApiKey != settings.ApiKey || _settings.ServerUrl != settings.ServerUrl)
113119
{
@@ -135,7 +141,7 @@ public async Task UpdateSettingsAsync(PluginSettings settings)
135141
cacheInvalidationRequired = true;
136142
}
137143

138-
_settings = settings;
144+
_settings = settings.Clone();
139145

140146
if (cacheInvalidationRequired)
141147
{
@@ -187,15 +193,15 @@ private async Task ReloadCacheAsync()
187193
var workspaces = await _clockifyClient.V1.Workspaces.GetAsync();
188194
_workspace = workspaces?.SingleOrDefault(w => w.Name == _settings.WorkspaceName);
189195

190-
if (_workspace != null)
196+
if (_workspace is not null)
191197
{
192-
_project = await FindMatchingProjectAsync(_workspace.Id, _settings.ProjectName);
198+
_client = await FindMatchingClientAsync(_workspace.Id, _settings.ClientName);
199+
_project = await FindMatchingProjectAsync(_workspace.Id, _settings.ProjectName, _client?.Id);
193200
_tags = await FindMatchingTagsAsync(_workspace.Id, _settings.Tags);
194201

195-
if (_project != null)
202+
if (_project is not null)
196203
{
197204
_task = await FindMatchingTaskAsync(_workspace.Id, _project.Id, _settings.TaskName);
198-
_client = await FindMatchingClientAsync(_workspace.Id, _settings.ClientName);
199205
}
200206
}
201207

@@ -216,7 +222,7 @@ private async Task<TimeEntryWithRatesDtoV1> StopRunningTimerAsync()
216222
}
217223

218224
var runningTimer = await GetRunningTimerAsync();
219-
if (runningTimer == null)
225+
if (runningTimer is null)
220226
{
221227
// No running timer
222228
return null;
@@ -246,9 +252,9 @@ private async Task<TimeEntryWithRatesDtoV1> StopRunningTimerAsync()
246252
return runningTimer;
247253
}
248254

249-
private async Task<ProjectDtoV1> FindMatchingProjectAsync(string workspaceId, string projectName)
255+
private async Task<ProjectDtoV1> FindMatchingProjectAsync(string workspaceId, string projectName, string clientId = null)
250256
{
251-
if (string.IsNullOrEmpty(projectName))
257+
if (string.IsNullOrEmpty(workspaceId) || string.IsNullOrEmpty(projectName))
252258
{
253259
return null;
254260
}
@@ -262,9 +268,9 @@ private async Task<ProjectDtoV1> FindMatchingProjectAsync(string workspaceId, st
262268
q.QueryParameters.StrictNameSearch = true;
263269
q.QueryParameters.PageSize = MaxPageSize;
264270

265-
if (_client is not null)
271+
if (clientId is not null)
266272
{
267-
q.QueryParameters.Clients = [_client.Id];
273+
q.QueryParameters.Clients = [clientId];
268274
}
269275
});
270276

@@ -292,7 +298,7 @@ private async Task<ProjectDtoV1> FindMatchingProjectAsync(string workspaceId, st
292298

293299
private async Task<ClientWithCurrencyDtoV1> FindMatchingClientAsync(string workspaceId, string clientName)
294300
{
295-
if (string.IsNullOrEmpty(clientName))
301+
if (string.IsNullOrEmpty(workspaceId) || string.IsNullOrEmpty(clientName))
296302
{
297303
return null;
298304
}
@@ -317,7 +323,7 @@ private async Task<ClientWithCurrencyDtoV1> FindMatchingClientAsync(string works
317323

318324
private async Task<TaskDtoV1> FindMatchingTaskAsync(string workspaceId, string projectId, string taskName)
319325
{
320-
if (string.IsNullOrEmpty(taskName))
326+
if (string.IsNullOrEmpty(workspaceId) || string.IsNullOrEmpty(projectId) || string.IsNullOrEmpty(taskName))
321327
{
322328
return null;
323329
}
@@ -343,7 +349,7 @@ private async Task<TaskDtoV1> FindMatchingTaskAsync(string workspaceId, string p
343349

344350
private async Task<string> FindOrCreateTaskAsync(string workspaceId, string projectId, string taskName)
345351
{
346-
if (string.IsNullOrEmpty(taskName))
352+
if (string.IsNullOrEmpty(workspaceId) || string.IsNullOrEmpty(projectId) || string.IsNullOrEmpty(taskName))
347353
{
348354
return null;
349355
}
@@ -374,7 +380,7 @@ private async Task<string> FindOrCreateTaskAsync(string workspaceId, string proj
374380

375381
private async Task<List<string>> FindMatchingTagsAsync(string workspaceId, string tags)
376382
{
377-
if (string.IsNullOrEmpty(tags))
383+
if (string.IsNullOrEmpty(workspaceId) || string.IsNullOrEmpty(tags))
378384
{
379385
return [];
380386
}
@@ -395,7 +401,7 @@ private async Task<List<string>> FindMatchingTagsAsync(string workspaceId, strin
395401
var tagsOnWorkspace = await _clockifyClient.V1.Workspaces[workspaceId].Tags
396402
.GetAsync(q => q.QueryParameters.PageSize = MaxPageSize);
397403

398-
return tagsOnWorkspace == null ? [] : tagsOnWorkspace.Where(t => tagList.Contains(t.Name)).Select(t => t.Id).ToList();
404+
return tagsOnWorkspace is null ? [] : tagsOnWorkspace.Where(t => tagList.Contains(t.Name)).Select(t => t.Id).ToList();
399405
}
400406
catch (Exception exception) when (exception is ApiException or HttpRequestException)
401407
{

Clockify/PluginSettings.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ namespace Clockify;
44

55
public class PluginSettings
66
{
7+
public PluginSettings Clone()
8+
{
9+
return new PluginSettings
10+
{
11+
ApiKey = ApiKey,
12+
WorkspaceName = WorkspaceName,
13+
ProjectName = ProjectName,
14+
TaskName = TaskName,
15+
TimerName = TimerName,
16+
Tags = Tags,
17+
ClientName = ClientName,
18+
Billable = Billable,
19+
TitleFormat = TitleFormat,
20+
RefreshRate = RefreshRate,
21+
ServerUrl = ServerUrl
22+
};
23+
}
24+
725
[JsonProperty(PropertyName = "apiKey")]
826
public string ApiKey { get; set; } = string.Empty;
927

0 commit comments

Comments
 (0)