File size: 3,061 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package schedule

import (
	"context"

	"github.com/target/goalert/config"
	"github.com/target/goalert/gadb"
	"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 DestFromID(scheduleID string) gadb.DestV1 {
	return gadb.DestV1{
		Type: DestTypeSchedule,
		Args: map[string]string{FieldScheduleID: scheduleID},
	}
}

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")
}