Skip to content

Commit

Permalink
[FIX][#5] Send email registration and new account
Browse files Browse the repository at this point in the history
  • Loading branch information
jacky-htg committed Jan 17, 2021
1 parent d429d26 commit e1e1b52
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
8 changes: 4 additions & 4 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func main() {

ctx := context.Background()

auth := users.NewAuthServiceClient(conn)
// auth := users.NewAuthServiceClient(conn)
// user := users.NewUserServiceClient(conn)
// company := users.NewCompanyServiceClient(conn)
company := users.NewCompanyServiceClient(conn)
// region := users.NewRegionServiceClient(conn)
// branch := users.NewBranchServiceClient(conn)
// employee := users.NewEmployeeServiceClient(conn)
Expand All @@ -33,7 +33,7 @@ func main() {
// group := users.NewGroupServiceClient(conn)

// ctx = service.Login(ctx, auth)
service.ForgotPassword(ctx, auth)
// service.ForgotPassword(ctx, auth)
// service.ResetPassword(ctx, auth)
// service.ChangePassword(ctx, auth)
// service.IsAuth(ctx, auth)
Expand All @@ -43,7 +43,7 @@ func main() {
// service.DeleteUser(ctx, user)
// service.GetUserByToken(ctx, user)
// service.ListUser(ctx, user)
// service.Registration(ctx, company)
service.Registration(ctx, company)
// service.UpdateCompany(ctx, company)
// service.ViewCompany(ctx, company)
// service.CreateRegion(ctx, region)
Expand Down
25 changes: 24 additions & 1 deletion internal/service/company.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package service
import (
"context"
"database/sql"
"os"
"regexp"
users "user-service/pb"

"user-service/internal/model"
"user-service/internal/pkg/app"
"user-service/internal/pkg/db/redis"
"user-service/internal/pkg/email"

"github.com/sendgrid/sendgrid-go/helpers/mail"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -192,7 +195,27 @@ func (u *Company) Registration(ctx context.Context, in *users.CompanyRegistratio
tx.Commit()

// TODO : currenly we accept logo url, next we should accept logo file and process to upload log
// TODO : send email to inform username and password

// send email registration info
from := mail.NewEmail(os.Getenv("SENDGRID_FROM_NAME"), os.Getenv("SENDGRID_FROM_EMAIL"))
p := mail.NewPersonalization()
tos := []*mail.Email{
mail.NewEmail(companyRegisterModel.Pb.GetUser().GetName(), companyRegisterModel.Pb.GetUser().GetEmail()),
}
p.AddTos(tos...)

p.SetDynamicTemplateData("name", companyRegisterModel.Pb.GetUser().GetName())
p.SetDynamicTemplateData("username", companyRegisterModel.Pb.GetUser().GetUsername())
p.SetDynamicTemplateData("password", companyRegisterModel.Password)
p.SetDynamicTemplateData("app_name", os.Getenv("APP_NAME"))
p.SetDynamicTemplateData("cs_email", os.Getenv("CUSTOMERSERVICE_EMAIL"))
p.SetDynamicTemplateData("cs_phone", os.Getenv("CUSTOMERSERVICE_PHONE"))

err = email.SendMailV3(from, p, os.Getenv("SENDGRID_TEMPLATE_REGISTRATION"))
if err != nil {
return &output, status.Errorf(codes.Internal, "send registration email: %v", err)
}

return &companyRegisterModel.Pb, err
}

Expand Down
23 changes: 22 additions & 1 deletion internal/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package service
import (
"context"
"database/sql"
"os"
"regexp"
"user-service/internal/model"
"user-service/internal/pkg/app"
"user-service/internal/pkg/db/redis"
"user-service/internal/pkg/email"
"user-service/internal/pkg/token"
users "user-service/pb"

"github.com/sendgrid/sendgrid-go/helpers/mail"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -200,7 +203,25 @@ func (u *User) Create(ctx context.Context, in *users.User) (*users.User, error)
return &output, err
}

// TODO : send email to inform username and password
// send email registration info
from := mail.NewEmail(os.Getenv("SENDGRID_FROM_NAME"), os.Getenv("SENDGRID_FROM_EMAIL"))
p := mail.NewPersonalization()
tos := []*mail.Email{
mail.NewEmail(userModel.Pb.GetName(), userModel.Pb.GetEmail()),
}
p.AddTos(tos...)

p.SetDynamicTemplateData("name", userModel.Pb.GetName())
p.SetDynamicTemplateData("username", userModel.Pb.GetUsername())
p.SetDynamicTemplateData("password", userModel.Password)
p.SetDynamicTemplateData("app_name", os.Getenv("APP_NAME"))
p.SetDynamicTemplateData("cs_email", os.Getenv("CUSTOMERSERVICE_EMAIL"))
p.SetDynamicTemplateData("cs_phone", os.Getenv("CUSTOMERSERVICE_PHONE"))

err = email.SendMailV3(from, p, os.Getenv("SENDGRID_TEMPLATE_NEW_USER"))
if err != nil {
return &output, status.Errorf(codes.Internal, "send new account email: %v", err)
}

return &userModel.Pb, nil
}
Expand Down

0 comments on commit e1e1b52

Please sign in to comment.