-
Notifications
You must be signed in to change notification settings - Fork 460
/
SMTPNotificationService.go
271 lines (251 loc) · 9.3 KB
/
SMTPNotificationService.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
/*
* Copyright (c) 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 SMTP_CONFIG_TYPE = "smtp"
type SMTPNotificationService interface {
SaveOrEditNotificationConfig(channelReq []*SMTPConfigDto, userId int32) ([]int, error)
FetchSMTPNotificationConfigById(id int) (*SMTPConfigDto, error)
FetchAllSMTPNotificationConfig() ([]*SMTPConfigDto, error)
FetchAllSMTPNotificationConfigAutocomplete() ([]*NotificationChannelAutoResponse, error)
DeleteNotificationConfig(channelReq *SMTPConfigDto, userId int32) error
}
type SMTPNotificationServiceImpl struct {
logger *zap.SugaredLogger
teamService team.TeamService
smtpRepository repository.SMTPNotificationRepository
notificationSettingsRepository repository.NotificationSettingsRepository
}
func NewSMTPNotificationServiceImpl(logger *zap.SugaredLogger, smtpRepository repository.SMTPNotificationRepository,
teamService team.TeamService, notificationSettingsRepository repository.NotificationSettingsRepository) *SMTPNotificationServiceImpl {
return &SMTPNotificationServiceImpl{
logger: logger,
teamService: teamService,
smtpRepository: smtpRepository,
notificationSettingsRepository: notificationSettingsRepository,
}
}
type SMTPChannelConfig struct {
Channel util2.Channel `json:"channel" validate:"required"`
SMTPConfigDtos []*SMTPConfigDto `json:"configs"`
}
type SMTPConfigDto struct {
Id int `json:"id"`
Port string `json:"port"`
Host string `json:"host"`
AuthType string `json:"authType"`
AuthUser string `json:"authUser"`
AuthPassword string `json:"authPassword"`
FromEmail string `json:"fromEmail"`
ConfigName string `json:"configName"`
Description string `json:"description"`
OwnerId int32 `json:"ownerId"`
Default bool `json:"default"`
Deleted bool `json:"deleted"`
}
func (impl *SMTPNotificationServiceImpl) SaveOrEditNotificationConfig(channelReq []*SMTPConfigDto, userId int32) ([]int, error) {
var responseIds []int
smtpConfigs := buildSMTPNewConfigs(channelReq, userId)
for _, config := range smtpConfigs {
if config.Id != 0 {
if config.Default {
_, err := impl.smtpRepository.UpdateSMTPConfigDefault()
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("err while updating smtp config", "err", err)
return []int{}, err
}
} else {
_, err := impl.smtpRepository.FindDefault()
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("err while updating smtp config", "err", err)
return []int{}, err
} else if util.IsErrNoRows(err) {
config.Default = true
}
}
model, err := impl.smtpRepository.FindOne(config.Id)
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("err while fetching smtp config", "err", err)
return []int{}, err
}
impl.buildConfigUpdateModel(config, model, userId)
model, uErr := impl.smtpRepository.UpdateSMTPConfig(model)
if uErr != nil {
impl.logger.Errorw("err while updating smtp config", "err", err)
return []int{}, uErr
}
} else {
if config.Default {
_, err := impl.smtpRepository.UpdateSMTPConfigDefault()
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("err while updating smtp config", "err", err)
return []int{}, err
}
} else {
_, err := impl.smtpRepository.FindDefault()
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("err while updating smtp config", "err", err)
return []int{}, err
} else if util.IsErrNoRows(err) {
config.Default = true
}
}
_, iErr := impl.smtpRepository.SaveSMTPConfig(config)
if iErr != nil {
impl.logger.Errorw("err while inserting smtp config", "err", iErr)
return []int{}, iErr
}
}
responseIds = append(responseIds, config.Id)
}
return responseIds, nil
}
func (impl *SMTPNotificationServiceImpl) FetchSMTPNotificationConfigById(id int) (*SMTPConfigDto, error) {
smtpConfig, err := impl.smtpRepository.FindOne(id)
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("cannot find all smtp config", "err", err)
return nil, err
}
smtpConfigDto := impl.adaptSMTPConfig(smtpConfig)
return smtpConfigDto, nil
}
func (impl *SMTPNotificationServiceImpl) FetchAllSMTPNotificationConfig() ([]*SMTPConfigDto, error) {
var responseDto []*SMTPConfigDto
smtpConfigs, err := impl.smtpRepository.FindAll()
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("cannot find all smtp config", "err", err)
return []*SMTPConfigDto{}, err
}
for _, smtpConfig := range smtpConfigs {
smtpConfigDto := impl.adaptSMTPConfig(smtpConfig)
smtpConfigDto.AuthPassword = "**********"
responseDto = append(responseDto, smtpConfigDto)
}
if responseDto == nil {
responseDto = make([]*SMTPConfigDto, 0)
}
return responseDto, nil
}
func (impl *SMTPNotificationServiceImpl) FetchAllSMTPNotificationConfigAutocomplete() ([]*NotificationChannelAutoResponse, error) {
var responseDto []*NotificationChannelAutoResponse
smtpConfigs, err := impl.smtpRepository.FindAll()
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("cannot find all smtp config", "err", err)
return []*NotificationChannelAutoResponse{}, err
}
for _, smtpConfig := range smtpConfigs {
smtpConfigDto := &NotificationChannelAutoResponse{
Id: smtpConfig.Id,
ConfigName: smtpConfig.ConfigName}
responseDto = append(responseDto, smtpConfigDto)
}
return responseDto, nil
}
func (impl *SMTPNotificationServiceImpl) adaptSMTPConfig(smtpConfig *repository.SMTPConfig) *SMTPConfigDto {
smtpConfigDto := &SMTPConfigDto{
OwnerId: smtpConfig.OwnerId,
Port: smtpConfig.Port,
Host: smtpConfig.Host,
AuthType: smtpConfig.AuthType,
AuthUser: smtpConfig.AuthUser,
AuthPassword: smtpConfig.AuthPassword,
FromEmail: smtpConfig.FromEmail,
ConfigName: smtpConfig.ConfigName,
Description: smtpConfig.Description,
Id: smtpConfig.Id,
Default: smtpConfig.Default,
Deleted: false,
}
return smtpConfigDto
}
func buildSMTPNewConfigs(smtpReq []*SMTPConfigDto, userId int32) []*repository.SMTPConfig {
var smtpConfigs []*repository.SMTPConfig
for _, c := range smtpReq {
smtpConfig := &repository.SMTPConfig{
Id: c.Id,
Port: c.Port,
Host: c.Host,
AuthType: c.AuthType,
AuthUser: c.AuthUser,
AuthPassword: c.AuthPassword,
ConfigName: c.ConfigName,
FromEmail: c.FromEmail,
Deleted: false,
Description: c.Description,
Default: c.Default,
AuditLog: sql.AuditLog{
CreatedBy: userId,
CreatedOn: time.Now(),
UpdatedOn: time.Now(),
UpdatedBy: userId,
},
}
smtpConfig.OwnerId = userId
smtpConfigs = append(smtpConfigs, smtpConfig)
}
return smtpConfigs
}
func (impl *SMTPNotificationServiceImpl) buildConfigUpdateModel(smtpConfig *repository.SMTPConfig, model *repository.SMTPConfig, userId int32) {
model.Id = smtpConfig.Id
model.OwnerId = smtpConfig.OwnerId
model.Port = smtpConfig.Port
model.Host = smtpConfig.Host
model.AuthUser = smtpConfig.AuthUser
model.AuthType = smtpConfig.AuthType
model.AuthPassword = smtpConfig.AuthPassword
model.FromEmail = smtpConfig.FromEmail
model.ConfigName = smtpConfig.ConfigName
model.Description = smtpConfig.Description
model.Default = smtpConfig.Default
model.UpdatedOn = time.Now()
model.UpdatedBy = userId
model.Deleted = false
}
func (impl *SMTPNotificationServiceImpl) DeleteNotificationConfig(deleteReq *SMTPConfigDto, userId int32) error {
existingConfig, err := impl.smtpRepository.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, SMTP_CONFIG_TYPE)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in deleting smtp 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 smtp config
err = impl.smtpRepository.MarkSMTPConfigDeleted(existingConfig)
if err != nil {
impl.logger.Errorw("error in deleting smtp config", "err", err, "id", existingConfig.Id)
return err
}
return nil
}