-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTimeEntryStoreService.cs
More file actions
55 lines (49 loc) · 2.24 KB
/
TimeEntryStoreService.cs
File metadata and controls
55 lines (49 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bot.Clockify.Client;
using Bot.Clockify.Models;
using Bot.Common;
using Microsoft.Extensions.Configuration;
namespace Bot.Clockify.Fill
{
public class TimeEntryStoreService : ITimeEntryStoreService
{
private readonly IClockifyService _clockifyService;
private readonly string _tagName;
private readonly IDateTimeProvider _dateTimeProvider;
public TimeEntryStoreService(IClockifyService clockifyService, IConfiguration configuration,
IDateTimeProvider dateTimeProvider)
{
_clockifyService = clockifyService;
_dateTimeProvider = dateTimeProvider;
_tagName = configuration["Tag"];
}
public async Task<double> AddTimeEntries(string clockifyToken, ProjectDo project, TaskDo? task, DateTime start,
DateTime end, TimeZoneInfo timeZone)
{
string? tagId = await _clockifyService.GetTagAsync(clockifyToken, project.WorkspaceId, _tagName);
string userId = (await _clockifyService.GetCurrentUserAsync(clockifyToken)).Id;
string workspaceId = project.WorkspaceId;
var timeEntry = new TimeEntryReq
(
project.Id,
start,
taskId: task?.Id,
billable: project.Billable,
end: end,
tagIds: tagId != null ? new List<string> { tagId } : null
);
await _clockifyService.AddTimeEntryAsync(clockifyToken, workspaceId, timeEntry);
// TODO Extract total hours calculation into another method
var userToday = TimeZoneInfo.ConvertTime(_dateTimeProvider.DateTimeUtcNow(), timeZone).Date;
var todayEntries = await _clockifyService.GetHydratedTimeEntriesAsync(clockifyToken, workspaceId, userId,
new DateTimeOffset(userToday), new DateTimeOffset(userToday.AddDays(1)));
return todayEntries
.Where(entry => entry.TimeInterval.Start.HasValue && entry.TimeInterval.End.HasValue)
.Select(entry => (entry.TimeInterval.End!.Value - entry.TimeInterval.Start!.Value).TotalHours)
.Sum();
}
}
}