From 6979ba47e3271c1f90eaf94041aa3dbddef87722 Mon Sep 17 00:00:00 2001 From: Harold Wanyama Date: Tue, 29 Oct 2024 15:24:52 +0300 Subject: [PATCH] [#4460] Bug/Corporate Contributors API pagination - Fixed issue with pagesize greater than the total Signed-off-by: Harold Wanyama --- cla-backend-go/signatures/repository.go | 38 ++++++++++++++++--------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/cla-backend-go/signatures/repository.go b/cla-backend-go/signatures/repository.go index fa52dde05..48db47aa4 100644 --- a/cla-backend-go/signatures/repository.go +++ b/cla-backend-go/signatures/repository.go @@ -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) @@ -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(), @@ -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) @@ -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