Files
jobtrackingapp/JobTrackerApi/Services/ApplicationWorkspaceService.cs
T
cesnimda 3a906b881e
CI and Deploy / test (push) Failing after 1m8s
CI and Deploy / deploy (push) Has been skipped
feat(workspace): unified application checklist (Phase 5 milestone 2)
Evolve the existing readiness workflow into one persisted, user-controlled
checklist rather than adding a second tracker.

ApplicationChecklistItem records only completion state and user intent. Each
default system item carries a stable SystemKey and an AutoSignal — the same
signal /readiness already computed — and re-syncs on every read: a satisfied
signal auto-completes the item, a reverted signal reopens it, and a manual tick
always wins. Users can add, reorder, dismiss and delete.

Readiness is refactored into a projection of the checklist (score = completion
percentage, completed/missing = live items by status). Its DTO shape and the
workflowSignal/reminders health view are unchanged, so no API contract breaks.

The workspace's next recommended action now comes from the first pending
checklist item in category priority order (preparation, submission, follow-up,
interview, custom), replacing the parallel ruleset — so the overview can never
recommend something already ticked off, and a user's own task can be next.

The table follows the established MariaDB-safe path: the scaffolded migration is
a no-op and the idempotent reconciler owns the DDL for both providers. Verified
on MariaDB 11 — auto_increment PK, varchar/datetime(6)/tinyint(1) columns, both
indexes inside the key limit, cascade delete, unique system key per application,
and NULL system keys not colliding for custom items.

329 backend tests, 94 frontend tests, type check, production build and both
Docker builds pass locally.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-19 11:15:46 +02:00

142 lines
5.9 KiB
C#

using JobTrackerApi.Data;
using JobTrackerApi.Models;
using Microsoft.EntityFrameworkCore;
namespace JobTrackerApi.Services;
// Phase 5 Milestone 1 — the Application Workspace overview.
//
// This is an AGGREGATE READ ONLY. It owns no data and duplicates none: the CV comes from the Phase 4
// CvVariant lens, documents from Attachments, activity from JobEvent, AI history from AiInteraction,
// stage semantics from JobPipeline. Nothing here writes, and no career data is copied.
// docs/architecture/application-workspace.md.
public sealed record WorkspaceCvDto(int? VariantId, string? VariantName, string? ThemeId, bool HasTailoredCvText, DateTimeOffset? UpdatedAtUtc);
public sealed record WorkspaceActivityDto(string Type, string? Detail, DateTime At);
public sealed record WorkspaceNextStepDto(string Key, string Label, string Reason, string? Section);
public sealed record WorkspaceOverviewDto(
int Id,
string JobTitle,
string? Company,
string? Location,
string? Salary,
string Status,
string StageGroup,
int StageOrder,
DateTime? DateApplied,
DateTime? Deadline,
DateTime? FollowUpAt,
string? NextAction,
string? JobUrl,
bool HasJobDescription,
WorkspaceCvDto Cv,
bool HasCoverLetter,
int DocumentCount,
bool HasPortfolio,
int AiInteractionCount,
DateTimeOffset? LastAiAtUtc,
IReadOnlyList<WorkspaceActivityDto> RecentActivity,
WorkspaceNextStepDto? NextStep,
ChecklistProgressDto? ChecklistProgress);
public interface IApplicationWorkspaceService
{
Task<WorkspaceOverviewDto?> GetOverviewAsync(string ownerUserId, int jobApplicationId, CancellationToken ct);
}
public sealed class ApplicationWorkspaceService : IApplicationWorkspaceService
{
private const int RecentActivityCount = 8;
private readonly JobTrackerContext _db;
private readonly IApplicationChecklistService _checklist;
public ApplicationWorkspaceService(JobTrackerContext db, IApplicationChecklistService checklist)
{
_db = db;
_checklist = checklist;
}
public async Task<WorkspaceOverviewDto?> GetOverviewAsync(string ownerUserId, int jobApplicationId, CancellationToken ct)
{
var job = await _db.JobApplications.AsNoTracking().Include(j => j.Company)
.FirstOrDefaultAsync(j => j.Id == jobApplicationId && j.OwnerUserId == ownerUserId, ct);
if (job is null) return null;
// CV: the most recently touched variant attached to this application (Phase 4 lens).
var variant = await _db.CvVariants.AsNoTracking()
.Where(v => v.OwnerUserId == ownerUserId && v.JobApplicationId == jobApplicationId)
.OrderByDescending(v => v.UpdatedAtUtc)
.FirstOrDefaultAsync(ct);
var cv = new WorkspaceCvDto(
variant?.Id,
variant?.Name,
variant is null ? null : CvVariantSettingsJson.Deserialize(variant.SettingsJson).ThemeId,
!string.IsNullOrWhiteSpace(job.TailoredCvText),
variant?.UpdatedAtUtc);
var documentCount = await _db.Attachments.AsNoTracking()
.CountAsync(a => a.JobApplicationId == jobApplicationId, ct);
var aiCount = await _db.AiInteractions.AsNoTracking()
.CountAsync(a => a.OwnerUserId == ownerUserId && a.JobApplicationId == jobApplicationId, ct);
var lastAi = await _db.AiInteractions.AsNoTracking()
.Where(a => a.OwnerUserId == ownerUserId && a.JobApplicationId == jobApplicationId)
.OrderByDescending(a => a.CreatedAtUtc)
.Select(a => (DateTimeOffset?)a.CreatedAtUtc)
.FirstOrDefaultAsync(ct);
var activity = await _db.JobEvents.AsNoTracking()
.Where(e => e.JobApplicationId == jobApplicationId)
.OrderByDescending(e => e.At)
.Take(RecentActivityCount)
.Select(e => new WorkspaceActivityDto(e.Type, e.Note ?? e.NewValue, e.At))
.ToListAsync(ct);
// The checklist is the single workflow surface, so the overview's "next step" and progress both
// come from it rather than a parallel ruleset. Seeds itself on first read.
var checklist = await _checklist.GetAsync(ownerUserId, jobApplicationId, ct);
var stage = JobPipeline.Stages.FirstOrDefault(s => string.Equals(s.Key, JobPipeline.Normalize(job.Status), StringComparison.OrdinalIgnoreCase));
var hasCoverLetter = job.HasCoverLetter || !string.IsNullOrWhiteSpace(job.CoverLetterText);
return new WorkspaceOverviewDto(
job.Id,
job.JobTitle,
job.Company?.Name,
job.Location,
job.Salary,
job.Status,
stage?.Group.ToString() ?? "Active",
JobPipeline.OrderOf(job.Status),
job.DateApplied,
job.Deadline,
job.FollowUpAt,
job.NextAction,
job.JobUrl,
!string.IsNullOrWhiteSpace(job.Description),
cv,
hasCoverLetter,
documentCount,
job.HasPortfolio,
aiCount,
lastAi,
activity,
NextStep(checklist),
checklist?.Progress);
}
// "The user should never ask what to do next." Milestone 2 moved this onto the checklist: the first
// pending item, in category priority order (preparation, submission, follow-up, interview, custom)
// then the user's own ordering. One workflow surface — the overview cannot recommend something the
// checklist has already been ticked off, and a user-added task can be the next action.
private static WorkspaceNextStepDto? NextStep(ChecklistDto? checklist)
{
if (checklist is null) return null;
var next = ApplicationChecklistService.NextPending(checklist);
return next is null
? null
: new WorkspaceNextStepDto(next.SystemKey ?? $"custom-{next.Id}", next.Title, next.Description ?? string.Empty, next.Section ?? "checklist");
}
}