-
Notifications
You must be signed in to change notification settings - Fork 462
/
SESNotificationService.go
278 lines (255 loc) · 9.53 KB
/
SESNotificationService.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright (c) 2020-2024. Devtron Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package notifier
import (
"fmt"
"github.com/devtron-labs/devtron/internal/sql/repository"
"github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/sql"
"github.com/devtron-labs/devtron/pkg/team"
util2 "github.com/devtron-labs/devtron/util/event"
"github.com/go-pg/pg"
"go.uber.org/zap"
"time"
)
const SES_CONFIG_TYPE = "ses"
type SESNotificationService interface {
SaveOrEditNotificationConfig(channelReq []*SESConfigDto, userId int32) ([]int, error)
FetchSESNotificationConfigById(id int) (*SESConfigDto, error)
FetchAllSESNotificationConfig() ([]*SESConfigDto, error)
FetchAllSESNotificationConfigAutocomplete() ([]*NotificationChannelAutoResponse, error)
DeleteNotificationConfig(channelReq *SESConfigDto, userId int32) error
}
type SESNotificationServiceImpl struct {
logger *zap.SugaredLogger
teamService team.TeamService
sesRepository repository.SESNotificationRepository
notificationSettingsRepository repository.NotificationSettingsRepository
}
type SESChannelConfig struct {
Channel util2.Channel `json:"channel" validate:"required"`
SESConfigDtos []*SESConfigDto `json:"configs"`
}
type SESConfigDto struct {
OwnerId int32 `json:"userId" validate:"number"`
TeamId int `json:"teamId" validate:"number"`
Region string `json:"region" validate:"required"`
AccessKey string `json:"accessKey" validate:"required"`
SecretKey string `json:"secretKey" validate:"required"`
FromEmail string `json:"fromEmail" validate:"email,required"`
ToEmail string `json:"toEmail"`
SessionToken string `json:"sessionToken"`
ConfigName string `json:"configName" validate:"required"`
Description string `json:"description"`
Id int `json:"id" validate:"number"`
Default bool `json:"default,notnull"`
}
type NotificationChannelAutoResponse struct {
ConfigName string `json:"configName"`
Id int `json:"id"`
TeamId int `json:"-"`
}
type NotificationRecipientListingResponse struct {
Dest util2.Channel `json:"dest"`
ConfigId int `json:"configId"`
Recipient string `json:"recipient"`
}
func NewSESNotificationServiceImpl(logger *zap.SugaredLogger, sesRepository repository.SESNotificationRepository,
teamService team.TeamService, notificationSettingsRepository repository.NotificationSettingsRepository) *SESNotificationServiceImpl {
return &SESNotificationServiceImpl{
logger: logger,
teamService: teamService,
sesRepository: sesRepository,
notificationSettingsRepository: notificationSettingsRepository,
}
}
func (impl *SESNotificationServiceImpl) SaveOrEditNotificationConfig(channelReq []*SESConfigDto, userId int32) ([]int, error) {
var responseIds []int
sesConfigs := buildSESNewConfigs(channelReq, userId)
for _, config := range sesConfigs {
if config.Id != 0 {
if config.Default {
_, err := impl.sesRepository.UpdateSESConfigDefault()
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("err while updating ses config", "err", err)
return []int{}, err
}
} else {
_, err := impl.sesRepository.FindDefault()
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("err while updating ses config", "err", err)
return []int{}, err
} else if util.IsErrNoRows(err) {
config.Default = true
}
}
model, err := impl.sesRepository.FindOne(config.Id)
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("err while fetching ses config", "err", err)
return []int{}, err
}
impl.buildConfigUpdateModel(config, model, userId)
model, uErr := impl.sesRepository.UpdateSESConfig(model)
if uErr != nil {
impl.logger.Errorw("err while updating ses config", "err", err)
return []int{}, uErr
}
} else {
if config.Default {
_, err := impl.sesRepository.UpdateSESConfigDefault()
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("err while updating ses config", "err", err)
return []int{}, err
}
} else {
_, err := impl.sesRepository.FindDefault()
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("err while updating ses config", "err", err)
return []int{}, err
} else if util.IsErrNoRows(err) {
config.Default = true
}
}
_, iErr := impl.sesRepository.SaveSESConfig(config)
if iErr != nil {
impl.logger.Errorw("err while inserting ses config", "err", iErr)
return []int{}, iErr
}
}
responseIds = append(responseIds, config.Id)
}
return responseIds, nil
}
func (impl *SESNotificationServiceImpl) FetchSESNotificationConfigById(id int) (*SESConfigDto, error) {
sesConfig, err := impl.sesRepository.FindOne(id)
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("cannot find all slack config", "err", err)
return nil, err
}
sesConfigDto := impl.adaptSESConfig(sesConfig)
return sesConfigDto, nil
}
func (impl *SESNotificationServiceImpl) FetchAllSESNotificationConfig() ([]*SESConfigDto, error) {
var responseDto []*SESConfigDto
sesConfigs, err := impl.sesRepository.FindAll()
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("cannot find all slack config", "err", err)
return []*SESConfigDto{}, err
}
for _, sesConfig := range sesConfigs {
sesConfigDto := impl.adaptSESConfig(sesConfig)
sesConfigDto.SecretKey = "**********"
responseDto = append(responseDto, sesConfigDto)
}
if responseDto == nil {
responseDto = make([]*SESConfigDto, 0)
}
return responseDto, nil
}
func (impl *SESNotificationServiceImpl) FetchAllSESNotificationConfigAutocomplete() ([]*NotificationChannelAutoResponse, error) {
var responseDto []*NotificationChannelAutoResponse
sesConfigs, err := impl.sesRepository.FindAll()
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("cannot find all slack config", "err", err)
return []*NotificationChannelAutoResponse{}, err
}
for _, sesConfig := range sesConfigs {
sesConfigDto := &NotificationChannelAutoResponse{
Id: sesConfig.Id,
ConfigName: sesConfig.ConfigName}
responseDto = append(responseDto, sesConfigDto)
}
return responseDto, nil
}
func (impl *SESNotificationServiceImpl) adaptSESConfig(sesConfig *repository.SESConfig) *SESConfigDto {
sesConfigDto := &SESConfigDto{
OwnerId: sesConfig.OwnerId,
Region: sesConfig.Region,
AccessKey: sesConfig.AccessKey,
SecretKey: sesConfig.SecretKey,
FromEmail: sesConfig.FromEmail,
SessionToken: sesConfig.SessionToken,
ConfigName: sesConfig.ConfigName,
Description: sesConfig.Description,
Id: sesConfig.Id,
Default: sesConfig.Default,
}
return sesConfigDto
}
func buildSESNewConfigs(sesReq []*SESConfigDto, userId int32) []*repository.SESConfig {
var sesConfigs []*repository.SESConfig
for _, c := range sesReq {
sesConfig := &repository.SESConfig{
Id: c.Id,
Region: c.Region,
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
ConfigName: c.ConfigName,
FromEmail: c.FromEmail,
SessionToken: c.SessionToken,
Description: c.Description,
Default: c.Default,
AuditLog: sql.AuditLog{
CreatedBy: userId,
CreatedOn: time.Now(),
UpdatedOn: time.Now(),
UpdatedBy: userId,
},
}
sesConfig.OwnerId = userId
sesConfigs = append(sesConfigs, sesConfig)
}
return sesConfigs
}
func (impl *SESNotificationServiceImpl) buildConfigUpdateModel(sesConfig *repository.SESConfig, model *repository.SESConfig, userId int32) {
model.Id = sesConfig.Id
model.OwnerId = sesConfig.OwnerId
model.Region = sesConfig.Region
model.AccessKey = sesConfig.AccessKey
model.SecretKey = sesConfig.SecretKey
model.FromEmail = sesConfig.FromEmail
model.SessionToken = sesConfig.SessionToken
model.ConfigName = sesConfig.ConfigName
model.Description = sesConfig.Description
model.Default = sesConfig.Default
model.UpdatedOn = time.Now()
model.UpdatedBy = userId
}
func (impl *SESNotificationServiceImpl) DeleteNotificationConfig(deleteReq *SESConfigDto, userId int32) error {
existingConfig, err := impl.sesRepository.FindOne(deleteReq.Id)
if err != nil {
impl.logger.Errorw("No matching entry found for delete", "err", err, "id", deleteReq.Id)
return err
}
notifications, err := impl.notificationSettingsRepository.FindNotificationSettingsByConfigIdAndConfigType(deleteReq.Id, SES_CONFIG_TYPE)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in deleting ses config", "config", deleteReq)
return err
}
if len(notifications) > 0 {
impl.logger.Errorw("found notifications using this config, cannot delete", "config", deleteReq)
return fmt.Errorf(" Please delete all notifications using this config before deleting")
}
existingConfig.UpdatedOn = time.Now()
existingConfig.UpdatedBy = userId
//deleting slack config
err = impl.sesRepository.MarkSESConfigDeleted(existingConfig)
if err != nil {
impl.logger.Errorw("error in deleting ses config", "err", err, "id", existingConfig.Id)
return err
}
return nil
}