forked from raystack/guardian
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(job): pending approvals reminder job (#160)
* 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
Showing
27 changed files
with
847 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.