Add email notifications for task completions

When a task is completed, now sends both:
- Push notification (via Gorush)
- Email notification (via SMTP)

to all residence users except the person who completed the task.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Trey t
2025-11-26 23:29:57 -06:00
parent 5576343175
commit b05ee453c7
3 changed files with 110 additions and 19 deletions

View File

@@ -29,6 +29,7 @@ type TaskService struct {
taskRepo *repositories.TaskRepository
residenceRepo *repositories.ResidenceRepository
notificationService *NotificationService
emailService *EmailService
}
// NewTaskService creates a new task service
@@ -44,6 +45,11 @@ func (s *TaskService) SetNotificationService(ns *NotificationService) {
s.notificationService = ns
}
// SetEmailService sets the email service
func (s *TaskService) SetEmailService(es *EmailService) {
s.emailService = es
}
// === Task CRUD ===
// GetTask gets a task by ID with access check
@@ -485,11 +491,6 @@ func (s *TaskService) CreateCompletion(req *requests.CreateTaskCompletionRequest
// sendTaskCompletedNotification sends notifications when a task is completed
func (s *TaskService) sendTaskCompletedNotification(task *models.Task, completion *models.TaskCompletion) {
if s.notificationService == nil {
log.Debug().Msg("Notification service not configured, skipping task completion notification")
return
}
// Get all users with access to this residence
users, err := s.residenceRepo.GetResidenceUsers(task.ResidenceID)
if err != nil {
@@ -497,6 +498,13 @@ func (s *TaskService) sendTaskCompletedNotification(task *models.Task, completio
return
}
// Get residence name
residence, err := s.residenceRepo.FindByIDSimple(task.ResidenceID)
residenceName := "your property"
if err == nil && residence != nil {
residenceName = residence.Name
}
completedByName := "Someone"
if completion.CompletedBy.ID > 0 {
completedByName = completion.CompletedBy.GetFullName()
@@ -517,19 +525,39 @@ func (s *TaskService) sendTaskCompletedNotification(task *models.Task, completio
continue // Don't notify the person who completed it
}
go func(userID uint) {
ctx := context.Background()
if err := s.notificationService.CreateAndSendNotification(
ctx,
userID,
models.NotificationTaskCompleted,
title,
body,
data,
); err != nil {
log.Error().Err(err).Uint("user_id", userID).Uint("task_id", task.ID).Msg("Failed to send task completion notification")
}
}(user.ID)
// Send push notification
if s.notificationService != nil {
go func(userID uint) {
ctx := context.Background()
if err := s.notificationService.CreateAndSendNotification(
ctx,
userID,
models.NotificationTaskCompleted,
title,
body,
data,
); err != nil {
log.Error().Err(err).Uint("user_id", userID).Uint("task_id", task.ID).Msg("Failed to send task completion push notification")
}
}(user.ID)
}
// Send email notification
if s.emailService != nil && user.Email != "" {
go func(u models.User) {
if err := s.emailService.SendTaskCompletedEmail(
u.Email,
u.GetFullName(),
task.Title,
completedByName,
residenceName,
); err != nil {
log.Error().Err(err).Str("email", u.Email).Uint("task_id", task.ID).Msg("Failed to send task completion email")
} else {
log.Info().Str("email", u.Email).Uint("task_id", task.ID).Msg("Task completion email sent")
}
}(user)
}
}
}