This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
Logic for batch user import #553
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
567c71d
batch import logic
whaught 6581a02
multierror, api comments
whaught 7fbcb6c
remove sleep
whaught 669fe29
review
whaught e095483
Merge remote-tracking branch 'upstream/main' into csvs
whaught b6df843
User server side user firebase create
whaught 34945e1
Merge remote-tracking branch 'upstream/main' into csvs
whaught 498ed9c
fix up email verified
whaught 53625af
email verified realm setting
whaught eca9578
store session correctly
whaught 7fce404
Merge remote-tracking branch 'upstream/main' into csvs
whaught 6bee3df
update with createfirebaseuser
whaught a7e5412
kebabs
whaught 85b2a52
put it back
whaught 47b23d9
progress as you go
whaught 0df3b9b
Merge remote-tracking branch 'upstream/main' into csvs
whaught befbe95
import cancel
whaught b9337a6
error handling
whaught c2f134c
merge from main
whaught 4858170
main merge
whaught 81de5aa
10 batchsize
whaught 144c05a
SCs
whaught 477c8bd
caps
whaught ba65668
spelling
whaught File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package user | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/google/exposure-notifications-verification-server/pkg/api" | ||
"github.com/google/exposure-notifications-verification-server/pkg/controller" | ||
"github.com/google/exposure-notifications-verification-server/pkg/database" | ||
"github.com/hashicorp/go-multierror" | ||
) | ||
|
||
func (c *Controller) HandleImportBatch() http.Handler { | ||
logger := c.logger.Named("user.HandleImportBatch") | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
|
||
realm := controller.RealmFromContext(ctx) | ||
if realm == nil { | ||
controller.MissingRealm(w, r, c.h) | ||
return | ||
} | ||
|
||
var request api.UserBatchRequest | ||
if err := controller.BindJSON(w, r, &request); err != nil { | ||
logger.Errorw("Error decoding request", "error", err) | ||
c.h.RenderJSON(w, http.StatusBadRequest, api.Error(err)) | ||
return | ||
} | ||
|
||
newUsers := make([]*api.BatchUser, 0, len(request.Users)) | ||
|
||
var batchErr *multierror.Error | ||
for _, batchUser := range request.Users { | ||
// See if the user already exists by email - they may be a member of another | ||
// realm. | ||
user, err := c.db.FindUserByEmail(batchUser.Email) | ||
alreadyExists := true | ||
if err != nil { | ||
if !database.IsNotFound(err) { | ||
logger.Errorw("Error finding user", "error", err) | ||
batchErr = multierror.Append(batchErr, err) | ||
continue | ||
} | ||
|
||
user = new(database.User) | ||
alreadyExists = false | ||
} | ||
|
||
// Build the user struct - keeping email and name if user already exists in another realm. | ||
if !alreadyExists { | ||
user.Email = batchUser.Email | ||
user.Name = batchUser.Name | ||
} | ||
user.Realms = append(user.Realms, realm) | ||
if err := c.db.SaveUser(user); err != nil { | ||
logger.Errorw("Error saving user", "error", err) | ||
batchErr = multierror.Append(batchErr, err) | ||
continue | ||
} | ||
|
||
if created, err := user.CreateFirebaseUser(ctx, c.client); err != nil { | ||
logger.Errorw("Error creating firebase user", "error", err) | ||
batchErr = multierror.Append(batchErr, err) | ||
continue | ||
} else if created { | ||
newUsers = append(newUsers, &batchUser) | ||
} | ||
} | ||
|
||
response := &api.UserBatchResponse{ | ||
NewUsers: newUsers, | ||
} | ||
|
||
if err := batchErr.ErrorOrNil(); err != nil { | ||
response.Error = err.Error() | ||
response.ErrorCode = string(http.StatusInternalServerError) | ||
|
||
if len(newUsers) == 0 { // We return partial success if any succeeded. | ||
c.h.RenderJSON(w, http.StatusInternalServerError, response) | ||
return | ||
} | ||
} | ||
|
||
c.h.RenderJSON(w, http.StatusOK, response) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm this is a lot of javascript. Is there a strong reason to do this on the client vs the server? It seems like a traditional form submit + background processing would suffice here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also worried about the "modern" requirements here. When you think about most hospitals and medical facilities, they aren't running the latest and greatest browsers. I wouldn't be surprised if we have some clients using IE on XP still, with no support for the HTML5 file objects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main reason is so that it can batch and update progress as it goes. If we submit the whole file, we still need to return a response with all the users so we can do the password reset in javascript.