Skip to content

Commit

Permalink
Adds edge cases to handle deleted and re-created reviewer workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanStonebraker committed Dec 16, 2024
1 parent 10a9078 commit 40de363
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
59 changes: 58 additions & 1 deletion api/ws/handlers/user-management.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,67 @@ func HandleReviewerMagicLinkCreateReq(req *protos.ReviewerMagicLinkCreateReq, hc

if len(users.Users) > 0 {
user := users.Users[0]
if user.AppMetadata != nil && user.AppMetadata["workspaceId"] == req.WorkspaceId {
isExpired := false
noMongoUser := false

userDB, err := wsHelpers.GetDBUserByEmail(user.GetEmail(), hctx.Svcs.MongoDB)
if err != nil {
if err != mongo.ErrNoDocuments {
log.Fatalf("failed to get existing reviewer user (%+v) from mongoDB: %+v", *user.ID, err)
} else {
noMongoUser = true
}
}

isExpired = noMongoUser || (userDB.Info.ExpirationDateUnixSec > 0 && userDB.Info.ExpirationDateUnixSec < time.Now().Unix())
if user.AppMetadata != nil && user.AppMetadata["workspaceId"] == req.WorkspaceId && !isExpired {
// If it's not expired, but the new expiration date differs, update it
if req.AccessLength == 0 && userDB.Info.ExpirationDateUnixSec != 0 {
userDB.Info.ExpirationDateUnixSec = 0
ctx := context.TODO()
coll := hctx.Svcs.MongoDB.Collection(dbCollections.UsersName)
_, err := coll.UpdateOne(ctx, bson.M{"_id": userDB.Id}, bson.D{{Key: "$set", Value: &userDB}}, options.Update())
if err != nil {
log.Fatalf("failed to update user in mongoDB: %+v", err)
}

return &protos.ReviewerMagicLinkCreateResp{
MagicLink: userDB.Id,
}, nil

} else if req.AccessLength > 0 && userDB.Info.ExpirationDateUnixSec != time.Now().Add(time.Duration(req.AccessLength)*time.Second).Unix() {
userDB.Info.ExpirationDateUnixSec = time.Now().Add(time.Duration(req.AccessLength) * time.Second).Unix()
ctx := context.TODO()
coll := hctx.Svcs.MongoDB.Collection(dbCollections.UsersName)
_, err := coll.UpdateOne(ctx, bson.M{"_id": userDB.Id}, bson.D{{Key: "$set", Value: &userDB}}, options.Update())
if err != nil {
log.Fatalf("failed to update user in mongoDB: %+v", err)
}

return &protos.ReviewerMagicLinkCreateResp{
MagicLink: userDB.Id,
}, nil
}

return &protos.ReviewerMagicLinkCreateResp{
MagicLink: *users.Users[0].ID,
}, nil
} else if isExpired {

if !noMongoUser {
ctx := context.TODO()
coll := hctx.Svcs.MongoDB.Collection(dbCollections.UsersName)
_, err := coll.DeleteOne(ctx, bson.M{"_id": userDB.Id})
if err != nil {
log.Fatalf("failed to delete expired reviewer user from mongo DB: %+v", err)
}
}

// Delete the user from Auth0, as the user has expired
err = auth0API.User.Delete(*user.ID)
if err != nil {
log.Fatalf("failed to delete expired reviewer user: %+v", err)
}
} else {
return nil, errors.New("user with email (" + email + ") already exists, but not for this workspace")
}
Expand Down
21 changes: 21 additions & 0 deletions api/ws/wsHelpers/userDBCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ func GetDBUser(userId string, db *mongo.Database) (*protos.UserDBItem, error) {
return &userDBItem, nil
}

func GetDBUserByEmail(email string, db *mongo.Database) (*protos.UserDBItem, error) {
userResult := db.Collection(dbCollections.UsersName).FindOne(context.TODO(), bson.M{"info.email": email})
if userResult.Err() != nil {
return nil, userResult.Err()
}

userDBItem := protos.UserDBItem{}
err := userResult.Decode(&userDBItem)
if err != nil {
return nil, err
}

if userDBItem.NotificationSettings == nil {
userDBItem.NotificationSettings = &protos.UserNotificationSettings{
TopicSettings: map[string]protos.NotificationMethod{},
}
}

return &userDBItem, nil
}

// This uses a cache as it may be reading the same thing many times in bursts.
// Cache is told when user info changes, and also has a time stamp so we don't
// keep reading from cache forever
Expand Down

0 comments on commit 40de363

Please sign in to comment.