Skip to content

Commit

Permalink
fix: deleting channel assigned to rule
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgacsal committed Aug 22, 2024
1 parent ea7ceac commit e84d2fa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/notification/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ func (r repository) ListRules(ctx context.Context, params notification.ListRules
query = query.Where(ruledb.TypeIn(params.Types...))
}

if len(params.Channels) > 0 {
query = query.Where(ruledb.HasChannelsWith(channeldb.IDIn(params.Channels...)))
}

// Eager load Channels
query = query.WithChannels()

order := entutils.GetOrdering(sortx.OrderDefault)
Expand Down
1 change: 1 addition & 0 deletions internal/notification/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ type ListRulesInput struct {
Rules []string
IncludeDisabled bool
Types []RuleType
Channels []string

OrderBy api.ListNotificationRulesParamsOrderBy
Order sortx.Order
Expand Down
21 changes: 21 additions & 0 deletions internal/notification/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,27 @@ func (c service) DeleteChannel(ctx context.Context, params DeleteChannelInput) e
return fmt.Errorf("invalid params: %w", err)
}

rules, err := c.repo.ListRules(ctx, ListRulesInput{
Namespaces: []string{params.Namespace},
IncludeDisabled: true,
Channels: []string{params.ID},
})
if err != nil {
return fmt.Errorf("failed to list rules for channel: %w", err)
}

if rules.TotalCount > 0 {
ruleIDs := make([]string, 0, len(rules.Items))

for _, rule := range rules.Items {
ruleIDs = append(ruleIDs, rule.ID)
}

return ValidationError{
Err: fmt.Errorf("cannot delete channel as it is assigned to one or more rules: %v", ruleIDs),
}
}

txFunc := func(ctx context.Context, repo TxRepository) error {
if err := c.webhook.DeleteWebhook(ctx, webhook.DeleteWebhookInput{
Namespace: params.Namespace,
Expand Down

0 comments on commit e84d2fa

Please sign in to comment.