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

Reuse the previous invite code when updating an invitation #3719

Merged
merged 2 commits into from
Jun 26, 2024
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
12 changes: 6 additions & 6 deletions database/mock/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions database/query/invitations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ INSERT INTO user_invites (code, email, role, project, sponsor) VALUES ($1, $2, $
-- name: DeleteInvitation :one
DELETE FROM user_invites WHERE code = $1 RETURNING *;

-- UpdateInvitation updates an invitation by its code. This is intended to be
-- called by a user who has issued an invitation and then decided to bump its
-- expiration.
-- UpdateInvitationRole updates an invitation by its code. This is intended to be
-- called by a user who has issued an invitation and then decided to change the
-- role of the invitee.

-- name: UpdateInvitation :one
UPDATE user_invites SET updated_at = NOW() WHERE code = $1 RETURNING *;
-- name: UpdateInvitationRole :one
UPDATE user_invites SET role = $2, updated_at = NOW() WHERE code = $1 RETURNING *;
40 changes: 14 additions & 26 deletions internal/controlplane/handlers_authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,32 +702,16 @@ func (s *Server) updateInvite(
}
defer s.store.Rollback(tx)

// At this point, there should be exactly 1 invitation. We should either update its expiration or
// discard it and create a new one
if existingInvites[0].Role != authzRole.String() {
// If there's an existing invite with a different role, we should delete it and create a new one
// Delete the existing invitation
_, err = s.store.DeleteInvitation(ctx, existingInvites[0].Code)
if err != nil {
return nil, status.Errorf(codes.Internal, "error deleting previous invitation: %v", err)
}
// Create a new invitation
userInvite, err = s.store.CreateInvitation(ctx, db.CreateInvitationParams{
Code: invite.GenerateCode(),
Email: email,
Role: authzRole.String(),
Project: targetProject,
Sponsor: currentUser.ID,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "error creating invitation: %v", err)
}
} else {
// If the role is the same, we should update the expiration
userInvite, err = s.store.UpdateInvitation(ctx, existingInvites[0].Code)
if err != nil {
return nil, status.Errorf(codes.Internal, "error updating invitation: %v", err)
}
// At this point, there should be exactly 1 invitation.
// Depending on the role from the request, we can either update the role and its expiration
// or just bump the expiration date.
// In both cases, we can use the same query.
userInvite, err = s.store.UpdateInvitationRole(ctx, db.UpdateInvitationRoleParams{
Code: existingInvites[0].Code,
Role: authzRole.String(),
})
if err != nil {
return nil, status.Errorf(codes.Internal, "error updating invitation: %v", err)
}

// Resolve the project's display name
Expand All @@ -747,6 +731,10 @@ func (s *Server) updateInvite(
return nil, status.Errorf(codes.Internal, "error committing transaction: %v", err)
}

// TODO: Publish the event for sending the invitation email
// This will happen only if the role is updated (existingInvites[0].Role != authzRole.String())
// or the role stayed the same but the invite was created at least a day ago.

return &minder.UpdateRoleResponse{
Invitations: []*minder.Invitation{
{
Expand Down
19 changes: 12 additions & 7 deletions internal/db/invitations.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions internal/db/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.