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

Added Defensive Code for Avoid Creating Duplicate Company Records #2709

Merged
merged 1 commit into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
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
22 changes: 20 additions & 2 deletions cla-backend-go/company/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -1217,14 +1217,31 @@ func (repo repository) UpdateCompanyAccessList(ctx context.Context, companyID st
// CreateCompany creates a new company record
func (repo repository) CreateCompany(ctx context.Context, in *models.Company) (*models.Company, error) {
f := logrus.Fields{
"functionName": "company.repository.CreateCompany",
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
"functionName": "company.repository.CreateCompany",
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
"companyName": in.CompanyName,
"signingEntityName": in.SigningEntityName,
"companySFID": in.CompanyExternalID,
}

// Don't create duplicates - check to see if any exist
existingModel, queryErr := repo.GetCompanyByName(ctx, in.CompanyName)
if queryErr != nil {
log.WithFields(f).WithError(queryErr).Warn("problem querying for existing company record by name")
return nil, queryErr
}
// Already exists - don't re-create
if existingModel != nil {
return existingModel, nil
}

companyID, err := uuid.NewV4()
if err != nil {
log.WithFields(f).Warnf("Unable to generate a UUID for a pending invite, error: %v", err)
return nil, err
}
f["companyID"] = companyID

_, now := utils.CurrentTime()
comp := &DBModel{
CompanyID: companyID.String(),
Expand Down Expand Up @@ -1260,6 +1277,7 @@ func (repo repository) CreateCompany(ctx context.Context, in *models.Company) (*
TableName: aws.String(repo.companyTableName),
})
if err != nil {
log.WithFields(f).WithError(err).Warn("problem creating new company")
return nil, err
}

Expand Down
Loading