softplayer-backend/api/v1/email.go

72 lines
1.9 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"
"google.golang.org/grpc/metadata"
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
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
}
}
func (c *EmailServer) SendRequest(ctx context.Context, in *proto_email.RequestValidation) (*emptypb.Empty, error) {
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
}
trailer := metadata.Pairs("trailer-key", "val")
if err := grpc.SetTrailer(ctx, trailer); 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-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
}