From 89dc703bd65b370ea795bb986f5204757ffab6e3 Mon Sep 17 00:00:00 2001 From: Krisztian Gacsal Date: Thu, 29 Aug 2024 12:07:12 +0200 Subject: [PATCH] fix: rule assignements lost of channel update Fix bug where updating notification channel resulted in notification rules get unassigned on Svix side due to no rule assignement information passed in update API call. --- openmeter/notification/service/channel.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/openmeter/notification/service/channel.go b/openmeter/notification/service/channel.go index 94684264b..723d7a86b 100644 --- a/openmeter/notification/service/channel.go +++ b/openmeter/notification/service/channel.go @@ -178,6 +178,22 @@ func (s Service) UpdateChannel(ctx context.Context, params notification.UpdateCh } txFunc := func(ctx context.Context, repo notification.TxRepository) (*notification.Channel, error) { + // Fetch rules assigned to channel as we need to make sure that we do not remove rule assignments + // from channel during update. + rules, err := s.repo.ListRules(ctx, notification.ListRulesInput{ + Namespaces: []string{params.Namespace}, + IncludeDisabled: true, + Channels: []string{params.ID}, + }) + if err != nil { + return nil, fmt.Errorf("failed to list rules for channel: %w", err) + } + + ruleIDs := make([]string, 0, len(rules.Items)) + for _, rule := range rules.Items { + ruleIDs = append(ruleIDs, rule.ID) + } + channel, err = repo.UpdateChannel(ctx, params) if err != nil { return nil, fmt.Errorf("failed to create channel: %w", err) @@ -204,6 +220,7 @@ func (s Service) UpdateChannel(ctx context.Context, params notification.UpdateCh ChannelIDMetadataKey: channel.ID, }, Description: convert.ToPointer("Notification Channel: " + channel.ID), + Channels: ruleIDs, }) if err != nil { return nil, fmt.Errorf("failed to update webhook for channel: %w", err)