-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix error response when activating account * fix activate account endpoint tests * add generating client urls from appsettings (class and options) * refactored sending emails to use client urls * add complete registration endpoint, command, command handler and domain logic * add complete registration endpoint tests * additional refactoring
- Loading branch information
1 parent
62a653a
commit 9e68bdf
Showing
21 changed files
with
306 additions
and
14 deletions.
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
34 changes: 34 additions & 0 deletions
34
src/TeamUp.Api/Endpoints/UserAccess/CompleteRegistrationEndpoint.cs
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,34 @@ | ||
using MediatR; | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
|
||
using TeamUp.Api.Extensions; | ||
using TeamUp.Application.Users.Activation; | ||
using TeamUp.Application.Users.CompleteRegistration; | ||
using TeamUp.Contracts.Users; | ||
|
||
namespace TeamUp.Api.Endpoints.UserAccess; | ||
|
||
public sealed class CompleteRegistrationEndpoint : IEndpointGroup | ||
{ | ||
public void MapEndpoints(RouteGroupBuilder group) | ||
{ | ||
group.MapPost("/{userId:guid}/generated/complete", ActivateAccountAsync) | ||
.Produces(StatusCodes.Status200OK) | ||
.ProducesProblem(StatusCodes.Status400BadRequest) | ||
.ProducesProblem(StatusCodes.Status404NotFound) | ||
.WithName(nameof(CompleteRegistrationEndpoint)) | ||
.MapToApiVersion(1); | ||
} | ||
|
||
private async Task<IResult> ActivateAccountAsync( | ||
[FromRoute] Guid userId, | ||
[FromHeader(Name = UserConstants.HTTP_HEADER_PASSWORD)] string password, | ||
[FromServices] ISender sender, | ||
CancellationToken ct) | ||
{ | ||
var command = new CompleteRegistrationCommand(UserId.FromGuid(userId), password); | ||
var result = await sender.Send(command, ct); | ||
return result.ToResponse(TypedResults.Ok); | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
src/TeamUp.Application/Users/CompleteRegistration/CompleteRegistrationCommand Handler.cs
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,29 @@ | ||
using TeamUp.Application.Abstractions; | ||
using TeamUp.Domain.Abstractions; | ||
using TeamUp.Domain.Aggregates.Users; | ||
|
||
namespace TeamUp.Application.Users.CompleteRegistration; | ||
|
||
internal sealed class CompleteRegistrationCommandHandler : ICommandHandler<CompleteRegistrationCommand, Result> | ||
{ | ||
private readonly IUserRepository _userRepository; | ||
private readonly IPasswordService _passwordService; | ||
private readonly IUnitOfWork _unitOfWork; | ||
|
||
public CompleteRegistrationCommandHandler(IUserRepository userRepository, IPasswordService passwordService, IUnitOfWork unitOfWork) | ||
{ | ||
_userRepository = userRepository; | ||
_passwordService = passwordService; | ||
_unitOfWork = unitOfWork; | ||
} | ||
|
||
public async Task<Result> Handle(CompleteRegistrationCommand command, CancellationToken ct) | ||
{ | ||
var user = await _userRepository.GetUserByIdAsync(command.UserId, ct); | ||
return await user | ||
.EnsureNotNull(UserErrors.UserNotFound) | ||
.Tap(user => user.CompleteGeneratedRegistration(_passwordService.HashPassword(command.Password))) | ||
.TapAsync(_ => _unitOfWork.SaveChangesAsync(ct)) | ||
.ToResultAsync(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/TeamUp.Application/Users/CompleteRegistration/CompleteRegistrationCommand.cs
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,6 @@ | ||
using TeamUp.Application.Abstractions; | ||
using TeamUp.Contracts.Users; | ||
|
||
namespace TeamUp.Application.Users.CompleteRegistration; | ||
|
||
public sealed record CompleteRegistrationCommand(UserId UserId, string Password) : ICommand<Result>; |
25 changes: 25 additions & 0 deletions
25
src/TeamUp.Application/Users/CompleteRegistration/UserGeneratedEventHandler.cs
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,25 @@ | ||
using TeamUp.Common.Abstractions; | ||
using TeamUp.Domain.Abstractions; | ||
using TeamUp.Domain.Aggregates.Users.IntegrationEvents; | ||
|
||
namespace TeamUp.Application.Users.Register; | ||
|
||
internal sealed class UserGeneratedEventHandler : IIntegrationEventHandler<UserGeneratedEvent> | ||
{ | ||
private readonly IEmailSender _emailSender; | ||
private readonly IClientUrlGenerator _urlGenerator; | ||
|
||
public UserGeneratedEventHandler(IEmailSender emailSender, IClientUrlGenerator urlGenerator) | ||
{ | ||
_emailSender = emailSender; | ||
_urlGenerator = urlGenerator; | ||
} | ||
|
||
public Task Handle(UserGeneratedEvent integrationEvent, CancellationToken ct) | ||
{ | ||
return _emailSender.SendEmailAsync( | ||
email: integrationEvent.Email, | ||
subject: "Account has been created", | ||
message: $"You need to finalize your registration at {_urlGenerator.GetCompleteAccountRegistrationUrl(integrationEvent.UserId)}.", ct); | ||
} | ||
} |
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,9 @@ | ||
using TeamUp.Contracts.Users; | ||
|
||
namespace TeamUp.Domain.Abstractions; | ||
|
||
public interface IClientUrlGenerator | ||
{ | ||
public string GetActivationUrl(UserId userId); | ||
public string GetCompleteAccountRegistrationUrl(UserId userId); | ||
} |
6 changes: 6 additions & 0 deletions
6
src/TeamUp.Domain/Aggregates/Users/IntegrationEvents/UserGeneratedEvent.cs
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,6 @@ | ||
using TeamUp.Contracts.Users; | ||
using TeamUp.Domain.Abstractions; | ||
|
||
namespace TeamUp.Domain.Aggregates.Users.IntegrationEvents; | ||
|
||
public sealed record UserGeneratedEvent(UserId UserId, string Email, string UserName) : IIntegrationEvent; |
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,23 @@ | ||
using Microsoft.Extensions.Options; | ||
|
||
using TeamUp.Contracts.Users; | ||
using TeamUp.Domain.Abstractions; | ||
using TeamUp.Infrastructure.Options; | ||
|
||
namespace TeamUp.Infrastructure.Core; | ||
|
||
internal sealed class ClientUrlGenerator : IClientUrlGenerator | ||
{ | ||
private readonly ClientOptions _options; | ||
|
||
public ClientUrlGenerator(IOptions<ClientOptions> options) | ||
{ | ||
_options = options.Value; | ||
} | ||
|
||
public string GetActivationUrl(UserId userId) => | ||
string.Format(_options.ActivateAccountUrl, _options.Url, userId.Value); | ||
|
||
public string GetCompleteAccountRegistrationUrl(UserId userId) => | ||
string.Format(_options.CompleteAccountRegistrationUrl, _options.Url, userId.Value); | ||
} |
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 |
---|---|---|
@@ -1,8 +1,17 @@ | ||
namespace TeamUp.Infrastructure.Options; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace TeamUp.Infrastructure.Options; | ||
|
||
internal sealed class ClientOptions : IApplicationOptions | ||
{ | ||
public static string SectionName => "Client"; | ||
|
||
[Required] | ||
public required string Url { get; init; } | ||
|
||
[Required] | ||
public required string ActivateAccountUrl { get; init; } | ||
|
||
[Required] | ||
public required string CompleteAccountRegistrationUrl { get; init; } | ||
} |
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
Oops, something went wrong.