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

[#4002]Feature/Docsign intgeration Lint #4156

Merged
merged 1 commit into from
Oct 23, 2023
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
111 changes: 72 additions & 39 deletions cla-backend-go/v2/sign/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ func (s *service) populateSignURL(ctx context.Context,
authorityOrSignatoryName, authorityOrSignatoryEmail string,
sendAsEmail bool,
claManagerName, claManagerEmail string,
defaultValues map[string]interface{}, preferredEmail string) error {
defaultValues map[string]interface{}, preferredEmail string) error {

f := logrus.Fields{
"functionName": "sign.populateSignURL",
Expand All @@ -663,8 +663,8 @@ func (s *service) populateSignURL(ctx context.Context,
log.WithFields(f).Debugf("signatureReferenceType: %s", signatureReferenceType)
log.WithFields(f).Debugf("processing signing request...")

userSignatureName := Unknown
userSignatureEmail := Unknown
var userSignatureName string
var userSignatureEmail string
var document v1Models.ClaGroupDocument
var project *v1Models.ClaGroup
var companyModel *v1Models.Company
Expand All @@ -673,45 +673,21 @@ func (s *service) populateSignURL(ctx context.Context,
var emailBody string
var emailSubject string

if signatureReferenceType == utils.SignatureReferenceTypeCompany {
companyModel, err = s.companyRepo.GetCompany(ctx, latestSignature.SignatureReferenceID)
if err != nil {
log.WithFields(f).WithError(err).Warnf("unable to lookup company by ID: %s", latestSignature.SignatureReferenceID)
return err
}
if companyModel == nil {
log.WithFields(f).WithError(err).Warnf("unable to lookup company by ID: %s", latestSignature.SignatureReferenceID)
return errors.New("no CLA manager lookup error")
}
userSignatureName = claManagerName
userSignatureEmail = claManagerEmail
} else if signatureReferenceType == utils.SignatureReferenceTypeUser {
if !sendAsEmail {
userModel, userErr := s.userService.GetUser(latestSignature.SignatureReferenceID)
if userErr != nil {
log.WithFields(f).WithError(userErr).Warnf("unable to lookup user by ID: %s", latestSignature.SignatureReferenceID)
return userErr
}
log.WithFields(f).Debugf("loaded user : %+v", userModel)

if userModel == nil {
log.WithFields(f).WithError(userErr).Warnf("unable to lookup user by ID: %s", latestSignature.SignatureReferenceID)
msg := fmt.Sprintf("No user lookup error for user ID: %s", latestSignature.SignatureReferenceID)
return errors.New(msg)
}
// companyModel, userSignatureName, userSignatureEmail, shouldReturn, returnValue := newFunction(ctx, signatureReferenceType, companyModel, err, s, latestSignature, f, userSignatureName, claManagerName, userSignatureEmail, claManagerEmail, sendAsEmail, preferredEmail)
// if shouldReturn {
// return returnValue
// }

if userModel.Username != "" {
userSignatureName = userModel.Username
}
if getUserEmail(userModel, preferredEmail) != "" {
userSignatureEmail = getUserEmail(userModel, preferredEmail)
}
}
} else {
log.WithFields(f).Warnf("unknown signature reference type: %s", signatureReferenceType)
return errors.New("unknown signature reference type")
// populate user details
userDetails, err := s.populateUserDetails(ctx, signatureReferenceType, latestSignature, claManagerName, claManagerEmail, sendAsEmail, preferredEmail)
if err != nil {
log.WithFields(f).WithError(err).Warnf("unable to populate user details for signatureReferenceType: %s", signatureReferenceType)
return err
}

userSignatureName = userDetails.userSignatureName
userSignatureEmail = userDetails.userSignatureEmail

// Get the document template to sign
log.WithFields(f).Debugf("getting document template to sign...")
project, err = s.projectRepo.GetCLAGroupByID(ctx, latestSignature.ProjectID, DontLoadRepoDetails)
Expand Down Expand Up @@ -994,6 +970,63 @@ func (s *service) populateSignURL(ctx context.Context,
return nil
}

type UserSignDetails struct {
userSignatureName string
userSignatureEmail string
}

func (s *service) populateUserDetails(ctx context.Context, signatureReferenceType string, latestSignature *v1Models.Signature, claManagerName, claManagerEmail string, sendAsEmail bool, preferredEmail string) (*UserSignDetails, error) {
f := logrus.Fields{
"functionName": "sign.populateUserDetails",
}
log.WithFields(f).Debugf("populating user details...")
userSignDetails := &UserSignDetails{
userSignatureName: Unknown,
userSignatureEmail: Unknown,
}

if signatureReferenceType == utils.SignatureReferenceTypeCompany {
companyModel, err := s.companyRepo.GetCompany(ctx, latestSignature.SignatureReferenceID)
if err != nil {
log.WithFields(f).WithError(err).Warnf("unable to lookup company by ID: %s", latestSignature.SignatureReferenceID)
return nil, err
}
if companyModel == nil {
log.WithFields(f).WithError(err).Warnf("unable to lookup company by ID: %s", latestSignature.SignatureReferenceID)
return nil, errors.New("no CLA manager lookup error")
}
userSignDetails.userSignatureEmail = claManagerEmail
userSignDetails.userSignatureName = claManagerName

} else if signatureReferenceType == utils.SignatureReferenceTypeUser {
if !sendAsEmail {
userModel, userErr := s.userService.GetUser(latestSignature.SignatureReferenceID)
if userErr != nil {
log.WithFields(f).WithError(userErr).Warnf("unable to lookup user by ID: %s", latestSignature.SignatureReferenceID)
return nil, userErr
}
log.WithFields(f).Debugf("loaded user : %+v", userModel)

if userModel == nil {
log.WithFields(f).WithError(userErr).Warnf("unable to lookup user by ID: %s", latestSignature.SignatureReferenceID)
msg := fmt.Sprintf("No user lookup error for user ID: %s", latestSignature.SignatureReferenceID)
return nil, errors.New(msg)
}

if userModel.Username != "" {
userSignDetails.userSignatureName = userModel.Username
}
if getUserEmail(userModel, preferredEmail) != "" {
userSignDetails.userSignatureEmail = getUserEmail(userModel, preferredEmail)
}
}
} else {
log.WithFields(f).Warnf("unknown signature reference type: %s", signatureReferenceType)
return nil, errors.New("unknown signature reference type")
}
return userSignDetails, nil
}

func (s *service) getDocumentResource(urlString string) ([]byte, error) {

// validate the URL
Expand Down
Loading