Skip to content

Commit

Permalink
feat: send notification to the grants owner
Browse files Browse the repository at this point in the history
  • Loading branch information
rahmatrhd committed Aug 2, 2023
1 parent 1ac3355 commit 65fdb77
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
64 changes: 64 additions & 0 deletions core/grant/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package grant

import (
"context"
"encoding/json"
"fmt"
"sort"
"strings"
"time"

"github.com/go-playground/validator/v10"
Expand Down Expand Up @@ -540,6 +542,7 @@ func (s *Service) DormancyCheck(ctx context.Context, criteria domain.DormancyChe
s.logger.Info("checking grants dormancy...")
var dormantGrants []*domain.Grant
var dormantGrantsIDs []string
var dormantGrantsByOwner = map[string][]*domain.Grant{}
for _, g := range grantsPointer {
if len(g.Activities) == 0 {
g.ExpirationDateReason = fmt.Sprintf("%s: %s", domain.GrantExpirationReasonDormant, criteria.RetainDuration)
Expand All @@ -548,6 +551,8 @@ func (s *Service) DormancyCheck(ctx context.Context, criteria domain.DormancyChe

dormantGrants = append(dormantGrants, g)
dormantGrantsIDs = append(dormantGrantsIDs, g.ID)

dormantGrantsByOwner[g.Owner] = append(dormantGrantsByOwner[g.Owner], g)
}
}
s.logger.Info(fmt.Sprintf("found %d dormant grants for provider %q", len(dormantGrants), provider.URN), "grant_ids", dormantGrantsIDs)
Expand All @@ -560,6 +565,48 @@ func (s *Service) DormancyCheck(ctx context.Context, criteria domain.DormancyChe
if err := s.repo.BulkUpsert(ctx, dormantGrants); err != nil {
return fmt.Errorf("updating grants expiration date: %w", err)
}

go func() { // send notifications
var notifications []domain.Notification
prepare_notifications:
for owner, grants := range dormantGrantsByOwner {
var grantsMap []map[string]interface{}
var grantIDs []string

for _, g := range grants {
grantMap, err := structToMap(g)
if err != nil {
s.logger.Error("failed to convert grant to map", "error", err)
continue prepare_notifications
}
grantsMap = append(grantsMap, grantMap)
}

notifications = append(notifications, domain.Notification{
User: owner,
Labels: map[string]string{
"owner": owner,
"grant_ids": strings.Join(grantIDs, ", "),
},
Message: domain.NotificationMessage{
Type: domain.NotificationTypeUnusedGrant,
Variables: map[string]interface{}{
"dormant_grants": grantsMap,
"period": criteria.Period.String(),
"retain_duration": criteria.RetainDuration.String(),
"start_date_formatted": startDate.Format("Jan 02, 2006 15:04:05 UTC"),
},
},
})
}

if errs := s.notifier.Notify(notifications); errs != nil {
for _, err1 := range errs {
s.logger.Error("failed to send notifications", "error", err1.Error())
}
}
}()

return nil
}

Expand Down Expand Up @@ -624,3 +671,20 @@ func getGrantIDs(grants []domain.Grant) []string {
}
return ids
}

func structToMap(item interface{}) (map[string]interface{}, error) {
result := map[string]interface{}{}

if item != nil {
jsonString, err := json.Marshal(item)
if err != nil {
return nil, err
}

if err := json.Unmarshal(jsonString, &result); err != nil {
return nil, err
}
}

return result, nil
}
2 changes: 2 additions & 0 deletions domain/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type NotificationMessages struct {
ApproverNotification string `mapstructure:"approver_notification"`
OthersAppealApproved string `mapstructure:"others_appeal_approved"`
GrantOwnerChanged string `mapstructure:"grant_owner_changed"`
UnusedGrant string `mapstructure:"unused_grant"`
}

const (
Expand All @@ -18,6 +19,7 @@ const (
NotificationTypeAccessRevoked = "AccessRevoked"
NotificationTypeApproverNotification = "ApproverNotification"
NotificationTypeGrantOwnerChanged = "GrantOwnerChanged"
NotificationTypeUnusedGrant = "UnusedGrant"
)

type NotificationMessage struct {
Expand Down
3 changes: 3 additions & 0 deletions jobs/grant_dormancy_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func (h *handler) GrantDormancyCheck(ctx context.Context, c Config) error {
}

for _, p := range providers {
if p.URN != "pilotdata-integration-bigquery" {
continue
}
h.logger.Info(fmt.Sprintf("checking dormancy for grants under provider: %q", p.URN))
if err := h.grantService.DormancyCheck(ctx, domain.DormancyCheckCriteria{
ProviderID: p.ID,
Expand Down
9 changes: 9 additions & 0 deletions plugins/notifiers/slack/templates/UnusedGrant.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"type":"section",
"text":{
"type":"mrkdwn",
"text":"We have advanced the expiration date for the following grants due to inactivity since `{{.start_date}}`:{{ range .dormant_grants }}\n>*ID*: `{{.id}}`\n>*Account ID*: `{{.account_id}}`\n>*Resource*: `{{.resource.urn}}` ({{.resource.provider_type}} {{.resource.type}})\n>*Role*: `{{.role}}`\n>*Expiration Date* (new): `{{.expiration_date}}`\n{{end}}"
}
}
]

0 comments on commit 65fdb77

Please sign in to comment.