PDFs erzeugen
This commit is contained in:
@@ -83,6 +83,7 @@ builder.Services.AddSingleton<IUserNotificationService>(sp => sp.GetRequiredServ
|
||||
// Register DB-backed services as the interfaces
|
||||
builder.Services.AddScoped<IAuthService, AuthService>();
|
||||
builder.Services.AddScoped<ITimetrackerService, TimetrackerService>();
|
||||
builder.Services.AddScoped<timetracker.Server.Services.PdfGeneratorService>();
|
||||
builder.Services.AddScoped<IHolidayService, HolidayService>();
|
||||
|
||||
// Add services to the container.
|
||||
@@ -738,6 +739,51 @@ trackerApi.MapGet("/export/month", async (ClaimsPrincipal claimsPrincipal, [From
|
||||
$"Monatsbericht_{monthName}_{year}.xlsx");
|
||||
});
|
||||
|
||||
trackerApi.MapGet("/export/month/pdf", async (
|
||||
ClaimsPrincipal claimsPrincipal,
|
||||
[FromQuery] int userId,
|
||||
[FromQuery] int year,
|
||||
[FromQuery] int month,
|
||||
ITimetrackerService trackerService,
|
||||
IHolidayService holidayService,
|
||||
TimetrackerDbContext db,
|
||||
timetracker.Server.Services.PdfGeneratorService pdfService) =>
|
||||
{
|
||||
var idClaim = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
if (!int.TryParse(idClaim, out var currentUserId)) return Results.Unauthorized();
|
||||
|
||||
if (userId != currentUserId && !claimsPrincipal.IsInRole("TenantAdmin") && !claimsPrincipal.IsInRole("SuperAdmin"))
|
||||
{
|
||||
return Results.Forbid();
|
||||
}
|
||||
|
||||
if (month < 1 || month > 12) return Results.BadRequest("Ungültiger Monat.");
|
||||
|
||||
var user = await db.Users.IgnoreQueryFilters().FirstOrDefaultAsync(u => u.Id == userId);
|
||||
if (user == null) return Results.NotFound("Benutzer nicht gefunden.");
|
||||
|
||||
Tenant? tenant = null;
|
||||
if (user.TenantId.HasValue)
|
||||
{
|
||||
tenant = await db.Tenants.IgnoreQueryFilters().FirstOrDefaultAsync(t => t.Id == user.TenantId.Value);
|
||||
}
|
||||
|
||||
var deCulture = new System.Globalization.CultureInfo("de-DE");
|
||||
var monthName = deCulture.DateTimeFormat.GetMonthName(month);
|
||||
|
||||
var workDays = await trackerService.GetMonthAsync(userId, year, month);
|
||||
var vacations = await trackerService.GetVacationDaysAsync(userId, year);
|
||||
var settings = await trackerService.GetSettingsAsync(userId);
|
||||
var holidays = await holidayService.GetHolidaysAsync(year, settings.GermanState);
|
||||
|
||||
var pdfBytes = pdfService.GenerateTimesheetPdf(user, tenant, settings, year, month, workDays, vacations, holidays);
|
||||
|
||||
return Results.File(
|
||||
pdfBytes,
|
||||
"application/pdf",
|
||||
$"Stundenzettel_{monthName}_{year}_{user.Username}.pdf");
|
||||
});
|
||||
|
||||
trackerApi.MapGet("/export/year", async (ClaimsPrincipal claimsPrincipal, [FromQuery] int year, ITimetrackerService trackerService, TimetrackerDbContext db) =>
|
||||
{
|
||||
var idClaim = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
|
||||
Reference in New Issue
Block a user