/
organization.go
270 lines (228 loc) · 9.42 KB
/
organization.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
package repository
import (
"context"
"github.com/google/uuid"
"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/pagination"
"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/log"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// Interfaces
type IOrganizationRepository interface {
Create(ctx context.Context, dto *model.Organization) (model.Organization, error)
Fetch(ctx context.Context, pg *pagination.Pagination) (res *[]model.Organization, err error)
Get(ctx context.Context, organizationId string) (res model.Organization, err error)
Update(ctx context.Context, organizationId string, in model.Organization) (model.Organization, error)
UpdatePrimaryClusterId(ctx context.Context, organizationId string, primaryClusterId string) error
UpdateAdminId(ctx context.Context, organizationId string, adminId uuid.UUID) error
AddStackTemplates(ctx context.Context, organizationId string, stackTemplates []model.StackTemplate) (err error)
RemoveStackTemplates(ctx context.Context, organizationId string, stackTemplates []model.StackTemplate) (err error)
AddSystemNotificationTemplates(ctx context.Context, organizationId string, systemNotificationTemplates []model.SystemNotificationTemplate) (err error)
RemoveSystemNotificationTemplates(ctx context.Context, organizationId string, systemNotificationTemplates []model.SystemNotificationTemplate) (err error)
Delete(ctx context.Context, organizationId string) (err error)
InitWorkflow(ctx context.Context, organizationId string, workflowId string, status domain.OrganizationStatus) error
AddPermittedPolicyTemplatesByID(ctx context.Context, organizationId string, policyTemplates []model.PolicyTemplate) (err error)
UpdatePermittedPolicyTemplatesByID(ctx context.Context, organizationId string, policyTemplates []model.PolicyTemplate) (err error)
DeletePermittedPolicyTemplatesByID(ctx context.Context, organizationId string, policyTemplateids []uuid.UUID) (err error)
}
type OrganizationRepository struct {
db *gorm.DB
}
func NewOrganizationRepository(db *gorm.DB) IOrganizationRepository {
return &OrganizationRepository{
db: db,
}
}
//
//// Models
//type Organization struct {
// gorm.Model
//
// ID string `gorm:"primarykey;type:varchar(36);not null"`
// Name string
// Description string
// Phone string
// WorkflowId string
// Status model.OrganizationStatus
// StatusDesc string
// Creator uuid.UUID
// PrimaryClusterId string // allow null
//}
//func (c *Organization) BeforeCreate(tx *gorm.DB) (err error) {
// c.ID = helper.GenerateOrganizationId()
// return nil
//}
func (r *OrganizationRepository) Create(ctx context.Context, dto *model.Organization) (model.Organization, error) {
organization := model.Organization{
ID: dto.ID,
Name: dto.Name,
CreatorId: dto.CreatorId,
Description: dto.Description,
Status: domain.OrganizationStatus_PENDING,
Phone: dto.Phone,
}
res := r.db.WithContext(ctx).Create(&organization)
if res.Error != nil {
log.Errorf(ctx, "error is :%s(%T)", res.Error.Error(), res.Error)
return model.Organization{}, res.Error
}
return organization, nil
}
func (r *OrganizationRepository) Fetch(ctx context.Context, pg *pagination.Pagination) (out *[]model.Organization, err error) {
if pg == nil {
pg = pagination.NewPagination(nil)
}
db := r.db.WithContext(ctx).Preload(clause.Associations).Model(&model.Organization{})
// [TODO] more pretty!
for _, filter := range pg.Filters {
if filter.Relation == "Admin" {
db = db.Joins("join users on users.id::text = organizations.admin_id::text").
Where("users.name ilike ?", "%"+filter.Values[0]+"%")
break
}
}
_, res := pg.Fetch(db, &out)
if res.Error != nil {
return nil, res.Error
}
return
}
func (r *OrganizationRepository) Get(ctx context.Context, id string) (out model.Organization, err error) {
res := r.db.WithContext(ctx).Preload(clause.Associations).
First(&out, "id = ?", id)
if res.Error != nil {
log.Errorf(ctx, "error is :%s(%T)", res.Error.Error(), res.Error)
return model.Organization{}, res.Error
}
return
}
func (r *OrganizationRepository) Update(ctx context.Context, organizationId string, in model.Organization) (out model.Organization, err error) {
res := r.db.WithContext(ctx).Model(&model.Organization{}).
Where("id = ?", organizationId).
Updates(map[string]interface{}{
"name": in.Name,
"description": in.Description,
})
if res.Error != nil {
log.Errorf(ctx, "error is :%s(%T)", res.Error.Error(), res.Error)
return model.Organization{}, res.Error
}
res = r.db.Model(&model.Organization{}).Where("id = ?", organizationId).Find(&out)
if res.Error != nil {
log.Errorf(ctx, "error is :%s(%T)", res.Error.Error(), res.Error)
return model.Organization{}, res.Error
}
return
}
func (r *OrganizationRepository) UpdatePrimaryClusterId(ctx context.Context, organizationId string, primaryClusterId string) error {
res := r.db.WithContext(ctx).Model(&model.Organization{}).
Where("id = ?", organizationId).
Updates(map[string]interface{}{
"primary_cluster_id": primaryClusterId,
})
if res.Error != nil {
log.Errorf(ctx, "error is :%s(%T)", res.Error.Error(), res.Error)
return res.Error
}
return nil
}
func (r *OrganizationRepository) UpdateAdminId(ctx context.Context, organizationId string, adminId uuid.UUID) (err error) {
res := r.db.WithContext(ctx).Model(&model.Organization{}).
Where("id = ?", organizationId).
Updates(map[string]interface{}{
"admin_id": adminId,
})
if res.Error != nil {
log.Errorf(ctx, "error is :%s(%T)", res.Error.Error(), res.Error)
return res.Error
}
return nil
}
func (r *OrganizationRepository) Delete(ctx context.Context, organizationId string) error {
res := r.db.WithContext(ctx).Delete(&model.Organization{}, "id = ?", organizationId)
if res.Error != nil {
log.Errorf(ctx, "error is :%s(%T)", res.Error.Error(), res.Error)
return res.Error
}
return nil
}
func (r *OrganizationRepository) InitWorkflow(ctx context.Context, organizationId string, workflowId string, status domain.OrganizationStatus) error {
res := r.db.WithContext(ctx).Model(&model.Organization{}).
Where("ID = ?", organizationId).
Updates(map[string]interface{}{"Status": status, "WorkflowId": workflowId})
if res.Error != nil {
log.Errorf(ctx, "error is :%s(%T)", res.Error.Error(), res.Error)
return res.Error
}
return nil
}
func (r *OrganizationRepository) AddStackTemplates(ctx context.Context, organizationId string, stackTemplates []model.StackTemplate) (err error) {
var organization = model.Organization{}
res := r.db.WithContext(ctx).Preload("StackTemplates").First(&organization, "id = ?", organizationId)
if res.Error != nil {
return res.Error
}
err = r.db.WithContext(ctx).Model(&organization).Association("StackTemplates").Append(stackTemplates)
if err != nil {
return err
}
return nil
}
func (r *OrganizationRepository) RemoveStackTemplates(ctx context.Context, organizationId string, stackTemplates []model.StackTemplate) (err error) {
var organization = model.Organization{}
res := r.db.WithContext(ctx).Preload("StackTemplates").First(&organization, "id = ?", organizationId)
if res.Error != nil {
return res.Error
}
err = r.db.WithContext(ctx).Model(&organization).Association("StackTemplates").Delete(stackTemplates)
if err != nil {
return err
}
return nil
}
func (r *OrganizationRepository) AddSystemNotificationTemplates(ctx context.Context, organizationId string, templates []model.SystemNotificationTemplate) (err error) {
var organization = model.Organization{}
res := r.db.WithContext(ctx).Preload("SystemNotificationTemplates").First(&organization, "id = ?", organizationId)
if res.Error != nil {
return res.Error
}
err = r.db.WithContext(ctx).Model(&organization).Association("SystemNotificationTemplates").Append(templates)
if err != nil {
return err
}
return nil
}
func (r *OrganizationRepository) RemoveSystemNotificationTemplates(ctx context.Context, organizationId string, templates []model.SystemNotificationTemplate) (err error) {
var organization = model.Organization{}
res := r.db.WithContext(ctx).Preload("SystemNotificationTemplates").First(&organization, "id = ?", organizationId)
if res.Error != nil {
return res.Error
}
err = r.db.WithContext(ctx).Model(&organization).Association("SystemNotificationTemplates").Delete(templates)
if err != nil {
return err
}
return nil
}
func (r *OrganizationRepository) AddPermittedPolicyTemplatesByID(ctx context.Context, organizationId string, policyTemplates []model.PolicyTemplate) (err error) {
var organization model.Organization
organization.ID = organizationId
err = r.db.WithContext(ctx).Model(&organization).
Association("PolicyTemplates").Append(policyTemplates)
return err
}
func (r *OrganizationRepository) UpdatePermittedPolicyTemplatesByID(ctx context.Context, organizationId string, policyTemplates []model.PolicyTemplate) (err error) {
var organization model.Organization
organization.ID = organizationId
err = r.db.WithContext(ctx).Model(&organization).
Association("PolicyTemplates").Replace(policyTemplates)
return err
}
func (r *OrganizationRepository) DeletePermittedPolicyTemplatesByID(ctx context.Context, organizationId string, policyTemplateids []uuid.UUID) (err error) {
return r.db.WithContext(ctx).
Where("organization_id = ?", organizationId).
Where("policy_template_id in ?", policyTemplateids).
Delete(&model.PolicyTemplatePermittedOrganization{}).Error
}