in-app notification bug fix

This commit is contained in:
Mazyar
2026-05-31 03:05:40 +03:30
parent bca94cd69a
commit ef57b302d9
4 changed files with 134 additions and 110 deletions
+29 -10
View File
@@ -19,7 +19,7 @@ var (
// Notification represents a stored notification in the database
type Notification struct {
ID string `bson:"_id,omitempty" json:"id"`
orm.Model `bson:",inline"`
UserID string `bson:"user_id" json:"user_id"`
Title string `bson:"title" json:"title"`
Message string `bson:"message" json:"message"`
@@ -32,19 +32,27 @@ type Notification struct {
Methods map[string]string `bson:"methods,omitempty" json:"methods,omitempty"`
Metadata map[string]any `bson:"metadata,omitempty" json:"metadata,omitempty"`
ScheduleAt int64 `bson:"schedule_at,omitempty" json:"schedule_at,omitempty"`
CreatedAt int64 `bson:"created_at" json:"created_at"`
UpdatedAt int64 `bson:"updated_at" json:"updated_at"`
Seen bool `bson:"seen" json:"seen"`
SeenAt int64 `bson:"seen_at,omitempty" json:"seen_at,omitempty"`
IsScheduled bool `bson:"is_scheduled" json:"is_scheduled"`
ScheduledAt int64 `bson:"scheduled_at,omitempty" json:"scheduled_at,omitempty"`
}
// SetID sets the notification ID (implements IDSetter interface)
func (n *Notification) SetID(id string) {
n.Model.SetID(id)
}
// GetID returns the notification ID (implements IDGetter interface)
func (n *Notification) GetID() string {
return n.Model.GetID()
}
// Repository defines methods for notification data access
type Repository interface {
Create(ctx context.Context, notification *Notification) error
GetByID(ctx context.Context, id string) (*Notification, error)
GetByUserID(ctx context.Context, userID string, status string, seen *bool, priority, eventType, notificationType string, pagination *response.Pagination) (*orm.PaginatedResult[Notification], error)
GetByUserID(ctx context.Context, userIDs []string, status string, seen *bool, priority, eventType, notificationType string, pagination *response.Pagination) (*orm.PaginatedResult[Notification], error)
Update(ctx context.Context, notification *Notification) error
MarkAsSeen(ctx context.Context, notificationID, userID string) error
MarkAllAsSeen(ctx context.Context, userID string) error
@@ -106,9 +114,15 @@ func (r *notificationRepository) GetByID(ctx context.Context, id string) (*Notif
return r.ormRepo.FindByID(ctx, id)
}
// GetByUserID retrieves notifications for a specific user with filters
func (r *notificationRepository) GetByUserID(ctx context.Context, userID string, status string, seen *bool, priority, eventType, notificationType string, pagination *response.Pagination) (*orm.PaginatedResult[Notification], error) {
filter := bson.M{"user_id": userID}
// GetByUserID retrieves notifications with optional user filters
func (r *notificationRepository) GetByUserID(ctx context.Context, userIDs []string, status string, seen *bool, priority, eventType, notificationType string, pagination *response.Pagination) (*orm.PaginatedResult[Notification], error) {
filter := bson.M{}
if len(userIDs) == 1 {
filter["user_id"] = userIDs[0]
} else if len(userIDs) > 1 {
filter["user_id"] = bson.M{"$in": userIDs}
}
if status != "" {
filter["status"] = status
@@ -146,8 +160,8 @@ func (r *notificationRepository) GetByUserID(ctx context.Context, userID string,
result, err := r.ormRepo.FindAll(ctx, filter, mongoPagination)
if err != nil {
r.logger.Error("Failed to find notifications", map[string]interface{}{
"error": err.Error(),
"user_id": userID,
"error": err.Error(),
"user_ids": userIDs,
})
return nil, err
}
@@ -173,8 +187,13 @@ func (r *notificationRepository) Update(ctx context.Context, notification *Notif
// MarkAsSeen marks a notification as seen
func (r *notificationRepository) MarkAsSeen(ctx context.Context, notificationID, userID string) error {
objectID, err := bson.ObjectIDFromHex(notificationID)
if err != nil {
return ErrNotificationNotFound
}
filter := bson.M{
"_id": notificationID,
"_id": objectID,
"user_id": userID,
}