@@ -16,6 +16,8 @@ namespace FitSyncHub.Functions.Functions;
1616
1717public class XertWorkoutToIntervalsICUExporterHttpTriggerFunction
1818{
19+ private const string IntervalsIcuEventTagXert = "Xert" ;
20+
1921 private readonly IXertHttpClient _xertHttpClient ;
2022 private readonly IntervalsIcuHttpClient _intervalsIcuHttpClient ;
2123 private readonly string _athleteId ;
@@ -42,6 +44,8 @@ public async Task<ActionResult> Run(
4244
4345 _logger . LogInformation ( "Starting Xert to Intervals.icu export function" ) ;
4446
47+ var today = DateTime . Now . Date ;
48+
4549 var ti = await _xertHttpClient . GetTrainingInfo ( XertWorkoutFormat . ZWO , cancellationToken ) ;
4650 _logger . LogInformation ( "Retrieved training info value from Xert" ) ;
4751
@@ -54,14 +58,28 @@ public async Task<ActionResult> Run(
5458 var zwo = await _xertHttpClient . GetDownloadWorkout ( workoutOfTheDayUrl , cancellationToken ) ;
5559 _logger . LogInformation ( "Downloaded workout zwo from Xert" ) ;
5660
61+ var intervalsIcuEvents = await _intervalsIcuHttpClient . ListEvents ( _athleteId ,
62+ new ListEventsQueryParams ( DateOnly . FromDateTime ( today ) , DateOnly . FromDateTime ( today ) ) , cancellationToken ) ;
63+ _logger . LogInformation ( "Retrieved {Count} existing Intervals.icu events" , intervalsIcuEvents . Count ) ;
64+
65+ var intervalsIcuEventsMapping = intervalsIcuEvents
66+ . Where ( x => x . Tags ? . Contains ( IntervalsIcuEventTagXert ) == true && x . PairedActivityId == null )
67+ . ToDictionary ( x => x . Name ) ;
68+
69+ if ( ti . WorkoutOfTheDay . Name is { } && intervalsIcuEventsMapping . ContainsKey ( ti . WorkoutOfTheDay . Name ) )
70+ {
71+ return new OkObjectResult ( "Event already exists." ) ;
72+ }
73+
5774 var base64EncodedWorkoutStructure = Convert . ToBase64String ( Encoding . UTF8 . GetBytes ( zwo ) ) ;
5875
5976 var createdEvent = await _intervalsIcuHttpClient . CreateEvent ( _athleteId , new CreateEventFromFileRequest
6077 {
6178 Category = EventCategory . Workout ,
6279 Type = EventType . Ride ,
63- StartDateLocal = DateTime . Now . Date ,
64- FileContentsBase64 = base64EncodedWorkoutStructure
80+ StartDateLocal = today ,
81+ FileContentsBase64 = base64EncodedWorkoutStructure ,
82+ Tags = [ IntervalsIcuEventTagXert ] ,
6583 } , default ) ;
6684
6785 var intervalsIcuFutureGarminEventsOverview = ResponseOverviewHelper . IntervalsIcuEventsResponseOverview ( createdEvent ) ;
0 commit comments