feat: persist candidate questionnaires and enforce company quotas
This commit is contained in:
1 parent
73c7caa225
commit
3a61a8e8bb
51 files changed
+6280
-38
No files matched your search
@@ -0,0 +1,134 @@
|
||||
// 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
|
||||
}
|
||||
Reference in new issue
Block a user