Skip to content

Commit dcb9f98

Browse files
authored
Merge pull request #6398 from devtron-labs/notif-back-com
misc: Notif back com
2 parents ea98cd9 + ab70a1d commit dcb9f98

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

api/restHandler/NotificationRestHandler.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const (
5252

5353
type NotificationRestHandler interface {
5454
SaveNotificationSettings(w http.ResponseWriter, r *http.Request)
55+
SaveNotificationSettingsV2(w http.ResponseWriter, r *http.Request)
5556
UpdateNotificationSettings(w http.ResponseWriter, r *http.Request)
5657
SaveNotificationChannelConfig(w http.ResponseWriter, r *http.Request)
5758
FindSESConfig(w http.ResponseWriter, r *http.Request)
@@ -117,6 +118,7 @@ func NewNotificationRestHandlerImpl(dockerRegistryConfig pipeline.DockerRegistry
117118
}
118119
}
119120

121+
// SaveNotificationSettings will be deprecated in future
120122
func (impl NotificationRestHandlerImpl) SaveNotificationSettings(w http.ResponseWriter, r *http.Request) {
121123
userId, err := impl.userAuthService.GetLoggedInUser(r)
122124
if userId == 0 || err != nil {
@@ -146,6 +148,65 @@ func (impl NotificationRestHandlerImpl) SaveNotificationSettings(w http.Response
146148
}
147149
//RBAC
148150

151+
providers := notificationSetting.Providers
152+
153+
if len(providers) != 0 {
154+
for _, provider := range providers {
155+
if provider.Destination == util.SMTP || provider.Destination == util.SES {
156+
if provider.Recipient == "" {
157+
userEmail, err := impl.userAuthService.GetEmailById(int32(provider.ConfigId))
158+
if err != nil {
159+
impl.logger.Errorw("service err, SaveNotificationSettings", "err", err, "payload", notificationSetting)
160+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
161+
return
162+
}
163+
provider.Recipient = userEmail
164+
}
165+
// get default configID for SES and SMTP
166+
provider.ConfigId = notificationSetting.SesConfigId
167+
}
168+
}
169+
}
170+
171+
res, err := impl.notificationService.CreateOrUpdateNotificationSettings(&notificationSetting, userId)
172+
if err != nil {
173+
impl.logger.Errorw("service err, SaveNotificationSettings", "err", err, "payload", notificationSetting)
174+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
175+
return
176+
}
177+
w.Header().Set("Content-Type", "application/json")
178+
common.WriteJsonResp(w, nil, res, http.StatusOK)
179+
}
180+
181+
func (impl NotificationRestHandlerImpl) SaveNotificationSettingsV2(w http.ResponseWriter, r *http.Request) {
182+
userId, err := impl.userAuthService.GetLoggedInUser(r)
183+
if userId == 0 || err != nil {
184+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
185+
return
186+
}
187+
var notificationSetting beans.NotificationRequest
188+
err = json.NewDecoder(r.Body).Decode(&notificationSetting)
189+
if err != nil {
190+
impl.logger.Errorw("request err, SaveNotificationSettings", "err", err, "payload", notificationSetting)
191+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
192+
return
193+
}
194+
impl.logger.Infow("request payload, SaveNotificationSettings", "err", err, "payload", notificationSetting)
195+
err = impl.validator.Struct(notificationSetting)
196+
if err != nil {
197+
impl.logger.Errorw("validation err, SaveNotificationSettings", "err", err, "payload", notificationSetting)
198+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
199+
return
200+
}
201+
202+
//RBAC
203+
token := r.Header.Get("token")
204+
if isSuperAdmin := impl.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionGet, "*"); !isSuperAdmin {
205+
common.WriteJsonResp(w, err, nil, http.StatusForbidden)
206+
return
207+
}
208+
//RBAC
209+
149210
res, err := impl.notificationService.CreateOrUpdateNotificationSettings(&notificationSetting, userId)
150211
if err != nil {
151212
impl.logger.Errorw("service err, SaveNotificationSettings", "err", err, "payload", notificationSetting)

api/router/NotificationRouter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ func NewNotificationRouterImpl(notificationRestHandler restHandler.NotificationR
3232
return &NotificationRouterImpl{notificationRestHandler: notificationRestHandler}
3333
}
3434
func (impl NotificationRouterImpl) InitNotificationRegRouter(configRouter *mux.Router) {
35+
// to maintain backward compatibility, will be deprecated in future
3536
configRouter.Path("").
3637
HandlerFunc(impl.notificationRestHandler.SaveNotificationSettings).
3738
Methods("POST")
39+
// new router to save notification settings
40+
configRouter.Path("/v2").
41+
HandlerFunc(impl.notificationRestHandler.SaveNotificationSettingsV2).
42+
Methods("POST")
3843
configRouter.Path("").
3944
HandlerFunc(impl.notificationRestHandler.UpdateNotificationSettings).
4045
Methods("PUT")

pkg/notifier/beans/beans.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ type NotificationRequest struct {
226226
UpdateType util.UpdateType `json:"updateType,omitempty"`
227227
Providers []*bean.Provider `json:"providers"`
228228
NotificationConfigRequest []*NotificationConfigRequest `json:"notificationConfigRequest" validate:"required"`
229+
// will be deprecated in future
230+
SesConfigId int `json:"sesConfigId"`
229231
}
230232

231233
type NotificationUpdateRequest struct {

0 commit comments

Comments
 (0)