From 84c8cfc178586a637ea212deeef1aa7ad383c35b Mon Sep 17 00:00:00 2001 From: "taekyu.kang" Date: Tue, 16 Apr 2024 14:50:55 +0900 Subject: [PATCH] trivial. support array filter on users --- api/swagger/docs.go | 6 +-- api/swagger/swagger.json | 6 +-- api/swagger/swagger.yaml | 6 +-- internal/repository/user.go | 15 +++++++- internal/usecase/system-notification.go | 51 ++++++++++++------------- 5 files changed, 48 insertions(+), 36 deletions(-) diff --git a/api/swagger/docs.go b/api/swagger/docs.go index 507e0c42..5cbd0a84 100644 --- a/api/swagger/docs.go +++ b/api/swagger/docs.go @@ -12737,11 +12737,11 @@ const docTemplate = `{ "type": "string", "example": "708d1e5b-4e6f-40e9-87a3-329e2fd051a5" }, - "templateLatestVerson": { + "templateLatestVersion": { "type": "string", "example": "v1.0.3" }, - "templateLatestVersonReleaseDate": { + "templateLatestVersionReleaseDate": { "type": "string", "format": "date-time" }, @@ -14394,7 +14394,7 @@ const docTemplate = `{ "type": "string", "example": "708d1e5b-4e6f-40e9-87a3-329e2fd051a5" }, - "templateLatestVerson": { + "templateLatestVersion": { "type": "string", "example": "v1.0.3" }, diff --git a/api/swagger/swagger.json b/api/swagger/swagger.json index b0f2f0ee..fc187166 100644 --- a/api/swagger/swagger.json +++ b/api/swagger/swagger.json @@ -12731,11 +12731,11 @@ "type": "string", "example": "708d1e5b-4e6f-40e9-87a3-329e2fd051a5" }, - "templateLatestVerson": { + "templateLatestVersion": { "type": "string", "example": "v1.0.3" }, - "templateLatestVersonReleaseDate": { + "templateLatestVersionReleaseDate": { "type": "string", "format": "date-time" }, @@ -14388,7 +14388,7 @@ "type": "string", "example": "708d1e5b-4e6f-40e9-87a3-329e2fd051a5" }, - "templateLatestVerson": { + "templateLatestVersion": { "type": "string", "example": "v1.0.3" }, diff --git a/api/swagger/swagger.yaml b/api/swagger/swagger.yaml index be01be37..4b0abe43 100644 --- a/api/swagger/swagger.yaml +++ b/api/swagger/swagger.yaml @@ -1912,10 +1912,10 @@ definitions: templateId: example: 708d1e5b-4e6f-40e9-87a3-329e2fd051a5 type: string - templateLatestVerson: + templateLatestVersion: example: v1.0.3 type: string - templateLatestVersonReleaseDate: + templateLatestVersionReleaseDate: format: date-time type: string templateMandatory: @@ -3016,7 +3016,7 @@ definitions: templateId: example: 708d1e5b-4e6f-40e9-87a3-329e2fd051a5 type: string - templateLatestVerson: + templateLatestVersion: example: v1.0.3 type: string templateName: diff --git a/internal/repository/user.go b/internal/repository/user.go index fdbae67c..839f9b37 100644 --- a/internal/repository/user.go +++ b/internal/repository/user.go @@ -10,6 +10,7 @@ import ( "github.com/openinfradev/tks-api/pkg/httpErrors" "github.com/openinfradev/tks-api/pkg/log" "gorm.io/gorm" + "gorm.io/gorm/clause" ) // Interface @@ -120,7 +121,19 @@ func (r *UserRepository) ListWithPagination(ctx context.Context, pg *pagination. pg = pagination.NewPagination(nil) } - _, res := pg.Fetch(r.db.WithContext(ctx).Preload("Organization").Preload("Roles").Model(&model.User{}).Where("users.organization_id = ?", organizationId), &users) + db := r.db.WithContext(ctx).Preload(clause.Associations).Model(&model.User{}). + Where("users.organization_id = ?", organizationId) + + // [TODO] more pretty! + for _, filter := range pg.Filters { + log.Info(ctx, "KTKFREE ", filter.Relation) + if filter.Relation == "Roles" { + db = db.Where("id IN (SELECT user_id FROM user_roles WHERE name IN ?)", filter.Values) + break + } + } + + _, res := pg.Fetch(db, &users) if res.Error != nil { log.Errorf(ctx, "error is :%s(%T)", res.Error.Error(), res.Error) return nil, res.Error diff --git a/internal/usecase/system-notification.go b/internal/usecase/system-notification.go index 8ca3130c..0d3f984e 100644 --- a/internal/usecase/system-notification.go +++ b/internal/usecase/system-notification.go @@ -124,38 +124,37 @@ func (u *SystemNotificationUsecase) Create(ctx context.Context, input domain.Cre continue } - if systemNotification.Annotations.SystemNotificationRuleId == "" { - log.Error(ctx, "Invalid systemNotificationRuleId ") - continue - } - - systemNotificationRuleId, err := uuid.Parse(systemNotification.Annotations.SystemNotificationRuleId) - if err != nil { - log.Error(ctx, "Failed to parse uuid ", err) - continue - } - rule, err := u.systemNotificationRuleRepo.Get(ctx, systemNotificationRuleId) - if err != nil { - log.Error(ctx, "Failed to get systemNotificationRule ", err) - continue - } - - if rule.SystemNotificationCondition.EnableEmail { - to := []string{} - for _, user := range rule.TargetUsers { - to = append(to, user.Email) - } - message, err := mail.MakeSystemNotificationMessage(ctx, organizationId, systemNotification.Annotations.Message, to) + // 사용자가 생성한 알림 + if systemNotification.Annotations.SystemNotificationRuleId != "" { + systemNotificationRuleId, err := uuid.Parse(systemNotification.Annotations.SystemNotificationRuleId) if err != nil { - log.Error(ctx, fmt.Sprintf("Failed to make email content. err : %s", err.Error())) + log.Error(ctx, "Failed to parse uuid ", err) continue } - mailer := mail.New(message) - if err := mailer.SendMail(ctx); err != nil { - log.Error(ctx, fmt.Sprintf("Failed to send email to %s. err : %s", to, err.Error())) + rule, err := u.systemNotificationRuleRepo.Get(ctx, systemNotificationRuleId) + if err != nil { + log.Error(ctx, "Failed to get systemNotificationRule ", err) continue } + + if rule.SystemNotificationCondition.EnableEmail { + to := []string{} + for _, user := range rule.TargetUsers { + to = append(to, user.Email) + } + message, err := mail.MakeSystemNotificationMessage(ctx, organizationId, systemNotification.Annotations.Message, to) + if err != nil { + log.Error(ctx, fmt.Sprintf("Failed to make email content. err : %s", err.Error())) + continue + } + mailer := mail.New(message) + if err := mailer.SendMail(ctx); err != nil { + log.Error(ctx, fmt.Sprintf("Failed to send email to %s. err : %s", to, err.Error())) + continue + } + } } + } return nil