Skip to content

Commit

Permalink
Merge pull request #1864 from target/favorite-users
Browse files Browse the repository at this point in the history
users: add user favorite support
  • Loading branch information
pnengchu authored Oct 12, 2021
2 parents 5efc0c8 + adcfee4 commit b8450e3
Show file tree
Hide file tree
Showing 21 changed files with 334 additions and 48 deletions.
4 changes: 3 additions & 1 deletion devtools/resetdb/datagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (d *datagen) NewAlert(status alert.Status) {
// NewFavorite will generate a new favorite for the provided user ID.
func (d *datagen) NewFavorite(userID string) {
var tgt assignment.Target
switch rand.Intn(4) {
switch rand.Intn(5) {
case 0:
tgt = assignment.ServiceTarget(d.ids.Gen(func() string { return d.Services[rand.Intn(len(d.Services))].ID }, "favSvc", userID))
case 1:
Expand All @@ -353,6 +353,8 @@ func (d *datagen) NewFavorite(userID string) {
tgt = assignment.ScheduleTarget(d.ids.Gen(func() string { return d.Schedules[rand.Intn(len(d.Schedules))].ID }, "favSched", userID))
case 3:
tgt = assignment.EscalationPolicyTarget(d.ids.Gen(func() string { return d.EscalationPolicies[rand.Intn(len(d.EscalationPolicies))].ID }, "favEP", userID))
case 4:
tgt = assignment.UserTarget(d.ids.Gen(func() string { return d.Users[rand.Intn(len(d.Users))].ID }, "favUsr", userID))
}

d.Favorites = append(d.Favorites, userFavorite{
Expand Down
91 changes: 91 additions & 0 deletions graphql2/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions graphql2/graphqlapp/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/target/goalert/validation/validate"

"github.com/pkg/errors"
"github.com/target/goalert/assignment"
"github.com/target/goalert/escalation"
"github.com/target/goalert/graphql2"
"github.com/target/goalert/permission"
Expand Down Expand Up @@ -95,6 +96,12 @@ func (a *Mutation) CreateUser(ctx context.Context, input graphql2.CreateUserInpu
if err != nil {
return err
}
if input.Favorite != nil && *input.Favorite {
err = a.FavoriteStore.SetTx(ctx, tx, permission.UserID(ctx), assignment.UserTarget(newUser.ID))
if err != nil {
return err
}
}
err = a.AuthBasicStore.CreateTx(ctx, tx, newUser.ID, input.Username, input.Password)
if err != nil {
return err
Expand Down Expand Up @@ -150,6 +157,7 @@ func (q *Query) Users(ctx context.Context, opts *graphql2.UserSearchOptions, fir
}

var searchOpts user.SearchOptions
searchOpts.FavoritesUserID = permission.UserID(ctx)
if opts.Search != nil {
searchOpts.Search = *opts.Search
}
Expand All @@ -172,6 +180,12 @@ func (q *Query) Users(ctx context.Context, opts *graphql2.UserSearchOptions, fir
if opts.CMType != nil {
searchOpts.CMType = *opts.CMType
}
if opts.FavoritesOnly != nil {
searchOpts.FavoritesOnly = *opts.FavoritesOnly
}
if opts.FavoritesFirst != nil {
searchOpts.FavoritesFirst = *opts.FavoritesFirst
}

searchOpts.Limit++
users, err := q.UserStore.Search(ctx, &searchOpts)
Expand Down Expand Up @@ -208,3 +222,7 @@ func (a *Query) User(ctx context.Context, id *string) (*user.User, error) {
}
return (*App)(a).FindOneUser(ctx, userID)
}

func (a *User) IsFavorite(ctx context.Context, raw *user.User) (bool, error) {
return raw.IsUserFavorite(), nil
}
15 changes: 9 additions & 6 deletions graphql2/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions graphql2/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ input CreateUserInput {
name: String
email: String
role: UserRole
favorite: Boolean
}

input CreateUserCalendarSubscriptionInput {
Expand Down Expand Up @@ -806,6 +807,12 @@ input UserSearchOptions {
omit: [ID!]
CMValue: String = ""
CMType: ContactMethodType

# Include only favorited services in the results.
favoritesOnly: Boolean = false

# Sort favorite services first.
favoritesFirst: Boolean = false
}

input AlertSearchOptions {
Expand Down Expand Up @@ -1083,6 +1090,8 @@ type User {
sessions: [UserSession!]!

onCallSteps: [EscalationPolicyStep!]!

isFavorite: Boolean!
}

type UserSession {
Expand Down
9 changes: 9 additions & 0 deletions migrate/migrations/20210817132557-add-user-favorite.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- +migrate Up
ALTER TABLE user_favorites
ADD COLUMN tgt_user_id UUID REFERENCES users(id) ON DELETE CASCADE,
ADD CONSTRAINT user_favorites_user_id_tgt_user_id_key UNIQUE(user_id, tgt_user_id);

-- +migrate Down
ALTER TABLE user_favorites
DROP CONSTRAINT user_favorites_user_id_tgt_user_id_key,
DROP COLUMN tgt_user_id;
Loading

0 comments on commit b8450e3

Please sign in to comment.