fix(notifications): update and list password (#1951)

This commit is contained in:
ze0s 2025-01-25 18:34:49 +01:00 committed by GitHub
parent 3f8bc0140c
commit 9eff694a5f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 44 additions and 37 deletions

View file

@ -30,7 +30,7 @@ func NewNotificationRepo(log logger.Logger, db *DB) domain.NotificationRepo {
func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQueryParams) ([]domain.Notification, int, error) { func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQueryParams) ([]domain.Notification, int, error) {
queryBuilder := r.db.squirrel. queryBuilder := r.db.squirrel.
Select("id", "name", "type", "enabled", "events", "webhook", "token", "api_key", "channel", "priority", "topic", "host", "username", "created_at", "updated_at", "COUNT(*) OVER() AS total_count"). Select("id", "name", "type", "enabled", "events", "webhook", "token", "api_key", "channel", "priority", "topic", "host", "username", "password", "created_at", "updated_at", "COUNT(*) OVER() AS total_count").
From("notification"). From("notification").
OrderBy("name") OrderBy("name")
@ -51,19 +51,20 @@ func (r *NotificationRepo) Find(ctx context.Context, params domain.NotificationQ
for rows.Next() { for rows.Next() {
var n domain.Notification var n domain.Notification
var webhook, token, apiKey, channel, host, topic, username sql.NullString var webhook, token, apiKey, channel, host, topic, username, password sql.Null[string]
if err := rows.Scan(&n.ID, &n.Name, &n.Type, &n.Enabled, pq.Array(&n.Events), &webhook, &token, &apiKey, &channel, &n.Priority, &topic, &host, &username, &n.CreatedAt, &n.UpdatedAt, &totalCount); err != nil { if err := rows.Scan(&n.ID, &n.Name, &n.Type, &n.Enabled, pq.Array(&n.Events), &webhook, &token, &apiKey, &channel, &n.Priority, &topic, &host, &username, &password, &n.CreatedAt, &n.UpdatedAt, &totalCount); err != nil {
return nil, 0, errors.Wrap(err, "error scanning row") return nil, 0, errors.Wrap(err, "error scanning row")
} }
n.APIKey = apiKey.String n.APIKey = apiKey.V
n.Webhook = webhook.String n.Webhook = webhook.V
n.Token = token.String n.Token = token.V
n.Channel = channel.String n.Channel = channel.V
n.Topic = topic.String n.Topic = topic.V
n.Host = host.String n.Host = host.V
n.Username = username.String n.Username = username.V
n.Password = password.V
notifications = append(notifications, n) notifications = append(notifications, n)
} }
@ -87,24 +88,24 @@ func (r *NotificationRepo) List(ctx context.Context) ([]domain.Notification, err
var n domain.Notification var n domain.Notification
//var eventsSlice []string //var eventsSlice []string
var token, apiKey, webhook, title, icon, host, username, password, channel, targets, devices, topic sql.NullString var token, apiKey, webhook, title, icon, host, username, password, channel, targets, devices, topic sql.Null[string]
if err := rows.Scan(&n.ID, &n.Name, &n.Type, &n.Enabled, pq.Array(&n.Events), &token, &apiKey, &webhook, &title, &icon, &host, &username, &password, &channel, &targets, &devices, &n.Priority, &topic, &n.CreatedAt, &n.UpdatedAt); err != nil { if err := rows.Scan(&n.ID, &n.Name, &n.Type, &n.Enabled, pq.Array(&n.Events), &token, &apiKey, &webhook, &title, &icon, &host, &username, &password, &channel, &targets, &devices, &n.Priority, &topic, &n.CreatedAt, &n.UpdatedAt); err != nil {
return nil, errors.Wrap(err, "error scanning row") return nil, errors.Wrap(err, "error scanning row")
} }
//n.Events = ([]domain.NotificationEvent)(eventsSlice) //n.Events = ([]domain.NotificationEvent)(eventsSlice)
n.Token = token.String n.Token = token.V
n.APIKey = apiKey.String n.APIKey = apiKey.V
n.Webhook = webhook.String n.Webhook = webhook.V
n.Title = title.String n.Title = title.V
n.Icon = icon.String n.Icon = icon.V
n.Host = host.String n.Host = host.V
n.Username = username.String n.Username = username.V
n.Password = password.String n.Password = password.V
n.Channel = channel.String n.Channel = channel.V
n.Targets = targets.String n.Targets = targets.V
n.Devices = devices.String n.Devices = devices.V
n.Topic = topic.String n.Topic = topic.V
notifications = append(notifications, n) notifications = append(notifications, n)
} }
@ -154,7 +155,7 @@ func (r *NotificationRepo) FindByID(ctx context.Context, id int) (*domain.Notifi
var n domain.Notification var n domain.Notification
var token, apiKey, webhook, title, icon, host, username, password, channel, targets, devices, topic sql.NullString var token, apiKey, webhook, title, icon, host, username, password, channel, targets, devices, topic sql.Null[string]
if err := row.Scan(&n.ID, &n.Name, &n.Type, &n.Enabled, pq.Array(&n.Events), &token, &apiKey, &webhook, &title, &icon, &host, &username, &password, &channel, &targets, &devices, &n.Priority, &topic, &n.CreatedAt, &n.UpdatedAt); err != nil { if err := row.Scan(&n.ID, &n.Name, &n.Type, &n.Enabled, pq.Array(&n.Events), &token, &apiKey, &webhook, &title, &icon, &host, &username, &password, &channel, &targets, &devices, &n.Priority, &topic, &n.CreatedAt, &n.UpdatedAt); err != nil {
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, sql.ErrNoRows) {
return nil, domain.ErrRecordNotFound return nil, domain.ErrRecordNotFound
@ -163,18 +164,18 @@ func (r *NotificationRepo) FindByID(ctx context.Context, id int) (*domain.Notifi
return nil, errors.Wrap(err, "error scanning row") return nil, errors.Wrap(err, "error scanning row")
} }
n.Token = token.String n.Token = token.V
n.APIKey = apiKey.String n.APIKey = apiKey.V
n.Webhook = webhook.String n.Webhook = webhook.V
n.Title = title.String n.Title = title.V
n.Icon = icon.String n.Icon = icon.V
n.Host = host.String n.Host = host.V
n.Username = username.String n.Username = username.V
n.Password = password.String n.Password = password.V
n.Channel = channel.String n.Channel = channel.V
n.Targets = targets.String n.Targets = targets.V
n.Devices = devices.String n.Devices = devices.V
n.Topic = topic.String n.Topic = topic.V
return &n, nil return &n, nil
} }
@ -195,6 +196,7 @@ func (r *NotificationRepo) Store(ctx context.Context, notification *domain.Notif
"topic", "topic",
"host", "host",
"username", "username",
"password",
). ).
Values( Values(
notification.Name, notification.Name,
@ -209,6 +211,7 @@ func (r *NotificationRepo) Store(ctx context.Context, notification *domain.Notif
toNullString(notification.Topic), toNullString(notification.Topic),
toNullString(notification.Host), toNullString(notification.Host),
toNullString(notification.Username), toNullString(notification.Username),
toNullString(notification.Password),
). ).
Suffix("RETURNING id").RunWith(r.db.handler) Suffix("RETURNING id").RunWith(r.db.handler)
@ -236,6 +239,7 @@ func (r *NotificationRepo) Update(ctx context.Context, notification *domain.Noti
Set("topic", toNullString(notification.Topic)). Set("topic", toNullString(notification.Topic)).
Set("host", toNullString(notification.Host)). Set("host", toNullString(notification.Host)).
Set("username", toNullString(notification.Username)). Set("username", toNullString(notification.Username)).
Set("password", toNullString(notification.Password)).
Set("updated_at", sq.Expr("CURRENT_TIMESTAMP")). Set("updated_at", sq.Expr("CURRENT_TIMESTAMP")).
Where(sq.Eq{"id": notification.ID}) Where(sq.Eq{"id": notification.ID})

View file

@ -575,6 +575,7 @@ interface InitialValues {
host?: string; host?: string;
events: NotificationEvent[]; events: NotificationEvent[];
username?: string username?: string
password?: string
} }
export function NotificationUpdateForm({ isOpen, toggle, data: notification }: UpdateFormProps<ServiceNotification>) { export function NotificationUpdateForm({ isOpen, toggle, data: notification }: UpdateFormProps<ServiceNotification>) {
@ -625,7 +626,8 @@ export function NotificationUpdateForm({ isOpen, toggle, data: notification }: U
topic: notification.topic, topic: notification.topic,
host: notification.host, host: notification.host,
events: notification.events || [], events: notification.events || [],
username: notification.username username: notification.username,
password: notification.password
}; };
return ( return (

View file

@ -26,4 +26,5 @@ interface ServiceNotification {
topic?: string; topic?: string;
host?: string; host?: string;
username?: string; username?: string;
password?: string;
} }