Exemple #1
0
        public void Run()
        {
            using (var togglRepo = new ReportsRepository(_togglClient))
                using (var togglUserRepo = new UserRepository(_togglClient))
                    using (var redmineRepo = new TimeEntriesRepository(_redmineClient))
                        using (var appState = new AppStateRepository(_dbContext))
                        {
                            int counter = 0;

                            var state = appState.GetState();

                            var userInfo = togglUserRepo.GetInfo().GetAwaiter().GetResult();

                            var reports = togglRepo.GetAll(state.LastSynchronized, userId: userInfo.Id).GetAwaiter().GetResult();
                            _logger.LogInformation($"Found {reports.Count} reports since last synced date: {state.LastSynchronized}.");

                            foreach (var report in reports)
                            {
                                try
                                {
                                    var timeInHours = report.GetDurationInHours();
                                    var issueId     = ExtractIssueId(report);

                                    if (report.End < state.LastSynchronized)
                                    {
                                        _logger.LogInformation($"{counter++} Skipping {timeInHours} hours for issue {issueId}, already imported...");
                                        continue;
                                    }

                                    _logger.LogInformation($"{counter++} Logging {timeInHours} hours on issue {issueId}...");
                                    redmineRepo.Add(new TimeEntry()
                                    {
                                        Issue = new Issue {
                                            Id = issueId
                                        },
                                        Activity = new Activity {
                                            Id = 9
                                        },
                                        Comments = $"toggl - {report.Description}",
                                        SpentOn  = report.Start.UtcDateTime,
                                        Hours    = timeInHours,
                                    }).GetAwaiter().GetResult();
                                }
                                catch (KeyNotFoundException ex)
                                {
                                    _logger.LogInformation($"{counter++} Skipping {report.Id}: {ex.Message}");
                                    continue;
                                }
                                catch (Exception ex)
                                {
                                    _logger.LogInformation($"Unexpected error on report {report.Id}: {ex.Message}");
                                }
                            }

                            _logger.LogInformation($"Persisting state to state db...");

                            state.LastSynchronized = DateTimeOffset.UtcNow;
                            appState.SetState(state);

                            _logger.LogInformation($"All done for today!");
                        }
        }