Add separate session endpoint for additional non-user data
This is conceptually much cleaner that encumbering the user object.
This commit is contained in:
parent
00ebc27069
commit
42d111aac9
16 changed files with 166 additions and 177 deletions
|
|
@ -27,10 +27,10 @@ type Notification struct {
|
|||
Time time.Time
|
||||
Type string
|
||||
}
|
||||
type UserNotificationCounts struct {
|
||||
Communications uint `json:"communication"`
|
||||
Home uint `json:"home"`
|
||||
Review uint `json:"review"`
|
||||
type notificationCounts struct {
|
||||
Communications uint
|
||||
Home uint
|
||||
Review uint
|
||||
}
|
||||
|
||||
// Clear all notifications for a given user with the given path
|
||||
|
|
@ -105,7 +105,7 @@ func NotificationsForUser(ctx context.Context, u User) ([]Notification, error) {
|
|||
}
|
||||
return results, nil
|
||||
}
|
||||
func NotificationCountsForUser(ctx context.Context, u User) (*UserNotificationCounts, error) {
|
||||
func NotificationCountsForUser(ctx context.Context, u User) (*notificationCounts, error) {
|
||||
count_home, err := u.model.UserNotifications(
|
||||
models.SelectWhere.Notifications.ResolvedAt.IsNull(),
|
||||
).Count(ctx, db.PGInstance.BobDB)
|
||||
|
|
@ -125,7 +125,7 @@ func NotificationCountsForUser(ctx context.Context, u User) (*UserNotificationCo
|
|||
return nil, fmt.Errorf("Failed to get review notification count: %w", err)
|
||||
}
|
||||
//log.Debug().Int64("reports", count_reports).Int64("home", count_home).Int64("review", count_review).Int("user", u.ID).Msg("calculated notification counts")
|
||||
return &UserNotificationCounts{
|
||||
return ¬ificationCounts{
|
||||
Communications: uint(count_reports),
|
||||
Home: uint(count_home),
|
||||
Review: uint(count_review),
|
||||
|
|
|
|||
|
|
@ -8,12 +8,13 @@ import (
|
|||
"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
||||
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
||||
//"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Organization struct {
|
||||
ID int32 `json:"id"`
|
||||
ServiceArea *ServiceArea `json:"service_area"`
|
||||
ID int32 `json:"id"`
|
||||
ServiceArea *types.ServiceArea `json:"service_area"`
|
||||
|
||||
model *models.Organization
|
||||
}
|
||||
|
|
@ -75,11 +76,6 @@ func (o Organization) FieldseekerSyncLatest(ctx context.Context) (*models.Fields
|
|||
return sync, nil
|
||||
}
|
||||
|
||||
type ServiceArea struct {
|
||||
Min Point `json:"min"`
|
||||
Max Point `json:"max"`
|
||||
}
|
||||
|
||||
func (o Organization) ServiceRequestRecent(ctx context.Context) ([]*models.FieldseekerServicerequest, error) {
|
||||
results, err := o.model.Servicerequests(sm.OrderBy("creationdate").Desc(), sm.Limit(10)).All(ctx, db.PGInstance.BobDB)
|
||||
if err != nil {
|
||||
|
|
@ -114,19 +110,19 @@ func OrganizationList(ctx context.Context, user User) ([]*Organization, error) {
|
|||
return results, err
|
||||
}
|
||||
func newOrganization(org *models.Organization) Organization {
|
||||
var sa *ServiceArea
|
||||
var sa *types.ServiceArea
|
||||
if org.ServiceAreaXmax.IsValue() &&
|
||||
org.ServiceAreaXmin.IsValue() &&
|
||||
org.ServiceAreaYmax.IsValue() &&
|
||||
org.ServiceAreaYmin.IsValue() {
|
||||
sa = &ServiceArea{
|
||||
Min: Point{
|
||||
X: org.ServiceAreaXmin.MustGet(),
|
||||
Y: org.ServiceAreaYmin.MustGet(),
|
||||
sa = &types.ServiceArea{
|
||||
Min: types.Location{
|
||||
Longitude: org.ServiceAreaXmin.MustGet(),
|
||||
Latitude: org.ServiceAreaYmin.MustGet(),
|
||||
},
|
||||
Max: Point{
|
||||
X: org.ServiceAreaXmax.MustGet(),
|
||||
Y: org.ServiceAreaYmax.MustGet(),
|
||||
Max: types.Location{
|
||||
Longitude: org.ServiceAreaXmax.MustGet(),
|
||||
Latitude: org.ServiceAreaYmax.MustGet(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,19 +25,17 @@ type NoUserError struct{}
|
|||
func (e NoUserError) Error() string { return "That user does not exist" }
|
||||
|
||||
type User struct {
|
||||
Active bool
|
||||
Avatar *uuid.UUID
|
||||
DisplayName string
|
||||
ID int
|
||||
Initials string
|
||||
Notifications []Notification
|
||||
NotificationCounts UserNotificationCounts
|
||||
Organization Organization
|
||||
PasswordHash string
|
||||
PasswordHashType string
|
||||
Role string
|
||||
Tags []string
|
||||
Username string
|
||||
Active bool
|
||||
Avatar *uuid.UUID
|
||||
DisplayName string
|
||||
ID int
|
||||
Initials string
|
||||
Organization Organization
|
||||
PasswordHash string
|
||||
PasswordHashType string
|
||||
Role string
|
||||
Tags []string
|
||||
Username string
|
||||
|
||||
model *models.User
|
||||
}
|
||||
|
|
@ -55,27 +53,20 @@ func (u User) HasRoot() bool {
|
|||
func newUser(ctx context.Context, org Organization, user *models.User) User {
|
||||
avatar := user.Avatar.Ptr()
|
||||
u := User{
|
||||
Active: true,
|
||||
Avatar: avatar,
|
||||
DisplayName: user.DisplayName,
|
||||
ID: int(user.ID),
|
||||
Initials: extractInitials(user.DisplayName),
|
||||
Notifications: []Notification{},
|
||||
NotificationCounts: UserNotificationCounts{},
|
||||
Organization: org,
|
||||
PasswordHash: user.PasswordHash,
|
||||
PasswordHashType: string(user.PasswordHashType),
|
||||
Role: user.Role.String(),
|
||||
Tags: []string{},
|
||||
Username: user.Username,
|
||||
Active: true,
|
||||
Avatar: avatar,
|
||||
DisplayName: user.DisplayName,
|
||||
ID: int(user.ID),
|
||||
Initials: extractInitials(user.DisplayName),
|
||||
Organization: org,
|
||||
PasswordHash: user.PasswordHash,
|
||||
PasswordHashType: string(user.PasswordHashType),
|
||||
Role: user.Role.String(),
|
||||
Tags: []string{},
|
||||
Username: user.Username,
|
||||
|
||||
model: user,
|
||||
}
|
||||
counts, err := NotificationCountsForUser(ctx, u)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Int32("id", user.ID).Msg("failed to get notification counts for user")
|
||||
}
|
||||
u.NotificationCounts = *counts
|
||||
return u
|
||||
}
|
||||
|
||||
|
|
@ -272,16 +263,14 @@ func extractInitials(name string) string {
|
|||
}
|
||||
func toUser(user *models.User) User {
|
||||
return User{
|
||||
DisplayName: user.DisplayName,
|
||||
ID: int(user.ID),
|
||||
Initials: extractInitials(user.DisplayName),
|
||||
Notifications: []Notification{},
|
||||
NotificationCounts: UserNotificationCounts{},
|
||||
Organization: Organization{},
|
||||
PasswordHash: user.PasswordHash,
|
||||
PasswordHashType: string(user.PasswordHashType),
|
||||
Role: user.Role.String(),
|
||||
Username: user.Username,
|
||||
DisplayName: user.DisplayName,
|
||||
ID: int(user.ID),
|
||||
Initials: extractInitials(user.DisplayName),
|
||||
Organization: Organization{},
|
||||
PasswordHash: user.PasswordHash,
|
||||
PasswordHashType: string(user.PasswordHashType),
|
||||
Role: user.Role.String(),
|
||||
Username: user.Username,
|
||||
|
||||
model: user,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue