-
Notifications
You must be signed in to change notification settings - Fork 485
/
NotificationSettingsRepository.go
307 lines (278 loc) · 13.4 KB
/
NotificationSettingsRepository.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright (c) 2020 Devtron Labs
*
* 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 repository
import (
"github.com/devtron-labs/devtron/internal/sql/models"
"github.com/go-pg/pg"
)
type NotificationSettingsRepository interface {
FindNSViewCount() (int, error)
SaveNotificationSettingsConfig(notificationSettingsView *NotificationSettingsView, tx *pg.Tx) (*NotificationSettingsView, error)
FindNotificationSettingsViewById(id int) (*NotificationSettingsView, error)
FindNotificationSettingsViewByIds(id []*int) ([]*NotificationSettingsView, error)
UpdateNotificationSettingsView(notificationSettingsView *NotificationSettingsView, tx *pg.Tx) (*NotificationSettingsView, error)
SaveNotificationSetting(notificationSettings *NotificationSettings, tx *pg.Tx) (*NotificationSettings, error)
UpdateNotificationSettings(notificationSettings *NotificationSettings, tx *pg.Tx) (*NotificationSettings, error)
FindNotificationSettingsByViewId(viewId int) ([]NotificationSettings, error)
SaveAllNotificationSettings(notificationSettings []NotificationSettings, tx *pg.Tx) (int, error)
DeleteNotificationSettingsByConfigId(viewId int, tx *pg.Tx) (int, error)
FindAll(offset int, size int) ([]*NotificationSettingsView, error)
DeleteNotificationSettingsViewById(id int, tx *pg.Tx) (int, error)
FindNotificationSettingDeploymentOptions(settingRequest *SearchRequest) ([]*SettingOptionDTO, error)
FindNotificationSettingBuildOptions(settingRequest *SearchRequest) ([]*SettingOptionDTO, error)
FetchNotificationSettingGroupBy(viewId int) ([]NotificationSettings, error)
}
type NotificationSettingsRepositoryImpl struct {
dbConnection *pg.DB
}
func NewNotificationSettingsRepositoryImpl(dbConnection *pg.DB) *NotificationSettingsRepositoryImpl {
return &NotificationSettingsRepositoryImpl{dbConnection: dbConnection}
}
type NotificationSettingsView struct {
tableName struct{} `sql:"notification_settings_view" pg:",discard_unknown_columns"`
Id int `sql:"id,pk"`
Config string `sql:"config"`
//ConfigName string `sql:"config_name"`
//AppId *int `sql:"app_id"`
//EnvironmentId *int `sql:"env_id"`
models.AuditLog
}
type NotificationSettingsViewWithAppEnv struct {
Id int `json:"id"`
AppId *int `json:"app_id"`
EnvId *int `json:"env_id"`
ConfigName string `sql:"config_name"`
Config string `sql:"config"`
AppName string `json:"app_name"`
EnvironmentName string `json:"env_name"`
models.AuditLog
}
type NotificationSettings struct {
tableName struct{} `sql:"notification_settings"`
Id int `sql:"id,pk"`
TeamId *int `sql:"team_id"`
AppId *int `sql:"app_id"`
EnvId *int `sql:"env_id"`
PipelineId *int `sql:"pipeline_id"`
PipelineType string `sql:"pipeline_type"`
EventTypeId int `sql:"event_type_id"`
Config string `sql:"config"`
ViewId int `sql:"view_id"`
}
type SettingOptionDTO struct {
//TeamId int `json:"-"`
//AppId int `json:"-"`
//EnvId int `json:"-"`
PipelineId int `json:"pipelineId"`
PipelineName string `json:"pipelineName"`
PipelineType string `json:"pipelineType"`
AppName string `json:"appName"`
EnvironmentName string `json:"environmentName,omitempty"`
}
func (impl *NotificationSettingsRepositoryImpl) FindNSViewCount() (int, error) {
count, err := impl.dbConnection.Model(&NotificationSettingsView{}).Count()
if err != nil {
return 0, err
}
return count, nil
}
func (impl *NotificationSettingsRepositoryImpl) FindAll(offset int, size int) ([]*NotificationSettingsView, error) {
var ns []*NotificationSettingsView
err := impl.dbConnection.Model(&ns).Order("created_on desc").Offset(offset).Limit(size).Select()
if err != nil {
return nil, err
}
return ns, err
}
func (impl *NotificationSettingsRepositoryImpl) SaveNotificationSettingsConfig(notificationSettingsView *NotificationSettingsView, tx *pg.Tx) (*NotificationSettingsView, error) {
err := tx.Insert(notificationSettingsView)
if err != nil {
return nil, err
}
return notificationSettingsView, nil
}
func (impl *NotificationSettingsRepositoryImpl) FindNotificationSettingsViewById(id int) (*NotificationSettingsView, error) {
notificationSettingsView := &NotificationSettingsView{}
err := impl.dbConnection.Model(notificationSettingsView).Where("id = ?", id).Select()
if err != nil {
return nil, err
}
return notificationSettingsView, nil
}
func (impl *NotificationSettingsRepositoryImpl) FindNotificationSettingsViewByIds(ids []*int) ([]*NotificationSettingsView, error) {
var notificationSettingsView []*NotificationSettingsView
if ids == nil || len(ids) == 0 {
return notificationSettingsView, nil
}
err := impl.dbConnection.Model(¬ificationSettingsView).Where("id in (?)", pg.In(ids)).Select()
if err != nil {
return nil, err
}
return notificationSettingsView, nil
}
func (impl *NotificationSettingsRepositoryImpl) UpdateNotificationSettingsView(notificationSettingsView *NotificationSettingsView, tx *pg.Tx) (*NotificationSettingsView, error) {
err := tx.Update(notificationSettingsView)
if err != nil {
return nil, err
}
return notificationSettingsView, nil
}
func (impl *NotificationSettingsRepositoryImpl) SaveNotificationSetting(notificationSettings *NotificationSettings, tx *pg.Tx) (*NotificationSettings, error) {
err := tx.Insert(notificationSettings)
if err != nil {
return nil, err
}
return notificationSettings, nil
}
func (impl *NotificationSettingsRepositoryImpl) SaveAllNotificationSettings(notificationSettings []NotificationSettings, tx *pg.Tx) (int, error) {
res, err := tx.Model(¬ificationSettings).Insert()
if err != nil {
return 0, err
}
return res.RowsAffected(), nil
}
func (impl *NotificationSettingsRepositoryImpl) UpdateNotificationSettings(notificationSettings *NotificationSettings, tx *pg.Tx) (*NotificationSettings, error) {
err := tx.Update(notificationSettings)
if err != nil {
return nil, err
}
return notificationSettings, nil
}
func (impl *NotificationSettingsRepositoryImpl) FindNotificationSettingsByViewId(viewId int) ([]NotificationSettings, error) {
var notificationSettings []NotificationSettings
err := impl.dbConnection.Model(¬ificationSettings).Where("view_id = ?", viewId).Select()
if err != nil {
return nil, err
}
return notificationSettings, nil
}
func (impl *NotificationSettingsRepositoryImpl) DeleteNotificationSettingsByConfigId(viewId int, tx *pg.Tx) (int, error) {
var notificationSettings *NotificationSettings
res, err := tx.Model(notificationSettings).Where("view_id = ?", viewId).Delete()
if err != nil {
return 0, err
}
return res.RowsAffected(), nil
}
func (impl *NotificationSettingsRepositoryImpl) DeleteNotificationSettingsViewById(id int, tx *pg.Tx) (int, error) {
var notificationSettingsView *NotificationSettingsView
res, err := tx.Model(notificationSettingsView).Where("id = ?", id).Delete()
if err != nil {
return 0, err
}
return res.RowsAffected(), nil
}
func (impl *NotificationSettingsRepositoryImpl) FindNotificationSettingDeploymentOptions(settingRequest *SearchRequest) ([]*SettingOptionDTO, error) {
var settingOption []*SettingOptionDTO
query := "SELECT p.id as pipeline_id,p.pipeline_name, env.environment_name, a.app_name FROM pipeline p" +
" INNER JOIN app a on a.id=p.app_id" +
" INNER JOIN environment env on env.id = p.environment_id"
query = query + " WHERE p.deleted = false"
if len(settingRequest.TeamId) > 0 {
query = query + " AND a.team_id in (?)"
} else if len(settingRequest.EnvId) > 0 {
query = query + " AND p.environment_id in (?)"
} else if len(settingRequest.AppId) > 0 {
query = query + " AND p.app_id in (?)"
} else if len(settingRequest.PipelineName) > 0 {
query = query + " AND p.pipeline_name like (?)"
}
query = query + " GROUP BY 1,2,3,4;"
var err error
if len(settingRequest.TeamId) > 0 && len(settingRequest.EnvId) == 0 && len(settingRequest.AppId) == 0 {
_, err = impl.dbConnection.Query(&settingOption, query, pg.In(settingRequest.TeamId))
} else if len(settingRequest.TeamId) == 0 && len(settingRequest.EnvId) > 0 && len(settingRequest.AppId) == 0 {
_, err = impl.dbConnection.Query(&settingOption, query, pg.In(settingRequest.EnvId))
} else if len(settingRequest.TeamId) == 0 && len(settingRequest.EnvId) == 0 && len(settingRequest.AppId) > 0 {
_, err = impl.dbConnection.Query(&settingOption, query, pg.In(settingRequest.AppId))
} else if len(settingRequest.TeamId) > 0 && len(settingRequest.EnvId) > 0 && len(settingRequest.AppId) == 0 {
_, err = impl.dbConnection.Query(&settingOption, query, pg.In(settingRequest.TeamId), pg.In(settingRequest.EnvId))
} else if len(settingRequest.TeamId) > 0 && len(settingRequest.EnvId) == 0 && len(settingRequest.AppId) > 0 {
_, err = impl.dbConnection.Query(&settingOption, query, pg.In(settingRequest.TeamId), pg.In(settingRequest.AppId))
} else if len(settingRequest.TeamId) == 0 && len(settingRequest.EnvId) > 0 && len(settingRequest.AppId) > 0 {
_, err = impl.dbConnection.Query(&settingOption, query, pg.In(settingRequest.EnvId), pg.In(settingRequest.AppId))
} else if len(settingRequest.TeamId) > 0 && len(settingRequest.EnvId) > 0 && len(settingRequest.AppId) > 0 {
_, err = impl.dbConnection.Query(&settingOption, query, pg.In(settingRequest.TeamId), pg.In(settingRequest.EnvId), pg.In(settingRequest.AppId))
} else if len(settingRequest.PipelineName) > 0 {
_, err = impl.dbConnection.Query(&settingOption, query, settingRequest.PipelineName)
}
if err != nil {
return nil, err
}
return settingOption, err
}
func (impl *NotificationSettingsRepositoryImpl) FindNotificationSettingBuildOptions(settingRequest *SearchRequest) ([]*SettingOptionDTO, error) {
var settingOption []*SettingOptionDTO
query := "SELECT cip.id as pipeline_id,cip.name as pipeline_name, a.app_name from ci_pipeline cip" +
" INNER JOIN app a on a.id = cip.app_id" +
" INNER JOIN team t on t.id= a.team_id"
if len(settingRequest.EnvId) > 0 {
query = query + " INNER JOIN ci_artifact cia on cia.pipeline_id = cip.id"
query = query + " INNER JOIN cd_workflow wf on wf.ci_artifact_id = cia.id"
query = query + " INNER JOIN pipeline p on p.id = wf.pipeline_id"
}
query = query + " WHERE cip.deleted = false"
if len(settingRequest.TeamId) > 0 {
query = query + " AND a.team_id in (?)"
} else if len(settingRequest.EnvId) > 0 {
query = query + " AND p.environment_id in (?)"
} else if len(settingRequest.AppId) > 0 {
query = query + " AND cip.app_id in (?)"
} else if len(settingRequest.PipelineName) > 0 {
query = query + " AND cip.name like (?)"
}
query = query + " GROUP BY 1,2,3;"
var err error
if len(settingRequest.TeamId) > 0 && len(settingRequest.EnvId) == 0 && len(settingRequest.AppId) == 0 {
_, err = impl.dbConnection.Query(&settingOption, query, pg.In(settingRequest.TeamId))
} else if len(settingRequest.TeamId) == 0 && len(settingRequest.EnvId) > 0 && len(settingRequest.AppId) == 0 {
_, err = impl.dbConnection.Query(&settingOption, query, pg.In(settingRequest.EnvId))
} else if len(settingRequest.TeamId) == 0 && len(settingRequest.EnvId) == 0 && len(settingRequest.AppId) > 0 {
_, err = impl.dbConnection.Query(&settingOption, query, pg.In(settingRequest.AppId))
} else if len(settingRequest.TeamId) > 0 && len(settingRequest.EnvId) > 0 && len(settingRequest.AppId) == 0 {
_, err = impl.dbConnection.Query(&settingOption, query, pg.In(settingRequest.TeamId), pg.In(settingRequest.EnvId))
} else if len(settingRequest.TeamId) > 0 && len(settingRequest.EnvId) == 0 && len(settingRequest.AppId) > 0 {
_, err = impl.dbConnection.Query(&settingOption, query, pg.In(settingRequest.TeamId), pg.In(settingRequest.AppId))
} else if len(settingRequest.TeamId) == 0 && len(settingRequest.EnvId) > 0 && len(settingRequest.AppId) > 0 {
_, err = impl.dbConnection.Query(&settingOption, query, pg.In(settingRequest.EnvId), pg.In(settingRequest.AppId))
} else if len(settingRequest.TeamId) > 0 && len(settingRequest.EnvId) > 0 && len(settingRequest.AppId) > 0 {
_, err = impl.dbConnection.Query(&settingOption, query, pg.In(settingRequest.TeamId), pg.In(settingRequest.EnvId), pg.In(settingRequest.AppId))
} else if len(settingRequest.PipelineName) > 0 {
_, err = impl.dbConnection.Query(&settingOption, query, settingRequest.PipelineName)
}
if err != nil {
return nil, err
}
return settingOption, err
}
type SearchRequest struct {
TeamId []*int `json:"teamId" validate:"number"`
EnvId []*int `json:"envId" validate:"number"`
AppId []*int `json:"appId" validate:"number"`
PipelineName string `json:"pipelineName"`
UserId int32 `json:"-"`
}
func (impl *NotificationSettingsRepositoryImpl) FetchNotificationSettingGroupBy(viewId int) ([]NotificationSettings, error) {
var ns []NotificationSettings
queryTemp := "select ns.team_id,ns.env_id,ns.app_id,ns.pipeline_id,ns.pipeline_type from notification_settings ns" +
" where ns.view_id=? group by 1,2,3,4,5;"
_, err := impl.dbConnection.Query(&ns, queryTemp, viewId)
if err != nil {
return nil, err
}
return ns, err
}