Ejemplo n.º 1
0
        public void Sync(DateTime from, DateTime to, string fromWorkspaceName, string toWorkspaceName)
        {
            if (TryGetWorkspaceId(fromWorkspaceName, out long sourceWorkspaceId) == false)
            {
                return;
            }
            if (TryGetWorkspaceId(toWorkspaceName, out long destinationWorkspaceId) == false)
            {
                return;
            }
            var sourceEntries      = GetEntries(from, to, sourceWorkspaceId);
            var destinationEntries = GetEntries(from, to, destinationWorkspaceId);

            // clean up all entries
            Print("Cleaninig old entries");
            if (destinationEntries.Any())
            {
                timeEntryService.Delete(destinationEntries.Select(x => x.Id.Value).Distinct().ToArray());
            }

            Print("Starting actual sync");
            foreach (var entry in sourceEntries)
            {
                var found = workspacesClientsProjects.FirstOrDefault(x => x.WorkspaceId == destinationWorkspaceId &&
                                                                     x.ClientName.Equals(entry.ClientName, StringComparison.OrdinalIgnoreCase) &&
                                                                     x.ProjectName.Equals(entry.ProjectName, StringComparison.OrdinalIgnoreCase));

                if (found is null)
                {
                    throw new Exception();
                }

                var timeEntry = new TimeEntry
                {
                    CreatedWith = "toggle2toggle",
                    Description = entry.Description,
                    Duration    = entry.RoundedDuration,
                    ProjectId   = found.ProjectId,
                    Start       = DateTime.ParseExact(entry.Start, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture).ToString("yyyy-MM-ddTHH:mm:sszzz"),
                    Stop        = DateTime.ParseExact(entry.Start, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture).AddSeconds(entry.RoundedDuration.GetValueOrDefault()).ToString("yyyy-MM-ddTHH:mm:sszzz"),
                    IsBillable  = false,
                    TagNames    = entry.TagNames
                };

                Print(entry);
                timeEntryService.Add(timeEntry);
            }

            PrintTotal(sourceEntries);
            Print("Finished actual sync");
        }