85 lines
3.0 KiB
Go
85 lines
3.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// NotificationSubscriptionRepo provides data access for NotificationSubscription.
|
|
type NotificationSubscriptionRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewNotificationSubscriptionRepo(db *gorm.DB) *NotificationSubscriptionRepo {
|
|
return &NotificationSubscriptionRepo{db: db}
|
|
}
|
|
|
|
// Create inserts a new notification subscription.
|
|
func (r *NotificationSubscriptionRepo) Create(ctx context.Context, sub *model.NotificationSubscription) error {
|
|
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
|
|
err := r.db.WithContext(ctx).First(&sub, id).Error
|
|
return &sub, err
|
|
}
|
|
|
|
// FindByUserID retrieves all subscriptions for a user.
|
|
func (r *NotificationSubscriptionRepo) FindByUserID(ctx context.Context, userID uint) ([]model.NotificationSubscription, error) {
|
|
var subs []model.NotificationSubscription
|
|
err := r.db.WithContext(ctx).Where("user_id = ?", userID).Find(&subs).Error
|
|
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
|
|
err := r.db.WithContext(ctx).Where("identifier = ?", identifier).First(&sub).Error
|
|
return &sub, err
|
|
}
|
|
|
|
// Delete removes a subscription by ID.
|
|
func (r *NotificationSubscriptionRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.NotificationSubscription{}, id).Error
|
|
}
|
|
|
|
// DeleteByUserIDAndIdentifier removes a specific subscription by user and identifier.
|
|
func (r *NotificationSubscriptionRepo) DeleteByUserIDAndIdentifier(ctx context.Context, userID uint, identifier string) error {
|
|
return r.db.WithContext(ctx).
|
|
Where("user_id = ? AND identifier = ?", userID, identifier).
|
|
Delete(&model.NotificationSubscription{}).Error
|
|
}
|