Skip to content

Commit

Permalink
Add Gender migration.
Browse files Browse the repository at this point in the history
Remove idenitty grpc versioning.
Remove EmailOptionsSetup in Idenity.
Add Logic in SendConfirmationUserMessage in Email.Application project.
  • Loading branch information
a-sharifov committed Feb 6, 2024
1 parent 7bf14d9 commit f9f2eb9
Show file tree
Hide file tree
Showing 40 changed files with 462 additions and 147 deletions.
2 changes: 1 addition & 1 deletion crs/CommonComponents/Contracts/Contracts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<ItemGroup>
<Protobuf Include="Abstractions\Abstractions.proto" GrpcServices="Both" />
<Protobuf Include="Services\Identity\identity.v1.proto" GrpcServices="Both" />
<Protobuf Include="Services\Identity\identity.proto" GrpcServices="Both" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
syntax = "proto3";

package identity.v1;
package identity.protobuf;

service IdentityService {
rpc GetUserInfo(GetUserRequest) returns (UserInfo);
Expand Down
4 changes: 2 additions & 2 deletions crs/Docker/.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AUTH_ISSUER=https://localhost:8081
WEB_AUDIENCE=https://localhost:7061
AUTH_ISSUER=http://localhost:8080
WEB_AUDIENCE=http://localhost:7060
JWT_SECURITY_KEY=86A7B43F17CA48BEE7519EB8D6BDBA2SNSD

INFLUXDB_USER=admin
Expand Down
4 changes: 1 addition & 3 deletions crs/Docker/docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ services:
- ${APPDATA}\microsoft\UserSecrets\:/root/.microsoft/usersecrets
- ${USERPROFILE}\.aspnet\https:/root/.aspnet/https/


email.app:
environment:
- ASPNETCORE_URLS=http://+:80
Expand All @@ -91,11 +90,10 @@ services:
- AUTH_ISSUER=${AUTH_ISSUER}
- WEB_AUDIENCE=${WEB_AUDIENCE}
- JWT_SECURITY_KEY=${JWT_SECURITY_KEY}
- IDENTITY_URL=${AUTH_ISSUER}
ports:
- "5110:80"



mssql:
environment:
- ACCEPT_EULA=Y
Expand Down
2 changes: 1 addition & 1 deletion crs/Services/Catalog/Catalog.App/Env.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class Env
public static string JWT_SECURITY_KEY => GetEnvironmentVariable("JWT_SECURITY_KEY");

private static string GetEnvironmentVariable(string key) =>
Environment.GetEnvironmentVariable(key) ??
Environment.GetEnvironmentVariable(key) ??
throw new Exception($"Environment variable {key} not found");

public static class ConnectionStrings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public async Task<Result<Brand>> Handle(GetBrandByIdQuery request, CancellationT
{
var brandId = new BrandId(request.Id);

var brand = await _brandRepository.GetByIdAsync(brandId);
var brand = await _brandRepository.GetByIdAsync(brandId, cancellationToken);

return brand ?? Result.Failure<Brand>(
BrandErrors.BrandNotFound);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ internal sealed class GrpcServiceInstaller : IServiceInstaller
{
public void Install(IServiceCollection services, IConfiguration configuration)
{
services.AddGrpcClient<Identity.V1.IdentityService.IdentityServiceClient>(options =>
services.AddGrpcClient<IdentityService.IdentityServiceClient>(options =>
{
options.Address = new Uri(Env.IDENTITY_URL);
});
Expand Down
2 changes: 1 addition & 1 deletion crs/Services/Email/Email.App/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
global using FluentValidation;
global using Email.App.OptionsSetup;
global using Common.Infrastructure.Middleware;
global using Contracts.Services.Identity;
global using Microsoft.AspNetCore.Authentication.JwtBearer;
global using Microsoft.IdentityModel.Tokens;
global using System.Security.Claims;
global using System.Text;
global using Identity.Protobuf;
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
namespace Email.Application.Emails.Commands.SendConfirmationUserMessage;

public sealed record SendConfirmationUserMessageCommand(Guid UserId, string ReturnUrl) : ICommand;
public sealed record SendConfirmationUserMessageCommand(
Guid UserId,
string ReturnUrl,
string ConfirmationEmailToken) : ICommand;
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
namespace Email.Application.Emails.Commands.SendConfirmationUserMessage;
using Email.Infrastructure.Email.Models;
using Email.Infrastructure.Grpc.Idenitty;

internal sealed class SendConfirmationUserMessageCommandHandler(IIdentityEmailService emailService)
namespace Email.Application.Emails.Commands.SendConfirmationUserMessage;

internal sealed class SendConfirmationUserMessageCommandHandler(
IIdentityEmailService emailService,
IIdentityGrpcService identityGrpcService)
: ICommandHandler<SendConfirmationUserMessageCommand>
{
private readonly IIdentityEmailService _emailService = emailService;
private readonly IIdentityGrpcService _identityGrpcService = identityGrpcService;


public Task<Result> Handle(SendConfirmationUserMessageCommand request, CancellationToken cancellationToken)
public async Task<Result> Handle(SendConfirmationUserMessageCommand request, CancellationToken cancellationToken)
{
var userInfo = await _identityGrpcService.GetUserInfoAsync(request.UserId, cancellationToken);

var emailRequest = new SendConfirmationEmailRequest(
userInfo.FirstName,
userInfo.LastName,
userInfo.UserId,
userInfo.Email,
request.ConfirmationEmailToken,
request.ReturnUrl);

await _emailService.SendConfirmationEmailAsync(emailRequest, cancellationToken);

_emailService.SendConfirmationEmailAsync(
new SendConfirmationEmailRequest
{
UserId = request.UserId,
EmailConfirmationToken = request.EmailConfirmationToken,
ReturnUrl = request.ReturnUrl,
FirstName = request.FirstName,
LastName = request.LastName,
Email = request.Email
}, cancellationToken);
return Result.Success();
}
}
1 change: 1 addition & 0 deletions crs/Services/Email/Email.Application/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
global using Common.Application.Abstractions.Messaging.Command;
global using Common.Domain.Primitives.Results;
global using Email.Infrastructure.Email.Abstractions;
global using Email.Infrastructure.Grpc;
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Grpc.Core.Api" Version="2.60.0" />
<PackageReference Include="MailKit" Version="4.3.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.1" />
<PackageReference Include="MimeKit" Version="4.3.0" />
Expand All @@ -15,6 +16,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\CommonComponents\Contracts\Contracts.csproj" />
<ProjectReference Include="..\Email.Templates\Email.Templates.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ public record SendConfirmationEmailRequest(
string UserId,
string Email,
string EmailConfirmationToken,
string ReturnUrl,
string Subject,
string EmailConfirmPagePath
string ReturnUrl
);
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public async Task SendConfirmationEmailAsync(SendConfirmationEmailRequest reques
await File.ReadAllTextAsync(confirmEmailTemplatePath, cancellationToken);

var confirmUrl =
$@"{thi}?UserId={request.UserId}&EmailConfirmationToken={request.EmailConfirmationToken}&ReturnUrl={request.ReturnUrl}";
$@"?UserId={request.UserId}&EmailConfirmationToken={request.EmailConfirmationToken}&ReturnUrl={request.ReturnUrl}";

var confirmUrlEncode = HtmlEncoder.Default.Encode(confirmUrl);

Expand All @@ -25,7 +25,7 @@ public async Task SendConfirmationEmailAsync(SendConfirmationEmailRequest reques

var sendMessageRequest = new SendMessageRequest(
To: request.Email,
Subject: request.Subject,
Subject: $"Eshop - confirm email",
Body: confirmEmailTemplate
);

Expand Down
4 changes: 3 additions & 1 deletion crs/Services/Email/Email.Infrastructure/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
global using MailKit.Net.Smtp;
global using System.Reflection;
global using System.Text.Encodings.Web;
global using Polly;
global using Polly;
global using Grpc.Core;
global using Identity.Protobuf;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Email.Infrastructure.Grpc.Idenitty;

public interface IIdentityGrpcService
{
public Task<UserInfo> GetUserInfoAsync(Guid id, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using static Identity.Protobuf.IdentityService;

namespace Email.Infrastructure.Grpc.Idenitty;

internal sealed class IdentityGrpcService(IdentityServiceClient client) : IIdentityGrpcService
{
private readonly IdentityServiceClient _client = client;

public async Task<UserInfo> GetUserInfoAsync(Guid id, CancellationToken cancellationToken = default)
{
var request = new GetUserRequest() { Id = id.ToString() };
return await _client.GetUserInfoAsync(request, cancellationToken: cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ public override async Task Handle(ConsumeContext<UserCreatedConfirmationEmailSen
{
var request = new SendConfirmationUserMessageCommand(
context.Message.UserId,
context.Message.ReturnUrl);
context.Message.ReturnUrl,
context.Message.ConfirmationEmailToken);

var result = await _sender.Send(request);

if (result.IsFailure)
{
throw new Exception(result.Error);
}
await _sender.Send(request);
}
}
33 changes: 22 additions & 11 deletions crs/Services/Identity/Idenitty.Grpc/IdentityGrpcServiceV1.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Identity.Application.Users.Queries.GetUserInfoById;
using Identity.V1;
using Identity.Application.Users.Queries.GetUserInfoByIdString;
using Identity.Protobuf;

namespace Idenitty.Grpc;

Expand All @@ -9,16 +9,27 @@ public sealed class IdentityGrpcServiceV1(ISender sender) : IdentityService.Iden

public override async Task<UserInfo> GetUserInfo(GetUserRequest request, ServerCallContext context)
{
Guid.TryParse(request.Id, out Guid userId);
var query = new GetUserInfoByIdStringQuery(request.Id);
var result = await _sender.Send(query, context.CancellationToken);

//if(userId == Guid.Empty)
//{
// context.Status = new Status(StatusCode.InvalidArgument, "Invalid user id");
//}
if (result.IsFailure)
{
throw new RpcException(new Status(StatusCode.NotFound, result.Error));
}

var query = new GetUserInfoByIdQuery(userId);
var result = await _sender.Send(query);
var response = new UserInfo() { s};
return base.GetUser(request, context);
var user = result.Value;

var response = new UserInfo()
{
UserId = user.UserId.ToString(),
Email = user.Email,
FirstName = user.FirstName,
LastName = user.LastName,
IsEmailConfirmed = user.IsEmailConfirmed,
Role = Enum.Parse<Role>(user.Role),
Gender = Enum.Parse<Gender>(user.Gender)
};

return response;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Identity.Application.Abstractions;

namespace Identity.App.Configurations;
namespace Identity.App.Configurations;

internal sealed class InfrastructureServiceInstaller : IServiceInstaller
{
Expand All @@ -16,7 +14,5 @@ public void Install(IServiceCollection services, IConfiguration configuration)
.AddClasses(false)
.AsImplementedInterfaces()
.WithScopedLifetime());

services.ConfigureOptions<EmailOptionsSetup>();
}
}
2 changes: 1 addition & 1 deletion crs/Services/Identity/Identity.App/Env.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ public static class ConnectionStrings

private static string GetEnvironmentVariable(string key) =>
Environment.GetEnvironmentVariable(key) ??
throw new Exception($"Environment variable {key} not found");
throw new Exception($"environment variable {key} not found");
}

This file was deleted.

1 change: 1 addition & 0 deletions crs/Services/Identity/Identity.Application/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
global using Identity.Infrastructure.Hashing;
global using Identity.Infrastructure.Authentication;
global using Identity.Application.Users.Common;
global using MediatR;
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.3.1" />
</ItemGroup>

<ItemGroup>
<Folder Include="Users\Common\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace Identity.Application.Users.Common;
namespace Identity.Application.Users.Common;

public sealed record UserInfo(
public sealed record UserDto(
Guid UserId,
string Email,
string FirstName,
string LastName,
bool IsEmailConfirmed,
string Role,
string Gender);
string Gender);
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public Task<Result<GetRolesQueryResponse>> Handle(GetRolesQuery request, Cancell
{
var roles = Role.GetNames();
var response = Result.Success(new GetRolesQueryResponse(roles));

return Task.FromResult(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Identity.Application.Users.Queries.GetUserInfoById;

internal sealed class GetUserByIdQueryHandler(IUserRepository userRepository)
: IQueryHandler<GetUserInfoByIdQuery, UserDto>
{
private readonly IUserRepository _userRepository = userRepository;

public async Task<Result<UserDto>> Handle(GetUserInfoByIdQuery request, CancellationToken cancellationToken)
{
var userId = new UserId(request.Id);

var user = await _userRepository.GetUserByIdAsync(userId, cancellationToken);

if (user is null)
{
return Result.Failure<UserDto>(
UserErrors.UserDoesNotExist);
}

var userInfo = new UserDto(
user.Id.Value,
user.Email.Value,
user.FirstName.Value,
user.LastName.Value,
user.IsEmailConfirmed,
user.Role.Name,
user.Gender.Name);

return Result.Success(userInfo);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Identity.Application.Users.Queries.GetUserInfoById;

public sealed record GetUserInfoByIdQuery(Guid Id) : IQuery<UserInfo>;
public sealed record GetUserInfoByIdQuery(Guid Id) : IQuery<UserDto>;

Loading

0 comments on commit f9f2eb9

Please sign in to comment.