Skip to content

Commit

Permalink
Add: support for inviting colleagues through email
Browse files Browse the repository at this point in the history
Signed-off-by: guacamole <gunjanwalecha@gmail.com>
  • Loading branch information
guacamole committed Mar 31, 2022
1 parent b7ab065 commit a410535
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 14 deletions.
1 change: 1 addition & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Authentication interface {
RenewAccessToken(ctx echo.Context) error
VerifyEmail(ctx echo.Context) error
ResetPassword(ctx echo.Context) error
Invites(ctx echo.Context) error
}

// New is the constructor function returns an Authentication implementation
Expand Down
36 changes: 36 additions & 0 deletions auth/invites.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package auth

import (
"encoding/json"
"github.com/labstack/echo/v4"
"net/http"
"strings"
)

type List struct {
Emails string
}

func (a *auth) Invites(ctx echo.Context) error {
var list List
err := json.NewDecoder(ctx.Request().Body).Decode(&list)
if err != nil {
a.logger.Log(ctx, err)
return ctx.JSON(http.StatusBadRequest, echo.Map{
"error": err.Error(),
"msg": "error decode body, expecting and array of emails",
})
}
err = a.emailClient.WelcomeEmail(strings.Split(list.Emails, ","))
if err != nil {
return ctx.JSON(http.StatusInternalServerError, echo.Map{
"error": err.Error(),
"msg": "err sending invites",
})
}

a.logger.Log(ctx, err)
return ctx.JSON(http.StatusAccepted, echo.Map{
"msg": "success",
})
}
2 changes: 2 additions & 0 deletions router/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
// RegisterAuthRoutes includes all the auth related endpoints
func RegisterAuthRoutes(authRouter *echo.Group, authSvc auth.Authentication) {

//send-email/welcome
authRouter.Add(http.MethodPost, "/signup", authSvc.SignUp)
authRouter.Add(http.MethodPost, "/send-email/welcome", authSvc.Invites)
authRouter.Add(http.MethodGet, "/signup/verify", authSvc.VerifyEmail)
authRouter.Add(http.MethodPost, "/signin", authSvc.SignIn)
authRouter.Add(http.MethodPost, "/token", authSvc.SignIn)
Expand Down
20 changes: 7 additions & 13 deletions services/email/createEmail.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,39 @@ import (
"fmt"

"github.com/containerish/OpenRegistry/types"
"github.com/fatih/color"
"github.com/sendgrid/sendgrid-go/helpers/mail"
)

func (e *email) CreateEmail(u *types.User, kind EmailKind, token string) (*mail.SGMailV3, error) {
mailReq := &Mail{}
m := mail.NewV3Mail()

mailReq.To = u.Email
mailReq.To = append(mailReq.To, u.Email)
mailReq.Data.Username = u.Username

switch kind {
case VerifyEmailKind:
m.SetTemplateID(e.config.VerifyEmailTemplateId)
mailReq.Name = "Verify Email"
mailReq.Subject = "OpenRegistry - Verify Email"
mailReq.Name = "OpenRegistry"
mailReq.Subject = "Verify Email"
mailReq.Data.Link = fmt.Sprintf("%s/auth/signup/verify?token=%s", e.backendEndpoint, token)

case ResetPasswordEmailKind:
m.SetTemplateID(e.config.ForgotPasswordTemplateId)
mailReq.Name = "Password Reset"
mailReq.Subject = "OpenRegistry - Forgot Password"
mailReq.Name = "OpenRegistry"
mailReq.Subject = "Forgot Password"
mailReq.Data.Link = fmt.Sprintf("%s/auth/reset-password?token=%s", e.backendEndpoint, token)

case WelcomeEmailKind:
mailReq.Name = "Welcome to OpenRegistry"
mailReq.Subject = "Welcome to OpenRegistry"
mailReq.Data.Link = fmt.Sprintf("%s/send-email/welcome", e.backendEndpoint)
m.SetTemplateID(e.config.WelcomeEmailTemplateId)
default:
return nil, fmt.Errorf("incorrect email kind")
}

color.Magenta("SendAs: %s", e.config.SendAs)
email := mail.NewEmail(mailReq.Name, e.config.SendAs)
m.SetFrom(email)
p := mail.NewPersonalization()

tos := []*mail.Email{
mail.NewEmail(mailReq.To, mailReq.To),
mail.NewEmail(mailReq.To[0], mailReq.To[0]),
}

p.AddTos(tos...)
Expand Down
3 changes: 2 additions & 1 deletion services/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type MailData struct {
type Mail struct {
Data MailData
Name string
To string
To []string
Subject string
Body string
Mtype MailType
Expand All @@ -32,6 +32,7 @@ type Mail struct {
type MailService interface {
CreateEmail(u *types.User, kind EmailKind, token string) (*mail.SGMailV3, error)
SendEmail(u *types.User, token string, kind EmailKind) error
WelcomeEmail(list []string) error
}

func New(config *config.Email, backendEndpoint string) MailService {
Expand Down
35 changes: 35 additions & 0 deletions services/email/welcome_email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package email

import (
"fmt"
"github.com/sendgrid/sendgrid-go/helpers/mail"
"net/http"
)

func (e *email) WelcomeEmail(list []string) error {

mailReq := &Mail{}
m := mail.NewV3Mail()

m.SetTemplateID(e.config.WelcomeEmailTemplateId)
mailReq.Name = "OpenRegistry"
mailReq.Subject = "Welcome to OpenRegistry"
mailReq.Data.Link = fmt.Sprintf("%s/send-email/welcome", e.backendEndpoint)

email := mail.NewEmail(mailReq.Name, e.config.SendAs)
m.SetFrom(email)
p := mail.NewPersonalization()

var tos []*mail.Email
for _, v := range list {
tos = append(tos, mail.NewEmail(v, v))
}
p.AddTos(tos...)
m.AddPersonalizations(p)

resp, err := e.client.Send(m)
if err != nil && resp.StatusCode != http.StatusAccepted {
return fmt.Errorf("ERR_SEND_EMAIL: %w", err)
}
return nil
}

0 comments on commit a410535

Please sign in to comment.