feat(webhooks): align account payloads
This commit is contained in:
@@ -2,6 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
@@ -22,7 +23,32 @@ func NewWebhookSubscriptionRepo(db *gorm.DB) *WebhookSubscriptionRepo {
|
||||
// FindByID retrieves a webhook subscription by primary key.
|
||||
func (r *WebhookSubscriptionRepo) FindByID(ctx context.Context, id uint) (*model.WebhookSubscription, error) {
|
||||
var s model.WebhookSubscription
|
||||
err := r.db.WithContext(ctx).First(&s, id).Error
|
||||
err := r.db.WithContext(ctx).Preload("Inbox").First(&s, id).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
// FindByAccountAndID retrieves a webhook subscription scoped to an account.
|
||||
func (r *WebhookSubscriptionRepo) FindByAccountAndID(ctx context.Context, accountID, id uint) (*model.WebhookSubscription, error) {
|
||||
var s model.WebhookSubscription
|
||||
err := r.db.WithContext(ctx).
|
||||
Preload("Inbox").
|
||||
Where("account_id = ?", accountID).
|
||||
First(&s, id).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
// FindByAccountAndURL retrieves an active webhook by account and URL.
|
||||
func (r *WebhookSubscriptionRepo) FindByAccountAndURL(ctx context.Context, accountID uint, url string) (*model.WebhookSubscription, error) {
|
||||
var s model.WebhookSubscription
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("account_id = ? AND url = ? AND active = ?", accountID, url, true).
|
||||
First(&s).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -37,7 +63,11 @@ func (r *WebhookSubscriptionRepo) GetByID(ctx context.Context, id uint) (*model.
|
||||
// ListByAccount retrieves all webhook subscriptions for an account.
|
||||
func (r *WebhookSubscriptionRepo) ListByAccount(ctx context.Context, accountID uint) ([]model.WebhookSubscription, error) {
|
||||
var subs []model.WebhookSubscription
|
||||
err := r.db.WithContext(ctx).Where("account_id = ? AND active = ?", accountID, true).Find(&subs).Error
|
||||
err := r.db.WithContext(ctx).
|
||||
Preload("Inbox").
|
||||
Where("account_id = ? AND active = ?", accountID, true).
|
||||
Order("id ASC").
|
||||
Find(&subs).Error
|
||||
return subs, err
|
||||
}
|
||||
|
||||
@@ -45,20 +75,33 @@ func (r *WebhookSubscriptionRepo) ListByAccount(ctx context.Context, accountID u
|
||||
func (r *WebhookSubscriptionRepo) ListActiveByAccount(ctx context.Context, accountID uint) ([]model.WebhookSubscription, error) {
|
||||
var subs []model.WebhookSubscription
|
||||
err := r.db.WithContext(ctx).
|
||||
Preload("Inbox").
|
||||
Where("account_id = ? AND active = ?", accountID, true).
|
||||
Order("id ASC").
|
||||
Find(&subs).Error
|
||||
return subs, err
|
||||
}
|
||||
|
||||
// ListByAccountAndEvent retrieves webhook subscriptions that include a specific event type.
|
||||
func (r *WebhookSubscriptionRepo) ListByAccountAndEvent(ctx context.Context, accountID uint, eventType string) ([]model.WebhookSubscription, error) {
|
||||
var subs []model.WebhookSubscription
|
||||
// Use JSON containment operator for PostgreSQL: events @> '["event_type"]'
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("account_id = ? AND active = ?", accountID, true).
|
||||
Where("events @> ?", eventType).
|
||||
Find(&subs).Error
|
||||
return subs, err
|
||||
subs, err := r.ListActiveByAccount(ctx, accountID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
filtered := make([]model.WebhookSubscription, 0, len(subs))
|
||||
for _, sub := range subs {
|
||||
var events []string
|
||||
if err := json.Unmarshal(sub.Events, &events); err != nil {
|
||||
continue
|
||||
}
|
||||
for _, event := range events {
|
||||
if event == eventType {
|
||||
filtered = append(filtered, sub)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return filtered, nil
|
||||
}
|
||||
|
||||
// Create inserts a new webhook subscription.
|
||||
@@ -97,4 +140,4 @@ func (r *WebhookSubscriptionRepo) ListDeliveriesBySubscription(ctx context.Conte
|
||||
Limit(limit).
|
||||
Find(&deliveries).Error
|
||||
return deliveries, err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user