fix(rd21): guard career and job saves with durable revisions

This commit is contained in:
cesnimda
2026-10-23 08:07:23 +02:00
parent 8f2cad67f7
commit 12f342c09c
32 changed files with 1185 additions and 277 deletions
@@ -4,6 +4,7 @@ using JobTrackerApi.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace JobTrackerApi.Controllers;
@@ -35,8 +36,8 @@ public sealed class CareerProfileController : ControllerBase
var user = await _users.GetUserAsync(User);
if (user is null) return Unauthorized();
var profile = await _career.LoadStructuredAsync(user.Id, cancellationToken);
return Ok(new CareerProfileDto(profile, CareerCompleteness.Evaluate(profile), user.ProfileCvText));
var snapshot = await _career.LoadSnapshotAsync(user.Id, cancellationToken);
return Ok(new CareerProfileDto(snapshot.Profile, CareerCompleteness.Evaluate(snapshot.Profile), snapshot.CvText, snapshot.Revision));
}
/// <summary>
@@ -50,21 +51,27 @@ public sealed class CareerProfileController : ControllerBase
{
var user = await _users.GetUserAsync(User);
if (user is null) return StatusCode(501, "The career profile can only be edited on local accounts.");
if (request is null || request.ExpectedVersion is null or < 0) return BadRequest("expectedVersion is required.");
var profile = StructuredCvProfileJson.NormalizeForPersistence(request?.Profile);
var profile = StructuredCvProfileJson.NormalizeForPersistence(request.Profile);
var error = CareerProfileValidator.Validate(profile);
if (error is not null) return BadRequest(error);
var saved = await _career.SaveVersionAsync(user.Id, profile, "manual", cancellationToken);
// Keep the derived projection in sync for legacy readers. CvText (the raw imported text) is
// part of the career profile and is set here too; identity fields are never touched.
user.ProfileCvStructureJson = StructuredCvProfileJson.SerializePersisted(saved);
if (request?.CvText is not null) user.ProfileCvText = string.IsNullOrWhiteSpace(request.CvText) ? null : request.CvText;
var res = await _users.UpdateAsync(user);
if (!res.Succeeded) return BadRequest(string.Join("; ", res.Errors.Select(e => e.Description)));
return Ok(new CareerProfileDto(saved, CareerCompleteness.Evaluate(saved), user.ProfileCvText));
try
{
var dto = await _career.ExecuteAtomicAsync(async () =>
{
var saved = await _career.SaveVersionAsync(user.Id, profile, "manual", request.ExpectedVersion.Value, cancellationToken);
user.ProfileCvStructureJson = StructuredCvProfileJson.SerializePersisted(saved);
if (request.CvText is not null) user.ProfileCvText = string.IsNullOrWhiteSpace(request.CvText) ? null : request.CvText;
var res = await _users.UpdateAsync(user);
if (!res.Succeeded) throw new CareerProjectionException(string.Join("; ", res.Errors.Select(e => e.Description)));
return new CareerProfileDto(saved, CareerCompleteness.Evaluate(saved), user.ProfileCvText, request.ExpectedVersion.Value + 1);
}, cancellationToken);
return Ok(dto);
}
catch (DbUpdateConcurrencyException) { return Conflict(new { code = "revision_conflict", message = "The career profile changed. Reload and review your draft before saving." }); }
catch (CareerProjectionException ex) { return BadRequest(ex.Message); }
}
/// <summary>Just the completeness scorecard, for the /career overview.</summary>
@@ -92,25 +99,34 @@ public sealed class CareerProfileController : ControllerBase
/// </summary>
[HttpPost("versions/{version:int}/restore")]
[Authorize(AuthenticationSchemes = "local")]
public async Task<ActionResult<CareerProfileDto>> Restore([FromRoute] int version, CancellationToken cancellationToken)
public async Task<ActionResult<CareerProfileDto>> Restore([FromRoute] int version, [FromBody] CareerProfileRestoreRequest? request, CancellationToken cancellationToken)
{
var user = await _users.GetUserAsync(User);
if (user is null) return StatusCode(501, "The career profile can only be edited on local accounts.");
if (request is null || request.ExpectedVersion is null or < 0) return BadRequest("expectedVersion is required.");
var restored = await _career.RestoreVersionAsync(user.Id, version, cancellationToken);
if (restored is null) return NotFound($"Version {version} was not found.");
user.ProfileCvStructureJson = StructuredCvProfileJson.SerializePersisted(restored);
var res = await _users.UpdateAsync(user);
if (!res.Succeeded) return BadRequest(string.Join("; ", res.Errors.Select(e => e.Description)));
return Ok(new CareerProfileDto(restored, CareerCompleteness.Evaluate(restored), user.ProfileCvText));
try
{
var dto = await _career.ExecuteAtomicAsync(async () =>
{
var restored = await _career.RestoreVersionAsync(user.Id, version, request.ExpectedVersion.Value, cancellationToken);
if (restored is null) return null;
user.ProfileCvStructureJson = StructuredCvProfileJson.SerializePersisted(restored);
var res = await _users.UpdateAsync(user);
if (!res.Succeeded) throw new CareerProjectionException(string.Join("; ", res.Errors.Select(e => e.Description)));
return new CareerProfileDto(restored, CareerCompleteness.Evaluate(restored), user.ProfileCvText, request.ExpectedVersion.Value + 1);
}, cancellationToken);
return dto is null ? NotFound($"Version {version} was not found.") : Ok(dto);
}
catch (DbUpdateConcurrencyException) { return Conflict(new { code = "revision_conflict", message = "The career profile changed. Reload before restoring a version." }); }
catch (CareerProjectionException ex) { return BadRequest(ex.Message); }
}
}
public sealed record CareerProfileSaveRequest(StructuredCvProfile? Profile, string? CvText);
public sealed record CareerProfileSaveRequest(StructuredCvProfile? Profile, string? CvText, int? ExpectedVersion = null);
public sealed record CareerProfileRestoreRequest(int? ExpectedVersion);
public sealed record CareerProfileDto(StructuredCvProfile Profile, CareerCompletenessDto Completeness, string? CvText);
public sealed record CareerProfileDto(StructuredCvProfile Profile, CareerCompletenessDto Completeness, string? CvText, int Revision);
public sealed record CareerCompletenessDto(int Percent, List<string> Missing, List<CareerSectionStatusDto> Sections);