Preload org from user

We need this to avoid a nil dereference when querying by org.
This commit is contained in:
Eli Ribble 2026-01-06 16:21:59 +00:00
parent 05b3caaa73
commit 4d02357671
3 changed files with 18 additions and 3 deletions

View file

@ -15,6 +15,8 @@ import (
"github.com/aarondl/opt/omit"
"github.com/aarondl/opt/omitnull"
"github.com/rs/zerolog/log"
"github.com/stephenafamo/bob/dialect/psql"
"github.com/stephenafamo/bob/dialect/psql/sm"
"golang.org/x/crypto/bcrypt"
)
@ -157,7 +159,11 @@ func SignupUser(ctx context.Context, username string, name string, password stri
// Helper function to translate strings into solid error types for operating on
func findUser(ctx context.Context, user_id int) (*models.User, error) {
user, err := models.FindUser(ctx, db.PGInstance.BobDB, int32(user_id))
//user, err := models.FindUser(ctx, db.PGInstance.BobDB, int32(user_id))
user, err := models.Users.Query(
models.Preload.User.Organization(),
sm.Where(models.Users.Columns.ID.EQ(psql.Arg(user_id))),
).One(ctx, db.PGInstance.BobDB)
if err != nil {
if err.Error() == "No such user" || err.Error() == "sql: no rows in result set" {
return nil, &NoUserError{}

View file

@ -2,6 +2,7 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
@ -20,7 +21,6 @@ import (
"github.com/rs/zerolog/log"
)
var BaseURL, ClientID, ClientSecret, Environment, FieldseekerSchemaDirectory, MapboxToken string
func main() {
@ -216,6 +216,7 @@ func LoggerMiddleware(logger *zerolog.Logger) func(next http.Handler) http.Handl
Interface("recover_info", rec).
Bytes("debug_stack", debug.Stack()).
Msg("log system error")
fmt.Println("Stack:", string(debug.Stack()))
http.Error(ww, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}

View file

@ -11,7 +11,15 @@ import (
)
func fieldseeker(ctx context.Context, u *models.User, since *time.Time) (fsync FieldseekerRecordsSync, err error) {
pl, err := u.R.Organization.Pointlocations().All(ctx, db.PGInstance.BobDB)
if u == nil {
return fsync, fmt.Errorf("Wha! Nil user!")
}
org := u.R.Organization
if org == nil {
return fsync, fmt.Errorf("Whoa nil org from user %d and org %d.", u.ID, u.OrganizationID)
}
db_connection := db.PGInstance.BobDB
pl, err := org.Pointlocations().All(ctx, db_connection)
if err != nil {
return fsync, fmt.Errorf("Failed to get point locations: %w", err)
}