feat(postit): migrate editable profile data and admin profile flow

This commit is contained in:
Paul Schneider 2026-10-07 12:20:26 +01:00
commit af1e572f32
Signed by: notazof
GPG key ID: AD4B9B545FBD7D12
38 changed files with 6573 additions and 19 deletions

View file

@ -0,0 +1,96 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Yavsc.Abstract.Resources;
using Yavsc.Models;
using Yavsc.Server.Helpers;
namespace Yavsc.ApiControllers.Administration;
[Produces("application/json")]
[Authorize]
[Route(Constants.APIPrefix + "/" + Constants.ResourceUsagePath)]
public sealed class ResourceUsageApiController : Controller
{
private readonly ApplicationDbContext _dbContext;
public ResourceUsageApiController(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
private static ResourceUsageSummary ToSummary(ResourceUsageRecord record)
=> new()
{
UserId = record.UserId,
UserName = record.UserName,
RecordedAtUtc = new DateTimeOffset(record.RecordedAtUtc, TimeSpan.Zero),
ApiCalls = record.ApiCalls,
CpuSeconds = record.CpuSeconds,
BandwidthMb = record.BandwidthMb,
StorageMb = record.StorageMb,
EstimatedCompensation = record.EstimatedCompensation,
Currency = record.Currency
};
[HttpGet(Constants.ResourceUsageCurrentUserRoute)]
public async Task<ActionResult<ResourceUsageSummary>> GetCurrentUserUsage()
{
var userId = User.GetUserId();
if (string.IsNullOrWhiteSpace(userId))
{
return Unauthorized();
}
var userName = User.Identity?.Name ?? Constants.ResourceUsageCurrentUserDisplayName;
var record = await _dbContext.ResourceUsageRecords
.Where(r => r.UserId == userId)
.OrderByDescending(r => r.RecordedAtUtc)
.FirstOrDefaultAsync();
if (record is null)
{
var zeroUsage = new ResourceUsageRecord
{
UserId = userId,
UserName = userName,
RecordedAtUtc = DateTime.UtcNow,
ApiCalls = 0m,
CpuSeconds = 0m,
BandwidthMb = 0m,
StorageMb = 0m,
EstimatedCompensation = 0m,
Currency = Constants.ResourceUsageDefaultCurrency,
UserCreated = userId,
UserModified = userId,
DateCreated = DateTime.UtcNow,
DateModified = DateTime.UtcNow,
};
_dbContext.ResourceUsageRecords.Add(zeroUsage);
await _dbContext.SaveChangesAsync(userId);
return Ok(ToSummary(zeroUsage));
}
return Ok(ToSummary(record));
}
[HttpGet(Constants.ResourceUsageAdminRoute)]
public async Task<ActionResult<List<ResourceUsageSummary>>> GetAdminOverview()
{
if (!User.IsInRole(Constants.AdminGroupName))
{
return Forbid();
}
var latestByUser = (await _dbContext.ResourceUsageRecords
.OrderByDescending(r => r.RecordedAtUtc)
.ToListAsync())
.GroupBy(r => r.UserId)
.Select(g => g.First())
.OrderByDescending(r => r.RecordedAtUtc)
.ToList();
return Ok(latestByUser.Select(ToSummary).ToList());
}
}

View file

@ -1,3 +1,4 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Razor;
@ -129,6 +130,9 @@ namespace Yavsc.ApiControllers
Temp = siteSettings.TempDir,
};
var stopwatch = Stopwatch.StartNew();
FileInfo? renderedPdf = null;
try
{
// 1. Render the LaTeX template (Estimate_tex) to a TeX source
@ -141,10 +145,28 @@ namespace Yavsc.ApiControllers
// we stream the generated PDF bytes — but driving the
// generation through the view keeps the template as the
// output producer, per the original design.
if (model.GenerateEstimatePdf() is FileInfo fo && fo.Exists)
renderedPdf = model.GenerateEstimatePdf();
if (renderedPdf is FileInfo fo && fo.Exists)
{
return File(fo.OpenRead(), "application/pdf");
await ResourceUsageTracker.RecordAsync(
dbContext,
uid,
User.Identity?.Name ?? uid,
cpuSeconds: (decimal)stopwatch.Elapsed.TotalSeconds,
storageBytes: fo.Length,
actorUserId: uid);
return File(fo.OpenRead(), "application/pdf");
}
await ResourceUsageTracker.RecordAsync(
dbContext,
uid,
User.Identity?.Name ?? uid,
cpuSeconds: (decimal)stopwatch.Elapsed.TotalSeconds,
storageBytes: 0,
actorUserId: uid);
string msg = "TeX render failed: " + model.GenerationErrorMessage;
logger.LogError(msg);
return Problem(msg);
@ -152,6 +174,14 @@ namespace Yavsc.ApiControllers
}
catch (Exception ex)
{
await ResourceUsageTracker.RecordAsync(
dbContext,
uid,
User.Identity?.Name ?? uid,
cpuSeconds: (decimal)stopwatch.Elapsed.TotalSeconds,
storageBytes: renderedPdf?.Length ?? 0,
actorUserId: uid);
logger.LogError(ex, "estimate {QueryId}: TeX/PDF render failed", queryId);
return Problem("TeX render failed: " + ex.Message);
}

View file

@ -9,6 +9,8 @@ using Yavsc.Api.Helpers;
using Yavsc.Server.Helpers;
using System.Diagnostics;
using Microsoft.Extensions.Options;
using Yavsc.Abstract.Identity;
using Yavsc.Abstract.Workflow;
namespace Yavsc.WebApi.Controllers
{
@ -30,14 +32,16 @@ namespace Yavsc.WebApi.Controllers
readonly ApplicationDbContext _dbContext;
private readonly SiteSettings siteSettings;
private readonly ILogger _logger;
private readonly UserManager<ApplicationUser> _userManager;
public ApiAccountController(
ILoggerFactory loggerFactory, ApplicationDbContext dbContext,
IOptions<SiteSettings> siteSettings)
IOptions<SiteSettings> siteSettings, UserManager<ApplicationUser> userManager)
{
_logger = loggerFactory.CreateLogger(nameof(ApiAccountController));
_dbContext = dbContext;
this.siteSettings = siteSettings.Value;
_userManager = userManager;
}
[HttpGet("me")]
@ -52,7 +56,8 @@ namespace Yavsc.WebApi.Controllers
Debug.Assert(userData != null, "userData is null");
var user = new Yavsc.Models.Auth.Me(userData.Id, userData.UserName, userData.Email,
userData.Avatar,
userData.PostalAddress, userData.DedicatedGoogleCalendar);
userData.PostalAddress, userData.DedicatedGoogleCalendar, userData.FullName);
user.Address = userData.PostalAddress?.Address ?? string.Empty;
var userRoles = _dbContext.UserRoles.Where(u => u.UserId == uid).Select(r => r.RoleId).ToArray();
@ -71,6 +76,190 @@ namespace Yavsc.WebApi.Controllers
.FirstAsync(u => u.Id == uid);
}
[HttpPut("me")]
public async Task<IActionResult> UpdateMe([FromBody] ProfileUpdateRequest request)
{
if (request is null)
{
return BadRequest(new { error = "Payload is required." });
}
var uid = User.GetUserId();
if (string.IsNullOrWhiteSpace(uid))
{
return Challenge();
}
var user = await _dbContext.Users
.Include(u => u.PostalAddress)
.FirstOrDefaultAsync(u => u.Id == uid);
if (user is null)
{
return NotFound();
}
if (!string.IsNullOrWhiteSpace(request.FullName))
{
user.FullName = request.FullName.Trim();
}
if (!string.IsNullOrWhiteSpace(request.Address))
{
if (user.PostalAddress is null)
{
user.PostalAddress = new Yavsc.Models.Relationship.Location { Address = request.Address.Trim() };
}
else
{
user.PostalAddress.Address = request.Address.Trim();
}
}
await _dbContext.SaveChangesAsync(uid);
return Ok(new { status = "saved", fullName = user.FullName, address = user.PostalAddress?.Address });
}
[HttpGet("performer-profile")]
public async Task<IActionResult> GetPerformerProfile()
{
var uid = User.GetUserId();
if (string.IsNullOrWhiteSpace(uid))
{
return Challenge();
}
var profile = await _dbContext.Performers
.AsNoTracking()
.FirstOrDefaultAsync(p => p.PerformerId == uid);
var selectedActivityCodes = await _dbContext.UserActivities
.AsNoTracking()
.Where(x => x.UserId == uid)
.Select(x => x.DoesCode)
.ToListAsync();
return Ok(new PerformerProfileSettings
{
PerformerId = uid,
UserName = User.Identity?.Name ?? string.Empty,
ExerciseCountryCode = profile?.ExerciseCountryCode ?? "fr",
SIREN = profile?.SIREN ?? string.Empty,
WebSite = profile?.WebSite ?? string.Empty,
Active = profile?.Active ?? false,
AcceptNotifications = profile?.AcceptNotifications ?? false,
AcceptPublicContact = profile?.AcceptPublicContact ?? false,
UseGeoLocalizationToReduceDistanceWithClients = profile?.UseGeoLocalizationToReduceDistanceWithClients ?? false,
SelectedActivityCodes = selectedActivityCodes
});
}
[HttpPut("performer-profile")]
public async Task<IActionResult> SavePerformerProfile([FromBody] PerformerProfileSettings model)
{
if (model is null)
{
return BadRequest(new { error = "Payload is required." });
}
var uid = User.GetUserId();
if (string.IsNullOrWhiteSpace(uid) || !string.Equals(model.PerformerId, uid, StringComparison.Ordinal))
{
return Forbid();
}
var user = await _dbContext.Users.FirstOrDefaultAsync(u => u.Id == uid);
if (user is null)
{
return NotFound();
}
if (string.IsNullOrWhiteSpace(model.ExerciseCountryCode))
{
model.ExerciseCountryCode = "fr";
}
var profile = await _dbContext.Performers.FirstOrDefaultAsync(p => p.PerformerId == uid);
if (profile is null)
{
profile = new Yavsc.Models.Workflow.PerformerProfile
{
PerformerId = uid,
Performer = user,
ExerciseCountryCode = model.ExerciseCountryCode,
SIREN = model.SIREN ?? string.Empty,
WebSite = model.WebSite ?? string.Empty,
Active = model.Active,
AcceptNotifications = model.AcceptNotifications,
AcceptPublicContact = model.AcceptPublicContact,
UseGeoLocalizationToReduceDistanceWithClients = model.UseGeoLocalizationToReduceDistanceWithClients,
OrganizationAddress = new Yavsc.Models.Relationship.Location()
};
_dbContext.Performers.Add(profile);
}
else
{
profile.ExerciseCountryCode = model.ExerciseCountryCode;
profile.SIREN = model.SIREN ?? string.Empty;
profile.WebSite = model.WebSite ?? string.Empty;
profile.Active = model.Active;
profile.AcceptNotifications = model.AcceptNotifications;
profile.AcceptPublicContact = model.AcceptPublicContact;
profile.UseGeoLocalizationToReduceDistanceWithClients = model.UseGeoLocalizationToReduceDistanceWithClients;
}
var desiredCodes = (model.SelectedActivityCodes ?? [])
.Where(code => !string.IsNullOrWhiteSpace(code))
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
var existingLinks = await _dbContext.UserActivities
.Where(x => x.UserId == uid)
.ToListAsync();
foreach (var link in existingLinks)
{
if (!desiredCodes.Contains(link.DoesCode, StringComparer.OrdinalIgnoreCase))
{
_dbContext.UserActivities.Remove(link);
}
}
foreach (var code in desiredCodes)
{
if (existingLinks.Any(x => string.Equals(x.DoesCode, code, StringComparison.OrdinalIgnoreCase)))
{
continue;
}
var activity = await _dbContext.Activities
.FirstOrDefaultAsync(a => a.Code == code);
if (activity is not null)
{
_dbContext.UserActivities.Add(new Yavsc.Models.Workflow.UserActivity
{
UserId = uid,
DoesCode = activity.Code,
Weight = 100
});
}
}
await _dbContext.SaveChangesAsync(uid);
if (!User.IsInRole("Performer"))
{
var roleResult = await _userManager.AddToRoleAsync(user, "Performer");
if (!roleResult.Succeeded)
{
return BadRequest(new { error = "Performer role could not be assigned.", details = roleResult.Errors.Select(e => e.Description) });
}
}
return Ok(new { status = "saved", performer = model });
}
[HttpGet("myhost")]
public IActionResult MyHost ()
{