-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalert_subscription.go
138 lines (128 loc) · 4.73 KB
/
alert_subscription.go
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
package handler
import (
"errors"
"net/http"
"github.com/USACE/instrumentation-api/api/internal/db"
"github.com/USACE/instrumentation-api/api/internal/dto"
"github.com/USACE/instrumentation-api/api/internal/httperr"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
)
// SubscribeProfileToAlerts godoc
//
// @Summary subscribes a profile to an alert
// @Tags alert-subscription
// @Produce json
// @Param project_id path string true "project uuid" Format(uuid)
// @Param instrument_id path string true "instrument uuid" Format(uuid)
// @Param alert_config_id path string true "alert config uuid" Format(uuid)
// @Param key query string false "api key"
// @Success 201 {object} db.AlertProfileSubscription
// @Failure 400 {object} echo.HTTPError
// @Failure 404 {object} echo.HTTPError
// @Failure 500 {object} echo.HTTPError
// @Router /projects/{project_id}/instruments/{instrument_id}/alert_configs/{alert_config_id}/subscribe [post]
// @Security Bearer
func (h *ApiHandler) SubscribeProfileToAlerts(c echo.Context) error {
p := c.Get("profile").(db.VProfile)
profileID := p.ID
alertConfigID, err := uuid.Parse(c.Param("alert_config_id"))
if err != nil {
return httperr.MalformedID(err)
}
pa, err := h.DBService.AlertProfileSubscriptionCreateForAlertConfigProfile(c.Request().Context(), alertConfigID, profileID)
if err != nil {
return httperr.InternalServerError(err)
}
return c.JSON(http.StatusOK, pa)
}
// UnsubscribeProfileToAlerts godoc
//
// @Summary unsubscribes a profile to an alert
// @Tags alert-subscription
// @Produce json
// @Param project_id path string true "project uuid" Format(uuid)
// @Param instrument_id path string true "instrument uuid" Format(uuid)
// @Param alert_config_id path string true "alert config uuid" Format(uuid)
// @Param key query string false "api key"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} echo.HTTPError
// @Failure 404 {object} echo.HTTPError
// @Failure 500 {object} echo.HTTPError
// @Router /projects/{project_id}/instruments/{instrument_id}/alert_configs/{alert_config_id}/unsubscribe [post]
// @Security Bearer
func (h *ApiHandler) UnsubscribeProfileToAlerts(c echo.Context) error {
p := c.Get("profile").(db.VProfile)
profileID := p.ID
alertConfigID, err := uuid.Parse(c.Param("alert_config_id"))
if err != nil {
return httperr.MalformedID(err)
}
if err = h.DBService.AlertProfileSubscriptionDelete(c.Request().Context(), db.AlertProfileSubscriptionDeleteParams{
AlertConfigID: alertConfigID,
ProfileID: profileID,
}); err != nil {
return httperr.InternalServerError(err)
}
return c.JSON(http.StatusOK, make(map[string]interface{}))
}
// ListMyAlertSubscriptions godoc
//
// @Summary lists all alerts subscribed to by the current profile
// @Tags alert-subscription
// @Produce json
// @Param key query string false "api key"
// @Success 200 {array} db.AlertProfileSubscription
// @Failure 400 {object} echo.HTTPError
// @Failure 404 {object} echo.HTTPError
// @Failure 500 {object} echo.HTTPError
// @Router /my_alert_subscriptions [get]
// @Security Bearer
func (h *ApiHandler) ListMyAlertSubscriptions(c echo.Context) error {
p := c.Get("profile").(db.VProfile)
profileID := p.ID
ss, err := h.DBService.AlertSubscriptionListForProfile(c.Request().Context(), profileID)
if err != nil {
return httperr.InternalServerError(err)
}
return c.JSON(http.StatusOK, ss)
}
// UpdateMyAlertSubscription godoc
//
// @Summary updates settings for an alert subscription
// @Tags alert-subscription
// @Accept json
// @Produce json
// @Param alert_subscription_id path string true "alert subscription id" Format(uuid)
// @Param alert_subscription body dto.AlertSubscription true "alert subscription payload"
// @Param key query string false "api key"
// @Success 200 {array} db.AlertProfileSubscription
// @Failure 400 {object} echo.HTTPError
// @Failure 404 {object} echo.HTTPError
// @Failure 500 {object} echo.HTTPError
// @Router /alert_subscriptions/{alert_subscription_id} [put]
// @Security Bearer
func (h *ApiHandler) UpdateMyAlertSubscription(c echo.Context) error {
var s dto.AlertSubscription
if err := c.Bind(&s); err != nil {
return httperr.MalformedBody(err)
}
sID, err := uuid.Parse(c.Param("alert_subscription_id"))
if err != nil {
return httperr.MalformedID(err)
}
s.ID = sID
p := c.Get("profile").(db.VProfile)
t, err := h.DBService.AlertSubscriptionGet(c.Request().Context(), sID)
if err != nil {
return httperr.InternalServerError(err)
}
if p.ID != t.ProfileID {
return httperr.Unauthorized(errors.New("profile id or requester did not match alert subscription id"))
}
sUpdated, err := h.DBService.AlertProfileSubscriptionUpdateForProfile(c.Request().Context(), s)
if err != nil {
return httperr.InternalServerError(err)
}
return c.JSON(http.StatusOK, sUpdated)
}