File size: 3,315 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package graphqlapp

import (
	context "context"
	"database/sql"

	"github.com/target/goalert/expflag"
	"github.com/target/goalert/gadb"
	"github.com/target/goalert/graphql2"
	"github.com/target/goalert/notification"
	"github.com/target/goalert/search"
	"github.com/target/goalert/validation/validate"

	"github.com/pkg/errors"
)

type (
	Query        App
	DebugMessage App
)

func (a *App) Query() graphql2.QueryResolver { return (*Query)(a) }

func (q *Query) MessageStatusHistory(ctx context.Context, idStr string) ([]graphql2.MessageStatusHistory, error) {
	id, err := validate.ParseUUID("ID", idStr)
	if err != nil {
		return nil, err
	}

	msgs, err := gadb.New(q.DB).GraphQL_MessageStatusHistory(ctx, id)
	if errors.Is(err, sql.ErrNoRows) {
		return nil, nil
	}
	if err != nil {
		return nil, err
	}

	hist := make([]graphql2.MessageStatusHistory, len(msgs))
	for i, m := range msgs {
		hist[i].Status = string(m.Status)
		hist[i].Details = m.StatusDetails
		hist[i].Timestamp = m.Timestamp
	}
	return hist, nil
}

func (q *Query) ListGQLFields(ctx context.Context, query *string) ([]string, error) {
	if query == nil || *query == "" {
		// List all fields if no query is provided.
		return graphql2.SchemaFields(), nil
	}

	return graphql2.QueryFields(*query)
}

func (a *Query) ExperimentalFlags(ctx context.Context) ([]string, error) {
	var flags []string
	for _, f := range expflag.AllFlags() {
		if !expflag.ContextHas(ctx, f) {
			continue
		}

		flags = append(flags, string(f))
	}

	return flags, nil
}

func (a *Query) LinkAccountInfo(ctx context.Context, token string) (*graphql2.LinkAccountInfo, error) {
	m, err := a.AuthLinkStore.FindLinkMetadata(ctx, token)
	if err != nil {
		return nil, err
	}
	if m == nil {
		return nil, nil
	}

	info := &graphql2.LinkAccountInfo{
		UserDetails: m.UserDetails,
	}
	if m.AlertID > 0 {
		info.AlertID = &m.AlertID
	}
	var s graphql2.AlertStatus
	switch m.AlertAction {
	case notification.ResultAcknowledge.String():
		s = graphql2.AlertStatusStatusAcknowledged
		info.AlertNewStatus = &s
	case notification.ResultResolve.String():
		s = graphql2.AlertStatusStatusClosed
		info.AlertNewStatus = &s
	}

	return info, nil
}

func (a *Query) AuthSubjectsForProvider(ctx context.Context, _first *int, _after *string, providerID string) (conn *graphql2.AuthSubjectConnection, err error) {
	var first int
	var after string
	if _after != nil {
		after = *_after
	}
	if _first != nil {
		first = *_first
	} else {
		first = 15
	}
	err = validate.Range("First", first, 1, 300)
	if err != nil {
		return nil, err
	}

	var c struct {
		ProviderID string
		LastID     string
	}

	if after != "" {
		err = search.ParseCursor(after, &c)
		if err != nil {
			return nil, errors.Wrap(err, "parse cursor")
		}
	} else {
		c.ProviderID = providerID
	}

	conn = new(graphql2.AuthSubjectConnection)
	conn.PageInfo = &graphql2.PageInfo{}
	conn.Nodes, err = a.UserStore.FindSomeAuthSubjectsForProvider(ctx, first+1, c.LastID, c.ProviderID)
	if err != nil {
		return nil, err
	}
	if len(conn.Nodes) > first {
		conn.Nodes = conn.Nodes[:first]
		conn.PageInfo.HasNextPage = true
	}
	if len(conn.Nodes) > 0 {
		c.LastID = conn.Nodes[len(conn.Nodes)-1].SubjectID
	}

	cur, err := search.Cursor(c)
	if err != nil {
		return nil, err
	}
	conn.PageInfo.EndCursor = &cur
	return conn, nil
}