File size: 1,301 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
package graphqlapp

import (
	context "context"
	"database/sql"

	"github.com/target/goalert/graphql2"
	"github.com/target/goalert/user/contactmethod"
	"github.com/target/goalert/user/notificationrule"
	"github.com/target/goalert/validation/validate"
)

type UserNotificationRule App

func (a *App) UserNotificationRule() graphql2.UserNotificationRuleResolver {
	return (*UserNotificationRule)(a)
}

func (m *Mutation) CreateUserNotificationRule(ctx context.Context, input graphql2.CreateUserNotificationRuleInput) (*notificationrule.NotificationRule, error) {
	nr := &notificationrule.NotificationRule{
		DelayMinutes: input.DelayMinutes,
	}

	if input.UserID != nil {
		nr.UserID = *input.UserID
	}

	if input.ContactMethodID != nil {
		id, err := validate.ParseUUID("ContactMethodID", *input.ContactMethodID)
		if err != nil {
			return nil, err
		}
		nr.ContactMethodID = id
	}

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

	return nr, nil
}

func (nr *UserNotificationRule) ContactMethod(ctx context.Context, raw *notificationrule.NotificationRule) (*contactmethod.ContactMethod, error) {
	return (*App)(nr).FindOneCM(ctx, raw.ContactMethodID)
}