fix(notifications): disable notification and events have no effect (#1754)

fix(notifications): disable notificatio nand events
This commit is contained in:
ze0s 2024-10-06 14:12:01 +02:00 committed by GitHub
parent 009647fcd1
commit ca2d956e02
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 118 additions and 125 deletions

View file

@ -179,15 +179,7 @@ func (r *NotificationRepo) FindByID(ctx context.Context, id int) (*domain.Notifi
return &n, nil
}
func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notification) (*domain.Notification, error) {
webhook := toNullString(notification.Webhook)
token := toNullString(notification.Token)
apiKey := toNullString(notification.APIKey)
channel := toNullString(notification.Channel)
topic := toNullString(notification.Topic)
host := toNullString(notification.Host)
username := toNullString(notification.Username)
func (r *NotificationRepo) Store(ctx context.Context, notification *domain.Notification) error {
queryBuilder := r.db.squirrel.
Insert("notification").
Columns(
@ -209,68 +201,56 @@ func (r *NotificationRepo) Store(ctx context.Context, notification domain.Notifi
notification.Type,
notification.Enabled,
pq.Array(notification.Events),
webhook,
token,
apiKey,
channel,
toNullString(notification.Webhook),
toNullString(notification.Token),
toNullString(notification.APIKey),
toNullString(notification.Channel),
notification.Priority,
topic,
host,
username,
toNullString(notification.Topic),
toNullString(notification.Host),
toNullString(notification.Username),
).
Suffix("RETURNING id").RunWith(r.db.handler)
// return values
var retID int64
if err := queryBuilder.QueryRowContext(ctx).Scan(&retID); err != nil {
return nil, errors.Wrap(err, "error executing query")
if err := queryBuilder.QueryRowContext(ctx).Scan(&notification.ID); err != nil {
return errors.Wrap(err, "error executing query")
}
r.log.Debug().Msgf("notification.store: added new %v", retID)
notification.ID = int(retID)
r.log.Debug().Msgf("notification.store: added new %v", notification.ID)
return &notification, nil
return nil
}
func (r *NotificationRepo) Update(ctx context.Context, notification domain.Notification) (*domain.Notification, error) {
webhook := toNullString(notification.Webhook)
token := toNullString(notification.Token)
apiKey := toNullString(notification.APIKey)
channel := toNullString(notification.Channel)
topic := toNullString(notification.Topic)
host := toNullString(notification.Host)
username := toNullString(notification.Username)
func (r *NotificationRepo) Update(ctx context.Context, notification *domain.Notification) error {
queryBuilder := r.db.squirrel.
Update("notification").
Set("name", notification.Name).
Set("type", notification.Type).
Set("enabled", notification.Enabled).
Set("events", pq.Array(notification.Events)).
Set("webhook", webhook).
Set("token", token).
Set("api_key", apiKey).
Set("channel", channel).
Set("webhook", toNullString(notification.Webhook)).
Set("token", toNullString(notification.Token)).
Set("api_key", toNullString(notification.APIKey)).
Set("channel", toNullString(notification.Channel)).
Set("priority", notification.Priority).
Set("topic", topic).
Set("host", host).
Set("username", username).
Set("topic", toNullString(notification.Topic)).
Set("host", toNullString(notification.Host)).
Set("username", toNullString(notification.Username)).
Set("updated_at", sq.Expr("CURRENT_TIMESTAMP")).
Where(sq.Eq{"id": notification.ID})
query, args, err := queryBuilder.ToSql()
if err != nil {
return nil, errors.Wrap(err, "error building query")
return errors.Wrap(err, "error building query")
}
if _, err = r.db.handler.ExecContext(ctx, query, args...); err != nil {
return nil, errors.Wrap(err, "error executing query")
return errors.Wrap(err, "error executing query")
}
r.log.Debug().Msgf("notification.update: %v", notification.Name)
return &notification, nil
return nil
}
func (r *NotificationRepo) Delete(ctx context.Context, notificationID int) error {

View file

@ -54,8 +54,10 @@ func TestNotificationRepo_Store(t *testing.T) {
// Setup
assert.NotNil(t, mockData)
notification := getMockNotification()
// Execute
notification, err := repo.Store(context.Background(), mockData)
err := repo.Store(context.Background(), &notification)
// Verify
assert.NoError(t, err)
@ -77,28 +79,32 @@ func TestNotificationRepo_Update(t *testing.T) {
t.Run(fmt.Sprintf("Update_Succeeds [%s]", dbType), func(t *testing.T) {
// Initial setup and Store
notification, err := repo.Store(context.Background(), mockData)
err := repo.Store(context.Background(), &mockData)
assert.NoError(t, err)
assert.NotNil(t, notification)
assert.NotNil(t, &mockData)
// Modify some fields
updatedMockData := *notification
updatedMockData.Name = "UpdatedName"
updatedMockData.Type = domain.NotificationTypeTelegram
updatedMockData.Priority = 2
newName := "UpdatedName"
newType := domain.NotificationTypeTelegram
newPriority := int32(2)
updatedMockData := &mockData
updatedMockData.Name = newName
updatedMockData.Type = newType
updatedMockData.Priority = newPriority
// Execute Update
updatedNotification, err := repo.Update(context.Background(), updatedMockData)
err = repo.Update(context.Background(), updatedMockData)
// Verify
assert.NoError(t, err)
assert.NotNil(t, updatedNotification)
assert.Equal(t, updatedMockData.Name, updatedNotification.Name)
assert.Equal(t, updatedMockData.Type, updatedNotification.Type)
assert.Equal(t, updatedMockData.Priority, updatedNotification.Priority)
assert.NotNil(t, &mockData)
assert.Equal(t, updatedMockData.Name, newName)
assert.Equal(t, updatedMockData.Type, newType)
assert.Equal(t, updatedMockData.Priority, newPriority)
// Cleanup
_ = repo.Delete(context.Background(), updatedNotification.ID)
_ = repo.Delete(context.Background(), mockData.ID)
})
}
}
@ -108,11 +114,13 @@ func TestNotificationRepo_Delete(t *testing.T) {
log := setupLoggerForTest()
repo := NewNotificationRepo(log, db)
mockData := getMockNotification()
//mockData := getMockNotification()
t.Run(fmt.Sprintf("Delete_Succeeds [%s]", dbType), func(t *testing.T) {
notification := getMockNotification()
// Initial setup and Store
notification, err := repo.Store(context.Background(), mockData)
err := repo.Store(context.Background(), &notification)
assert.NoError(t, err)
assert.NotNil(t, notification)
@ -148,11 +156,11 @@ func TestNotificationRepo_Find(t *testing.T) {
_ = repo.Delete(context.Background(), notification.ID)
}
_, err := repo.Store(context.Background(), mockData1)
err := repo.Store(context.Background(), &mockData1)
assert.NoError(t, err)
_, err = repo.Store(context.Background(), mockData2)
err = repo.Store(context.Background(), &mockData2)
assert.NoError(t, err)
_, err = repo.Store(context.Background(), mockData3)
err = repo.Store(context.Background(), &mockData3)
assert.NoError(t, err)
// Setup query params
@ -188,11 +196,13 @@ func TestNotificationRepo_FindByID(t *testing.T) {
t.Run(fmt.Sprintf("FindByID_Succeeds [%s]", dbType), func(t *testing.T) {
// Setup
//notification := getMockNotification()
assert.NotNil(t, mockData)
notification, err := repo.Store(context.Background(), mockData)
err := repo.Store(context.Background(), &mockData)
// Execute
notification, err = repo.FindByID(context.Background(), notification.ID)
notification, err := repo.FindByID(context.Background(), mockData.ID)
// Verify
assert.NoError(t, err)
@ -221,7 +231,7 @@ func TestNotificationRepo_List(t *testing.T) {
}
for i := 0; i < 10; i++ {
_, err := repo.Store(context.Background(), mockData)
err := repo.Store(context.Background(), &mockData)
assert.NoError(t, err)
}