zitadel/internal/api/grpc/member/converter.go
Tim Möhlmann f680dd934d
refactor: rename package errors to zerrors (#7039)
* chore: rename package errors to zerrors

* rename package errors to gerrors

* fix error related linting issues

* fix zitadel error assertion

* fix gosimple linting issues

* fix deprecated linting issues

* resolve gci linting issues

* fix import structure

---------

Co-authored-by: Elio Bischof <elio@zitadel.com>
2023-12-08 15:30:55 +01:00

72 lines
2.3 KiB
Go

package member
import (
"github.com/zitadel/zitadel/internal/api/grpc/object"
"github.com/zitadel/zitadel/internal/api/grpc/user"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/query"
"github.com/zitadel/zitadel/internal/zerrors"
member_pb "github.com/zitadel/zitadel/pkg/grpc/member"
)
func MemberToDomain(member *member_pb.Member) *domain.Member {
return &domain.Member{
UserID: member.UserId,
Roles: member.Roles,
}
}
func MembersToPb(assetAPIPrefix string, members []*query.Member) []*member_pb.Member {
m := make([]*member_pb.Member, len(members))
for i, member := range members {
m[i] = MemberToPb(assetAPIPrefix, member)
}
return m
}
func MemberToPb(assetAPIPrefix string, m *query.Member) *member_pb.Member {
return &member_pb.Member{
UserId: m.UserID,
Roles: m.Roles,
PreferredLoginName: m.PreferredLoginName,
Email: m.Email,
FirstName: m.FirstName,
LastName: m.LastName,
DisplayName: m.DisplayName,
AvatarUrl: domain.AvatarURL(assetAPIPrefix, m.ResourceOwner, m.AvatarURL),
UserType: user.TypeToPb(m.UserType),
Details: object.ToViewDetailsPb(
m.Sequence,
m.CreationDate,
m.ChangeDate,
m.ResourceOwner,
),
}
}
func MemberQueriesToQuery(queries []*member_pb.SearchQuery) (q []query.SearchQuery, err error) {
q = make([]query.SearchQuery, len(queries))
for i, query := range queries {
q[i], err = MemberQueryToMember(query)
if err != nil {
return nil, err
}
}
return q, nil
}
func MemberQueryToMember(search *member_pb.SearchQuery) (query.SearchQuery, error) {
switch q := search.Query.(type) {
case *member_pb.SearchQuery_EmailQuery:
return query.NewMemberEmailSearchQuery(object.TextMethodToQuery(q.EmailQuery.Method), q.EmailQuery.Email)
case *member_pb.SearchQuery_FirstNameQuery:
return query.NewMemberFirstNameSearchQuery(object.TextMethodToQuery(q.FirstNameQuery.Method), q.FirstNameQuery.FirstName)
case *member_pb.SearchQuery_LastNameQuery:
return query.NewMemberLastNameSearchQuery(object.TextMethodToQuery(q.LastNameQuery.Method), q.LastNameQuery.LastName)
case *member_pb.SearchQuery_UserIdQuery:
return query.NewMemberUserIDSearchQuery(q.UserIdQuery.UserId)
default:
return nil, zerrors.ThrowInvalidArgument(nil, "MEMBE-7Bb92", "Errors.Query.InvalidRequest")
}
}