Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,6 @@ paket-files/

# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
*.pyc

appsettings.json
9 changes: 8 additions & 1 deletion CalendarSync.Console/CalendarSync.Console.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -15,10 +15,17 @@
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.Graph" Version="4.47.0" />
<PackageReference Include="Microsoft.PowerPlatform.Dataverse.Client" Version="1.0.26" />
<PackageReference Include="TaskScheduler" Version="2.10.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CalendarSync\CalendarSync.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
76 changes: 57 additions & 19 deletions CalendarSync.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,70 @@
using CalendarSync;
using CalendarSync.Console;
using CommandLine;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Win32.TaskScheduler;

const uint DefaultDaysToSync = 7;
const string DailyTaskName = "SyncCalendar";
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();

Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(async o =>
{
var host = Host.CreateDefaultBuilder().Build();
var logger = host.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Logging established");
var host = Host.CreateDefaultBuilder().Build();
var logger = host.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation(@"
BE NOT AFRAID
Comment thread
johnyenter-briars marked this conversation as resolved.
Your calendars will begin to sync
DO NOT close this window until you see the final logging message.
");

var primaryAccountRefreshToken = o.PrimaryAccountRefreshToken;
var secondaryAccountRefreshToken = o.SecondaryAccountRefreshToken;
var clientId = o.ClientId;
var orgConnectionString = o.OrgConnectionString;
var primaryAccountRefreshToken = configuration["PrimaryAccountRefreshToken"];
var primaryAccountSubjectPrefix = configuration["PrimaryAccountSubjectPrefix"];
var secondaryAccountRefreshToken = configuration["SecondaryAccountRefreshToken "];
var secondaryAccountSubjectPrefix = configuration["SecondaryAccountSubjectPrefix"];
var clientId = configuration["ClientId"];
var orgConnectionString = configuration["OrgConnectionString "];
var daysToSync = uint.Parse(configuration["DaysToSync"]);

var daysToSync = o.DaysToSync != 0 ? o.DaysToSync : DefaultDaysToSync;
try
{
using TaskService ts = new TaskService();
var calendarSyncTask = ts.GetTask(DailyTaskName);
if (calendarSyncTask == null)
{
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Every day at 9 am sync calendars";

var source = new SecondaryAccToPrimaryAccProfile(secondaryAccountRefreshToken, o.SecondaryAccountSubjectPrefix);
var dest = new PrimaryAccToSecondaryAccProfile(primaryAccountRefreshToken, o.PrimaryAccountSubjectPrefix);
var service = new CalendarSyncService(dest, source, logger, clientId, orgConnectionString);
var startTime = DateTime.UtcNow;
var endTime = startTime.AddDays(daysToSync);
DailyTrigger trigger = new()
{
StartBoundary = DateTime.Today + new TimeSpan(10, 45, 0),
DaysInterval = 1
};
td.Triggers.Add(trigger);

await service.SyncRangeBidirectionalAsync(startTime.ToString("O"), endTime.ToString("O"));
});
var dir = $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}/CalendarSync";

td.Actions.Add(new ExecAction($"{dir}/CalendarSync.Console.exe", dir, null));

ts.RootFolder.RegisterTaskDefinition(DailyTaskName, td);
}

var source = new SecondaryAccToPrimaryAccProfile(secondaryAccountRefreshToken, secondaryAccountSubjectPrefix);
var dest = new PrimaryAccToSecondaryAccProfile(primaryAccountRefreshToken, primaryAccountSubjectPrefix);
var service = new CalendarSyncService(dest, source, logger, clientId, orgConnectionString);
var startTime = DateTime.UtcNow;
var endTime = startTime.AddDays(daysToSync);

await service.SyncRangeBidirectionalAsync(startTime.ToString("O"), endTime.ToString("O"));

logger.LogInformation(@"
Calendar syncing complete!
Go forth about your day and be productive.
");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net6.0\publish\win-x86\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
<TargetFramework>net6.0</TargetFramework>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>true</PublishSingleFile>
<PublishReadyToRun>false</PublishReadyToRun>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion CalendarSync.Console/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"CalendarSync.Console": {
"commandName": "Project",
"commandLineArgs": "--secondaryAccountRefreshToken idk --primaryAccountRefreshToken test --primaryAccountSubjectPrefix [Root16] --secondaryAccountSubjectPrefix [RSM] --clientId lezfooo --orgConnectionString asgsgs --daysToSync I2"
"commandLineArgs": "--secondaryAccountRefreshToken idk --primaryAccountRefreshToken test --primaryAccountSubjectPrefix [primacct] --secondaryAccountSubjectPrefix [secacct] --clientId lezfooo --orgConnectionString asgsgs --daysToSync 2"
}
}
}