Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dest: move schedule and rotation to nfydest.Registry #3998

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ func (app *App) startup(ctx context.Context) error {
return app.startupErr
}

app.DestRegistry.RegisterProvider(ctx, app.ScheduleStore)
app.DestRegistry.RegisterProvider(ctx, app.UserStore)
app.DestRegistry.RegisterProvider(ctx, app.RotationStore)
app.DestRegistry.RegisterProvider(ctx, app.AlertStore)
app.DestRegistry.RegisterProvider(ctx, app.slackChan)
app.DestRegistry.RegisterProvider(ctx, app.slackChan.DMSender())
Expand Down
18 changes: 10 additions & 8 deletions graphql2/graphqlapp/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/target/goalert/notification/slack"
"github.com/target/goalert/notification/webhook"
"github.com/target/goalert/notificationchannel"
"github.com/target/goalert/schedule"
"github.com/target/goalert/schedule/rotation"
"github.com/target/goalert/user"
"github.com/target/goalert/user/contactmethod"
)
Expand All @@ -25,13 +27,13 @@ func CompatTargetToDest(tgt assignment.Target) (gadb.DestV1, error) {
}, nil
case assignment.TargetTypeRotation:
return gadb.DestV1{
Type: destRotation,
Args: map[string]string{fieldRotationID: tgt.TargetID()},
Type: rotation.DestTypeRotation,
Args: map[string]string{rotation.FieldRotationID: tgt.TargetID()},
}, nil
case assignment.TargetTypeSchedule:
return gadb.DestV1{
Type: destSchedule,
Args: map[string]string{fieldScheduleID: tgt.TargetID()},
Type: schedule.DestTypeSchedule,
Args: map[string]string{schedule.FieldScheduleID: tgt.TargetID()},
}, nil
case assignment.TargetTypeChanWebhook:
return gadb.DestV1{
Expand Down Expand Up @@ -111,15 +113,15 @@ func CompatDestToTarget(d gadb.DestV1) (assignment.RawTarget, error) {
Type: assignment.TargetTypeUser,
ID: d.Arg(user.FieldUserID),
}, nil
case destRotation:
case rotation.DestTypeRotation:
return assignment.RawTarget{
Type: assignment.TargetTypeRotation,
ID: d.Arg(fieldRotationID),
ID: d.Arg(rotation.FieldRotationID),
}, nil
case destSchedule:
case schedule.DestTypeSchedule:
return assignment.RawTarget{
Type: assignment.TargetTypeSchedule,
ID: d.Arg(fieldScheduleID),
ID: d.Arg(schedule.FieldScheduleID),
}, nil
case slack.DestTypeSlackChannel:
return assignment.RawTarget{
Expand Down
24 changes: 0 additions & 24 deletions graphql2/graphqlapp/destinationdisplayinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/mail"

"github.com/nyaruka/phonenumbers"
"github.com/target/goalert/config"
"github.com/target/goalert/gadb"
"github.com/target/goalert/graphql2"
"github.com/target/goalert/notification/nfydest"
Expand Down Expand Up @@ -62,7 +61,6 @@ func (a *Query) DestinationDisplayInfo(ctx context.Context, dest gadb.DestV1) (*

func (a *Query) _DestinationDisplayInfo(ctx context.Context, dest gadb.DestV1, skipValidation bool) (*nfydest.DisplayInfo, error) {
app := (*App)(a)
cfg := config.FromContext(ctx)
if !skipValidation {
if err := app.ValidateDestination(ctx, "input", &dest); err != nil {
return nil, err
Expand Down Expand Up @@ -100,28 +98,6 @@ func (a *Query) _DestinationDisplayInfo(ctx context.Context, dest gadb.DestV1, s
IconAltText: "Email",
Text: e.Address,
}, nil
case destRotation:
r, err := app.FindOneRotation(ctx, dest.Arg(fieldRotationID))
if err != nil {
return nil, err
}
return &nfydest.DisplayInfo{
IconURL: "builtin://rotation",
IconAltText: "Rotation",
LinkURL: cfg.CallbackURL("/rotations/" + r.ID),
Text: r.Name,
}, nil
case destSchedule:
s, err := app.FindOneSchedule(ctx, dest.Arg(fieldScheduleID))
if err != nil {
return nil, err
}
return &nfydest.DisplayInfo{
IconURL: "builtin://schedule",
IconAltText: "Schedule",
LinkURL: cfg.CallbackURL("/schedules/" + s.ID),
Text: s.Name,
}, nil
}

return app.DestReg.DisplayInfo(ctx, dest)
Expand Down
103 changes: 0 additions & 103 deletions graphql2/graphqlapp/destinationtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ const (
destTwilioSMS = "builtin-twilio-sms"
destTwilioVoice = "builtin-twilio-voice"
destSMTP = "builtin-smtp-email"
destRotation = "builtin-rotation"
destSchedule = "builtin-schedule"

fieldPhoneNumber = "phone_number"
fieldEmailAddress = "email_address"
fieldRotationID = "rotation_id"
fieldScheduleID = "schedule_id"
)

type (
Expand All @@ -32,85 +28,10 @@ type (
)

func (q *Query) DestinationFieldValueName(ctx context.Context, input graphql2.DestinationFieldValidateInput) (string, error) {
switch input.FieldID {

case fieldRotationID:
rot, err := q.Rotation(ctx, input.Value)
if err != nil {
return "", err
}

return rot.Name, nil
case fieldScheduleID:
sched, err := q.Schedule(ctx, input.Value)
if err != nil {
return "", err
}

return sched.Name, nil
}

return q.DestReg.FieldLabel(ctx, input.DestType, input.FieldID, input.Value)
}

func (q *Query) DestinationFieldSearch(ctx context.Context, input graphql2.DestinationFieldSearchInput) (*graphql2.FieldSearchConnection, error) {
favFirst := true

switch input.FieldID {
case fieldRotationID:
res, err := q.Rotations(ctx, &graphql2.RotationSearchOptions{
Omit: input.Omit,
First: input.First,
Search: input.Search,
After: input.After,
FavoritesFirst: &favFirst,
})
if err != nil {
return nil, err
}

var nodes []graphql2.FieldSearchResult
for _, rot := range res.Nodes {
nodes = append(nodes, graphql2.FieldSearchResult{
FieldID: input.FieldID,
Value: rot.ID,
Label: rot.Name,
IsFavorite: rot.IsUserFavorite(),
})
}

return &graphql2.FieldSearchConnection{
Nodes: nodes,
PageInfo: res.PageInfo,
}, nil
case fieldScheduleID:
res, err := q.Schedules(ctx, &graphql2.ScheduleSearchOptions{
Omit: input.Omit,
First: input.First,
Search: input.Search,
After: input.After,
FavoritesFirst: &favFirst,
})
if err != nil {
return nil, err
}

var nodes []graphql2.FieldSearchResult
for _, sched := range res.Nodes {
nodes = append(nodes, graphql2.FieldSearchResult{
FieldID: input.FieldID,
Value: sched.ID,
Label: sched.Name,
IsFavorite: sched.IsUserFavorite(),
})
}

return &graphql2.FieldSearchConnection{
Nodes: nodes,
PageInfo: res.PageInfo,
}, nil
}

var opts nfydest.SearchOptions
opts.Omit = input.Omit
if input.First != nil {
Expand Down Expand Up @@ -238,30 +159,6 @@ func (q *Query) DestinationTypes(ctx context.Context, isDynamicAction *bool) ([]
Hint: "Body of the email message.",
}},
},
{
Type: destRotation,
Name: "Rotation",
Enabled: true,
SupportsAlertNotifications: true,
RequiredFields: []nfydest.FieldConfig{{
FieldID: fieldRotationID,
Label: "Rotation",
InputType: "text",
SupportsSearch: true,
}},
},
{
Type: destSchedule,
Name: "Schedule",
Enabled: true,
SupportsAlertNotifications: true,
RequiredFields: []nfydest.FieldConfig{{
FieldID: fieldScheduleID,
Label: "Schedule",
InputType: "text",
SupportsSearch: true,
}},
},
}

fromReg, err := q.DestReg.Types(ctx)
Expand Down
31 changes: 0 additions & 31 deletions graphql2/graphqlapp/destinationvalidation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package graphqlapp

import (
"context"
"database/sql"
"errors"
"fmt"
"strconv"
Expand Down Expand Up @@ -155,36 +154,6 @@ func (a *App) ValidateDestination(ctx context.Context, fieldName string, dest *g
if err != nil {
return addDestFieldError(ctx, fieldName, fieldEmailAddress, err)
}
return nil
case destSchedule: // must be valid UUID and exist
_, err := validate.ParseUUID(fieldScheduleID, dest.Arg(fieldScheduleID))
if err != nil {
return addDestFieldError(ctx, fieldName, fieldScheduleID, err)
}

_, err = a.ScheduleStore.FindOne(ctx, dest.Arg(fieldScheduleID))
if errors.Is(err, sql.ErrNoRows) {
return addDestFieldError(ctx, fieldName, fieldScheduleID, validation.NewGenericError("schedule does not exist"))
}
if err != nil {
return addDestFieldError(ctx, fieldName, fieldScheduleID, err)
}

return nil
case destRotation: // must be valid UUID and exist
rotID := dest.Arg(fieldRotationID)
_, err := validate.ParseUUID(fieldRotationID, rotID)
if err != nil {
return addDestFieldError(ctx, fieldName, fieldRotationID, err)
}
_, err = a.RotationStore.FindRotation(ctx, rotID)
if errors.Is(err, sql.ErrNoRows) {
return addDestFieldError(ctx, fieldName, fieldRotationID, validation.NewGenericError("rotation does not exist"))
}
if err != nil {
return addDestFieldError(ctx, fieldName, fieldRotationID, err)
}

return nil
}

Expand Down
117 changes: 117 additions & 0 deletions schedule/nfydest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package schedule

import (
"context"

"github.com/target/goalert/config"
"github.com/target/goalert/notification/nfydest"
"github.com/target/goalert/permission"
"github.com/target/goalert/search"
"github.com/target/goalert/validation"
)

const (
DestTypeSchedule = "builtin-schedule"
FieldScheduleID = "schedule_id"

FallbackIconURL = "builtin://schedule"
)

var (
_ nfydest.Provider = (*Store)(nil)
_ nfydest.FieldSearcher = (*Store)(nil)
)

func (s *Store) ID() string { return DestTypeSchedule }
func (s *Store) TypeInfo(ctx context.Context) (*nfydest.TypeInfo, error) {
return &nfydest.TypeInfo{
Type: DestTypeSchedule,
Name: "Schedule",
Enabled: true,
SupportsAlertNotifications: true,
RequiredFields: []nfydest.FieldConfig{{
FieldID: FieldScheduleID,
Label: "Schedule",
InputType: "text",
SupportsSearch: true,
}},
}, nil
}

func (s *Store) DisplayInfo(ctx context.Context, args map[string]string) (*nfydest.DisplayInfo, error) {
cfg := config.FromContext(ctx)

sched, err := s.FindOne(ctx, args[FieldScheduleID])
if err != nil {
return nil, err
}

return &nfydest.DisplayInfo{
IconURL: FallbackIconURL,
IconAltText: "Schedule",
LinkURL: cfg.CallbackURL("/schedules/" + sched.ID),
Text: sched.Name,
}, nil
}

func (s *Store) ValidateField(ctx context.Context, fieldID, value string) error {
switch fieldID {
case FieldScheduleID:
_, err := s.FindOne(ctx, value)
return err
}

return validation.NewGenericError("unknown field ID")
}

func (s *Store) FieldLabel(ctx context.Context, fieldID, value string) (string, error) {
switch fieldID {
case FieldScheduleID:
sched, err := s.FindOne(ctx, value)
if err != nil {
return "", err
}
return sched.Name, nil
}

return "", validation.NewGenericError("unknown field ID")
}

func (s Schedule) AsField() nfydest.FieldValue {
return nfydest.FieldValue{
Value: s.ID,
Label: s.Name,
IsFavorite: s.isUserFavorite,
}
}

func (s Schedule) Cursor() (string, error) {
return search.Cursor(SearchCursor{
Name: s.Name,
IsFavorite: s.isUserFavorite,
})
}

func (so *SearchOptions) FromNotifyOptions(ctx context.Context, opts nfydest.SearchOptions) error {
so.Search = opts.Search
so.Omit = opts.Omit
so.Limit = opts.Limit
if opts.Cursor != "" {
err := search.ParseCursor(opts.Cursor, &so.After)
if err != nil {
return err
}
}
so.FavoritesFirst = true
so.FavoritesUserID = permission.UserID(ctx)
return nil
}

func (s *Store) SearchField(ctx context.Context, fieldID string, options nfydest.SearchOptions) (*nfydest.SearchResult, error) {
switch fieldID {
case FieldScheduleID:
return nfydest.SearchByCursorFunc(ctx, options, s.Search)
}

return nil, validation.NewGenericError("unknown field ID")
}
Loading
Loading