153 lines
5.4 KiB
Go
153 lines
5.4 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
var _ outbound.SupportCaseRubricRepository = supportCaseRubricRepository{}
|
|
|
|
type supportCaseRubricRepository struct {
|
|
queries *sqlc.Queries
|
|
bank assessmentBankRepository
|
|
}
|
|
|
|
func (unit *UnitOfWork) SupportCaseRubrics() outbound.SupportCaseRubricRepository {
|
|
return supportCaseRubricRepository{
|
|
queries: unit.queries,
|
|
bank: assessmentBankRepository{queries: unit.queries, transactional: true},
|
|
}
|
|
}
|
|
|
|
func (repository supportCaseRubricRepository) ResolveCurrent(
|
|
ctx context.Context,
|
|
specialization domain.SpecializationCode,
|
|
grade domain.GradeCode,
|
|
) (*outbound.SupportCaseRubric, error) {
|
|
const location = "supportCaseRubricRepository.ResolveCurrent"
|
|
if !repository.bank.transactional {
|
|
return nil, fmt.Errorf("%s - rubric resolution needs a transaction: %w", location, outbound.ErrAssessmentLockOutsideTransaction)
|
|
}
|
|
if !specialization.IsKnown() || !grade.IsKnown() {
|
|
return nil, fmt.Errorf("%s - target is invalid: %w", location, domain.ErrInvalidAssessmentFamily)
|
|
}
|
|
row, available, err := repository.currentUniqueOpening(ctx, specialization, grade)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s - resolve current target opening: %w", location, err)
|
|
}
|
|
if !available {
|
|
return nil, nil
|
|
}
|
|
selectionRow := supportRubricSelectionRow(row)
|
|
selection, err := repository.bank.currentSelectionFromRow(ctx, selectionRow)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s - validate reviewed rubric manifest: %w", location, err)
|
|
}
|
|
if !supportRubricSelectionMatches(selection, row) {
|
|
return nil, fmt.Errorf("%s - current rubric pin changed during resolution: %w", location, outbound.ErrAssessmentSelectionUnavailable)
|
|
}
|
|
|
|
return &outbound.SupportCaseRubric{
|
|
Version: selection.ProductPolicyVersion,
|
|
OpeningID: selection.Opening.ID,
|
|
PolicyVersionID: selection.Opening.PolicyVersionID,
|
|
}, nil
|
|
}
|
|
|
|
func (repository supportCaseRubricRepository) currentUniqueOpening(
|
|
ctx context.Context,
|
|
specialization domain.SpecializationCode,
|
|
grade domain.GradeCode,
|
|
) (sqlc.ResolveCurrentSupportCaseRubricsRow, bool, error) {
|
|
initial, err := repository.currentRows(ctx, specialization, grade)
|
|
if err != nil {
|
|
return sqlc.ResolveCurrentSupportCaseRubricsRow{}, false, fmt.Errorf("read current target bindings: %w", err)
|
|
}
|
|
initialRow, unique := uniqueSupportRubricOpening(initial)
|
|
if !unique {
|
|
return sqlc.ResolveCurrentSupportCaseRubricsRow{}, false, nil
|
|
}
|
|
category, err := domain.ParseAssessmentCategory(initialRow.Category)
|
|
if err != nil {
|
|
return sqlc.ResolveCurrentSupportCaseRubricsRow{}, false, fmt.Errorf("parse current category: %w", domain.ErrInvalidAssessmentFamily)
|
|
}
|
|
if err := repository.bank.LockCategory(ctx, category); err != nil {
|
|
return sqlc.ResolveCurrentSupportCaseRubricsRow{}, false, fmt.Errorf("lock current category: %w", err)
|
|
}
|
|
current, err := repository.currentRows(ctx, specialization, grade)
|
|
if err != nil {
|
|
return sqlc.ResolveCurrentSupportCaseRubricsRow{}, false, fmt.Errorf("recheck current target bindings: %w", err)
|
|
}
|
|
currentRow, unique := uniqueSupportRubricOpening(current)
|
|
if !unique || currentRow.OpeningID.Bytes != initialRow.OpeningID.Bytes {
|
|
return sqlc.ResolveCurrentSupportCaseRubricsRow{}, false, nil
|
|
}
|
|
return currentRow, true, nil
|
|
}
|
|
|
|
func supportRubricSelectionMatches(
|
|
selection outbound.AssessmentBankSelection,
|
|
row sqlc.ResolveCurrentSupportCaseRubricsRow,
|
|
) bool {
|
|
return selection.ProductPolicyVersion > 0 && !selection.Opening.PolicyVersionID.IsZero() &&
|
|
selection.Opening.ID == row.OpeningID.Bytes &&
|
|
selection.Opening.PolicyVersionID == domain.PolicyVersionID(row.PolicyVersionID.Bytes) &&
|
|
selection.ProductPolicyVersion == row.EffectivePolicyVersion.Int32
|
|
}
|
|
|
|
func (repository supportCaseRubricRepository) currentRows(
|
|
ctx context.Context,
|
|
specialization domain.SpecializationCode,
|
|
grade domain.GradeCode,
|
|
) ([]sqlc.ResolveCurrentSupportCaseRubricsRow, error) {
|
|
rows, err := repository.queries.ResolveCurrentSupportCaseRubrics(ctx, sqlc.ResolveCurrentSupportCaseRubricsParams{
|
|
Specialization: string(specialization), Grade: string(grade),
|
|
})
|
|
if err != nil {
|
|
return nil, mapAssessmentError(err)
|
|
}
|
|
current := make([]sqlc.ResolveCurrentSupportCaseRubricsRow, 0, len(rows))
|
|
for _, row := range rows {
|
|
metadata, available, err := currentAssessmentSelectionMetadata(supportRubricSelectionRow(row))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("validate current catalog binding: %w", err)
|
|
}
|
|
if !available || metadata.Specialization() != specialization || metadata.Grade() != grade {
|
|
continue
|
|
}
|
|
current = append(current, row)
|
|
}
|
|
return current, nil
|
|
}
|
|
|
|
func supportRubricSelectionRow(row sqlc.ResolveCurrentSupportCaseRubricsRow) sqlc.ResolveCurrentAssessmentSelectionRow {
|
|
return sqlc.ResolveCurrentAssessmentSelectionRow(row)
|
|
}
|
|
|
|
func uniqueSupportRubricOpening(
|
|
rows []sqlc.ResolveCurrentSupportCaseRubricsRow,
|
|
) (sqlc.ResolveCurrentSupportCaseRubricsRow, bool) {
|
|
var selected sqlc.ResolveCurrentSupportCaseRubricsRow
|
|
seen := make(map[uuid.UUID]struct{}, len(rows))
|
|
for _, row := range rows {
|
|
if !row.OpeningID.Valid || row.OpeningID.Bytes == uuid.Nil {
|
|
continue
|
|
}
|
|
if _, exists := seen[row.OpeningID.Bytes]; exists {
|
|
continue
|
|
}
|
|
seen[row.OpeningID.Bytes] = struct{}{}
|
|
if len(seen) > 1 {
|
|
return sqlc.ResolveCurrentSupportCaseRubricsRow{}, false
|
|
}
|
|
selected = row
|
|
}
|
|
return selected, len(seen) == 1
|
|
}
|