package invitation import ( "context" "errors" "fmt" "github.com/google/uuid" "lincodex/backend/internal/domain" "lincodex/backend/internal/port/outbound" ) const listMineLocation = "invitation.Service.ListMine" // CandidateListCommand selects one page of the inbox of the session candidate. // The candidate comes only from the session. type CandidateListCommand struct { ActorID uuid.UUID After *outbound.CandidateInvitationPosition Limit int32 } // CandidatePage is one page of the candidate's inbox, newest first by // delivery time. type CandidatePage struct { Items []domain.CandidateInvitationSummary HasMore bool } // ListMine reads one page of the invitations delivered to the session // candidate, in any later state. Listing has no side effect: it records no // view. Drafts and invitations of other candidates are never listed. func (service *Service) ListMine(ctx context.Context, command CandidateListCommand) (CandidatePage, error) { if command.ActorID == uuid.Nil { return CandidatePage{}, fmt.Errorf("%s - actor ID must be set: %w", listMineLocation, ErrForbidden) } if command.Limit < 1 || command.Limit > outbound.MaxInvitationPageSize { return CandidatePage{}, fmt.Errorf("%s - page limit must be 1 to %d: %w", listMineLocation, outbound.MaxInvitationPageSize, ErrInvalidRequest) } var result CandidatePage err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error { if err := authorizeCandidate(ctx, unit, command.ActorID); err != nil { return err } page, err := unit.Invitations().ListForCandidate(ctx, outbound.CandidateInvitationListQuery{ CandidateID: command.ActorID, After: command.After, Limit: command.Limit, }) if err != nil { return fmt.Errorf("%s - list invitations: %w", listMineLocation, err) } result = CandidatePage{Items: make([]domain.CandidateInvitationSummary, 0, len(page.Items)), HasMore: page.HasMore} for _, entry := range page.Items { summary, err := entry.Invitation.CandidateSummary(domain.InvitationOrganizationRef{ ID: entry.Invitation.OrganizationID(), Name: entry.OrganizationName, }) if err != nil { return fmt.Errorf("%s - build inbox entry: %w", listMineLocation, err) } result.Items = append(result.Items, summary) } return nil }) if err != nil { return CandidatePage{}, fmt.Errorf("%s - run transaction: %w", listMineLocation, err) } return result, nil } // authorizeCandidate requires an existing candidate account with a confirmed // email. An employer account, which has no inbox of invitations, is refused. func authorizeCandidate(ctx context.Context, unit outbound.UnitOfWork, actorID uuid.UUID) error { const location = "invitation.authorizeCandidate" user, err := unit.Users().GetByID(ctx, actorID) if errors.Is(err, outbound.ErrUserNotFound) { return fmt.Errorf("%s - require existing candidate account: %w", location, ErrForbidden) } if err != nil { return fmt.Errorf("%s - read account: %w", location, err) } if user.Scenario != domain.ScenarioCandidate || !user.EmailVerified() { return fmt.Errorf("%s - require verified candidate account: %w", location, ErrForbidden) } return nil }