Skip to content

Commit

Permalink
Merge pull request #4286 from nickmango/feature/update-approval-api
Browse files Browse the repository at this point in the history
Feature/Approval Status
  • Loading branch information
nickmango authored Apr 4, 2024
2 parents 4e2b084 + 407f34e commit a3e5a91
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 8 deletions.
63 changes: 55 additions & 8 deletions cla-backend-go/signatures/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3254,11 +3254,12 @@ func (repo repository) UpdateApprovalList(ctx context.Context, claManager *model
log.WithFields(f).Debugf("updating approval list table")

if params.AddEmailApprovalList != nil {
repo.updateApprovalTable(ctx, params.AddEmailApprovalList, utils.EmailApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName)
repo.updateApprovalTable(ctx, params.AddEmailApprovalList, utils.EmailApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName, true)
}

// if email removal update signature approvals
if params.RemoveEmailApprovalList != nil {
repo.updateApprovalTable(ctx, params.AddEmailApprovalList, utils.EmailApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName, false)
log.WithFields(f).Debugf("removing email: %+v the approval list", params.RemoveDomainApprovalList)
var wg sync.WaitGroup
wg.Add(len(params.RemoveEmailApprovalList))
Expand Down Expand Up @@ -3378,7 +3379,7 @@ func (repo repository) UpdateApprovalList(ctx context.Context, claManager *model

log.WithFields(f).Debugf("updating approval list table")
if params.AddDomainApprovalList != nil {
repo.updateApprovalTable(ctx, params.AddDomainApprovalList, utils.EmailApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName)
repo.updateApprovalTable(ctx, params.AddDomainApprovalList, utils.EmailApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName, true)
}

if params.RemoveDomainApprovalList != nil {
Expand Down Expand Up @@ -3418,6 +3419,7 @@ func (repo repository) UpdateApprovalList(ctx context.Context, claManager *model
}

repo.invalidateSignatures(ctx, &approvalList, claManager, eventArgs)
repo.updateApprovalTable(ctx, params.AddDomainApprovalList, utils.EmailApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName, false)
}
}

Expand All @@ -3442,9 +3444,11 @@ func (repo repository) UpdateApprovalList(ctx context.Context, claManager *model
}

if params.AddGithubUsernameApprovalList != nil {
repo.updateApprovalTable(ctx, params.AddGithubUsernameApprovalList, utils.GithubUsernameApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName)
repo.updateApprovalTable(ctx, params.AddGithubUsernameApprovalList, utils.GithubUsernameApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName, true)
}
if params.RemoveGithubUsernameApprovalList != nil {

repo.updateApprovalTable(ctx, params.AddGithubUsernameApprovalList, utils.GithubUsernameApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName, false)
// if email removal update signature approvals
if params.RemoveGithubUsernameApprovalList != nil {
var wg sync.WaitGroup
Expand Down Expand Up @@ -3529,7 +3533,7 @@ func (repo repository) UpdateApprovalList(ctx context.Context, claManager *model
}

if params.AddGithubOrgApprovalList != nil {
repo.updateApprovalTable(ctx, params.AddGithubOrgApprovalList, utils.GithubOrgApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName)
repo.updateApprovalTable(ctx, params.AddGithubOrgApprovalList, utils.GithubOrgApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName, true)
}

if params.RemoveGithubOrgApprovalList != nil {
Expand Down Expand Up @@ -3576,6 +3580,7 @@ func (repo repository) UpdateApprovalList(ctx context.Context, claManager *model
approvalList.GitHubUsernames = utils.RemoveDuplicates(ghUsernames)

repo.invalidateSignatures(ctx, &approvalList, claManager, eventArgs)
repo.updateApprovalTable(ctx, params.AddGithubOrgApprovalList, utils.GithubOrgApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName, false)
}
}

Expand All @@ -3599,9 +3604,10 @@ func (repo repository) UpdateApprovalList(ctx context.Context, claManager *model
updateExpression = updateExpression + " #GLU = :glu, "
}
if params.AddGitlabUsernameApprovalList != nil {
repo.updateApprovalTable(ctx, params.AddGitlabUsernameApprovalList, utils.GitlabUsernameApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName)
repo.updateApprovalTable(ctx, params.AddGitlabUsernameApprovalList, utils.GitlabUsernameApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName, true)
}
if params.RemoveGitlabUsernameApprovalList != nil {
repo.updateApprovalTable(ctx, params.AddGitlabUsernameApprovalList, utils.GitlabUsernameApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName, false)
// if email removal update signature approvals
if params.RemoveGitlabUsernameApprovalList != nil {
approvalList.Criteria = utils.GitlabUsernameCriteria
Expand Down Expand Up @@ -3688,10 +3694,11 @@ func (repo repository) UpdateApprovalList(ctx context.Context, claManager *model
}

if params.AddGitlabOrgApprovalList != nil {
repo.updateApprovalTable(ctx, params.AddGitlabOrgApprovalList, utils.GitlabOrgApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName)
repo.updateApprovalTable(ctx, params.AddGitlabOrgApprovalList, utils.GitlabOrgApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName, true)
}

if params.RemoveGitlabOrgApprovalList != nil {
repo.updateApprovalTable(ctx, params.AddGitlabOrgApprovalList, utils.GitlabOrgApprovalCriteria, signatureID, projectID, companyID, cclaSignature.SignatureReferenceName, false)
approvalList.Criteria = utils.GitlabOrgCriteria
approvalList.ApprovalList = params.RemoveGitlabOrgApprovalList
approvalList.Action = utils.RemoveApprovals
Expand Down Expand Up @@ -3779,7 +3786,7 @@ func (repo repository) UpdateApprovalList(ctx context.Context, claManager *model
return updatedSig, nil
}

func (repo *repository) updateApprovalTable(ctx context.Context, approvalList []string, criteria, signatureID, projectID, companyID, companyName string) {
func (repo *repository) updateApprovalTable(ctx context.Context, approvalList []string, criteria, signatureID, projectID, companyID, companyName string, add bool) {
f := logrus.Fields{
"functionName": "v1.signatures.repository.addApprovalList",
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
Expand All @@ -3793,6 +3800,37 @@ func (repo *repository) updateApprovalTable(ctx context.Context, approvalList []
continue
}
_, currentTime := utils.CurrentTime()

// Check if it exists
approvalItems, apprErr := repo.approvalRepo.SearchApprovalList(criteria, item, projectID, companyID, signatureID)
if apprErr != nil {
log.WithFields(f).WithError(apprErr).Warnf("unable to search approval list for item: %s", item)
continue
}

if len(approvalItems) > 0 {
// Update the existing record
approvalItem := approvalItems[0]
if add {
log.WithFields(f).Debugf("approval request for item: %s with criteria: %s already exists", item, criteria)
approvalItem.DateModified = currentTime
approvalItem.Active = true
} else {
log.WithFields(f).Debugf("approval request for item: %s with criteria: %s already exists", item, criteria)
approvalItem.DateModified = currentTime
approvalItem.Active = false
}
err = repo.approvalRepo.UpdateApprovalItem(approvalItem)

if err != nil {
log.WithFields(f).WithError(err).Warnf("unable to update approval request for item: %s", item)
continue
}
log.WithFields(f).Debugf("updated approval request for item: %s with criteria: %s", item, criteria)
continue
}

// create a new record
approvalItem := approvals.ApprovalItem{
ApprovalID: approvalID.String(),
SignatureID: signatureID,
Expand All @@ -3804,7 +3842,16 @@ func (repo *repository) updateApprovalTable(ctx context.Context, approvalList []
DateModified: currentTime,
ApprovalCompanyName: companyName,
DateAdded: currentTime,
Note: "Auto-Added",
}

if add {
approvalItem.Active = true
approvalItem.DateAdded = currentTime
approvalItem.Note = "Auto-Added"
} else {
approvalItem.Active = false
approvalItem.DateRemoved = currentTime
approvalItem.Note = "Auto-Removed"
}

err = repo.approvalRepo.AddApprovalList(approvalItem)
Expand Down
28 changes: 28 additions & 0 deletions cla-backend-go/v2/approvals/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type IRepository interface {
DeleteAll() error
GetApprovalListBySignature(signatureID string) ([]ApprovalItem, error)
AddApprovalList(approvalItem ApprovalItem) error
UpdateApprovalItem(approvalItem ApprovalItem) error
DeleteApprovalList(approvalID string) error
SearchApprovalList(criteria, approvalListName, claGroupID, companyID, signatureID string) ([]ApprovalItem, error)
BatchAddApprovalList(approvalItems []ApprovalItem) error
Expand Down Expand Up @@ -135,6 +136,33 @@ func (repo *repository) DeleteAll() error {
return nil
}

func (repo *repository) UpdateApprovalItem(approvalItem ApprovalItem) error {
f := logrus.Fields{
"functionName": "v2.approvals.repository.UpdateApprovalItem",
"approvalID": approvalItem.ApprovalID,
}

log.WithFields(f).Debugf("updating approval item: %+v", approvalItem)

av, err := dynamodbattribute.MarshalMap(approvalItem)
if err != nil {
log.WithFields(f).Warnf("unable to marshal data, error: %+v", err)
return err
}

_, err = repo.dynamoDBClient.PutItem(&dynamodb.PutItemInput{
TableName: aws.String(repo.tableName),
Item: av,
})

if err != nil {
log.WithFields(f).Warnf("repository.UpdateApprovalItem - unable to update data in table, error: %+v", err)
return err
}

return nil
}

func (repo *repository) GetApprovalList(approvalID string) (*ApprovalItem, error) {
f := logrus.Fields{
"functionName": "GetApprovalList",
Expand Down

0 comments on commit a3e5a91

Please sign in to comment.