Skip to content

Commit

Permalink
Merge pull request #4467 from nickmango/bug/corporate-contributors-pa…
Browse files Browse the repository at this point in the history
…gination

[#4460] Bug/Corporate Contributors API pagination
  • Loading branch information
nickmango authored Oct 29, 2024
2 parents ac1a811 + 6979ba4 commit eff666e
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions cla-backend-go/signatures/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4627,6 +4627,19 @@ func (repo repository) GetClaGroupCorporateContributors(ctx context.Context, cla
totalCountChannel := make(chan int64, 1)
go repo.getTotalCorporateContributorCount(ctx, claGroupID, companyID, searchTerm, totalCountChannel)

totalCount := <-totalCountChannel
log.WithFields(f).Debugf("total corporate contributor count: %d", totalCount)
// If the page size is nil, set it to the default
if pageSize == nil {
pageSize = aws.Int64(10)
}

if *pageSize > totalCount {
pageSize = aws.Int64(totalCount)
}

log.WithFields(f).Debugf("total corporate contributor count: %d, page size: %d", totalCount, *pageSize)

condition := expression.Key("signature_project_id").Equal(expression.Value(claGroupID))
// if companyID != nil {
// sortKey := fmt.Sprintf("%s#%v#%v#%v", utils.ClaTypeECLA, true, true, *companyID)
Expand Down Expand Up @@ -4659,11 +4672,6 @@ func (repo repository) GetClaGroupCorporateContributors(ctx context.Context, cla
return nil, err
}

// If the page size is nil, set it to the default
if pageSize == nil {
pageSize = aws.Int64(10)
}

// Assemble the query input parameters
queryInput := &dynamodb.QueryInput{
ExpressionAttributeNames: expr.Names(),
Expand Down Expand Up @@ -4691,7 +4699,9 @@ func (repo repository) GetClaGroupCorporateContributors(ctx context.Context, cla
out := &models.CorporateContributorList{List: make([]*models.CorporateContributor, 0)}
var lastEvaluatedKey string

for ok := true; ok; ok = lastEvaluatedKey != "" {
currentCount := int64(0)

for ok := true; ok; ok = lastEvaluatedKey != "" && currentCount < *pageSize {
// Make the DynamoDB Query API call
log.WithFields(f).Debug("querying signatures...")
results, queryErr := repo.dynamoDBClient.Query(queryInput)
Expand Down Expand Up @@ -4765,26 +4775,28 @@ func (repo repository) GetClaGroupCorporateContributors(ctx context.Context, cla
SignatureApproved: sig.SignatureApproved,
SignatureSigned: sig.SignatureSigned,
})

// Increment the current count
currentCount++
if currentCount >= *pageSize {
break
}
}

if results.LastEvaluatedKey["signature_id"] != nil {
if results.LastEvaluatedKey["signature_id"] != nil && currentCount < *pageSize {
lastEvaluatedKey = *results.LastEvaluatedKey["signature_id"].S
queryInput.ExclusiveStartKey = results.LastEvaluatedKey
} else {
lastEvaluatedKey = ""
}

if int64(len(out.List)) >= *pageSize {
break
}

}
sort.Slice(out.List, func(i, j int) bool {
return out.List[i].Name < out.List[j].Name
})

out.ResultCount = int64(len(out.List))
out.TotalCount = <-totalCountChannel
out.ResultCount = currentCount
out.TotalCount = totalCount
out.NextKey = lastEvaluatedKey

return out, nil
Expand Down

0 comments on commit eff666e

Please sign in to comment.