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/candidateview" "lincodex/backend/internal/usecase/matching" ) // MatchOperations searches and ranks the organization-visible candidate bank. type MatchOperations interface { Search(ctx context.Context, request matching.Request) (matching.Result, error) } // MatchHandler binds the match search use case to the v1 API. type MatchHandler struct { operations MatchOperations } func NewMatchHandler(operations MatchOperations) *MatchHandler { if operations == nil { panic("v1.NewMatchHandler - operations must not be nil") } return &MatchHandler{operations: operations} } func (handler *Handler) SearchOrganizationMatches( ctx context.Context, request api.MatchSearchRequest, params api.SearchOrganizationMatchesParams, ) (api.SearchOrganizationMatchesRes, error) { if handler.matches == nil { return handler.UnimplementedHandler.SearchOrganizationMatches(ctx, request, params) } return handler.matches.SearchOrganizationMatches(ctx, request, params) } func (handler *MatchHandler) SearchOrganizationMatches( ctx context.Context, request api.MatchSearchRequest, params api.SearchOrganizationMatchesParams, ) (api.SearchOrganizationMatchesRes, error) { principal, ok := PrincipalFromContext(ctx) if !ok { return (*api.SearchOrganizationMatchesUnauthorized)(&problem.New(ctx, api.ProblemCodeUnauthenticated).Response), nil } command, err := matchSearchCommand(principal.UserID, params.OrgID, request) if err != nil { return nil, err } result, err := handler.operations.Search(ctx, command) if err != nil { switch { case errors.Is(err, matching.ErrForbidden), errors.Is(err, candidateview.ErrForbidden), errors.Is(err, outbound.ErrOrganizationManagerRequired): return (*api.SearchOrganizationMatchesForbidden)(&problem.New(ctx, api.ProblemCodeForbidden).Response), nil case errors.Is(err, outbound.ErrOrganizationNotFound), errors.Is(err, matching.ErrNeedNotFound): return (*api.SearchOrganizationMatchesNotFound)(&problem.New(ctx, api.ProblemCodeNotFound).Response), nil } return nil, err } return &api.MatchSearchResponseHeaders{ CacheControl: api.CacheControlNoStoreNoStore, Response: matchSearchResponse(result), }, nil } func matchSearchCommand(actorID, organizationID uuid.UUID, request api.MatchSearchRequest) (matching.Request, error) { command := matching.Request{ActorID: actorID, OrganizationID: organizationID} switch { case request.IsMatchSearchByNeed(): byNeed := request.MatchSearchByNeed command.Need = &matching.NeedReference{NeedID: byNeed.NeedID, RequirementsVersion: int64(byNeed.RequirementsVersion)} if byNeed.Filters.Set { command.Filters = matchFilters(byNeed.Filters.Value) } case request.IsMatchSearchByBank(): command.Filters = matchFilters(request.MatchSearchByBank.Filters) default: return matching.Request{}, errors.New("v1.matchSearchCommand - request variant is not known") } return command, nil } func matchFilters(filters api.MatchSearchFilters) matching.Filters { result := matching.Filters{} if filters.Specialization.Set { value := domain.SpecializationCode(filters.Specialization.Value) result.Specialization = &value } if filters.ConfirmedGrade.Set { value := domain.GradeCode(filters.ConfirmedGrade.Value) result.ConfirmedGrade = &value } if filters.ConfirmedCategory.Set { category := filters.ConfirmedCategory.Value result.ConfirmedCategory = &matching.GradeFilter{ Specialization: domain.SpecializationCode(category.Specialization), Grade: domain.GradeCode(category.Grade), } } if filters.ConfirmationStatus.Set { value := domain.CandidateViewConfirmationStatus(filters.ConfirmationStatus.Value) result.ConfirmationStatus = &value } if filters.DeclaredGrade.Set { value := domain.GradeCode(filters.DeclaredGrade.Value) result.DeclaredGrade = &value } for _, tag := range filters.Stack { result.Stack = append(result.Stack, domain.CandidateViewSkillCode(tag)) } if filters.Conditions.Set { conditions := &matching.Conditions{} for _, format := range filters.Conditions.Value.WorkFormats { conditions.WorkFormats = append(conditions.WorkFormats, domain.QuestionnaireWorkFormat(format)) } if filters.Conditions.Value.SalaryCompatibility.Set { value := domain.CandidateViewSalaryCompatibility(filters.Conditions.Value.SalaryCompatibility.Value) conditions.SalaryCompatibility = &value } result.Conditions = conditions } return result } func matchSearchResponse(result matching.Result) api.MatchSearchResponse { response := api.MatchSearchResponse{ Source: matchSearchSource(result.Source), Truncated: result.Truncated, Groups: api.MatchSearchGroups{ Main: make([]api.MatchConfirmedCandidate, 0, len(result.Main)), Near: make([]api.MatchConfirmedCandidate, 0, len(result.Near)), Unconfirmed: make([]api.MatchUnconfirmedCandidate, 0, len(result.Unconfirmed)), }, } for _, entry := range result.Main { response.Groups.Main = append(response.Groups.Main, matchConfirmedCandidate(entry)) } for _, entry := range result.Near { response.Groups.Near = append(response.Groups.Near, matchConfirmedCandidate(entry)) } for _, entry := range result.Unconfirmed { response.Groups.Unconfirmed = append(response.Groups.Unconfirmed, matchUnconfirmedCandidate(entry)) } return response } func matchSearchSource(source matching.Source) api.MatchSearchSource { var response api.MatchSearchSource if source.Need != nil { response.SetMatchSearchNeedSource(api.MatchSearchNeedSource{ Kind: api.MatchSearchNeedSourceKindNeed, NeedID: source.Need.NeedID, RequirementsVersion: api.NeedVersion(source.Need.RequirementsVersion), }) return response } response.SetMatchSearchBankSource(api.MatchSearchBankSource{ Kind: api.MatchSearchBankSourceKindBank, Filters: matchAppliedFilters(source.Filters), }) return response } // matchAppliedFilters writes every key, with null for a filter that was not // asked for, so the result stays tied to its exact basis. func matchAppliedFilters(filters matching.Filters) api.MatchAppliedFilters { var applied api.MatchAppliedFilters applied.Specialization.SetToNull() applied.ConfirmedGrade.SetToNull() applied.ConfirmedCategory.SetToNull() applied.ConfirmationStatus.SetToNull() applied.DeclaredGrade.SetToNull() applied.Conditions.SetToNull() if filters.Specialization != nil { applied.Specialization.SetTo(api.SpecializationCode(*filters.Specialization)) } if filters.ConfirmedGrade != nil { applied.ConfirmedGrade.SetTo(api.GradeCode(*filters.ConfirmedGrade)) } if filters.ConfirmedCategory != nil { applied.ConfirmedCategory.SetTo(api.MatchGradeFilter{ Specialization: api.SpecializationCode(filters.ConfirmedCategory.Specialization), Grade: api.GradeCode(filters.ConfirmedCategory.Grade), }) } if filters.ConfirmationStatus != nil { applied.ConfirmationStatus.SetTo(api.MatchConfirmationStatus(*filters.ConfirmationStatus)) } if filters.DeclaredGrade != nil { applied.DeclaredGrade.SetTo(api.GradeCode(*filters.DeclaredGrade)) } for _, tag := range filters.Stack { applied.Stack = append(applied.Stack, api.MatchSearchStackTag(tag)) } if filters.Conditions != nil { conditions := api.MatchAppliedConditions{} conditions.SalaryCompatibility.SetToNull() for _, format := range filters.Conditions.WorkFormats { conditions.WorkFormats = append(conditions.WorkFormats, api.WorkFormat(format)) } if filters.Conditions.SalaryCompatibility != nil { conditions.SalaryCompatibility.SetTo(api.CandidateSalaryCompatibility(*filters.Conditions.SalaryCompatibility)) } applied.Conditions.SetTo(conditions) } return applied } func matchConfirmedCandidate(entry matching.Entry) api.MatchConfirmedCandidate { candidate := api.MatchConfirmedCandidate{ CandidateHandle: api.CandidateHandle(entry.CandidateHandle), Specialization: api.SpecializationCode(entry.Specialization), ConfirmationStatus: api.MatchConfirmedCandidateConfirmationStatusConfirmed, PublicContext: matchPublicContext(entry.PublicContext), EvidenceSummary: matchEvidenceSummary(entry.EvidenceSummary), UnknownFlags: matchUnknownFlags(entry.UnknownFlags), } candidate.DeclaredGrade = matchDeclaredGrade(entry.DeclaredGrade) if entry.Category != nil { candidate.Category = api.MatchCategory{ Specialization: api.SpecializationCode(entry.Category.Specialization), Grade: api.GradeCode(entry.Category.Grade), Basis: api.EvidenceBasis(entry.Category.Basis), } candidate.Strength = api.MatchStrength(entry.Category.Strength) } components := api.MatchConfirmedComponents{ Fit: api.MatchScore(entry.Components.Fit), Coverage: api.MatchScore(entry.Components.Coverage), Domain: api.MatchScore(entry.Components.Domain), Semantics: api.MatchScore(entry.Components.Semantics), Competition: api.MatchScore(entry.Components.Competition), Freshness: api.MatchScore(entry.Components.Freshness), Responsiveness: api.MatchScore(entry.Components.Responsiveness), RankScore: api.MatchScore(entry.Components.RankScore), } components.Percentile.SetToNull() if entry.Components.Percentile != nil { components.Percentile.SetTo(*entry.Components.Percentile) } candidate.Components = components return candidate } func matchUnconfirmedCandidate(entry matching.Entry) api.MatchUnconfirmedCandidate { return api.MatchUnconfirmedCandidate{ CandidateHandle: api.CandidateHandle(entry.CandidateHandle), Specialization: api.SpecializationCode(entry.Specialization), ConfirmationStatus: api.MatchUnconfirmedCandidateConfirmationStatusUnconfirmed, DeclaredGrade: matchDeclaredGrade(entry.DeclaredGrade), PublicContext: matchPublicContext(entry.PublicContext), EvidenceSummary: matchEvidenceSummary(entry.EvidenceSummary), Components: api.MatchUnconfirmedComponents{Fit: entry.Components.Fit, Coverage: entry.Components.Coverage}, UnknownFlags: matchUnknownFlags(entry.UnknownFlags), } } func matchDeclaredGrade(grade *domain.GradeCode) api.NilGradeCode { var declared api.NilGradeCode declared.SetToNull() if grade != nil { declared.SetTo(api.GradeCode(*grade)) } return declared } func matchPublicContext(context matching.PublicContext) api.MatchPublicContext { var response api.MatchPublicContext response.ExperienceBand.SetToNull() response.SalaryCompatibility.SetToNull() if len(context.Stack) > 0 { response.Stack = make(api.MatchPublicStack, 0, len(context.Stack)) for _, tag := range context.Stack { response.Stack = append(response.Stack, api.MatchSearchStackTag(tag)) } } if context.ExperienceBand != nil { response.ExperienceBand.SetTo(api.CandidateExperienceBand(*context.ExperienceBand)) } if len(context.WorkFormats) > 0 { response.WorkFormats = make([]api.WorkFormat, 0, len(context.WorkFormats)) for _, format := range context.WorkFormats { response.WorkFormats = append(response.WorkFormats, api.WorkFormat(format)) } } if context.SalaryCompatibility != nil { response.SalaryCompatibility.SetTo(api.CandidateSalaryCompatibility(*context.SalaryCompatibility)) } return response } func matchEvidenceSummary(items []matching.EvidenceItem) api.MatchEvidenceSummary { summary := make(api.MatchEvidenceSummary, 0, len(items)) for _, item := range items { summary = append(summary, api.MatchEvidenceSummaryItem{ Code: api.MatchEvidenceCode(item.Code), State: api.MatchEvidenceState(item.State), }) } return summary } func matchUnknownFlags(flags []matching.UnknownFlag) api.MatchUnknownFlags { response := make(api.MatchUnknownFlags, 0, len(flags)) for _, flag := range flags { response = append(response, api.MatchUnknownFlag{ Component: api.MatchUnknownComponent(flag.Component), Reason: api.MatchUnknownReason(flag.Reason), }) } return response }