Files
lincodex/backend/internal/usecase/profile/service.go
T

136 lines
4.6 KiB
Go

// Package profile reads and version-saves a candidate's private manual profile.
package profile
import (
"context"
"errors"
"fmt"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
var (
// ErrForbidden reports an account that cannot use the candidate profile.
ErrForbidden = errors.New("candidate profile operation forbidden")
// ErrEmailNotVerified reports an account whose current email is not confirmed.
ErrEmailNotVerified = errors.New("candidate profile requires a confirmed email")
)
// Service is stateless between calls. Profile data, its history, and onboarding
// movement are written through one transaction.
type Service struct {
transactor outbound.Transactor
clock func() time.Time
}
// NewService takes the transaction boundary and application clock. A nil
// dependency is a wiring error and panics at construction.
func NewService(transactor outbound.Transactor, clock func() time.Time) *Service {
if transactor == nil {
panic("profile.NewService - transactor must not be nil")
}
if clock == nil {
panic("profile.NewService - clock must not be nil")
}
return &Service{transactor: transactor, clock: clock}
}
// GetMyProfile reads only the profile owned by userID. Version zero means the
// candidate has not saved a profile yet.
func (service *Service) GetMyProfile(ctx context.Context, userID uuid.UUID) (domain.CandidateProfile, error) {
const location = "Service.GetMyProfile"
if userID == uuid.Nil {
return domain.CandidateProfile{}, fmt.Errorf("%s - user ID must be set: %w", location, domain.ErrInvalidCandidateProfile)
}
var result domain.CandidateProfile
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.CandidateProfiles().Get(ctx, userID)
if err != nil {
return fmt.Errorf("%s - read profile: %w", location, err)
}
return nil
})
if err != nil {
return domain.CandidateProfile{}, fmt.Errorf("%s - transaction: %w", location, err)
}
return result, nil
}
// SaveMyProfile replaces the candidate's manual profile when expectedVersion
// is current. Identical retries remain no-ops, while every successful request
// advances onboarding only from candidate_profile.
func (service *Service) SaveMyProfile(
ctx context.Context,
userID uuid.UUID,
expectedVersion int64,
input domain.CandidateProfileInput,
) (domain.CandidateProfile, error) {
const location = "Service.SaveMyProfile"
if userID == uuid.Nil {
return domain.CandidateProfile{}, fmt.Errorf("%s - user ID must be set: %w", location, domain.ErrInvalidCandidateProfile)
}
at := service.clock().UTC()
if at.IsZero() {
return domain.CandidateProfile{}, fmt.Errorf("%s - application clock returned zero: %w", location, domain.ErrInvalidCandidateProfile)
}
var result domain.CandidateProfile
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.CandidateProfiles()
current, err := repository.Get(ctx, userID)
if err != nil {
return fmt.Errorf("%s - read profile: %w", location, err)
}
updated, changed, err := current.Replace(input, expectedVersion, at)
if err != nil {
return fmt.Errorf("%s - apply profile: %w", location, err)
}
if changed {
updated, err = repository.Save(ctx, expectedVersion, updated)
if err != nil {
return fmt.Errorf("%s - save profile: %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.ErrCandidateProfileVersionConflict) || errors.Is(err, domain.ErrCandidateProfileVersionConflict) {
return domain.CandidateProfile{}, fmt.Errorf("%s - transaction: %w", location, domain.ErrCandidateProfileVersionConflict)
}
return domain.CandidateProfile{}, 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
}