From 331e85eb99a839be3dd377291f66cb58a1422691 Mon Sep 17 00:00:00 2001 From: Harold Wanyama Date: Tue, 16 May 2023 17:10:10 +0300 Subject: [PATCH] [#3920] Feature/Get Latest Signatures - Due to duplicate records returned resolved duplicate records by fetching latest signature Signed-off-by: Harold Wanyama --- cla-backend-go/signatures/repository.go | 42 ++++++++++++------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/cla-backend-go/signatures/repository.go b/cla-backend-go/signatures/repository.go index 93d5d2b21..01cfd17b6 100644 --- a/cla-backend-go/signatures/repository.go +++ b/cla-backend-go/signatures/repository.go @@ -1732,9 +1732,6 @@ func (repo repository) GetProjectCompanyEmployeeSignatures(ctx context.Context, // Add to the signature response model to the list sigs = append(sigs, signatureList...) - // remove duplicate values - sigs = removeDuplicates(sigs) - if results.LastEvaluatedKey["signature_id"] != nil { lastEvaluatedKey = *results.LastEvaluatedKey["signature_id"].S queryInput.ExclusiveStartKey = results.LastEvaluatedKey @@ -1748,6 +1745,9 @@ func (repo repository) GetProjectCompanyEmployeeSignatures(ctx context.Context, } log.WithFields(f).Debugf("finished signature query on table: %s - duration: %+v", repo.signatureTableName, time.Since(beforeQuery)) + // remove duplicate values + sigs = getLatestSignatures(sigs) + // Meta-data for the response if int64(len(sigs)) > pageSize { sigs = sigs[0:pageSize] @@ -1764,33 +1764,31 @@ func (repo repository) GetProjectCompanyEmployeeSignatures(ctx context.Context, }, nil } -func removeDuplicates(signatures []*models.Signature) []*models.Signature { +func getLatestSignatures(signatures []*models.Signature) []*models.Signature { f := logrus.Fields{ - "functionName": "v1.signatures.repository.removeDuplicates", + "functionName": "v1.signatures.repository.getLatestSignatures", } - type SignatureCheck struct { - SignatureRef string - SignatureApproved bool - SignatureSigned bool - } - - seen := make(map[SignatureCheck]bool) + signatureMap := make(map[string]*models.Signature) result := []*models.Signature{} - for _, model := range signatures { - check := SignatureCheck{ - SignatureRef: model.SignatureReferenceID, - SignatureApproved: model.SignatureApproved, - SignatureSigned: model.SignatureSigned, - } - if !seen[check] { - log.WithFields(f).Debugf("Signature : %+v does not exist", check) - seen[check] = true - result = append(result, model) + log.WithFields(f).Debug("get latest signatures per contributor...") + + for _, signature := range signatures { + if _, ok := signatureMap[signature.SignatureReferenceID]; !ok { + log.WithFields(f).Debugf("adding signature: %s to map", signature.SignatureReferenceID) + signatureMap[signature.SignatureReferenceID] = signature + } else { + if signature.Modified > signatureMap[signature.SignatureReferenceID].Modified { + signatureMap[signature.SignatureReferenceID] = signature + } } } + for _, signature := range signatureMap { + result = append(result, signature) + } + return result }