New: - GetAccount - ListAccounts - CreatePerson Refactored: - CreateAccount - RefreshToken - UpdateAccount
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package gapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/itsscb/df/bff/pb"
|
|
"google.golang.org/genproto/googleapis/rpc/errdetails"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
func (server *Server) GetAccount(ctx context.Context, req *pb.GetAccountRequest) (*pb.GetAccountResponse, error) {
|
|
authPayload, err := server.authorizeUser(ctx)
|
|
if err != nil {
|
|
return nil, unauthenticatedError(err)
|
|
}
|
|
|
|
violations := validateGetAccountRequest(req)
|
|
if violations != nil {
|
|
return nil, invalidArgumentError(violations)
|
|
}
|
|
|
|
account, err := server.store.GetAccount(ctx, req.GetId())
|
|
if err != nil {
|
|
return nil, status.Error(codes.NotFound, "failed to get account")
|
|
}
|
|
|
|
if authPayload.Email != account.Email {
|
|
if !server.isAdmin(ctx, authPayload) {
|
|
return nil, status.Error(codes.NotFound, "account not found")
|
|
}
|
|
}
|
|
|
|
rsp := &pb.GetAccountResponse{
|
|
Account: convertAccount(account),
|
|
}
|
|
|
|
return rsp, nil
|
|
}
|
|
|
|
func validateGetAccountRequest(req *pb.GetAccountRequest) (violations []*errdetails.BadRequest_FieldViolation) {
|
|
if req.GetId() < 1 {
|
|
violations = append(violations, fieldViolation("id", errors.New("must be greater than 0")))
|
|
}
|
|
|
|
return violations
|
|
}
|