Files
honeyDueAPI/internal/services/onboarding_email_service.go
Trey t 42a5533a56 Fix 113 hardening issues across entire Go backend
Security:
- Replace all binding: tags with validate: + c.Validate() in admin handlers
- Add rate limiting to auth endpoints (login, register, password reset)
- Add security headers (HSTS, XSS protection, nosniff, frame options)
- Wire Google Pub/Sub token verification into webhook handler
- Replace ParseUnverified with proper OIDC/JWKS key verification
- Verify inner Apple JWS signatures in webhook handler
- Add io.LimitReader (1MB) to all webhook body reads
- Add ownership verification to file deletion
- Move hardcoded admin credentials to env vars
- Add uniqueIndex to User.Email
- Hide ConfirmationCode from JSON serialization
- Mask confirmation codes in admin responses
- Use http.DetectContentType for upload validation
- Fix path traversal in storage service
- Replace os.Getenv with Viper in stripe service
- Sanitize Redis URLs before logging
- Separate DEBUG_FIXED_CODES from DEBUG flag
- Reject weak SECRET_KEY in production
- Add host check on /_next/* proxy routes
- Use explicit localhost CORS origins in debug mode
- Replace err.Error() with generic messages in all admin error responses

Critical fixes:
- Rewrite FCM to HTTP v1 API with OAuth 2.0 service account auth
- Fix user_customuser -> auth_user table names in raw SQL
- Fix dashboard verified query to use UserProfile model
- Add escapeLikeWildcards() to prevent SQL wildcard injection

Bug fixes:
- Add bounds checks for days/expiring_soon query params (1-3650)
- Add receipt_data/transaction_id empty-check to RestoreSubscription
- Change Active bool -> *bool in device handler
- Check all unchecked GORM/FindByIDWithProfile errors
- Add validation for notification hour fields (0-23)
- Add max=10000 validation on task description updates

Transactions & data integrity:
- Wrap registration flow in transaction
- Wrap QuickComplete in transaction
- Move image creation inside completion transaction
- Wrap SetSpecialties in transaction
- Wrap GetOrCreateToken in transaction
- Wrap completion+image deletion in transaction

Performance:
- Batch completion summaries (2 queries vs 2N)
- Reuse single http.Client in IAP validation
- Cache dashboard counts (30s TTL)
- Batch COUNT queries in admin user list
- Add Limit(500) to document queries
- Add reminder_stage+due_date filters to reminder queries
- Parse AllowedTypes once at init
- In-memory user cache in auth middleware (30s TTL)
- Timezone change detection cache
- Optimize P95 with per-endpoint sorted buffers
- Replace crypto/md5 with hash/fnv for ETags

Code quality:
- Add sync.Once to all monitoring Stop()/Close() methods
- Replace 8 fmt.Printf with zerolog in auth service
- Log previously discarded errors
- Standardize delete response shapes
- Route hardcoded English through i18n
- Remove FileURL from DocumentResponse (keep MediaURL only)
- Thread user timezone through kanban board responses
- Initialize empty slices to prevent null JSON
- Extract shared field map for task Update/UpdateTx
- Delete unused SoftDeleteModel, min(), formatCron, legacy handlers

Worker & jobs:
- Wire Asynq email infrastructure into worker
- Register HandleReminderLogCleanup with daily 3AM cron
- Use per-user timezone in HandleSmartReminder
- Replace direct DB queries with repository calls
- Delete legacy reminder handlers (~200 lines)
- Delete unused task type constants

Dependencies:
- Replace archived jung-kurt/gofpdf with go-pdf/fpdf
- Replace unmaintained gomail.v2 with wneessen/go-mail
- Add TODO for Echo jwt v3 transitive dep removal

Test infrastructure:
- Fix MakeRequest/SeedLookupData error handling
- Replace os.Exit(0) with t.Skip() in scope/consistency tests
- Add 11 new FCM v1 tests

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 23:14:13 -05:00

396 lines
13 KiB
Go

package services
import (
"crypto/rand"
"encoding/hex"
"fmt"
"time"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
"github.com/treytartt/honeydue-api/internal/models"
)
// OnboardingEmailService handles sending and tracking onboarding emails
type OnboardingEmailService struct {
db *gorm.DB
emailService *EmailService
baseURL string // Base URL for tracking pixel
}
// NewOnboardingEmailService creates a new onboarding email service
func NewOnboardingEmailService(db *gorm.DB, emailService *EmailService, baseURL string) *OnboardingEmailService {
return &OnboardingEmailService{
db: db,
emailService: emailService,
baseURL: baseURL,
}
}
// generateTrackingID generates a unique tracking ID for email open tracking
func generateTrackingID() string {
bytes := make([]byte, 32)
if _, err := rand.Read(bytes); err != nil {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
return hex.EncodeToString(bytes)
}
// HasSentEmail checks if a specific email type has already been sent to a user
func (s *OnboardingEmailService) HasSentEmail(userID uint, emailType models.OnboardingEmailType) bool {
// TODO(hardening): Replace with OnboardingEmailRepository.HasSentEmail()
var count int64
if err := s.db.Model(&models.OnboardingEmail{}).
Where("user_id = ? AND email_type = ?", userID, emailType).
Count(&count).Error; err != nil {
log.Error().Err(err).Uint("user_id", userID).Str("email_type", string(emailType)).Msg("Failed to check if email was sent")
return false
}
return count > 0
}
// RecordEmailSent records that an email was sent to a user
func (s *OnboardingEmailService) RecordEmailSent(userID uint, emailType models.OnboardingEmailType, trackingID string) error {
// TODO(hardening): Replace with OnboardingEmailRepository.Create()
email := &models.OnboardingEmail{
UserID: userID,
EmailType: emailType,
SentAt: time.Now().UTC(),
TrackingID: trackingID,
}
if err := s.db.Create(email).Error; err != nil {
return fmt.Errorf("failed to record email sent: %w", err)
}
return nil
}
// RecordEmailOpened records that an email was opened based on tracking ID
func (s *OnboardingEmailService) RecordEmailOpened(trackingID string) error {
// TODO(hardening): Replace with OnboardingEmailRepository.MarkOpened()
now := time.Now().UTC()
result := s.db.Model(&models.OnboardingEmail{}).
Where("tracking_id = ? AND opened_at IS NULL", trackingID).
Update("opened_at", now)
if result.Error != nil {
return fmt.Errorf("failed to record email opened: %w", result.Error)
}
if result.RowsAffected > 0 {
log.Info().Str("tracking_id", trackingID).Msg("Email open recorded")
}
return nil
}
// GetEmailHistory gets all onboarding emails for a specific user
func (s *OnboardingEmailService) GetEmailHistory(userID uint) ([]models.OnboardingEmail, error) {
// TODO(hardening): Replace with OnboardingEmailRepository.FindByUserID()
var emails []models.OnboardingEmail
if err := s.db.Where("user_id = ?", userID).Order("sent_at DESC").Find(&emails).Error; err != nil {
return nil, err
}
return emails, nil
}
// GetAllEmailHistory gets all onboarding emails with pagination
func (s *OnboardingEmailService) GetAllEmailHistory(page, pageSize int) ([]models.OnboardingEmail, int64, error) {
var emails []models.OnboardingEmail
var total int64
if page < 1 {
page = 1
}
if pageSize < 1 {
pageSize = 20
}
offset := (page - 1) * pageSize
// TODO(hardening): Replace with OnboardingEmailRepository.CountAll()
// Count total
if err := s.db.Model(&models.OnboardingEmail{}).Count(&total).Error; err != nil {
return nil, 0, err
}
// TODO(hardening): Replace with OnboardingEmailRepository.FindAllPaginated()
// Get paginated results with user info
if err := s.db.Preload("User").
Order("sent_at DESC").
Offset(offset).
Limit(pageSize).
Find(&emails).Error; err != nil {
return nil, 0, err
}
return emails, total, nil
}
// GetEmailStats returns statistics about onboarding emails
func (s *OnboardingEmailService) GetEmailStats() (*OnboardingEmailStats, error) {
stats := &OnboardingEmailStats{}
// TODO(hardening): Replace with OnboardingEmailRepository.GetStats()
// No residence email stats
var noResTotal, noResOpened int64
if err := s.db.Model(&models.OnboardingEmail{}).
Where("email_type = ?", models.OnboardingEmailNoResidence).
Count(&noResTotal).Error; err != nil {
log.Error().Err(err).Msg("Failed to count no-residence emails")
}
if err := s.db.Model(&models.OnboardingEmail{}).
Where("email_type = ? AND opened_at IS NOT NULL", models.OnboardingEmailNoResidence).
Count(&noResOpened).Error; err != nil {
log.Error().Err(err).Msg("Failed to count opened no-residence emails")
}
stats.NoResidenceTotal = noResTotal
stats.NoResidenceOpened = noResOpened
// No tasks email stats
var noTasksTotal, noTasksOpened int64
if err := s.db.Model(&models.OnboardingEmail{}).
Where("email_type = ?", models.OnboardingEmailNoTasks).
Count(&noTasksTotal).Error; err != nil {
log.Error().Err(err).Msg("Failed to count no-tasks emails")
}
if err := s.db.Model(&models.OnboardingEmail{}).
Where("email_type = ? AND opened_at IS NOT NULL", models.OnboardingEmailNoTasks).
Count(&noTasksOpened).Error; err != nil {
log.Error().Err(err).Msg("Failed to count opened no-tasks emails")
}
stats.NoTasksTotal = noTasksTotal
stats.NoTasksOpened = noTasksOpened
return stats, nil
}
// TODO(hardening): Move to internal/dto/responses/onboarding_email.go
// OnboardingEmailStats represents statistics about onboarding emails
type OnboardingEmailStats struct {
NoResidenceTotal int64 `json:"no_residence_total"`
NoResidenceOpened int64 `json:"no_residence_opened"`
NoTasksTotal int64 `json:"no_tasks_total"`
NoTasksOpened int64 `json:"no_tasks_opened"`
}
// UsersNeedingNoResidenceEmail finds verified users who registered 2+ days ago but have no residence
func (s *OnboardingEmailService) UsersNeedingNoResidenceEmail() ([]models.User, error) {
var users []models.User
twoDaysAgo := time.Now().UTC().AddDate(0, 0, -2)
// TODO(hardening): Replace with OnboardingEmailRepository.FindUsersWithoutResidence()
// Find users who:
// 1. Are verified
// 2. Registered 2+ days ago
// 3. Have no residences
// 4. Haven't received this email type yet
err := s.db.Raw(`
SELECT u.* FROM auth_user u
LEFT JOIN residence_residence r ON r.owner_id = u.id AND r.is_active = true
LEFT JOIN onboarding_emails oe ON oe.user_id = u.id AND oe.email_type = ?
WHERE u.is_active = true
AND u.date_joined < ?
AND r.id IS NULL
AND oe.id IS NULL
`, models.OnboardingEmailNoResidence, twoDaysAgo).Scan(&users).Error
if err != nil {
return nil, fmt.Errorf("failed to find users needing no-residence email: %w", err)
}
return users, nil
}
// UsersNeedingNoTasksEmail finds verified users who created their first residence 5+ days ago but have no tasks
func (s *OnboardingEmailService) UsersNeedingNoTasksEmail() ([]models.User, error) {
var users []models.User
fiveDaysAgo := time.Now().UTC().AddDate(0, 0, -5)
// TODO(hardening): Replace with OnboardingEmailRepository.FindUsersWithoutTasks()
// Find users who:
// 1. Are verified
// 2. Have at least one residence
// 3. Their first residence was created 5+ days ago
// 4. Have no tasks across ANY of their residences
// 5. Haven't received this email type yet
err := s.db.Raw(`
SELECT DISTINCT u.* FROM auth_user u
INNER JOIN residence_residence r ON r.owner_id = u.id AND r.is_active = true
LEFT JOIN task_task t ON t.residence_id IN (SELECT id FROM residence_residence WHERE owner_id = u.id AND is_active = true) AND t.is_cancelled = false AND t.is_archived = false
LEFT JOIN onboarding_emails oe ON oe.user_id = u.id AND oe.email_type = ?
WHERE u.is_active = true
AND t.id IS NULL
AND oe.id IS NULL
AND EXISTS (
SELECT 1 FROM residence_residence r2
WHERE r2.owner_id = u.id
AND r2.is_active = true
AND r2.created_at < ?
)
`, models.OnboardingEmailNoTasks, fiveDaysAgo).Scan(&users).Error
if err != nil {
return nil, fmt.Errorf("failed to find users needing no-tasks email: %w", err)
}
return users, nil
}
// CheckAndSendNoResidenceEmails finds eligible users and sends them the no-residence onboarding email
func (s *OnboardingEmailService) CheckAndSendNoResidenceEmails() (int, error) {
users, err := s.UsersNeedingNoResidenceEmail()
if err != nil {
return 0, err
}
sentCount := 0
for _, user := range users {
if err := s.sendNoResidenceEmail(user); err != nil {
log.Error().Err(err).Uint("user_id", user.ID).Msg("Failed to send no-residence onboarding email")
continue
}
sentCount++
}
if sentCount > 0 {
log.Info().Int("count", sentCount).Msg("Sent no-residence onboarding emails")
}
return sentCount, nil
}
// CheckAndSendNoTasksEmails finds eligible users and sends them the no-tasks onboarding email
func (s *OnboardingEmailService) CheckAndSendNoTasksEmails() (int, error) {
users, err := s.UsersNeedingNoTasksEmail()
if err != nil {
return 0, err
}
sentCount := 0
for _, user := range users {
if err := s.sendNoTasksEmail(user); err != nil {
log.Error().Err(err).Uint("user_id", user.ID).Msg("Failed to send no-tasks onboarding email")
continue
}
sentCount++
}
if sentCount > 0 {
log.Info().Int("count", sentCount).Msg("Sent no-tasks onboarding emails")
}
return sentCount, nil
}
// sendNoResidenceEmail sends the no-residence onboarding email to a user
func (s *OnboardingEmailService) sendNoResidenceEmail(user models.User) error {
if user.Email == "" {
return fmt.Errorf("user has no email address")
}
// Generate tracking ID
trackingID := generateTrackingID()
// Send email
if err := s.emailService.SendNoResidenceOnboardingEmail(user.Email, user.FirstName, s.baseURL, trackingID); err != nil {
return err
}
// Record that email was sent
if err := s.RecordEmailSent(user.ID, models.OnboardingEmailNoResidence, trackingID); err != nil {
// Email was sent but we failed to record it - log but don't fail
log.Error().Err(err).Uint("user_id", user.ID).Msg("Failed to record no-residence email sent")
}
return nil
}
// sendNoTasksEmail sends the no-tasks onboarding email to a user
func (s *OnboardingEmailService) sendNoTasksEmail(user models.User) error {
if user.Email == "" {
return fmt.Errorf("user has no email address")
}
// Generate tracking ID
trackingID := generateTrackingID()
// Send email
if err := s.emailService.SendNoTasksOnboardingEmail(user.Email, user.FirstName, s.baseURL, trackingID); err != nil {
return err
}
// Record that email was sent
if err := s.RecordEmailSent(user.ID, models.OnboardingEmailNoTasks, trackingID); err != nil {
// Email was sent but we failed to record it - log but don't fail
log.Error().Err(err).Uint("user_id", user.ID).Msg("Failed to record no-tasks email sent")
}
return nil
}
// SendOnboardingEmailToUser manually sends an onboarding email to a specific user
// This is used by admin to force-send emails regardless of eligibility criteria
func (s *OnboardingEmailService) SendOnboardingEmailToUser(userID uint, emailType models.OnboardingEmailType) error {
// TODO(hardening): Replace with UserRepository.FindByID() (inject UserRepository)
// Load the user
var user models.User
if err := s.db.First(&user, userID).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return fmt.Errorf("user not found")
}
return fmt.Errorf("failed to load user: %w", err)
}
if user.Email == "" {
return fmt.Errorf("user has no email address")
}
// Check if already sent (for tracking purposes, we still record but warn)
alreadySent := s.HasSentEmail(userID, emailType)
// Generate tracking ID
trackingID := generateTrackingID()
// Send email based on type
var sendErr error
switch emailType {
case models.OnboardingEmailNoResidence:
sendErr = s.emailService.SendNoResidenceOnboardingEmail(user.Email, user.FirstName, s.baseURL, trackingID)
case models.OnboardingEmailNoTasks:
sendErr = s.emailService.SendNoTasksOnboardingEmail(user.Email, user.FirstName, s.baseURL, trackingID)
default:
return fmt.Errorf("unknown email type: %s", emailType)
}
if sendErr != nil {
return fmt.Errorf("failed to send email: %w", sendErr)
}
// If already sent before, delete the old record first to allow re-recording
// This allows admins to "resend" emails while still tracking them
if alreadySent {
// TODO(hardening): Replace with OnboardingEmailRepository.DeleteByUserAndType()
if err := s.db.Where("user_id = ? AND email_type = ?", userID, emailType).Delete(&models.OnboardingEmail{}).Error; err != nil {
log.Error().Err(err).Uint("user_id", userID).Str("email_type", string(emailType)).Msg("Failed to delete old onboarding email record before resend")
}
}
// Record that email was sent
if err := s.RecordEmailSent(userID, emailType, trackingID); err != nil {
log.Error().Err(err).Uint("user_id", userID).Str("email_type", string(emailType)).Msg("Failed to record onboarding email sent")
}
log.Info().
Uint("user_id", userID).
Str("email_type", string(emailType)).
Str("email", user.Email).
Bool("was_resend", alreadySent).
Msg("Onboarding email sent manually")
return nil
}