45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package gapi
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/FernandoJVideira/simple_bank/internal/service"
|
|
"github.com/FernandoJVideira/simple_bank/internal/val"
|
|
"github.com/FernandoJVideira/simple_bank/pb"
|
|
"google.golang.org/genproto/googleapis/rpc/errdetails"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
func (s *Server) VerifyEmail(ctx context.Context, req *pb.VerifyEmailRequest) (*pb.VerifyEmailResponse, error) {
|
|
violations := validateVerifyEmailRequest(req)
|
|
if violations != nil {
|
|
return nil, invalidArgumentError(violations)
|
|
}
|
|
|
|
result, err := s.userService.VerifyEmail(ctx, service.EmailVerificationRequest{
|
|
EmailID: req.GetId(),
|
|
SecretCode: req.GetCode(),
|
|
})
|
|
if err != nil {
|
|
return nil, status.Error(codes.Internal, "failed to verify email")
|
|
}
|
|
|
|
resp := &pb.VerifyEmailResponse{
|
|
IsVerified: result.User.IsEmailVerified,
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func validateVerifyEmailRequest(req *pb.VerifyEmailRequest) (violations []*errdetails.BadRequest_FieldViolation) {
|
|
if err := val.ValidateEmailID(req.GetId()); err != nil {
|
|
violations = append(violations, fieldViolation("id", err))
|
|
}
|
|
|
|
if err := val.ValidateVerificationCode(req.GetCode()); err != nil {
|
|
violations = append(violations, fieldViolation("secret_code", err))
|
|
}
|
|
|
|
return violations
|
|
}
|