package v1 import ( "context" "errors" "github.com/google/uuid" "lincodex/backend/internal/adapter/primary/http/api" "lincodex/backend/internal/adapter/primary/http/problem" "lincodex/backend/internal/domain" "lincodex/backend/internal/port/outbound" "lincodex/backend/internal/usecase/profile" ) // ProfileOperations reads and version-saves the authenticated candidate's // private manual profile. type ProfileOperations interface { GetMyProfile(ctx context.Context, userID uuid.UUID) (domain.CandidateProfile, error) SaveMyProfile(ctx context.Context, userID uuid.UUID, expectedVersion int64, input domain.CandidateProfileInput) (domain.CandidateProfile, error) } // ProfileHandler serves the candidate's own profile operations. type ProfileHandler struct { operations ProfileOperations } func NewProfileHandler(operations ProfileOperations) *ProfileHandler { if operations == nil { panic("v1.NewProfileHandler - profile operations must not be nil") } return &ProfileHandler{operations: operations} } func (handler *Handler) GetMyProfile(ctx context.Context) (api.GetMyProfileRes, error) { if handler.profile == nil { return handler.UnimplementedHandler.GetMyProfile(ctx) } return handler.profile.GetMyProfile(ctx) } func (handler *Handler) SaveMyProfile(ctx context.Context, request *api.CandidateProfileUpdate) (api.SaveMyProfileRes, error) { if handler.profile == nil { return handler.UnimplementedHandler.SaveMyProfile(ctx, request) } return handler.profile.SaveMyProfile(ctx, request) } func (handler *ProfileHandler) GetMyProfile(ctx context.Context) (api.GetMyProfileRes, error) { principal, ok := PrincipalFromContext(ctx) if !ok { return (*api.GetMyProfileUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil } profile, err := handler.operations.GetMyProfile(ctx, principal.UserID) if err != nil { return profileReadFailure(ctx, err) } return &api.CandidateProfileHeaders{ CacheControl: api.CacheControlNoStoreNoStore, Response: candidateProfileResponse(profile), }, nil } func (handler *ProfileHandler) SaveMyProfile(ctx context.Context, request *api.CandidateProfileUpdate) (api.SaveMyProfileRes, error) { principal, ok := PrincipalFromContext(ctx) if !ok { return (*api.SaveMyProfileUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil } if request == nil { return (*api.SaveMyProfileBadRequest)(&problem.InvalidField(ctx, api.ProblemFieldLocationBody, "").Response), nil } profile, err := handler.operations.SaveMyProfile(ctx, principal.UserID, int64(request.ExpectedVersion), candidateProfileInput(request)) if err != nil { return profileSaveFailure(ctx, err) } return &api.CandidateProfileHeaders{ CacheControl: api.CacheControlNoStoreNoStore, Response: candidateProfileResponse(profile), }, nil } func profileReadFailure(ctx context.Context, err error) (api.GetMyProfileRes, error) { if errors.Is(err, profile.ErrForbidden) { return (*api.GetMyProfileForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil } if errors.Is(err, profile.ErrEmailNotVerified) { return (*api.GetMyProfileForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil } if errors.Is(err, outbound.ErrUserNotFound) { return (*api.GetMyProfileUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil } return nil, err } func profileSaveFailure(ctx context.Context, err error) (api.SaveMyProfileRes, error) { switch { case errors.Is(err, domain.ErrCandidateProfileVersionConflict), errors.Is(err, outbound.ErrCandidateProfileVersionConflict): return (*api.SaveMyProfilePreconditionFailed)(&problem.New(ctx, api.ProblemCodeVersionConflict).Response), nil case errors.Is(err, profile.ErrForbidden), errors.Is(err, profile.ErrEmailNotVerified): return (*api.SaveMyProfileForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil case errors.Is(err, outbound.ErrUserNotFound): return (*api.SaveMyProfileUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil case errors.Is(err, domain.ErrInvalidCandidateProfile): field, ok := errors.AsType[*domain.CandidateProfileFieldError](err) if !ok { return (*api.SaveMyProfileBadRequest)(&problem.InvalidField(ctx, api.ProblemFieldLocationBody, "").Response), nil } return (*api.SaveMyProfileBadRequest)(&problem.InvalidField(ctx, api.ProblemFieldLocationBody, field.Pointer).Response), nil default: return nil, err } } func candidateProfileInput(request *api.CandidateProfileUpdate) domain.CandidateProfileInput { input := domain.CandidateProfileInput{ Stack: make([]domain.ProfileClaimInput[string], 0, len(request.Stack)), Roles: make([]domain.ProfileClaimInput[domain.ProfileRole], 0, len(request.Roles)), Experience: make([]domain.ProfileClaimInput[domain.ProfileExperience], 0, len(request.Experience)), ParallelPeriodAcknowledgements: make([]domain.ProfileParallelPeriodAcknowledgement, 0, len(request.ConfirmedParallelPeriods)), SoftSkills: make([]domain.ProfileClaimInput[string], 0, len(request.SoftSkills)), } if value, ok := request.FullName.Get(); ok { input.FullName = profileStringInput(string(value.Value), value.Status) } if value, ok := request.Contacts.Email.Get(); ok { input.Email = profileStringInput(string(value.Value), value.Status) } if value, ok := request.Contacts.Phone.Get(); ok { input.Phone = profileStringInput(string(value.Value), value.Status) } if value, ok := request.Contacts.Telegram.Get(); ok { input.Telegram = profileStringInput(string(value.Value), value.Status) } for _, item := range request.Stack { input.Stack = append(input.Stack, domain.ProfileClaimInput[string]{Value: string(item.Tag), Status: domain.ProfileClaimStatus(item.Status)}) } for _, item := range request.Roles { grade, _ := item.SelfAssessedGrade.Get() input.Roles = append(input.Roles, domain.ProfileClaimInput[domain.ProfileRole]{ Value: domain.ProfileRole{Specialization: domain.SpecializationCode(item.Specialization), SelfAssessedGrade: domain.GradeCode(grade)}, Status: domain.ProfileClaimStatus(item.Status), }) } for _, item := range request.Experience { employer, _ := item.EmployerName.Get() endedOn := profileYearMonth(item.EndedOn) summary, _ := item.Summary.Get() input.Experience = append(input.Experience, domain.ProfileClaimInput[domain.ProfileExperience]{ Value: domain.ProfileExperience{ EmployerName: string(employer), Specialization: domain.SpecializationCode(item.Specialization), StartedOn: string(item.StartedOn), EndedOn: endedOn, Summary: string(summary), }, Status: domain.ProfileClaimStatus(item.Status), }) } for _, item := range request.ConfirmedParallelPeriods { input.ParallelPeriodAcknowledgements = append(input.ParallelPeriodAcknowledgements, domain.ProfileParallelPeriodAcknowledgement{ FirstIndex: item.FirstIndex, SecondIndex: item.SecondIndex, FirstPeriod: domain.ProfileExperiencePeriod{ StartedOn: string(item.FirstPeriod.StartedOn), EndedOn: profileYearMonth(item.FirstPeriod.EndedOn), }, SecondPeriod: domain.ProfileExperiencePeriod{ StartedOn: string(item.SecondPeriod.StartedOn), EndedOn: profileYearMonth(item.SecondPeriod.EndedOn), }, }) } for _, item := range request.SoftSkills { input.SoftSkills = append(input.SoftSkills, domain.ProfileClaimInput[string]{Value: string(item.Skill), Status: domain.ProfileClaimStatus(item.Status)}) } if value, ok := request.Terms.Location.Get(); ok { input.Location = profileStringInput(string(value.Value), value.Status) } if value, ok := request.Terms.Relocation.Get(); ok { input.Relocation = &domain.ProfileClaimInput[bool]{Value: value.Value, Status: domain.ProfileClaimStatus(value.Status)} } if value, ok := request.Terms.SalaryMinimum.Get(); ok { input.SalaryMinimum = &domain.ProfileClaimInput[domain.ProfileSalaryMinimum]{ Value: domain.ProfileSalaryMinimum{Amount: int64(value.Value.Amount), TaxBasis: domain.ProfileTaxBasis(value.Value.TaxBasis)}, Status: domain.ProfileClaimStatus(value.Status), } } return input } func profileStringInput(value string, status api.ProfileItemStatus) *domain.ProfileClaimInput[string] { return &domain.ProfileClaimInput[string]{Value: value, Status: domain.ProfileClaimStatus(status)} } func profileYearMonth(value api.NilProfileYearMonth) string { month, ok := value.Get() if !ok { return "" } return string(month) } func candidateProfileResponse(profile domain.CandidateProfile) api.CandidateProfile { content := profile.Content() response := api.CandidateProfile{ Version: api.ProfileVersion(profile.Version()), ExperienceOverlaps: make([]api.ProfileExperienceOverlap, 0), Stack: make([]api.ProfileStackItem, 0), Roles: make([]api.ProfileRoleItem, 0), Experience: make([]api.ProfileExperienceItem, 0), SoftSkills: make([]api.ProfileSoftSkillItem, 0), ConfirmedCategories: make([]api.ProfileConfirmedCategory, 0), } if name, ok := content.FullName(); ok { response.FullName = profileFullNameClaim(name) } else { response.FullName = profileFullNameClaim(domain.ProfileClaim[string]{}, false) } contacts := api.ProfileContacts{} if email, ok := content.Email(); ok { contacts.Email = profileEmailClaim(email) } else { contacts.Email = profileEmailClaim(domain.ProfileClaim[string]{}, false) } if phone, ok := content.Phone(); ok { contacts.Phone = profilePhoneClaim(phone) } else { contacts.Phone = profilePhoneClaim(domain.ProfileClaim[string]{}, false) } if telegram, ok := content.Telegram(); ok { contacts.Telegram = profileTelegramClaim(telegram) } else { contacts.Telegram = profileTelegramClaim(domain.ProfileClaim[string]{}, false) } response.Contacts = contacts for _, item := range content.Stack() { response.Stack = append(response.Stack, api.ProfileStackItem{Tag: api.StackTag(item.Value()), Status: api.ProfileItemStatus(item.Status()), Source: profileClaimSourceResponse(item.Source())}) } for _, item := range content.Roles() { grade := api.NilGradeCode{} if item.Value().SelfAssessedGrade == "" { grade.SetToNull() } else { grade.SetTo(api.GradeCode(item.Value().SelfAssessedGrade)) } response.Roles = append(response.Roles, api.ProfileRoleItem{ Specialization: api.SpecializationCode(item.Value().Specialization), SelfAssessedGrade: grade, Status: api.ProfileItemStatus(item.Status()), Source: profileClaimSourceResponse(item.Source()), }) } for _, item := range content.Experience() { value := item.Value() employer := api.NilProfileEmployerName{} if value.EmployerName == "" { employer.SetToNull() } else { employer.SetTo(api.ProfileEmployerName(value.EmployerName)) } endedOn := api.NilProfileYearMonth{} if value.EndedOn == "" { endedOn.SetToNull() } else { endedOn.SetTo(api.ProfileYearMonth(value.EndedOn)) } summary := api.NilProfileExperienceSummary{} if value.Summary == "" { summary.SetToNull() } else { summary.SetTo(api.ProfileExperienceSummary(value.Summary)) } response.Experience = append(response.Experience, api.ProfileExperienceItem{ EmployerName: employer, Specialization: api.SpecializationCode(value.Specialization), StartedOn: api.ProfileYearMonth(value.StartedOn), EndedOn: endedOn, Summary: summary, Status: api.ProfileItemStatus(item.Status()), Source: profileClaimSourceResponse(item.Source()), }) } for _, item := range content.ExperienceOverlaps() { response.ExperienceOverlaps = append(response.ExperienceOverlaps, api.ProfileExperienceOverlap{ FirstIndex: item.FirstIndex, SecondIndex: item.SecondIndex, Status: api.ProfileExperienceOverlapStatus(item.Status), }) } for _, item := range content.SoftSkills() { response.SoftSkills = append(response.SoftSkills, api.ProfileSoftSkillItem{ Skill: api.ProfileSoftSkill(item.Value()), Status: api.ProfileItemStatus(item.Status()), Source: profileClaimSourceResponse(item.Source()), }) } response.Terms = candidateProfileTerms(content) updatedAt := api.NilTimestamp{} if profile.UpdatedAt().IsZero() { updatedAt.SetToNull() } else { updatedAt.SetTo(api.Timestamp(profile.UpdatedAt())) } response.UpdatedAt = updatedAt return response } func candidateProfileTerms(content domain.CandidateProfileContent) api.ProfileTerms { terms := api.ProfileTerms{} if location, ok := content.Location(); ok { terms.Location = api.ProfileLocationClaim{ Status: api.ProfileClaimStatus(location.Status()), Value: api.NewNilProfileLocationName(api.ProfileLocationName(location.Value())), Source: api.NewNilProfileClaimSource(profileClaimSourceResponse(location.Source())), } } else { terms.Location = unknownProfileLocationClaim() } if relocation, ok := content.Relocation(); ok { terms.Relocation = api.ProfileRelocationClaim{ Status: api.ProfileClaimStatus(relocation.Status()), Value: api.NewNilBool(relocation.Value()), Source: api.NewNilProfileClaimSource(profileClaimSourceResponse(relocation.Source())), } } else { terms.Relocation = unknownProfileRelocationClaim() } if salary, ok := content.SalaryMinimum(); ok { terms.SalaryMinimum = api.ProfileSalaryMinimumClaim{ Status: api.ProfileClaimStatus(salary.Status()), Value: api.NewNilProfileSalaryMinimum(api.ProfileSalaryMinimum{ Amount: api.RubAmount(salary.Value().Amount), Currency: api.CurrencyCodeRUB, Period: api.SalaryPeriodMonth, TaxBasis: api.SalaryTaxBasis(salary.Value().TaxBasis), }), Source: api.NewNilProfileClaimSource(profileClaimSourceResponse(salary.Source())), } } else { terms.SalaryMinimum = unknownProfileSalaryClaim() } return terms } func profileFullNameClaim(claim domain.ProfileClaim[string], exists ...bool) api.ProfileFullNameClaim { if len(exists) > 0 && !exists[0] { value := api.NilContactFullName{} value.SetToNull() source := api.NilProfileClaimSource{} source.SetToNull() return api.ProfileFullNameClaim{Status: api.ProfileClaimStatusUnknown, Value: value, Source: source} } return api.ProfileFullNameClaim{Status: api.ProfileClaimStatus(claim.Status()), Value: api.NewNilContactFullName(api.ContactFullName(claim.Value())), Source: api.NewNilProfileClaimSource(profileClaimSourceResponse(claim.Source()))} } func profileEmailClaim(claim domain.ProfileClaim[string], exists ...bool) api.ProfileEmailClaim { if len(exists) > 0 && !exists[0] { value := api.NilEmailAddress{} value.SetToNull() source := api.NilProfileClaimSource{} source.SetToNull() return api.ProfileEmailClaim{Status: api.ProfileClaimStatusUnknown, Value: value, Source: source} } return api.ProfileEmailClaim{Status: api.ProfileClaimStatus(claim.Status()), Value: api.NewNilEmailAddress(api.EmailAddress(claim.Value())), Source: api.NewNilProfileClaimSource(profileClaimSourceResponse(claim.Source()))} } func profilePhoneClaim(claim domain.ProfileClaim[string], exists ...bool) api.ProfilePhoneClaim { if len(exists) > 0 && !exists[0] { value := api.NilContactPhoneNumber{} value.SetToNull() source := api.NilProfileClaimSource{} source.SetToNull() return api.ProfilePhoneClaim{Status: api.ProfileClaimStatusUnknown, Value: value, Source: source} } return api.ProfilePhoneClaim{Status: api.ProfileClaimStatus(claim.Status()), Value: api.NewNilContactPhoneNumber(api.ContactPhoneNumber(claim.Value())), Source: api.NewNilProfileClaimSource(profileClaimSourceResponse(claim.Source()))} } func profileTelegramClaim(claim domain.ProfileClaim[string], exists ...bool) api.ProfileTelegramClaim { if len(exists) > 0 && !exists[0] { value := api.NilContactTelegramUsername{} value.SetToNull() source := api.NilProfileClaimSource{} source.SetToNull() return api.ProfileTelegramClaim{Status: api.ProfileClaimStatusUnknown, Value: value, Source: source} } return api.ProfileTelegramClaim{Status: api.ProfileClaimStatus(claim.Status()), Value: api.NewNilContactTelegramUsername(api.ContactTelegramUsername(claim.Value())), Source: api.NewNilProfileClaimSource(profileClaimSourceResponse(claim.Source()))} } func unknownProfileLocationClaim() api.ProfileLocationClaim { value := api.NilProfileLocationName{} value.SetToNull() source := api.NilProfileClaimSource{} source.SetToNull() return api.ProfileLocationClaim{Status: api.ProfileClaimStatusUnknown, Value: value, Source: source} } func unknownProfileRelocationClaim() api.ProfileRelocationClaim { value := api.NilBool{} value.SetToNull() source := api.NilProfileClaimSource{} source.SetToNull() return api.ProfileRelocationClaim{Status: api.ProfileClaimStatusUnknown, Value: value, Source: source} } func unknownProfileSalaryClaim() api.ProfileSalaryMinimumClaim { value := api.NilProfileSalaryMinimum{} value.SetToNull() source := api.NilProfileClaimSource{} source.SetToNull() return api.ProfileSalaryMinimumClaim{Status: api.ProfileClaimStatusUnknown, Value: value, Source: source} } func profileClaimSourceResponse(source domain.ProfileClaimSource) api.ProfileClaimSource { quote := api.NilProfileClaimQuote{} if source.Quote() == "" { quote.SetToNull() } else { quote.SetTo(api.ProfileClaimQuote(source.Quote())) } extractionVersion := api.NilNeedVersionLabel{} if source.ExtractionVersion() == "" { extractionVersion.SetToNull() } else { extractionVersion.SetTo(api.NeedVersionLabel(source.ExtractionVersion())) } confirmedAt := api.NilTimestamp{} if source.ConfirmedAt().IsZero() { confirmedAt.SetToNull() } else { confirmedAt.SetTo(api.Timestamp(source.ConfirmedAt())) } return api.ProfileClaimSource{ Origin: api.ProfileClaimOrigin(source.Origin()), TrustLevel: api.ProfileClaimTrustLevel(domain.ProfileClaimTrustLevel), Quote: quote, ExtractionVersion: extractionVersion, ConfirmedAt: confirmedAt, } }