237 lines
7.6 KiB
Go
237 lines
7.6 KiB
Go
|
|
package services
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"log"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"attune-heart-therapy/internal/config"
|
||
|
|
"attune-heart-therapy/internal/models"
|
||
|
|
"attune-heart-therapy/internal/repositories"
|
||
|
|
"attune-heart-therapy/internal/templates"
|
||
|
|
|
||
|
|
"gopkg.in/gomail.v2"
|
||
|
|
)
|
||
|
|
|
||
|
|
// notificationService implements the NotificationService interface
|
||
|
|
type notificationService struct {
|
||
|
|
notificationRepo repositories.NotificationRepository
|
||
|
|
templateService *templates.EmailTemplateService
|
||
|
|
config *config.Config
|
||
|
|
dialer *gomail.Dialer
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewNotificationService creates a new instance of NotificationService
|
||
|
|
func NewNotificationService(notificationRepo repositories.NotificationRepository, cfg *config.Config) NotificationService {
|
||
|
|
dialer := gomail.NewDialer(
|
||
|
|
cfg.SMTP.Host,
|
||
|
|
cfg.SMTP.Port,
|
||
|
|
cfg.SMTP.Username,
|
||
|
|
cfg.SMTP.Password,
|
||
|
|
)
|
||
|
|
|
||
|
|
return ¬ificationService{
|
||
|
|
notificationRepo: notificationRepo,
|
||
|
|
templateService: templates.NewEmailTemplateService(),
|
||
|
|
config: cfg,
|
||
|
|
dialer: dialer,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// SendWelcomeEmail sends a welcome email to a newly registered user
|
||
|
|
func (s *notificationService) SendWelcomeEmail(user *models.User) error {
|
||
|
|
templateData := templates.TemplateData{
|
||
|
|
User: user,
|
||
|
|
}
|
||
|
|
|
||
|
|
emailTemplate, err := s.templateService.RenderTemplate(models.NotificationTypeWelcome, templateData)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("failed to render welcome email template: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
notification := &models.Notification{
|
||
|
|
UserID: user.ID,
|
||
|
|
Type: models.NotificationTypeWelcome,
|
||
|
|
Subject: emailTemplate.Subject,
|
||
|
|
Body: emailTemplate.Body,
|
||
|
|
Status: models.NotificationStatusPending,
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := s.notificationRepo.Create(notification); err != nil {
|
||
|
|
return fmt.Errorf("failed to create welcome notification: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return s.sendEmail(user.Email, emailTemplate.Subject, emailTemplate.Body)
|
||
|
|
}
|
||
|
|
|
||
|
|
// SendPaymentNotification sends payment status notification to user
|
||
|
|
func (s *notificationService) SendPaymentNotification(user *models.User, booking *models.Booking, success bool) error {
|
||
|
|
var notificationType models.NotificationType
|
||
|
|
if success {
|
||
|
|
notificationType = models.NotificationTypePaymentSuccess
|
||
|
|
} else {
|
||
|
|
notificationType = models.NotificationTypePaymentFailed
|
||
|
|
}
|
||
|
|
|
||
|
|
templateData := templates.TemplateData{
|
||
|
|
User: user,
|
||
|
|
Booking: booking,
|
||
|
|
}
|
||
|
|
|
||
|
|
emailTemplate, err := s.templateService.RenderTemplate(notificationType, templateData)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("failed to render payment notification template: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
notification := &models.Notification{
|
||
|
|
UserID: user.ID,
|
||
|
|
BookingID: &booking.ID,
|
||
|
|
Type: notificationType,
|
||
|
|
Subject: emailTemplate.Subject,
|
||
|
|
Body: emailTemplate.Body,
|
||
|
|
Status: models.NotificationStatusPending,
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := s.notificationRepo.Create(notification); err != nil {
|
||
|
|
return fmt.Errorf("failed to create payment notification: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return s.sendEmail(user.Email, emailTemplate.Subject, emailTemplate.Body)
|
||
|
|
}
|
||
|
|
|
||
|
|
// SendMeetingInfo sends meeting information to user after successful booking
|
||
|
|
func (s *notificationService) SendMeetingInfo(user *models.User, booking *models.Booking) error {
|
||
|
|
templateData := templates.TemplateData{
|
||
|
|
User: user,
|
||
|
|
Booking: booking,
|
||
|
|
}
|
||
|
|
|
||
|
|
emailTemplate, err := s.templateService.RenderTemplate(models.NotificationTypeMeetingInfo, templateData)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("failed to render meeting info template: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
notification := &models.Notification{
|
||
|
|
UserID: user.ID,
|
||
|
|
BookingID: &booking.ID,
|
||
|
|
Type: models.NotificationTypeMeetingInfo,
|
||
|
|
Subject: emailTemplate.Subject,
|
||
|
|
Body: emailTemplate.Body,
|
||
|
|
Status: models.NotificationStatusPending,
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := s.notificationRepo.Create(notification); err != nil {
|
||
|
|
return fmt.Errorf("failed to create meeting info notification: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return s.sendEmail(user.Email, emailTemplate.Subject, emailTemplate.Body)
|
||
|
|
}
|
||
|
|
|
||
|
|
// SendReminder sends a reminder notification to user before their meeting
|
||
|
|
func (s *notificationService) SendReminder(user *models.User, booking *models.Booking) error {
|
||
|
|
templateData := templates.TemplateData{
|
||
|
|
User: user,
|
||
|
|
Booking: booking,
|
||
|
|
ReminderText: templates.GetReminderText(booking.ScheduledAt),
|
||
|
|
}
|
||
|
|
|
||
|
|
emailTemplate, err := s.templateService.RenderTemplate(models.NotificationTypeReminder, templateData)
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("failed to render reminder template: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
notification := &models.Notification{
|
||
|
|
UserID: user.ID,
|
||
|
|
BookingID: &booking.ID,
|
||
|
|
Type: models.NotificationTypeReminder,
|
||
|
|
Subject: emailTemplate.Subject,
|
||
|
|
Body: emailTemplate.Body,
|
||
|
|
Status: models.NotificationStatusPending,
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := s.notificationRepo.Create(notification); err != nil {
|
||
|
|
return fmt.Errorf("failed to create reminder notification: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return s.sendEmail(user.Email, emailTemplate.Subject, emailTemplate.Body)
|
||
|
|
}
|
||
|
|
|
||
|
|
// ScheduleReminder schedules a reminder notification for a specific time
|
||
|
|
func (s *notificationService) ScheduleReminder(bookingID uint, reminderTime time.Time) error {
|
||
|
|
// Create a scheduled notification that will be processed later
|
||
|
|
notification := &models.Notification{
|
||
|
|
BookingID: &bookingID,
|
||
|
|
Type: models.NotificationTypeReminder,
|
||
|
|
Subject: "Scheduled Reminder",
|
||
|
|
Body: "This is a scheduled reminder notification",
|
||
|
|
Status: models.NotificationStatusPending,
|
||
|
|
ScheduledAt: &reminderTime,
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := s.notificationRepo.Create(notification); err != nil {
|
||
|
|
return fmt.Errorf("failed to schedule reminder: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
log.Printf("Reminder scheduled for booking %d at %s", bookingID, reminderTime.Format(time.RFC3339))
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// ProcessPendingNotifications processes all pending notifications that are ready to be sent
|
||
|
|
func (s *notificationService) ProcessPendingNotifications() error {
|
||
|
|
notifications, err := s.notificationRepo.GetPendingNotifications()
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("failed to get pending notifications: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, notification := range notifications {
|
||
|
|
if !notification.IsReadyToSend() {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
|
||
|
|
// For reminder notifications, we need to fetch the booking and user data
|
||
|
|
if notification.Type == models.NotificationTypeReminder && notification.BookingID != nil {
|
||
|
|
// This would require additional repository methods to fetch booking with user
|
||
|
|
// For now, we'll skip processing reminders in batch processing
|
||
|
|
log.Printf("Skipping reminder notification %d - requires specific booking context", notification.ID)
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
|
||
|
|
// Send the notification
|
||
|
|
if err := s.sendEmail(notification.User.Email, notification.Subject, notification.Body); err != nil {
|
||
|
|
notification.MarkAsFailed(err.Error())
|
||
|
|
log.Printf("Failed to send notification %d: %v", notification.ID, err)
|
||
|
|
} else {
|
||
|
|
notification.MarkAsSent()
|
||
|
|
log.Printf("Successfully sent notification %d to %s", notification.ID, notification.User.Email)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update the notification status
|
||
|
|
if err := s.notificationRepo.Update(¬ification); err != nil {
|
||
|
|
log.Printf("Failed to update notification %d status: %v", notification.ID, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// sendEmail sends an email using the configured SMTP settings
|
||
|
|
func (s *notificationService) sendEmail(to, subject, body string) error {
|
||
|
|
if s.config.SMTP.Host == "" || s.config.SMTP.From == "" {
|
||
|
|
log.Printf("SMTP not configured, skipping email to %s", to)
|
||
|
|
return nil // Don't fail if SMTP is not configured
|
||
|
|
}
|
||
|
|
|
||
|
|
message := gomail.NewMessage()
|
||
|
|
message.SetHeader("From", s.config.SMTP.From)
|
||
|
|
message.SetHeader("To", to)
|
||
|
|
message.SetHeader("Subject", subject)
|
||
|
|
message.SetBody("text/html", body)
|
||
|
|
|
||
|
|
if err := s.dialer.DialAndSend(message); err != nil {
|
||
|
|
return fmt.Errorf("failed to send email to %s: %w", to, err)
|
||
|
|
}
|
||
|
|
|
||
|
|
log.Printf("Email sent successfully to %s", to)
|
||
|
|
return nil
|
||
|
|
}
|