feat(notifications): align subscription payloads
This commit is contained in:
@@ -2,6 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
"gorm.io/gorm"
|
||||
@@ -21,6 +22,11 @@ func (r *NotificationSubscriptionRepo) Create(ctx context.Context, sub *model.No
|
||||
return r.db.WithContext(ctx).Create(sub).Error
|
||||
}
|
||||
|
||||
// Save inserts or updates a subscription.
|
||||
func (r *NotificationSubscriptionRepo) Save(ctx context.Context, sub *model.NotificationSubscription) error {
|
||||
return r.db.WithContext(ctx).Save(sub).Error
|
||||
}
|
||||
|
||||
// FindByID retrieves a subscription by ID.
|
||||
func (r *NotificationSubscriptionRepo) FindByID(ctx context.Context, id uint) (*model.NotificationSubscription, error) {
|
||||
var sub model.NotificationSubscription
|
||||
@@ -35,6 +41,29 @@ func (r *NotificationSubscriptionRepo) FindByUserID(ctx context.Context, userID
|
||||
return subs, err
|
||||
}
|
||||
|
||||
// FindByUserIDAndPushToken finds the Chatwoot destroy target by push_token.
|
||||
func (r *NotificationSubscriptionRepo) FindByUserIDAndPushToken(ctx context.Context, userID uint, pushToken string) (*model.NotificationSubscription, error) {
|
||||
subs, err := r.FindByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := range subs {
|
||||
if subs[i].Identifier == pushToken {
|
||||
return &subs[i], nil
|
||||
}
|
||||
var attrs map[string]any
|
||||
if err := json.Unmarshal(subs[i].SubscriptionAttributes, &attrs); err == nil {
|
||||
if value, _ := attrs["push_token"].(string); value == pushToken {
|
||||
return &subs[i], nil
|
||||
}
|
||||
if value, _ := attrs["endpoint"].(string); value == pushToken {
|
||||
return &subs[i], nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
// FindByIdentifier retrieves a subscription by its unique identifier.
|
||||
func (r *NotificationSubscriptionRepo) FindByIdentifier(ctx context.Context, identifier string) (*model.NotificationSubscription, error) {
|
||||
var sub model.NotificationSubscription
|
||||
@@ -52,4 +81,4 @@ func (r *NotificationSubscriptionRepo) DeleteByUserIDAndIdentifier(ctx context.C
|
||||
return r.db.WithContext(ctx).
|
||||
Where("user_id = ? AND identifier = ?", userID, identifier).
|
||||
Delete(&model.NotificationSubscription{}).Error
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user