File size: 3,298 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
package schedule

import (
	"context"
	"database/sql"
	"fmt"

	"github.com/google/uuid"
	"github.com/target/goalert/permission"
	"github.com/target/goalert/util/timeutil"
	"github.com/target/goalert/validation"
	"github.com/target/goalert/validation/validate"
)

const onCallNotificationRuleLimit = 50

// SetOnCallNotificationRules will set/replace all notification rules for the given schedule ID.
func (store *Store) SetOnCallNotificationRules(ctx context.Context, tx *sql.Tx, scheduleID uuid.UUID, rules []OnCallNotificationRule) error {
	err := permission.LimitCheckAny(ctx, permission.User)
	if err != nil {
		return err
	}

	err = validate.Range("Rules", len(rules), 0, onCallNotificationRuleLimit)
	if err != nil {
		return err
	}

	type dupkey struct {
		HasTime bool
		Time    timeutil.Clock
		Channel uuid.UUID
	}
	m := make(map[dupkey]struct{})
	for i, r := range rules {
		if r.WeekdayFilter != nil && r.WeekdayFilter.IsNever() {
			return validation.NewFieldError("Rules[%d].WeekdayFilter", "At least one day must be enabled when specifying a weekday filter.")
		}
		if r.WeekdayFilter != nil && r.Time == nil {
			return validation.NewFieldError("Rules[%d].WeekdayFilter", "Weekday filter may only be used with Time.")
		}
		if r.WeekdayFilter == nil && r.Time != nil {
			return validation.NewFieldError("Rules[%d].WeekdayFilter", "Weekday filter is required with Time.")
		}
		key := dupkey{
			HasTime: r.Time != nil,
			Channel: r.ChannelID,
		}
		if key.HasTime {
			key.Time = *r.Time
		}

		if _, ok := m[key]; ok {
			if key.HasTime {
				return validation.NewFieldError(fmt.Sprintf("Rules[%d]", i), "Rule already exists for that channel and time-of-day.")
			}

			return validation.NewFieldError(fmt.Sprintf("Rules[%d]", i), "On-change rule already exists for that channel.")
		}
		m[key] = struct{}{}
	}

	ids := make([]bool, onCallNotificationRuleLimit)
	for i, r := range rules {
		if !r.ID.valid {
			continue
		}
		fieldName := fmt.Sprintf("Rules[%d].ID", i)
		err = validate.Range(fieldName, r.ID.id, 0, onCallNotificationRuleLimit)
		if err != nil {
			return err
		}
		if r.ID.scheduleID != scheduleID {
			return validation.NewFieldError(fieldName, "wrong schedule ID")
		}
		if ids[r.ID.id] {
			return validation.NewFieldError(fieldName, "duplicate ID value not allowed")
		}
		ids[r.ID.id] = true
	}
	nextID := func() int {
		for i, used := range ids {
			if used {
				continue
			}
			ids[i] = true
			return i
		}

		// should be impossible
		panic("could not find unused ID")
	}

	for i, r := range rules {
		if r.ID.valid {
			continue
		}

		rules[i].ID.scheduleID = scheduleID
		rules[i].ID.valid = true
		rules[i].ID.id = nextID()
	}

	return store.updateScheduleData(ctx, tx, scheduleID, func(data *Data) error {
		data.V1.OnCallNotificationRules = rules

		return nil
	})
}

// OnCallNotificationRules returns the current set of OnCallNotificationRules for the provided scheduleID.
func (store *Store) OnCallNotificationRules(ctx context.Context, tx *sql.Tx, scheduleID uuid.UUID) ([]OnCallNotificationRule, error) {
	err := permission.LimitCheckAny(ctx, permission.User)
	if err != nil {
		return nil, err
	}

	data, err := store.scheduleData(ctx, tx, scheduleID)
	if err != nil {
		return nil, err
	}

	return data.V1.OnCallNotificationRules, nil
}