package supportcase import ( "context" "fmt" "github.com/google/uuid" "lincodex/backend/internal/domain" "lincodex/backend/internal/port/outbound" ) // A manual assessment request is an ordinary support case. This file holds the // rules that are specific to it: // // - It never reads, grants, or revokes any consent. A candidate who refused // C3, or who never answered the C3 question, reaches the same code path // as every other candidate, so the manual path cannot become a hidden // way to enable automated categorization. // - It never creates an attempt and never writes a grade or category. The // case only enters the expert queue (waiting_expert) in the creation // transaction. // - A prior attempt is optional. When the author references one it has to // belong to the same specialization; its outcome (including a failed // first test) does not matter and is never changed. // - The grading rubric is pinned when exactly one current reviewed category // answers the requested specialization and grade. Otherwise no rubric is // pinned and the case reports that honestly with a null rubric version. // resolveManualAssessmentRubric pins the current reviewed rubric for the // requested target. It leaves the pin empty, without an error, when the bank // has no single current category for the target: the request is still // accepted and waits for an expert. func resolveManualAssessmentRubric( ctx context.Context, repositories outbound.SupportCaseRepositories, fields *domain.NewSupportCaseInput, ) error { if fields.Type != domain.SupportCaseTypeManualAssessment || fields.AssessmentTarget == nil { return nil } rubric, err := repositories.SupportCaseRubrics().ResolveCurrent( ctx, fields.AssessmentTarget.Specialization, fields.AssessmentTarget.Grade, ) if err != nil { return fmt.Errorf("%s - resolve current manual assessment rubric: %w", createLocation, err) } if rubric == nil { return nil } if rubric.Version < 1 || rubric.OpeningID == uuid.Nil || rubric.PolicyVersionID.IsZero() { return fmt.Errorf("%s - rubric resolver returned an incomplete pin: %w", createLocation, domain.ErrInvalidSupportCase) } fields.RubricVersion = &rubric.Version fields.RubricOpeningID = &rubric.OpeningID fields.RubricPolicyID = &rubric.PolicyVersionID return nil } // validateManualAssessmentAttempt accepts an owned attempt of the requested // specialization in any state. A failed or unfinished attempt is exactly the // case a manual assessment exists for, so only the specialization is compared. func validateManualAssessmentAttempt(target *domain.SupportCaseAssessmentTarget, attempt domain.AssessmentAttempt) error { snapshot := attempt.Snapshot() if target == nil || snapshot.Specialization == nil || *snapshot.Specialization != target.Specialization { return ErrStateConflict } return nil }