File size: 2,201 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
-- name: SchedMgrNCDedupMapping :many
-- Returns the mapping of old notification channel IDs to new notification channel IDs.
SELECT
    old_id,
    new_id
FROM
    notification_channel_duplicates;

-- name: SchedMgrDataIDs :many
-- Returns all schedule IDs that have an entry in the schedule_data table.
SELECT
    schedule_id
FROM
    schedule_data;

-- name: SchedMgrGetData :one
-- Returns the data for a single schedule.
SELECT
    data
FROM
    schedule_data
WHERE
    schedule_id = $1;

-- name: SchedMgrSetDataV1Rules :exec
-- Sets the .V1.OnCallNotificationRules for a schedule.
UPDATE
    schedule_data
SET
    data = jsonb_set(data, '{V1,OnCallNotificationRules}', $2)
WHERE
    schedule_id = $1;

-- name: SchedMgrRules :many
SELECT
    rule.*,
    coalesce(rule.tgt_user_id, part.user_id) AS resolved_user_id
FROM
    schedule_rules rule
    LEFT JOIN rotation_state rState ON rState.rotation_id = rule.tgt_rotation_id
    LEFT JOIN rotation_participants part ON part.id = rState.rotation_participant_id
WHERE
    coalesce(rule.tgt_user_id, part.user_id)
    NOTNULL;

-- name: SchedMgrOverrides :many
SELECT
    add_user_id,
    remove_user_id,
    tgt_schedule_id
FROM
    user_overrides
WHERE
    now() BETWEEN start_time AND end_time;

-- name: SchedMgrDataForUpdate :many
SELECT
    schedule_id,
    data
FROM
    schedule_data
WHERE
    data NOTNULL
FOR UPDATE;

-- name: SchedMgrSetData :exec
UPDATE
    schedule_data
SET
    data = $2
WHERE
    schedule_id = $1;

-- name: SchedMgrTimezones :many
SELECT
    id,
    time_zone
FROM
    schedules;

-- name: SchedMgrOnCall :many
SELECT
    schedule_id,
    user_id
FROM
    schedule_on_call_users
WHERE
    end_time ISNULL;

-- name: SchedMgrStartOnCall :exec
INSERT INTO schedule_on_call_users(schedule_id, start_time, user_id)
SELECT
    $1,
    now(),
    $2
FROM
    users
WHERE
    id = $2;

-- name: SchedMgrEndOnCall :exec
UPDATE
    schedule_on_call_users
SET
    end_time = now()
WHERE
    schedule_id = $1
    AND user_id = $2
    AND end_time ISNULL;

-- name: SchedMgrInsertMessage :exec
INSERT INTO outgoing_messages(id, message_type, channel_id, schedule_id)
    VALUES ($1, 'schedule_on_call_notification', $2, $3);