Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#3920] Feature/Get Latest Signatures #3940

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions cla-backend-go/signatures/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand All @@ -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
}

Expand Down