Add email notification preference for task completion

- Add EmailTaskCompleted field to NotificationPreference model
- Update notification repository to include email preference in queries
- Check email preference before sending task completion emails
- Add email preference toggle to admin panel notification-prefs page
- Update API types for email preference support

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Trey t
2025-12-04 20:04:24 -06:00
parent 0825fd9a12
commit 5a6fbe0a36
7 changed files with 125 additions and 78 deletions

View File

@@ -156,6 +156,16 @@ export default function NotificationPrefsPage() {
/>
),
},
{
accessorKey: 'email_task_completed',
header: 'Email: Completed',
cell: ({ row }) => (
<Switch
checked={row.original.email_task_completed}
onCheckedChange={() => handleToggle(row.original.id, 'email_task_completed', row.original.email_task_completed)}
/>
),
},
{
id: 'actions',
cell: ({ row }) => {
@@ -194,7 +204,7 @@ export default function NotificationPrefsPage() {
<div>
<h1 className="text-2xl font-bold">Notification Preferences</h1>
<p className="text-muted-foreground">
Manage user notification preferences for push notifications
Manage user notification preferences for push notifications and emails
</p>
</div>
</div>

View File

@@ -597,6 +597,8 @@ export interface NotificationPreference {
task_assigned: boolean;
residence_shared: boolean;
warranty_expiring: boolean;
// Email preferences
email_task_completed: boolean;
created_at: string;
updated_at: string;
}
@@ -616,6 +618,8 @@ export interface UpdateNotificationPrefRequest {
task_assigned?: boolean;
residence_shared?: boolean;
warranty_expiring?: boolean;
// Email preferences
email_task_completed?: boolean;
}
// Notification Preferences API

View File

@@ -33,6 +33,10 @@ type NotificationPrefResponse struct {
TaskAssigned bool `json:"task_assigned"`
ResidenceShared bool `json:"residence_shared"`
WarrantyExpiring bool `json:"warranty_expiring"`
// Email preferences
EmailTaskCompleted bool `json:"email_task_completed"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
@@ -108,6 +112,7 @@ func (h *AdminNotificationPrefsHandler) List(c *gin.Context) {
TaskAssigned: pref.TaskAssigned,
ResidenceShared: pref.ResidenceShared,
WarrantyExpiring: pref.WarrantyExpiring,
EmailTaskCompleted: pref.EmailTaskCompleted,
CreatedAt: pref.CreatedAt.Format("2006-01-02T15:04:05Z"),
UpdatedAt: pref.UpdatedAt.Format("2006-01-02T15:04:05Z"),
}
@@ -148,6 +153,7 @@ func (h *AdminNotificationPrefsHandler) Get(c *gin.Context) {
TaskAssigned: pref.TaskAssigned,
ResidenceShared: pref.ResidenceShared,
WarrantyExpiring: pref.WarrantyExpiring,
EmailTaskCompleted: pref.EmailTaskCompleted,
CreatedAt: pref.CreatedAt.Format("2006-01-02T15:04:05Z"),
UpdatedAt: pref.UpdatedAt.Format("2006-01-02T15:04:05Z"),
})
@@ -161,6 +167,9 @@ type UpdateNotificationPrefRequest struct {
TaskAssigned *bool `json:"task_assigned"`
ResidenceShared *bool `json:"residence_shared"`
WarrantyExpiring *bool `json:"warranty_expiring"`
// Email preferences
EmailTaskCompleted *bool `json:"email_task_completed"`
}
// Update handles PUT /api/admin/notification-prefs/:id
@@ -206,6 +215,9 @@ func (h *AdminNotificationPrefsHandler) Update(c *gin.Context) {
if req.WarrantyExpiring != nil {
pref.WarrantyExpiring = *req.WarrantyExpiring
}
if req.EmailTaskCompleted != nil {
pref.EmailTaskCompleted = *req.EmailTaskCompleted
}
if err := h.db.Save(&pref).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update notification preference"})
@@ -226,6 +238,7 @@ func (h *AdminNotificationPrefsHandler) Update(c *gin.Context) {
TaskAssigned: pref.TaskAssigned,
ResidenceShared: pref.ResidenceShared,
WarrantyExpiring: pref.WarrantyExpiring,
EmailTaskCompleted: pref.EmailTaskCompleted,
CreatedAt: pref.CreatedAt.Format("2006-01-02T15:04:05Z"),
UpdatedAt: pref.UpdatedAt.Format("2006-01-02T15:04:05Z"),
})
@@ -284,6 +297,7 @@ func (h *AdminNotificationPrefsHandler) GetByUser(c *gin.Context) {
TaskAssigned: pref.TaskAssigned,
ResidenceShared: pref.ResidenceShared,
WarrantyExpiring: pref.WarrantyExpiring,
EmailTaskCompleted: pref.EmailTaskCompleted,
CreatedAt: pref.CreatedAt.Format("2006-01-02T15:04:05Z"),
UpdatedAt: pref.UpdatedAt.Format("2006-01-02T15:04:05Z"),
})

View File

@@ -20,6 +20,9 @@ type NotificationPreference struct {
// Document notifications
WarrantyExpiring bool `gorm:"column:warranty_expiring;default:true" json:"warranty_expiring"`
// Email preferences
EmailTaskCompleted bool `gorm:"column:email_task_completed;default:true" json:"email_task_completed"`
}
// TableName returns the table name for GORM

View File

@@ -149,6 +149,7 @@ func (r *NotificationRepository) GetOrCreatePreferences(userID uint) (*models.No
TaskAssigned: true,
ResidenceShared: true,
WarrantyExpiring: true,
EmailTaskCompleted: true,
}
if err := r.CreatePreferences(prefs); err != nil {
return nil, err

View File

@@ -190,6 +190,9 @@ func (s *NotificationService) UpdatePreferences(userID uint, req *UpdatePreferen
if req.WarrantyExpiring != nil {
prefs.WarrantyExpiring = *req.WarrantyExpiring
}
if req.EmailTaskCompleted != nil {
prefs.EmailTaskCompleted = *req.EmailTaskCompleted
}
if err := s.notificationRepo.UpdatePreferences(prefs); err != nil {
return nil, err
@@ -358,6 +361,9 @@ type NotificationPreferencesResponse struct {
TaskAssigned bool `json:"task_assigned"`
ResidenceShared bool `json:"residence_shared"`
WarrantyExpiring bool `json:"warranty_expiring"`
// Email preferences
EmailTaskCompleted bool `json:"email_task_completed"`
}
// NewNotificationPreferencesResponse creates a NotificationPreferencesResponse
@@ -369,6 +375,7 @@ func NewNotificationPreferencesResponse(p *models.NotificationPreference) *Notif
TaskAssigned: p.TaskAssigned,
ResidenceShared: p.ResidenceShared,
WarrantyExpiring: p.WarrantyExpiring,
EmailTaskCompleted: p.EmailTaskCompleted,
}
}
@@ -380,6 +387,9 @@ type UpdatePreferencesRequest struct {
TaskAssigned *bool `json:"task_assigned"`
ResidenceShared *bool `json:"residence_shared"`
WarrantyExpiring *bool `json:"warranty_expiring"`
// Email preferences
EmailTaskCompleted *bool `json:"email_task_completed"`
}
// DeviceResponse represents a device in API response

View File

@@ -570,7 +570,11 @@ func (s *TaskService) sendTaskCompletedNotification(task *models.Task, completio
}
// Send email notification (to everyone INCLUDING the person who completed it)
if s.emailService != nil && user.Email != "" {
// Check user's email notification preferences first
if s.emailService != nil && user.Email != "" && s.notificationService != nil {
prefs, err := s.notificationService.GetPreferences(user.ID)
if err != nil || (prefs != nil && prefs.EmailTaskCompleted) {
// Send email if we couldn't get prefs (fail-open) or if email notifications are enabled
go func(u models.User) {
if err := s.emailService.SendTaskCompletedEmail(
u.Email,
@@ -586,6 +590,7 @@ func (s *TaskService) sendTaskCompletedNotification(task *models.Task, completio
}(user)
}
}
}
}
// GetCompletion gets a task completion by ID