Skip to content

Commit

Permalink
Merge pull request #384 from openinfradev/fix_filter
Browse files Browse the repository at this point in the history
trivial. support array filter on users
  • Loading branch information
ktkfree authored Apr 16, 2024
2 parents 1afcaa8 + 84c8cfc commit cce3c64
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 36 deletions.
6 changes: 3 additions & 3 deletions api/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -14394,7 +14394,7 @@ const docTemplate = `{
"type": "string",
"example": "708d1e5b-4e6f-40e9-87a3-329e2fd051a5"
},
"templateLatestVerson": {
"templateLatestVersion": {
"type": "string",
"example": "v1.0.3"
},
Expand Down
6 changes: 3 additions & 3 deletions api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -14388,7 +14388,7 @@
"type": "string",
"example": "708d1e5b-4e6f-40e9-87a3-329e2fd051a5"
},
"templateLatestVerson": {
"templateLatestVersion": {
"type": "string",
"example": "v1.0.3"
},
Expand Down
6 changes: 3 additions & 3 deletions api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -3016,7 +3016,7 @@ definitions:
templateId:
example: 708d1e5b-4e6f-40e9-87a3-329e2fd051a5
type: string
templateLatestVerson:
templateLatestVersion:
example: v1.0.3
type: string
templateName:
Expand Down
15 changes: 14 additions & 1 deletion internal/repository/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
51 changes: 25 additions & 26 deletions internal/usecase/system-notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cce3c64

Please sign in to comment.