package production import ( "context" "slices" "github.com/google/uuid" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/model/dto" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/service" ) // parseIdentity maps malformed identity values to the authentication error. func parseIdentity(rawID string) (uuid.UUID, error) { id, err := parseID(rawID) if err != nil { return uuid.Nil, service.ErrUnauthorized } return id, nil } // requireUserID resolves an existing identity without loading a user profile. func (s *Service) requireUserID(ctx context.Context, rawID string) (uuid.UUID, error) { id, err := parseIdentity(rawID) if err != nil { return uuid.Nil, err } exists, err := s.repo.UsersExist(ctx, []uuid.UUID{id}) if err != nil { return uuid.Nil, normalizeStoreError(err) } if !exists { return uuid.Nil, service.ErrUnauthorized } return id, nil } // requireChatMember resolves an existing caller and a chat available to that caller. func (s *Service) requireChatMember(ctx context.Context, userID, chatID string) (uuid.UUID, dto.ChatDTO, error) { caller, err := s.requireUserID(ctx, userID) if err != nil { return uuid.Nil, dto.ChatDTO{}, err } chat, err := s.findChat(ctx, chatID) if err != nil { return uuid.Nil, dto.ChatDTO{}, err } if !slices.Contains(chat.MemberIDs, caller) { return uuid.Nil, dto.ChatDTO{}, service.ErrNotFound } return caller, chat, nil } // findChat parses a chat identifier and translates storage lookup errors. func (s *Service) findChat(ctx context.Context, rawID string) (dto.ChatDTO, error) { id, err := parseID(rawID) if err != nil { return dto.ChatDTO{}, err } chat, err := s.repo.Chat(ctx, id) return chat, normalizeStoreError(err) }