Uitnodigingen: naam opslaan en hetzelfde e-mailadres opnieuw kunnen uitnodigen

De naam uit het uitnodigingsformulier werd niet opgeslagen (profiel en verwijder-dialoog
toonden het e-mailadres). Een geaccepteerde uitnodiging bleef na het verwijderen van de
gebruiker staan en blokkeerde het adres; het conflict gaf een 500. Nu 409 met een
specifieke melding en opruimen van verouderde uitnodigingen.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 14:36:09 +01:00
co-authored by Claude Sonnet 5.5
parent 33a7500648
commit 3535d14987
9 changed files with 130 additions and 20 deletions
@@ -21,14 +21,31 @@ public class InvitationService : IInvitationService
public async Task<string> CreateInvitationAsync(string email, string role)
{
var invitationForEmail = await _context.Invitations.FirstOrDefaultAsync(x => x.Email == email);
var user = await _userManager.FindByEmailAsync(email);
if (invitationForEmail != null || user != null)
if (user != null)
{
throw new InvitationOrUserAlreadyExistsException("Een uitnodiging of gebruiker voor dit e-mailadres bestaat al.");
throw new InvitationOrUserAlreadyExistsException("Er bestaat al een gebruiker met dit e-mailadres.");
}
var normalized = email.ToLower();
var invitationsForEmail = await _context.Invitations
.Where(x => x.Email.ToLower() == normalized)
.ToListAsync();
if (invitationsForEmail.Any(i => !i.IsAccepted && !i.IsExpired))
{
throw new InvitationOrUserAlreadyExistsException(
"Er staat al een openstaande uitnodiging voor dit e-mailadres.");
}
// What is left is stale: an expired invitation, or an accepted one whose user no longer
// exists (the account was deleted). Neither can be used again, and an accepted one used to
// block re-inviting the same address for good.
if (invitationsForEmail.Count > 0)
{
_context.Invitations.RemoveRange(invitationsForEmail);
}
var token = GenerateSecureToken();
var invitation = new Invitation
{
@@ -51,7 +68,7 @@ public class InvitationService : IInvitationService
return invitation != null && !invitation.IsAccepted && !invitation.IsExpired;
}
public async Task CompleteInvitationAsync(string token, string password)
public async Task CompleteInvitationAsync(string token, string password, string? name = null)
{
var invitation = await _context.Invitations.FirstOrDefaultAsync(i => i.Token == token);
if (invitation == null || invitation.IsAccepted || invitation.IsExpired)
@@ -63,6 +80,7 @@ public class InvitationService : IInvitationService
{
UserName = invitation.Email,
Email = invitation.Email,
DisplayName = string.IsNullOrWhiteSpace(name) ? null : name.Trim(),
IsActive = true
};
@@ -85,10 +103,10 @@ public class InvitationService : IInvitationService
return invitation is null ? (false, null) : (true, invitation.Token);
}
public async Task DeletePendingInvitationsByEmailAsync(string email)
public async Task DeleteInvitationsByEmailAsync(string email)
{
var invitations = await _context.Invitations
.Where(i => i.Email == email && !i.IsAccepted)
.Where(i => i.Email.ToLower() == email.ToLower())
.ToListAsync();
if (invitations.Count > 0)
{