Enable template selection on admin single/bulk and scheduled emails, send welcome emails only after email registration OTP verification, and use the password_reset template for email-based reset links. Co-authored-by: Cursor <cursoragent@cursor.com>
90 lines
2.7 KiB
Go
90 lines
2.7 KiB
Go
package user
|
|
|
|
import (
|
|
"Yimaru-Backend/internal/domain"
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
func (s *Service) renderOtpMessage(ctx context.Context, otpCode, firstName string, otpFor domain.OtpFor, medium domain.OtpMedium, sentTo string) (domain.RenderedEmail, error) {
|
|
if otpFor == domain.OtpReset && medium == domain.OtpMediumEmail {
|
|
return s.renderPasswordResetEmail(ctx, otpCode, firstName, sentTo)
|
|
}
|
|
return s.emailTemplateSvc.Render(ctx, domain.EmailTemplateSlugOTP, map[string]any{
|
|
"OTP": otpCode,
|
|
"FirstName": firstName,
|
|
"ExpiresMinutes": int(OtpExpiry.Minutes()),
|
|
})
|
|
}
|
|
|
|
func (s *Service) renderPasswordResetEmail(ctx context.Context, otpCode, firstName, email string) (domain.RenderedEmail, error) {
|
|
resetLink := buildPasswordResetLink(s.config.PasswordResetBaseURL, s.config.LearnerAppBaseURL, email, otpCode)
|
|
return s.emailTemplateSvc.Render(ctx, domain.EmailTemplateSlugPasswordReset, map[string]any{
|
|
"FirstName": firstName,
|
|
"ResetLink": resetLink,
|
|
"ExpiresMinutes": int(OtpExpiry.Minutes()),
|
|
})
|
|
}
|
|
|
|
func buildPasswordResetLink(passwordResetBaseURL, learnerAppBaseURL, email, otp string) string {
|
|
base := strings.TrimRight(strings.TrimSpace(passwordResetBaseURL), "/")
|
|
if base == "" {
|
|
learnerBase := strings.TrimRight(strings.TrimSpace(learnerAppBaseURL), "/")
|
|
if learnerBase == "" {
|
|
learnerBase = "https://app.yimaruacademy.com"
|
|
}
|
|
base = learnerBase + "/reset-password"
|
|
}
|
|
|
|
u, err := url.Parse(base)
|
|
if err != nil {
|
|
return fmt.Sprintf("%s?email=%s&otp=%s", base, url.QueryEscape(email), url.QueryEscape(otp))
|
|
}
|
|
q := u.Query()
|
|
q.Set("email", email)
|
|
q.Set("otp", otp)
|
|
u.RawQuery = q.Encode()
|
|
return u.String()
|
|
}
|
|
|
|
func (s *Service) learnerLoginURL() string {
|
|
base := strings.TrimRight(strings.TrimSpace(s.config.LearnerAppBaseURL), "/")
|
|
if base == "" {
|
|
return "https://app.yimaruacademy.com/login"
|
|
}
|
|
if strings.HasSuffix(base, "/login") {
|
|
return base
|
|
}
|
|
return base + "/login"
|
|
}
|
|
|
|
func (s *Service) RenderWelcomeEmail(ctx context.Context, firstName string) (domain.RenderedEmail, error) {
|
|
return s.emailTemplateSvc.Render(ctx, domain.EmailTemplateSlugWelcome, map[string]any{
|
|
"FirstName": firstName,
|
|
"LoginURL": s.learnerLoginURL(),
|
|
})
|
|
}
|
|
|
|
func (s *Service) SendWelcomeEmail(ctx context.Context, userID int64) error {
|
|
if s.messengerSvc == nil || s.emailTemplateSvc == nil {
|
|
return fmt.Errorf("email services are not configured")
|
|
}
|
|
|
|
user, err := s.userStore.GetUserByID(ctx, userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if strings.TrimSpace(user.Email) == "" {
|
|
return nil
|
|
}
|
|
|
|
rendered, err := s.RenderWelcomeEmail(ctx, user.FirstName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return s.messengerSvc.SendEmail(ctx, user.Email, rendered.Text, rendered.HTML, rendered.Subject)
|
|
}
|