Skip to content

Commit

Permalink
feat(job): pending approvals reminder job (#160)
Browse files Browse the repository at this point in the history
* feat(job): pending approvals reminder job

* test: adding unit test

* chore: restructure

* chore: move repository

* test: refactor unit test postgres

* chore: update config

* chore: update list of approvers

* chore: update notif template

* refactor: domain and filter

* refactor: postgres test

* fix: query logic

* refactor: updated logic on core and added unit test

* fix: update init

* fix: missing approver email

---------

Co-authored-by: Muhammad Idil Haq Amir <[email protected]>
  • Loading branch information
idilhaq and Muhammad Idil Haq Amir authored Jun 20, 2024
1 parent e391745 commit 8c3e4f1
Show file tree
Hide file tree
Showing 27 changed files with 847 additions and 55 deletions.
7 changes: 7 additions & 0 deletions cli/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func runJobCmd() *cobra.Command {
$ guardian job run revoke_expired_grants
$ guardian job run revoke_grants_by_user_criteria
$ guardian job run grant_dormancy_check
$ guardian job run pending_approvals_reminder
`),
Args: cobra.ExactValidArgs(1),
ValidArgs: []string{
Expand All @@ -53,6 +54,7 @@ func runJobCmd() *cobra.Command {
string(jobs.TypeRevokeExpiredGrants),
string(jobs.TypeRevokeGrantsByUserCriteria),
string(jobs.TypeGrantDormancyCheck),
string(jobs.TypePendingApprovalsReminder),
},
RunE: func(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
Expand Down Expand Up @@ -86,6 +88,7 @@ func runJobCmd() *cobra.Command {
handler := jobs.NewHandler(
logger,
services.GrantService,
services.ReportService,
services.ProviderService,
notifier,
crypto,
Expand Down Expand Up @@ -116,6 +119,10 @@ func runJobCmd() *cobra.Command {
handler: handler.GrantDormancyCheck,
config: config.Jobs.GrantDormancyCheck.Config,
},
jobs.TypePendingApprovalsReminder: {
handler: handler.PendingApprovalsReminder,
config: config.Jobs.PendingApprovalsReminder.Config,
},
}

jobName := jobs.Type(args[0])
Expand Down
27 changes: 27 additions & 0 deletions core/report/domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package report

import (
"time"
)

type PendingApprovalModel struct {
AppealID string `json:"id" yaml:"id"`
Approver string `json:"approver" yaml:"approver"`
AppealCreatedAt time.Time `json:"created_at" yaml:"created_at"`
}

type PendingApproval struct {
Approver string
Count int
Appeals []PendingAppeal // ideally []*domain.Appeal, but only ID is needed for now
}

type PendingAppeal struct {
ID string
}

type PendingApprovalsReportFilter struct {
AppealStatuses []string `mapstructure:"appeal_statuses" validate:"omitempty,min=1"`
ApprovalStatuses []string `mapstructure:"approval_statuses" validate:"omitempty,min=1"`
ApprovalStale *bool `mapstructure:"approval_stale"`
}
82 changes: 82 additions & 0 deletions core/report/mocks/notifier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions core/report/mocks/repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions core/report/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package report

import (
"context"

"gorm.io/gorm"
)

type Repository struct {
db *gorm.DB
}

func NewRepository(db *gorm.DB) *Repository {
return &Repository{db}
}

func (r *Repository) GetPendingApprovalsList(ctx context.Context, filters *PendingApprovalsReportFilter) ([]*PendingApprovalModel, error) {
records := []*PendingApprovalModel{}

db := r.db.WithContext(ctx)
var err error
db, err = applyAppealFilter(db, filters)
if err != nil {
return nil, err
}

rows, err := db.Rows()
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
db.ScanRows(rows, &records)
}

return records, nil
}

func applyAppealFilter(db *gorm.DB, filters *PendingApprovalsReportFilter) (*gorm.DB, error) {
db = db.Table("approvers").
Select("appeals.id as appeal_id, approvers.email as approver, appeals.created_at as appeal_created_at").
Joins("join approvals on approvals.id = approvers.approval_id").
Joins("join appeals on appeals.id = approvals.appeal_id").
Where("approvers.deleted_at IS NULL").
Where("approvals.deleted_at IS NULL").
Where("appeals.deleted_at IS NULL")

if filters.AppealStatuses != nil {
db = db.Where(`"appeals"."status" IN ?`, filters.AppealStatuses)
}

if filters.ApprovalStatuses != nil {
db = db.Where(`"approvals"."status" IN ?`, filters.ApprovalStatuses)
}

if filters.ApprovalStale != nil {
db = db.Where(`"approvals"."is_stale" = ?`, *filters.ApprovalStale)
}

return db, nil
}
Loading

0 comments on commit 8c3e4f1

Please sign in to comment.