File size: 6,427 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package graphqlapp

import (
	"context"
	"database/sql"
	"errors"

	"github.com/target/goalert/config"
	"github.com/target/goalert/graphql2"
	"github.com/target/goalert/notification"
	"github.com/target/goalert/notification/twilio"
	"github.com/target/goalert/user/contactmethod"
	"github.com/target/goalert/validation"
	"github.com/target/goalert/validation/validate"
)

type (
	ContactMethod App
)

func (a *App) UserContactMethod() graphql2.UserContactMethodResolver {
	return (*ContactMethod)(a)
}

func (a *ContactMethod) Type(ctx context.Context, obj *contactmethod.ContactMethod) (*graphql2.ContactMethodType, error) {
	cmType, _ := CompatDestToCMTypeVal(obj.Dest)
	return &cmType, nil
}

func (a *ContactMethod) Value(ctx context.Context, obj *contactmethod.ContactMethod) (string, error) {
	_, cmVal := CompatDestToCMTypeVal(obj.Dest)
	return cmVal, nil
}

func (a *ContactMethod) StatusUpdates(ctx context.Context, obj *contactmethod.ContactMethod) (graphql2.StatusUpdateState, error) {
	info, err := a.DestReg.TypeInfo(ctx, obj.Dest.Type)
	if err != nil {
		return "", err
	}

	if !info.SupportsStatusUpdates {
		return graphql2.StatusUpdateStateDisabledForced, nil
	}

	if info.StatusUpdatesRequired {
		return graphql2.StatusUpdateStateEnabledForced, nil
	}

	if obj.StatusUpdates {
		return graphql2.StatusUpdateStateEnabled, nil
	}

	return graphql2.StatusUpdateStateDisabled, nil
}

func (a *ContactMethod) FormattedValue(ctx context.Context, obj *contactmethod.ContactMethod) (string, error) {
	info, err := a.DestReg.DisplayInfo(ctx, obj.Dest)
	if err != nil {
		return "", err
	}
	return info.Text, nil
}

func (a *ContactMethod) LastTestMessageState(ctx context.Context, obj *contactmethod.ContactMethod) (*graphql2.NotificationState, error) {
	t := obj.LastTestVerifyAt()
	if t.IsZero() {
		return nil, nil
	}

	status, _, err := a.NotificationStore.LastMessageStatus(ctx, notification.MessageTypeTest, obj.ID.String(), t)
	if err != nil {
		return nil, err
	}
	if status == nil {
		return nil, nil
	}

	cfg := config.FromContext(ctx)
	if obj.Dest.Type == twilio.DestTypeTwilioSMS && cfg.Twilio.RCSSenderID != "" && status.SrcValue == cfg.Twilio.RCSSenderID {
		// TODO: remove this when we have a better way to get the sender name
		status.SrcValue = cfg.ApplicationName()
	}

	return notificationStateFromSendResult(status.Status, status.SrcValue), nil
}

func (a *ContactMethod) LastVerifyMessageState(ctx context.Context, obj *contactmethod.ContactMethod) (*graphql2.NotificationState, error) {
	t := obj.LastTestVerifyAt()
	if t.IsZero() {
		return nil, nil
	}

	status, _, err := a.NotificationStore.LastMessageStatus(ctx, notification.MessageTypeVerification, obj.ID.String(), t)
	if err != nil {
		return nil, err
	}
	if status == nil {
		return nil, nil
	}

	cfg := config.FromContext(ctx)
	if obj.Dest.Type == twilio.DestTypeTwilioSMS && cfg.Twilio.RCSSenderID != "" && status.SrcValue == cfg.Twilio.RCSSenderID {
		// TODO: remove this when we have a better way to get the sender name
		status.SrcValue = cfg.ApplicationName()
	}

	return notificationStateFromSendResult(status.Status, status.SrcValue), nil
}

func (q *Query) UserContactMethod(ctx context.Context, idStr string) (*contactmethod.ContactMethod, error) {
	id, err := validate.ParseUUID("ID", idStr)
	if err != nil {
		return nil, err
	}
	return (*App)(q).FindOneCM(ctx, id)
}

func (m *Mutation) CreateUserContactMethod(ctx context.Context, input graphql2.CreateUserContactMethodInput) (*contactmethod.ContactMethod, error) {
	cm := &contactmethod.ContactMethod{
		Name:          input.Name,
		UserID:        input.UserID,
		Disabled:      true,
		StatusUpdates: input.EnableStatusUpdates != nil && *input.EnableStatusUpdates,
	}

	if input.Dest != nil {
		if err := (*App)(m).ValidateDestination(ctx, "input.dest", input.Dest); err != nil {
			return nil, err
		}
		cm.Dest = *input.Dest
	} else if input.Type != nil && input.Value != nil {
		var err error
		cm.Dest, err = CompatCMTypeValToDest(*input.Type, *input.Value)
		if err != nil {
			return nil, err
		}
	} else {
		return nil, validation.NewFieldError("input", "must provide either dest or type/value")
	}

	err := withContextTx(ctx, m.DB, func(ctx context.Context, tx *sql.Tx) error {
		var err error
		cm, err = m.CMStore.Create(ctx, tx, cm)
		if err != nil {
			return err
		}

		if input.NewUserNotificationRule != nil {
			input.NewUserNotificationRule.UserID = &input.UserID
			str := cm.ID.String()
			input.NewUserNotificationRule.ContactMethodID = &str

			_, err = m.CreateUserNotificationRule(ctx, *input.NewUserNotificationRule)
			if err != nil {
				return validation.AddPrefix("newUserNotificationRule.", err)
			}
		}
		return err
	})
	if err != nil {
		return nil, err
	}

	return cm, nil
}

func (m *Mutation) UpdateUserContactMethod(ctx context.Context, input graphql2.UpdateUserContactMethodInput) (bool, error) {
	if input.Value != nil {
		return false, validation.NewFieldError("input.value", "cannot update value")
	}

	err := withContextTx(ctx, m.DB, func(ctx context.Context, tx *sql.Tx) error {
		id, err := validate.ParseUUID("ID", input.ID)
		if err != nil {
			return err
		}
		cm, err := m.CMStore.FindOne(ctx, tx, id)
		if errors.Is(err, sql.ErrNoRows) {
			return validation.NewFieldError("id", "contact method not found")
		}
		if err != nil {
			return err
		}
		if input.Name != nil {
			err := validate.IDName("input.name", *input.Name)
			if err != nil {
				addInputError(ctx, err)
				return errAlreadySet
			}
			cm.Name = *input.Name
		}

		if input.EnableStatusUpdates != nil {
			cm.StatusUpdates = *input.EnableStatusUpdates
		}

		return m.CMStore.Update(ctx, tx, cm)
	})
	return err == nil, err
}

func (m *Mutation) SendContactMethodVerification(ctx context.Context, input graphql2.SendContactMethodVerificationInput) (bool, error) {
	err := m.NotificationStore.SendContactMethodVerification(ctx, input.ContactMethodID)
	return err == nil, err
}

func (m *Mutation) VerifyContactMethod(ctx context.Context, input graphql2.VerifyContactMethodInput) (bool, error) {
	err := validate.Range("Code", input.Code, 100000, 999999)
	if err != nil {
		// return "must be 6 digits" error as we care about # of digits, not the code's actual value
		return false, validation.NewFieldError("Code", "must be 6 digits")
	}

	err = m.NotificationStore.VerifyContactMethod(ctx, input.ContactMethodID, input.Code)
	return err == nil, err
}