diff --git a/Client/Pages/Index.razor.cs b/Client/Pages/Index.razor.cs index 9490291..8ba616f 100644 --- a/Client/Pages/Index.razor.cs +++ b/Client/Pages/Index.razor.cs @@ -115,8 +115,8 @@ namespace ShiftScheduler.Client.Pages _successMessage = string.Empty; StateHasChanged(); - var shiftsWithTransportList = SelectedShiftsWithTransport.Values.ToList(); - var response = await HttpClient.PostAsJsonAsync("api/shift/export_ics", shiftsWithTransportList); + var request = new { Year = SelectedDate.Year, Month = SelectedDate.Month }; + var response = await HttpClient.PostAsJsonAsync("api/shift/export_ics", request); if (response.IsSuccessStatusCode) { @@ -148,8 +148,8 @@ namespace ShiftScheduler.Client.Pages _successMessage = string.Empty; StateHasChanged(); - var shiftsWithTransportList = SelectedShiftsWithTransport.Values.ToList(); - var response = await HttpClient.PostAsJsonAsync("api/shift/export_pdf", shiftsWithTransportList); + var request = new { Year = SelectedDate.Year, Month = SelectedDate.Month }; + var response = await HttpClient.PostAsJsonAsync("api/shift/export_pdf", request); if (response.IsSuccessStatusCode) { @@ -232,11 +232,11 @@ namespace ShiftScheduler.Client.Pages _isSyncing = true; StateHasChanged(); - var shiftsWithTransportList = SelectedShiftsWithTransport.Values.ToList(); var request = new { CalendarId = calendarId, - ShiftsWithTransport = shiftsWithTransportList + Year = SelectedDate.Year, + Month = SelectedDate.Month }; var response = await HttpClient.PostAsJsonAsync("api/shift/sync_to_google_calendar", request); diff --git a/Server/Controllers/ShiftController.cs b/Server/Controllers/ShiftController.cs index 1b4f528..b22ad76 100644 --- a/Server/Controllers/ShiftController.cs +++ b/Server/Controllers/ShiftController.cs @@ -97,15 +97,17 @@ namespace ShiftScheduler.Server.Controllers } [HttpPost("export_ics")] - public IActionResult ExportIcsWithTransport([FromBody] List shiftsWithTransport) + public async Task ExportIcs([FromBody] ExportRequest request) { + var shiftsWithTransport = await BuildShiftsWithTransportAsync(request.Year, request.Month); var ics = _icsService.GenerateIcs(shiftsWithTransport); return File(System.Text.Encoding.UTF8.GetBytes(ics), "text/calendar", "schedule.ics"); } [HttpPost("export_pdf")] - public IActionResult ExportPdfWithTransport([FromBody] List shiftsWithTransport) + public async Task ExportPdf([FromBody] ExportRequest request) { + var shiftsWithTransport = await BuildShiftsWithTransportAsync(request.Year, request.Month); var pdf = _pdfExportService.GenerateMonthlySchedulePdf(shiftsWithTransport); return File(pdf, "application/pdf", "schedule.pdf"); } @@ -171,7 +173,8 @@ namespace ShiftScheduler.Server.Controllers { try { - await _googleCalendarService.SyncShiftsToCalendarAsync(request.CalendarId, request.ShiftsWithTransport); + var shiftsWithTransport = await BuildShiftsWithTransportAsync(request.Year, request.Month); + await _googleCalendarService.SyncShiftsToCalendarAsync(request.CalendarId, shiftsWithTransport); return Ok(new { message = "Shifts synced to Google Calendar successfully!" }); } catch (Exception ex) @@ -179,6 +182,61 @@ namespace ShiftScheduler.Server.Controllers return StatusCode(500, $"Error syncing to Google Calendar: {ex.Message}"); } } + + private async Task> BuildShiftsWithTransportAsync(int year, int month) + { + var schedule = await _configurationService.LoadScheduleAsync(year, month); + var shiftsWithTransport = new List(); + var transportConfig = _configurationService.GetTransportConfiguration(); + + foreach (var kvp in schedule) + { + var date = kvp.Key; + var shiftName = kvp.Value; + + var shift = _configurationService.GetShifts().FirstOrDefault(s => s.Name == shiftName); + if (shift == null) + continue; + + var shiftTimes = _configurationService.ParseShiftTimes(date, shift); + TransportConnection? morningTransport = null; + TransportConnection? afternoonTransport = null; + + // Get transport for morning shift if it has morning time + if (!string.IsNullOrEmpty(shift.MorningTime) && shiftTimes.MorningStart.HasValue) + { + morningTransport = await _transportService.GetConnectionAsync(shiftTimes.MorningStart.Value); + } + + // Get transport for afternoon shift if it has afternoon time + if (!string.IsNullOrEmpty(shift.AfternoonTime)) + { + var shouldLoadAfternoonTransport = true; + + // If both morning and afternoon shifts exist, check break duration + if (!string.IsNullOrEmpty(shift.MorningTime) && shiftTimes.MorningEnd.HasValue && shiftTimes.AfternoonStart.HasValue) + { + var breakDurationMinutes = (shiftTimes.AfternoonStart.Value - shiftTimes.MorningEnd.Value).TotalMinutes; + shouldLoadAfternoonTransport = breakDurationMinutes >= transportConfig.MinBreakMinutes; + } + + if (shouldLoadAfternoonTransport && shiftTimes.AfternoonStart.HasValue) + { + afternoonTransport = await _transportService.GetConnectionAsync(shiftTimes.AfternoonStart.Value); + } + } + + shiftsWithTransport.Add(new ShiftWithTransport + { + Date = date, + Shift = shift, + MorningTransport = morningTransport, + AfternoonTransport = afternoonTransport + }); + } + + return shiftsWithTransport; + } } public class ShiftTransportRequest @@ -194,9 +252,16 @@ namespace ShiftScheduler.Server.Controllers public Dictionary Schedule { get; set; } = new(); } + public class ExportRequest + { + public int Year { get; set; } + public int Month { get; set; } + } + public class SyncToGoogleCalendarRequest { public string CalendarId { get; set; } = string.Empty; - public List ShiftsWithTransport { get; set; } = []; + public int Year { get; set; } + public int Month { get; set; } } }