135 lines
4.7 KiB
Go
135 lines
4.7 KiB
Go
// Package questionnaire reads and version-saves a candidate's private questionnaire.
|
|
package questionnaire
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
var (
|
|
ErrForbidden = errors.New("candidate questionnaire operation forbidden")
|
|
ErrEmailNotVerified = errors.New("candidate questionnaire requires a confirmed email")
|
|
)
|
|
|
|
// Service keeps questionnaire answers and onboarding movement in one
|
|
// transaction.
|
|
type Service struct {
|
|
transactor outbound.Transactor
|
|
clock func() time.Time
|
|
}
|
|
|
|
func NewService(transactor outbound.Transactor, clock func() time.Time) *Service {
|
|
if transactor == nil {
|
|
panic("questionnaire.NewService - transactor must not be nil")
|
|
}
|
|
if clock == nil {
|
|
panic("questionnaire.NewService - clock must not be nil")
|
|
}
|
|
return &Service{transactor: transactor, clock: clock}
|
|
}
|
|
|
|
// GetMyQuestionnaire reads only the questionnaire owned by the verified
|
|
// candidate userID. Version zero means no questionnaire has been saved.
|
|
func (service *Service) GetMyQuestionnaire(ctx context.Context, userID uuid.UUID) (domain.CandidateQuestionnaire, error) {
|
|
const location = "Service.GetMyQuestionnaire"
|
|
if userID == uuid.Nil {
|
|
return domain.CandidateQuestionnaire{}, fmt.Errorf("%s - user ID must be set: %w", location, domain.ErrInvalidCandidateQuestionnaire)
|
|
}
|
|
var result domain.CandidateQuestionnaire
|
|
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
if err := requireCandidate(ctx, unit, userID, location); err != nil {
|
|
return err
|
|
}
|
|
var err error
|
|
result, err = unit.CandidateQuestionnaires().Get(ctx, userID)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - read questionnaire: %w", location, err)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return domain.CandidateQuestionnaire{}, fmt.Errorf("%s - transaction: %w", location, err)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// SaveMyQuestionnaire replaces the complete questionnaire at expectedVersion.
|
|
// A changed stale save fails; an identical retry is a no-op. The first valid
|
|
// save advances onboarding only from candidate_questionnaire.
|
|
func (service *Service) SaveMyQuestionnaire(
|
|
ctx context.Context,
|
|
userID uuid.UUID,
|
|
expectedVersion int64,
|
|
input domain.CandidateQuestionnaireInput,
|
|
) (domain.CandidateQuestionnaire, error) {
|
|
const location = "Service.SaveMyQuestionnaire"
|
|
if userID == uuid.Nil {
|
|
return domain.CandidateQuestionnaire{}, fmt.Errorf("%s - user ID must be set: %w", location, domain.ErrInvalidCandidateQuestionnaire)
|
|
}
|
|
at := service.clock().UTC()
|
|
if at.IsZero() {
|
|
return domain.CandidateQuestionnaire{}, fmt.Errorf("%s - application clock returned zero: %w", location, domain.ErrInvalidCandidateQuestionnaire)
|
|
}
|
|
var result domain.CandidateQuestionnaire
|
|
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
if err := requireCandidate(ctx, unit, userID, location); err != nil {
|
|
return err
|
|
}
|
|
repository := unit.CandidateQuestionnaires()
|
|
current, err := repository.Get(ctx, userID)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - read questionnaire: %w", location, err)
|
|
}
|
|
availability, err := unit.AssessmentBank().ListCurrentSpecializationAvailability(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - read current task language availability: %w", location, err)
|
|
}
|
|
updated, changed, err := current.Replace(input, expectedVersion, at, availability)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - apply replacement: %w", location, err)
|
|
}
|
|
if changed {
|
|
updated, err = repository.Save(ctx, expectedVersion, updated)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - save replacement: %w", location, err)
|
|
}
|
|
}
|
|
if err := repository.AdvanceOnboarding(ctx, userID, at); err != nil {
|
|
return fmt.Errorf("%s - advance onboarding: %w", location, err)
|
|
}
|
|
result = updated
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
if errors.Is(err, outbound.ErrCandidateQuestionnaireVersionConflict) || errors.Is(err, domain.ErrCandidateQuestionnaireVersionConflict) {
|
|
return domain.CandidateQuestionnaire{}, fmt.Errorf("%s - transaction: %w", location, domain.ErrCandidateQuestionnaireVersionConflict)
|
|
}
|
|
return domain.CandidateQuestionnaire{}, fmt.Errorf("%s - transaction: %w", location, err)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func requireCandidate(ctx context.Context, unit outbound.UnitOfWork, userID uuid.UUID, location string) error {
|
|
user, err := unit.Users().GetByID(ctx, userID)
|
|
if err != nil {
|
|
if errors.Is(err, outbound.ErrUserNotFound) {
|
|
return ErrForbidden
|
|
}
|
|
return fmt.Errorf("%s - read account: %w", location, err)
|
|
}
|
|
if !user.EmailVerified() {
|
|
return ErrEmailNotVerified
|
|
}
|
|
if user.Scenario != domain.ScenarioCandidate {
|
|
return ErrForbidden
|
|
}
|
|
return nil
|
|
}
|