gohorsejobs/backend/internal/services/notification_service.go
Tiago Yamamoto 841b1d780c feat: Email System, Avatar Upload, Email Templates UI, and Public Job Posting
- Backend: Email producer (LavinMQ), EmailService interface
- Backend: CRUD API for email_templates and email_settings
- Backend: avatar_url field in users table + UpdateMyProfile support
- Backend: StorageService for pre-signed URLs
- NestJS: Email consumer with Nodemailer and Handlebars
- Frontend: Email Templates admin pages (list/edit)
- Frontend: Updated profileApi.uploadAvatar with pre-signed URL flow
- Frontend: New /post-job public page (company registration + job creation wizard)
- Migrations: 027_create_email_system.sql, 028_add_avatar_url_to_users.sql
2025-12-26 12:21:34 -03:00

92 lines
2.3 KiB
Go

package services
import (
"context"
"database/sql"
"github.com/rede5/gohorsejobs/backend/internal/models"
)
type NotificationService struct {
DB *sql.DB
FCM *FCMService
}
func NewNotificationService(db *sql.DB, fcm *FCMService) *NotificationService {
return &NotificationService{DB: db, FCM: fcm}
}
func (s *NotificationService) CreateNotification(ctx context.Context, userID string, nType, title, message string, link *string) error {
query := `
INSERT INTO notifications (user_id, type, title, message, link, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, NOW(), NOW())
`
_, err := s.DB.ExecContext(ctx, query, userID, nType, title, message, link)
return err
}
func (s *NotificationService) ListNotifications(ctx context.Context, userID string) ([]models.Notification, error) {
query := `
SELECT id, user_id, type, title, message, link, read_at, created_at, updated_at
FROM notifications
WHERE user_id = $1
ORDER BY created_at DESC
LIMIT 50
`
rows, err := s.DB.QueryContext(ctx, query, userID)
if err != nil {
return nil, err
}
defer rows.Close()
notifications := []models.Notification{}
for rows.Next() {
var n models.Notification
if err := rows.Scan(
&n.ID,
&n.UserID,
&n.Type,
&n.Title,
&n.Message,
&n.Link,
&n.ReadAt,
&n.CreatedAt,
&n.UpdatedAt,
); err != nil {
return nil, err
}
notifications = append(notifications, n)
}
return notifications, nil
}
func (s *NotificationService) MarkAsRead(ctx context.Context, id string, userID string) error {
query := `
UPDATE notifications
SET read_at = NOW(), updated_at = NOW()
WHERE id = $1 AND user_id = $2
`
_, err := s.DB.ExecContext(ctx, query, id, userID)
return err
}
func (s *NotificationService) MarkAllAsRead(ctx context.Context, userID string) error {
query := `
UPDATE notifications
SET read_at = NOW(), updated_at = NOW()
WHERE user_id = $1 AND read_at IS NULL
`
_, err := s.DB.ExecContext(ctx, query, userID)
return err
}
func (s *NotificationService) SaveFCMToken(ctx context.Context, userID, token, platform string) error {
query := `
INSERT INTO fcm_tokens (user_id, token, platform, last_seen_at)
VALUES ($1, $2, $3, NOW())
ON CONFLICT (user_id, token)
DO UPDATE SET last_seen_at = NOW(), platform = EXCLUDED.platform
`
_, err := s.DB.ExecContext(ctx, query, userID, token, platform)
return err
}