Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 5 additions & 35 deletions pkg/notifier/NotificationConfigService.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,23 +294,14 @@ func (impl *NotificationConfigServiceImpl) BuildNotificationSettingsResponse(not
if config.Providers != nil && len(config.Providers) > 0 {
var slackIds []*int
var webhookIds []*int
var sesUserIds []int32
var smtpUserIds []int32
var providerConfigs []*beans.ProvidersConfig
for _, item := range config.Providers {
// if item.ConfigId > 0 that means, user is of user repository, else user email is custom
if item.ConfigId > 0 {
if item.Destination == util.Slack {
slackIds = append(slackIds, &item.ConfigId)
} else if item.Destination == util.SES {
sesUserIds = append(sesUserIds, int32(item.ConfigId))
} else if item.Destination == util.SMTP {
smtpUserIds = append(smtpUserIds, int32(item.ConfigId))
} else if item.Destination == util.Webhook {
webhookIds = append(webhookIds, &item.ConfigId)
}
if item.Destination == util.Slack {
slackIds = append(slackIds, &item.ConfigId)
} else if item.Destination == util.Webhook {
webhookIds = append(webhookIds, &item.ConfigId)
} else {
providerConfigs = append(providerConfigs, &beans.ProvidersConfig{Dest: string(item.Destination), Recipient: item.Recipient})
providerConfigs = append(providerConfigs, &beans.ProvidersConfig{Dest: string(item.Destination), Recipient: item.Recipient, Id: item.ConfigId})
}
}
if len(slackIds) > 0 {
Expand All @@ -333,27 +324,6 @@ func (impl *NotificationConfigServiceImpl) BuildNotificationSettingsResponse(not
providerConfigs = append(providerConfigs, &beans.ProvidersConfig{Id: item.Id, ConfigName: item.ConfigName, Dest: string(util.Webhook)})
}
}

if len(sesUserIds) > 0 {
sesConfigs, err := impl.userRepository.GetByIds(sesUserIds)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in fetching user", "sesUserIds", sesUserIds, "error", err)
return notificationSettingsResponses, deletedItemCount, err
}
for _, item := range sesConfigs {
providerConfigs = append(providerConfigs, &beans.ProvidersConfig{Id: int(item.Id), ConfigName: item.EmailId, Dest: string(util.SES)})
}
}
if len(smtpUserIds) > 0 {
smtpConfigs, err := impl.userRepository.GetByIds(smtpUserIds)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in fetching user", "smtpUserIds", smtpUserIds, "error", err)
return notificationSettingsResponses, deletedItemCount, err
}
for _, item := range smtpConfigs {
providerConfigs = append(providerConfigs, &beans.ProvidersConfig{Id: int(item.Id), ConfigName: item.EmailId, Dest: string(util.SMTP)})
}
}
notificationSettingsResponse.ProvidersConfig = providerConfigs
}

Expand Down
19 changes: 14 additions & 5 deletions pkg/notifier/SESNotificationService.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,23 @@ func (impl *SESNotificationServiceImpl) SaveOrEditNotificationConfig(channelReq
for _, config := range sesConfigs {
if config.Id != 0 {

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
}

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 {
// check if this config is already default, we don't want to reverse it
if model.Default {
return []int{}, fmt.Errorf("cannot update default config to non default")
}
_, err := impl.sesRepository.FindDefault()
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("err while updating ses config", "err", err)
Expand All @@ -76,11 +86,6 @@ func (impl *SESNotificationServiceImpl) SaveOrEditNotificationConfig(channelReq
}
}

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
}
adapter.BuildConfigUpdateModelForSES(config, model, userId)
model, uErr := impl.sesRepository.UpdateSESConfig(model)
if uErr != nil {
Expand Down Expand Up @@ -175,6 +180,10 @@ func (impl *SESNotificationServiceImpl) DeleteNotificationConfig(deleteReq *bean
impl.logger.Errorw("found notifications using this config, cannot delete", "config", deleteReq)
return fmt.Errorf(" Please delete all notifications using this config before deleting")
}
// check if default then dont delete
if existingConfig.Default {
return fmt.Errorf("default configuration cannot be deleted")
}
existingConfig.UpdatedOn = time.Now()
existingConfig.UpdatedBy = userId
//deleting slack config
Expand Down
19 changes: 14 additions & 5 deletions pkg/notifier/SMTPNotificationService.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,23 @@ func (impl *SMTPNotificationServiceImpl) SaveOrEditNotificationConfig(channelReq
for _, config := range smtpConfigs {
if config.Id != 0 {

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
}

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 {
// check if this config is already default, we don't want to reverse it
if model.Default {
return []int{}, fmt.Errorf("cannot update default config to non default")
}
_, err := impl.smtpRepository.FindDefault()
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("err while updating smtp config", "err", err)
Expand All @@ -76,11 +86,6 @@ func (impl *SMTPNotificationServiceImpl) SaveOrEditNotificationConfig(channelReq
}
}

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
}
adapter.BuildConfigUpdateModelForSMTP(config, model, userId)
model, uErr := impl.smtpRepository.UpdateSMTPConfig(model)
if uErr != nil {
Expand Down Expand Up @@ -175,6 +180,10 @@ func (impl *SMTPNotificationServiceImpl) DeleteNotificationConfig(deleteReq *bea
impl.logger.Errorw("found notifications using this config, cannot delete", "config", deleteReq)
return fmt.Errorf(" Please delete all notifications using this config before deleting")
}
// check if default then dont delete
if existingConfig.Default {
return fmt.Errorf("default configuration cannot be deleted")
}
existingConfig.UpdatedOn = time.Now()
existingConfig.UpdatedBy = userId
//deleting smtp config
Expand Down
1 change: 0 additions & 1 deletion pkg/notifier/beans/beans.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ type NSDeleteRequest struct {

type NotificationRequest struct {
UpdateType util.UpdateType `json:"updateType,omitempty"`
SesConfigId int `json:"sesConfigId,omitempty"`
Providers []*bean.Provider `json:"providers"`
NotificationConfigRequest []*NotificationConfigRequest `json:"notificationConfigRequest" validate:"required"`
}
Expand Down
Loading