/
system-notification-template.go
232 lines (196 loc) · 8.5 KB
/
system-notification-template.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
package usecase
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/openinfradev/tks-api/internal/middleware/auth/request"
"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/pagination"
"github.com/openinfradev/tks-api/internal/repository"
"github.com/openinfradev/tks-api/pkg/httpErrors"
"github.com/openinfradev/tks-api/pkg/log"
"github.com/pkg/errors"
"gorm.io/gorm"
)
type ISystemNotificationTemplateUsecase interface {
Get(ctx context.Context, alertId uuid.UUID) (model.SystemNotificationTemplate, error)
GetByName(ctx context.Context, name string) (model.SystemNotificationTemplate, error)
Fetch(ctx context.Context, pg *pagination.Pagination) ([]model.SystemNotificationTemplate, error)
FetchWithOrganization(ctx context.Context, organizationId string, pg *pagination.Pagination) ([]model.SystemNotificationTemplate, error)
Create(ctx context.Context, dto model.SystemNotificationTemplate) (systemNotificationTemplate uuid.UUID, err error)
Update(ctx context.Context, dto model.SystemNotificationTemplate) error
Delete(ctx context.Context, systemNotificationTemplateId uuid.UUID) error
AddOrganizationSystemNotificationTemplates(ctx context.Context, organizationId string, systemNotificationTemplateIds []string) error
RemoveOrganizationSystemNotificationTemplates(ctx context.Context, organizationId string, systemNotificationTemplateIds []string) error
}
type SystemNotificationTemplateUsecase struct {
repo repository.ISystemNotificationTemplateRepository
clusterRepo repository.IClusterRepository
organizationRepo repository.IOrganizationRepository
appGroupRepo repository.IAppGroupRepository
systemNotificationRuleRepo repository.ISystemNotificationRuleRepository
}
func NewSystemNotificationTemplateUsecase(r repository.Repository) ISystemNotificationTemplateUsecase {
return &SystemNotificationTemplateUsecase{
repo: r.SystemNotificationTemplate,
clusterRepo: r.Cluster,
appGroupRepo: r.AppGroup,
organizationRepo: r.Organization,
systemNotificationRuleRepo: r.SystemNotificationRule,
}
}
func (u *SystemNotificationTemplateUsecase) Create(ctx context.Context, dto model.SystemNotificationTemplate) (systemNotificationTemplate uuid.UUID, err error) {
user, ok := request.UserFrom(ctx)
if !ok {
return uuid.Nil, httpErrors.NewBadRequestError(fmt.Errorf("Invalid token"), "", "")
}
userId := user.GetUserId()
dto.CreatorId = &userId
dto.UpdatorId = &userId
if _, err = u.GetByName(ctx, dto.Name); err == nil {
return uuid.Nil, httpErrors.NewBadRequestError(fmt.Errorf("duplicate systemNotificationTemplate name"), "SNT_CREATE_ALREADY_EXISTED_NAME", "")
}
systemNotificationTemplate, err = u.repo.Create(ctx, dto)
if err != nil {
return uuid.Nil, httpErrors.NewInternalServerError(err, "", "")
}
log.Info(ctx, "newly created SystemNotificationTemplate ID:", systemNotificationTemplate)
dto.ID = systemNotificationTemplate
err = u.UpdateOrganizations(ctx, dto)
if err != nil {
return uuid.Nil, err
}
return systemNotificationTemplate, nil
}
func (u *SystemNotificationTemplateUsecase) Update(ctx context.Context, dto model.SystemNotificationTemplate) error {
_, err := u.repo.Get(ctx, dto.ID)
if err != nil {
return httpErrors.NewBadRequestError(err, "SNT_NOT_EXISTED_ALERT_TEMPLATE", "")
}
err = u.repo.Update(ctx, dto)
if err != nil {
return err
}
err = u.UpdateOrganizations(ctx, dto)
if err != nil {
return err
}
return nil
}
func (u *SystemNotificationTemplateUsecase) Get(ctx context.Context, systemNotificationTemplateId uuid.UUID) (systemNotificationTemplate model.SystemNotificationTemplate, err error) {
systemNotificationTemplate, err = u.repo.Get(ctx, systemNotificationTemplateId)
if err != nil {
return systemNotificationTemplate, err
}
return
}
func (u *SystemNotificationTemplateUsecase) GetByName(ctx context.Context, name string) (out model.SystemNotificationTemplate, err error) {
out, err = u.repo.GetByName(ctx, name)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return out, httpErrors.NewNotFoundError(err, "SNT_FAILED_FETCH_ALERT_TEMPLATE", "")
}
return out, err
}
return
}
func (u *SystemNotificationTemplateUsecase) Fetch(ctx context.Context, pg *pagination.Pagination) (alerts []model.SystemNotificationTemplate, err error) {
alerts, err = u.repo.Fetch(ctx, pg)
if err != nil {
return nil, err
}
return
}
func (u *SystemNotificationTemplateUsecase) FetchWithOrganization(ctx context.Context, organizationId string, pg *pagination.Pagination) (res []model.SystemNotificationTemplate, err error) {
res, err = u.repo.FetchWithOrganization(ctx, organizationId, pg)
if err != nil {
return nil, err
}
return res, nil
}
func (u *SystemNotificationTemplateUsecase) Delete(ctx context.Context, systemNotificationTemplateId uuid.UUID) (err error) {
systemNotificationTemplate, err := u.repo.Get(ctx, systemNotificationTemplateId)
if err != nil {
return err
}
user, ok := request.UserFrom(ctx)
if !ok {
return httpErrors.NewBadRequestError(fmt.Errorf("Invalid token"), "", "")
}
userId := user.GetUserId()
systemNotificationTemplate.UpdatorId = &userId
if systemNotificationTemplate.IsSystem {
return httpErrors.NewBadRequestError(fmt.Errorf("cannot delete systemNotificationTemplate"), "SNT_CANNOT_DELETE_SYSTEM_TEMPLATE", "")
}
// check if used
// system_notification_rules
pg := pagination.NewPaginationWithFilter("system_notification_template_id", "", "$eq", []string{systemNotificationTemplateId.String()})
res, err := u.systemNotificationRuleRepo.Fetch(ctx, pg)
if err != nil {
return err
}
if len(res) > 0 {
return httpErrors.NewBadRequestError(fmt.Errorf("Failed to delete systemNotificationTemplate %s", systemNotificationTemplateId.String()), "SNT_FAILED_DELETE_EXIST_RULES", "")
}
err = u.repo.Delete(ctx, systemNotificationTemplate)
if err != nil {
return err
}
return
}
func (u *SystemNotificationTemplateUsecase) UpdateOrganizations(ctx context.Context, dto model.SystemNotificationTemplate) error {
_, err := u.repo.Get(ctx, dto.ID)
if err != nil {
return httpErrors.NewBadRequestError(err, "SNT_NOT_EXISTED_ALERT_TEMPLATE", "")
}
organizations := make([]model.Organization, 0)
for _, organizationId := range dto.OrganizationIds {
organization, err := u.organizationRepo.Get(ctx, organizationId)
if err == nil {
organizations = append(organizations, organization)
}
}
err = u.repo.UpdateOrganizations(ctx, dto.ID, organizations)
if err != nil {
return httpErrors.NewBadRequestError(err, "SNT_FAILED_UPDATE_ORGANIZATION", "")
}
return nil
}
func (u *SystemNotificationTemplateUsecase) AddOrganizationSystemNotificationTemplates(ctx context.Context, organizationId string, systemNotificationTemplateIds []string) error {
_, err := u.organizationRepo.Get(ctx, organizationId)
if err != nil {
return httpErrors.NewBadRequestError(err, "ST_NOT_EXISTED_NAME", "")
}
systemNotificationTemplates := make([]model.SystemNotificationTemplate, 0)
for _, strId := range systemNotificationTemplateIds {
systemNotificationTemplateId, _ := uuid.Parse(strId)
systemNotificationTemplate, err := u.repo.Get(ctx, systemNotificationTemplateId)
if err == nil {
systemNotificationTemplates = append(systemNotificationTemplates, systemNotificationTemplate)
}
}
err = u.organizationRepo.AddSystemNotificationTemplates(ctx, organizationId, systemNotificationTemplates)
if err != nil {
return httpErrors.NewBadRequestError(err, "ST_FAILED_ADD_ORGANIZATION_SYSTEM_NOTIFICATION_TEMPLATE", "")
}
return nil
}
func (u *SystemNotificationTemplateUsecase) RemoveOrganizationSystemNotificationTemplates(ctx context.Context, organizationId string, systemNotificationTemplateIds []string) error {
_, err := u.organizationRepo.Get(ctx, organizationId)
if err != nil {
return httpErrors.NewBadRequestError(err, "O_NOT_EXISTED_NAME", "")
}
systemNotificationTemplates := make([]model.SystemNotificationTemplate, 0)
for _, strId := range systemNotificationTemplateIds {
systemNotificationTemplateId, _ := uuid.Parse(strId)
systemNotificationTemplate, err := u.repo.Get(ctx, systemNotificationTemplateId)
if err == nil {
systemNotificationTemplates = append(systemNotificationTemplates, systemNotificationTemplate)
}
}
err = u.organizationRepo.RemoveSystemNotificationTemplates(ctx, organizationId, systemNotificationTemplates)
if err != nil {
return httpErrors.NewBadRequestError(err, "ST_FAILED_REMOVE_ORGANIZATION_SYSTEM_NOTIFICATION_TEMPLATE", "")
}
return nil
}