softplayer-backend/api/v1/email.go

87 lines
2.3 KiB
Go
Raw Normal View History

2024-03-21 20:10:56 +00:00
package v1
import (
"context"
"fmt"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/email"
proto_email "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/email"
2024-04-03 18:05:23 +00:00
"google.golang.org/grpc"
2024-05-06 08:44:08 +00:00
"google.golang.org/grpc/codes"
2024-04-03 18:05:23 +00:00
"google.golang.org/grpc/metadata"
2024-05-06 08:44:08 +00:00
"google.golang.org/grpc/status"
2024-03-21 20:10:56 +00:00
"google.golang.org/protobuf/types/known/emptypb"
ctrl "sigs.k8s.io/controller-runtime"
)
type EmailServer struct {
proto_email.UnimplementedEmailValidationServer
2024-04-03 18:05:23 +00:00
emailConfig email.EmailConf
controller ctrl.Manager
2024-05-06 08:44:08 +00:00
// When dev mode is enabled, email won't be sent, instead the code will be returned in metadata
devMode bool
2024-03-21 20:10:56 +00:00
}
2024-04-03 18:05:23 +00:00
func InitEmailServer(controller ctrl.Manager, emailConfig *email.EmailConf, devMode bool) *EmailServer {
2024-03-21 20:10:56 +00:00
return &EmailServer{
2024-04-03 18:05:23 +00:00
controller: controller,
2024-03-21 20:10:56 +00:00
emailConfig: *emailConfig,
2024-04-03 18:05:23 +00:00
devMode: devMode,
2024-03-21 20:10:56 +00:00
}
}
2024-05-06 08:44:08 +00:00
// Send the validation code to email
2024-03-21 20:10:56 +00:00
func (c *EmailServer) SendRequest(ctx context.Context, in *proto_email.RequestValidation) (*emptypb.Empty, error) {
2024-05-06 08:44:08 +00:00
// Validation
if len(in.GetUserId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "user id must not be empty")
}
// Body
2024-04-03 18:05:23 +00:00
emailSvc := controllers.EmailSvc{
2024-03-21 20:10:56 +00:00
Data: controllers.EmailData{
UserID: in.GetUserId(),
},
EmailConfig: c.emailConfig,
2024-04-03 18:05:23 +00:00
Controller: c.controller,
DevMode: c.devMode,
2024-03-21 20:10:56 +00:00
}
err := emailSvc.SendVerification(ctx)
if err != nil {
return nil, err
}
2024-04-03 18:05:23 +00:00
if c.devMode {
header := metadata.Pairs("code", emailSvc.Data.Code)
if err := grpc.SendHeader(ctx, header); err != nil {
return nil, err
}
}
2024-03-21 20:10:56 +00:00
return &emptypb.Empty{}, nil
}
func (c *EmailServer) ValidateEmail(ctx context.Context, in *proto_email.ConfirmValidation) (*emptypb.Empty, error) {
2024-05-06 08:44:08 +00:00
// Validation
if len(in.GetUserId()) == 0 {
return nil, status.Error(codes.InvalidArgument, "user id must not be empty")
}
if in.GetCode() == 0 {
return nil, status.Error(codes.InvalidArgument, "code must not be empty")
}
// Body
2024-04-03 18:05:23 +00:00
emailSvc := controllers.EmailSvc{
2024-03-21 20:10:56 +00:00
Data: controllers.EmailData{
UserID: in.GetUserId(),
2024-04-03 18:05:23 +00:00
Code: fmt.Sprintf("%d", in.GetCode()),
2024-03-21 20:10:56 +00:00
},
EmailConfig: c.emailConfig,
2024-04-03 18:05:23 +00:00
Controller: c.controller,
2024-03-21 20:10:56 +00:00
}
err := emailSvc.ConfirmVerification(ctx)
if err != nil {
return nil, err
}
return &emptypb.Empty{}, nil
}