Spaces:
Configuration error
Configuration error
File size: 2,080 Bytes
98c95a0 48471f7 98c95a0 48471f7 98c95a0 48471f7 98c95a0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | package worker
import (
"api.qobiltu.id/assets"
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/hibiken/asynq"
"html/template"
"log/slog"
)
const (
TaskSendVerifyEmailMaxRetry = 3
TaskSendVerifyEmail = "task:send_verify_email"
TaskSendVerifyEmailSubject = "Verifikasi Email"
)
type PayloadSendVerifyEmail struct {
EmailAddress string `json:"email_address"`
VerificationCode string `json:"verification_code"`
ExpirationInMinutes int `json:"expiration_in_minutes"`
Subject string `json:"subject"`
}
func (d *RedisTaskDistributor) DistributeTaskSendVerifyEmail(
ctx context.Context,
payload *PayloadSendVerifyEmail,
opts ...asynq.Option,
) error {
jsonPayload, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("failed to marshal task payload: %w", err)
}
task := asynq.NewTask(TaskSendVerifyEmail, jsonPayload, opts...)
_, err = d.client.EnqueueContext(ctx, task)
if err != nil {
return fmt.Errorf("failed to enqueue task: %w", err)
}
return nil
}
func (p *RedisTaskProcessor) ProcessTaskSendVerifyEmail(ctx context.Context, task *asynq.Task) error {
var payload PayloadSendVerifyEmail
if err := json.Unmarshal(task.Payload(), &payload); err != nil {
return fmt.Errorf("failed to unmarshal payload: %w", asynq.SkipRetry)
}
var tmpl *template.Template
tmpl, err := template.ParseFS(assets.EmbeddedFiles, assets.EmailConfirmationTemplatePath)
if err != nil {
return fmt.Errorf("failed to parse email template: %w", err)
}
var body bytes.Buffer
if err := tmpl.ExecuteTemplate(&body, "htmlBody", payload); err != nil {
return fmt.Errorf("failed to execute email template: %w", err)
}
htmlContent := body.String()
slog.Info("Sending verification email", slog.String("email", payload.EmailAddress))
err = p.emailSender.Send(payload.EmailAddress, payload.Subject, htmlContent, payload)
if err != nil {
return fmt.Errorf("failed to send verify email: %w", err)
}
slog.Info("Verification email sent successfully", slog.String("email", payload.EmailAddress))
return nil
}
|