Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed join on soft delete #49

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/dbtools/notification_preferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ func GetNotificationPreferences(ctx context.Context, uid string, ex boil.Context
LEFT JOIN notification_targets ON notification_target_id = notification_targets.id
LEFT JOIN notification_types ON notification_type_id = notification_types.id
WHERE notification_preferences.user_id = $1
AND notification_types.deleted_at is NULL
AND notification_targets.deleted_at is NULL
)
`

Expand All @@ -101,6 +103,7 @@ func GetNotificationPreferences(ctx context.Context, uid string, ex boil.Context
jsonb_agg((nd.target_slug, IFNULL(np.enabled, nd.default_enabled))) AS notification_targets
FROM notification_defaults as nd
FULL OUTER JOIN np on (np.target_id = nd.target_id AND np.type_id = nd.type_id)
WHERE nd.type_slug IS NOT NULL
GROUP BY nd.type_slug
`,
)
Expand Down
81 changes: 81 additions & 0 deletions internal/dbtools/notification_preferences_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/metal-toolbox/governor-api/internal/models"
"github.com/pressly/goose/v3"
"github.com/stretchr/testify/suite"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)

// NotificationPreferencesTestSuite is a test suite to run unit tests on
Expand Down Expand Up @@ -358,6 +359,86 @@ func (s *NotificationPreferencesTestSuite) TestNotificationPreferences() {
wantWithoutDefaults: UserNotificationPreferences{nil},
wantErr: true,
},
{
name: "notification type deleted after user updates",
action: func() error {
_, err := s.db.Query(`INSERT INTO notification_types (id, name, slug, description, default_enabled) VALUES ('00000000-0000-0000-0000-000000000002', 'Notice', 'notice', 'notice', 'false')`)
if err != nil {
return err
}

if _, err := CreateOrUpdateNotificationPreferences(
context.TODO(), u,
UserNotificationPreferences{{
NotificationType: "notice",
Enabled: s.trueptr,
}},
sqlxdb, s.auditID, u,
); err != nil {
return err
}

nt, err := models.
NotificationTypes(qm.Where("slug = ?", "notice")).
One(context.TODO(), sqlxdb)
if err != nil {
return err
}

_, err = nt.Delete(context.TODO(), sqlxdb, false)
if err != nil {
return err
}

return RefreshNotificationDefaults(context.TODO(), sqlxdb)
},
wantWithDefaults: UserNotificationPreferences{{
NotificationType: "alert",
Enabled: s.trueptr,
NotificationTargets: UserNotificationPreferenceTargets{{
Target: "slack",
Enabled: s.falseptr,
}},
}},
wantWithoutDefaults: UserNotificationPreferences{{
NotificationType: "alert",
Enabled: s.trueptr,
NotificationTargets: UserNotificationPreferenceTargets{{
Target: "slack",
Enabled: s.falseptr,
}},
}},
wantErr: false,
},
{
name: "notification target deleted after user updates",
action: func() error {
nt, err := models.
NotificationTargets(qm.Where("slug = ?", "slack")).
One(context.TODO(), sqlxdb)
if err != nil {
return err
}

_, err = nt.Delete(context.TODO(), sqlxdb, false)
if err != nil {
return err
}

return RefreshNotificationDefaults(context.TODO(), sqlxdb)
},
wantWithDefaults: UserNotificationPreferences{{
NotificationType: "alert",
Enabled: s.trueptr,
NotificationTargets: UserNotificationPreferenceTargets{},
}},
wantWithoutDefaults: UserNotificationPreferences{{
NotificationType: "alert",
Enabled: s.trueptr,
NotificationTargets: UserNotificationPreferenceTargets{},
}},
wantErr: false,
},
}

for _, tc := range tests {
Expand Down