feat: add provider-agnostic calendar endpoints, register NextcloudCalendarService

This commit is contained in:
Claudio Schaad 2026-04-10 22:55:42 +02:00
parent 874942e25a
commit 3a42a01ebb
2 changed files with 48 additions and 63 deletions

View file

@ -15,19 +15,22 @@ namespace ShiftScheduler.Server.Controllers
private readonly ITransportService _transportService; private readonly ITransportService _transportService;
private readonly IConfigurationService _configurationService; private readonly IConfigurationService _configurationService;
private readonly IGoogleCalendarService _googleCalendarService; private readonly IGoogleCalendarService _googleCalendarService;
private readonly INextcloudCalendarService _nextcloudCalendarService;
public ShiftController( public ShiftController(
IcsExportService icsService, IcsExportService icsService,
PdfExportService pdfExportService, PdfExportService pdfExportService,
ITransportService transportService, ITransportService transportService,
IConfigurationService configurationService, IConfigurationService configurationService,
IGoogleCalendarService googleCalendarService) IGoogleCalendarService googleCalendarService,
INextcloudCalendarService nextcloudCalendarService)
{ {
_icsService = icsService; _icsService = icsService;
_pdfExportService = pdfExportService; _pdfExportService = pdfExportService;
_transportService = transportService; _transportService = transportService;
_configurationService = configurationService; _configurationService = configurationService;
_googleCalendarService = googleCalendarService; _googleCalendarService = googleCalendarService;
_nextcloudCalendarService = nextcloudCalendarService;
} }
[HttpGet("shifts")] [HttpGet("shifts")]
@ -41,59 +44,38 @@ namespace ShiftScheduler.Server.Controllers
{ {
var shift = _configurationService.GetShifts().FirstOrDefault(s => s.Name == request.ShiftName); var shift = _configurationService.GetShifts().FirstOrDefault(s => s.Name == request.ShiftName);
if (shift == null) if (shift == null)
{
return NotFound($"Shift '{request.ShiftName}' not found"); return NotFound($"Shift '{request.ShiftName}' not found");
}
var transportConfig = _configurationService.GetTransportConfiguration(); var transportConfig = _configurationService.GetTransportConfiguration();
TransportConnection? morningTransport = null; TransportConnection? morningTransport = null;
TransportConnection? afternoonTransport = null; TransportConnection? afternoonTransport = null;
var shifTimes = _configurationService.ParseShiftTimes(request.Date, shift); var shifTimes = _configurationService.ParseShiftTimes(request.Date, shift);
// Get transport for morning shift if it has morning time if (!string.IsNullOrEmpty(shift.MorningTime) && shifTimes.MorningStart.HasValue)
if (!string.IsNullOrEmpty(shift.MorningTime)) morningTransport = await _transportService.GetConnectionAsync(shifTimes.MorningStart.Value);
{
if (shifTimes.MorningStart.HasValue)
{
morningTransport = await _transportService.GetConnectionAsync(shifTimes.MorningStart.Value);
}
}
// Get transport for afternoon shift if it has afternoon time
// But only if the break between morning and afternoon is long enough
if (!string.IsNullOrEmpty(shift.AfternoonTime)) if (!string.IsNullOrEmpty(shift.AfternoonTime))
{ {
var shouldLoadAfternoonTransport = true; var shouldLoad = true;
// If both morning and afternoon shifts exist, check break duration if (!string.IsNullOrEmpty(shift.MorningTime) && shifTimes.MorningEnd.HasValue && shifTimes.AfternoonStart.HasValue)
if (!string.IsNullOrEmpty(shift.MorningTime) && !string.IsNullOrEmpty(shift.AfternoonTime))
{ {
if (shifTimes.MorningEnd.HasValue && shifTimes.AfternoonStart.HasValue) var breakMinutes = (shifTimes.AfternoonStart.Value - shifTimes.MorningEnd.Value).TotalMinutes;
{ shouldLoad = breakMinutes >= transportConfig.MinBreakMinutes;
var breakDurationMinutes = (shifTimes.AfternoonStart.Value - shifTimes.MorningEnd.Value).TotalMinutes;
shouldLoadAfternoonTransport = breakDurationMinutes >= transportConfig.MinBreakMinutes;
}
}
if (shouldLoadAfternoonTransport)
{
if (shifTimes.AfternoonStart.HasValue)
{
afternoonTransport = await _transportService.GetConnectionAsync(shifTimes.AfternoonStart.Value);
}
} }
if (shouldLoad && shifTimes.AfternoonStart.HasValue)
afternoonTransport = await _transportService.GetConnectionAsync(shifTimes.AfternoonStart.Value);
} }
var shiftWithTransport = new ShiftWithTransport return Ok(new ShiftWithTransport
{ {
Date = request.Date, Date = request.Date,
Shift = shift, Shift = shift,
MorningTransport = morningTransport, MorningTransport = morningTransport,
AfternoonTransport = afternoonTransport, AfternoonTransport = afternoonTransport
}; });
return Ok(shiftWithTransport);
} }
[HttpPost("export_ics")] [HttpPost("export_ics")]
@ -154,12 +136,16 @@ namespace ShiftScheduler.Server.Controllers
} }
} }
[HttpGet("google_calendars")] [HttpGet("calendars")]
public async Task<IActionResult> GetGoogleCalendars() public async Task<IActionResult> GetCalendars([FromQuery] string provider)
{ {
try try
{ {
var calendars = await _googleCalendarService.GetCalendarsAsync(); ICalendarService service = provider == "nextcloud"
? _nextcloudCalendarService
: _googleCalendarService;
var calendars = await service.GetCalendarsAsync();
return Ok(calendars); return Ok(calendars);
} }
catch (Exception ex) catch (Exception ex)
@ -168,18 +154,23 @@ namespace ShiftScheduler.Server.Controllers
} }
} }
[HttpPost("sync_to_google_calendar")] [HttpPost("sync_to_calendar")]
public async Task<IActionResult> SyncToGoogleCalendar([FromBody] SyncToGoogleCalendarRequest request) public async Task<IActionResult> SyncToCalendar([FromBody] SyncToCalendarRequest request)
{ {
try try
{ {
var shiftsWithTransport = await BuildShiftsWithTransportAsync(request.Year, request.Month); var shiftsWithTransport = await BuildShiftsWithTransportAsync(request.Year, request.Month);
await _googleCalendarService.SyncShiftsToCalendarAsync(request.CalendarId, shiftsWithTransport);
return Ok(new { message = "Shifts synced to Google Calendar successfully!" }); ICalendarService service = request.Provider == "nextcloud"
? _nextcloudCalendarService
: _googleCalendarService;
await service.SyncShiftsToCalendarAsync(request.CalendarId, shiftsWithTransport);
return Ok(new { message = "Shifts synced successfully!" });
} }
catch (Exception ex) catch (Exception ex)
{ {
return StatusCode(500, $"Error syncing to Google Calendar: {ex.Message}"); return StatusCode(500, $"Error syncing to calendar: {ex.Message}");
} }
} }
@ -193,37 +184,29 @@ namespace ShiftScheduler.Server.Controllers
{ {
var date = kvp.Key; var date = kvp.Key;
var shiftName = kvp.Value; var shiftName = kvp.Value;
var shift = _configurationService.GetShifts().FirstOrDefault(s => s.Name == shiftName); var shift = _configurationService.GetShifts().FirstOrDefault(s => s.Name == shiftName);
if (shift == null) if (shift == null) continue;
continue;
var shiftTimes = _configurationService.ParseShiftTimes(date, shift); var shiftTimes = _configurationService.ParseShiftTimes(date, shift);
TransportConnection? morningTransport = null; TransportConnection? morningTransport = null;
TransportConnection? afternoonTransport = null; TransportConnection? afternoonTransport = null;
// Get transport for morning shift if it has morning time
if (!string.IsNullOrEmpty(shift.MorningTime) && shiftTimes.MorningStart.HasValue) if (!string.IsNullOrEmpty(shift.MorningTime) && shiftTimes.MorningStart.HasValue)
{
morningTransport = await _transportService.GetConnectionAsync(shiftTimes.MorningStart.Value); morningTransport = await _transportService.GetConnectionAsync(shiftTimes.MorningStart.Value);
}
// Get transport for afternoon shift if it has afternoon time
if (!string.IsNullOrEmpty(shift.AfternoonTime)) if (!string.IsNullOrEmpty(shift.AfternoonTime))
{ {
var shouldLoadAfternoonTransport = true; var shouldLoad = true;
// If both morning and afternoon shifts exist, check break duration
if (!string.IsNullOrEmpty(shift.MorningTime) && shiftTimes.MorningEnd.HasValue && shiftTimes.AfternoonStart.HasValue) if (!string.IsNullOrEmpty(shift.MorningTime) && shiftTimes.MorningEnd.HasValue && shiftTimes.AfternoonStart.HasValue)
{ {
var breakDurationMinutes = (shiftTimes.AfternoonStart.Value - shiftTimes.MorningEnd.Value).TotalMinutes; var breakMinutes = (shiftTimes.AfternoonStart.Value - shiftTimes.MorningEnd.Value).TotalMinutes;
shouldLoadAfternoonTransport = breakDurationMinutes >= transportConfig.MinBreakMinutes; shouldLoad = breakMinutes >= transportConfig.MinBreakMinutes;
} }
if (shouldLoadAfternoonTransport && shiftTimes.AfternoonStart.HasValue) if (shouldLoad && shiftTimes.AfternoonStart.HasValue)
{
afternoonTransport = await _transportService.GetConnectionAsync(shiftTimes.AfternoonStart.Value); afternoonTransport = await _transportService.GetConnectionAsync(shiftTimes.AfternoonStart.Value);
}
} }
shiftsWithTransport.Add(new ShiftWithTransport shiftsWithTransport.Add(new ShiftWithTransport
@ -258,8 +241,9 @@ namespace ShiftScheduler.Server.Controllers
public int Month { get; set; } public int Month { get; set; }
} }
public class SyncToGoogleCalendarRequest public class SyncToCalendarRequest
{ {
public string Provider { get; set; } = string.Empty;
public string CalendarId { get; set; } = string.Empty; public string CalendarId { get; set; } = string.Empty;
public int Year { get; set; } public int Year { get; set; }
public int Month { get; set; } public int Month { get; set; }

View file

@ -30,6 +30,7 @@ builder.Services.AddSingleton<ITransportApiService, TransportApiService>();
builder.Services.AddSingleton<ITransportService, TransportService>(); builder.Services.AddSingleton<ITransportService, TransportService>();
builder.Services.AddHttpContextAccessor(); builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<IGoogleCalendarService, GoogleCalendarService>(); builder.Services.AddScoped<IGoogleCalendarService, GoogleCalendarService>();
builder.Services.AddScoped<INextcloudCalendarService, NextcloudCalendarService>();
// Configure authentication // Configure authentication
builder.Services.AddAuthentication(options => builder.Services.AddAuthentication(options =>