veza/veza-backend-api/internal/models/notification.go

47 lines
1.7 KiB
Go

package models
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
// Notification represents a user notification
// BE-SVC-009: Implement notification service
type Notification struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id" db:"id"`
UserID uuid.UUID `gorm:"type:uuid;not null;index:idx_notifications_user_id" json:"user_id" db:"user_id"`
Type string `gorm:"type:varchar(50);not null;index:idx_notifications_type" json:"type" db:"type"`
Title string `gorm:"type:varchar(255);not null" json:"title" db:"title"`
Content string `gorm:"type:text" json:"content" db:"content"`
Link string `gorm:"type:varchar(500)" json:"link,omitempty" db:"link"`
Read bool `gorm:"default:false;index:idx_notifications_user_id_read" json:"read" db:"read"`
ReadAt *time.Time `json:"read_at,omitempty" db:"read_at"`
CreatedAt time.Time `gorm:"autoCreateTime;index:idx_notifications_created_at_desc" json:"created_at" db:"created_at"`
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at" db:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-" db:"deleted_at"`
// Relations
User User `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" json:"-"`
}
// TableName defines the table name for GORM
func (Notification) TableName() string {
return "notifications"
}
// BeforeCreate hook GORM pour générer UUID si non défini
func (n *Notification) BeforeCreate(tx *gorm.DB) error {
if n.ID == uuid.Nil {
n.ID = uuid.New()
}
return nil
}
// MarkAsRead marks the notification as read
func (n *Notification) MarkAsRead() {
n.Read = true
now := time.Now()
n.ReadAt = &now
}