refactoring (no functionality changes)

This commit is contained in:
henrygd
2025-08-02 17:04:38 -04:00
parent aa2bc9f118
commit a0f271545a
8 changed files with 489 additions and 471 deletions

View File

@@ -6,6 +6,7 @@ import (
"log"
"net/http"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
)
@@ -42,29 +43,26 @@ func (um *UserManager) InitializeUserRole(e *core.RecordEvent) error {
// Initialize user settings with defaults if not set
func (um *UserManager) InitializeUserSettings(e *core.RecordEvent) error {
record := e.Record
// intialize settings with defaults
// intialize settings with defaults (zero values can be ignored)
settings := UserSettings{
ChartTime: "1h",
NotificationEmails: []string{},
NotificationWebhooks: []string{},
ChartTime: "1h",
}
record.UnmarshalJSONField("settings", &settings)
if len(settings.NotificationEmails) == 0 {
// get user email from auth record
if errs := um.app.ExpandRecord(record, []string{"user"}, nil); len(errs) == 0 {
// app.Logger().Error("failed to expand user relation", "errs", errs)
if user := record.ExpandedOne("user"); user != nil {
settings.NotificationEmails = []string{user.GetString("email")}
} else {
log.Println("Failed to get user email from auth record")
}
} else {
log.Println("failed to expand user relation", "errs", errs)
}
// get user email from auth record
var user struct {
Email string `db:"email"`
}
err := e.App.DB().NewQuery("SELECT email FROM users WHERE id = {:id}").Bind(dbx.Params{
"id": record.GetString("user"),
}).One(&user)
if err != nil {
log.Println("failed to get user email", "err", err)
return err
}
settings.NotificationEmails = []string{user.Email}
if len(settings.NotificationWebhooks) == 0 {
settings.NotificationWebhooks = []string{""}
}
// if len(settings.NotificationWebhooks) == 0 {
// settings.NotificationWebhooks = []string{""}
// }
record.Set("settings", settings)
return e.Next()
}