Skip to content

Latest commit

 

History

History
70 lines (57 loc) · 2.48 KB

File metadata and controls

70 lines (57 loc) · 2.48 KB

Validation Changes: Human-Only Applicant Users

Issue

Previously, the system allowed AI-type users to be set as ApplicantUsers on JobApplications, which doesn't align with the business logic where only human users should be able to be job applicants.

Solution

Implemented validation to ensure only Human-type users can be ApplicantUsers.

Changes Made

1. JobApplication Entity (JobTracker/Domain/Entities/JobApplication.cs)

Added a static validation method:

/// <summary>
/// Validates that the applicant user is of type Human.
/// AI users cannot be applicants.
/// </summary>
public static void ValidateApplicantUserType(User user)
{
    if (user.Type != UserType.Human)
    {
        throw new InvalidOperationException($"Only Human users can be applicants. User '{user.DisplayName}' (ID: {user.Id}) is of type {user.Type}.");
    }
}

2. JobApplicationsController (JobTracker/Controllers/JobApplicationsController.cs)

Updated the CreateJobApplication endpoint to validate the applicant user type:

// Validate that ApplicantUser exists and is of type Human
var applicantUser = await _context.Users
    .FirstOrDefaultAsync(u => u.Id == createJobApplicationDto.ApplicantUserId && u.ArchivedAt == null);
if (applicantUser == null)
{
    return BadRequest("Applicant user not found or archived.");
}

// Validate that only Human users can be applicants
try
{
    JobApplication.ValidateApplicantUserType(applicantUser);
}
catch (InvalidOperationException ex)
{
    return BadRequest(ex.Message);
}

3. Unit Tests (tests/JobTracker.Tests/Entities/JobApplicationTests.cs)

Added two new test cases:

  1. ValidateApplicantUserType_WithHumanUser_DoesNotThrow: Verifies that Human users pass validation
  2. ValidateApplicantUserType_WithAIUser_ThrowsInvalidOperationException: Verifies that AI users are rejected with an appropriate error message

Test Results

  • All 35 tests passed
  • Build succeeded with no errors or warnings

API Behavior

When attempting to create a JobApplication with an AI user as the applicant:

  • HTTP Status: 400 Bad Request
  • Error Message: "Only Human users can be applicants. User '[Name]' (ID: [GUID]) is of type AI."

Benefits

  1. Type Safety: Enforces business logic at the domain level
  2. Clear Error Messages: Provides detailed feedback when validation fails
  3. Comprehensive Testing: Validates both success and failure scenarios
  4. Maintainability: Centralized validation logic that can be reused if needed