Skip to content

Commit

Permalink
Refactored domain events and added user agent to messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nmakhmutov committed Aug 5, 2023
1 parent 480ac5a commit dbc92f6
Show file tree
Hide file tree
Showing 20 changed files with 140 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net;
using MediatR;
using Microsoft.EntityFrameworkCore;
using People.Api.Application.IntegrationEvents.Events;
Expand All @@ -10,7 +11,8 @@

namespace People.Api.Application.Commands.SignInByEmail;

internal sealed record SignInByEmailCommand(string Token, string Code) : IRequest<SignInResult>;
internal sealed record SignInByEmailCommand(string Token, string Code, IPAddress Ip, string? UserAgent) :
IRequest<SignInResult>;

internal sealed class SignInByEmailCommandHandler : IRequestHandler<SignInByEmailCommand, SignInResult>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace People.Api.Application.Commands.SignInByGoogle;

internal sealed record SignInByGoogleCommand(string Token, IPAddress Ip) : IRequest<SignInResult>;
internal sealed record SignInByGoogleCommand(string Token, IPAddress Ip, string? UserAgent) : IRequest<SignInResult>;

internal sealed class SignInByGoogleCommandHandler : IRequestHandler<SignInByGoogleCommand, SignInResult>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace People.Api.Application.Commands.SignInByMicrosoft;

internal sealed record SignInByMicrosoftCommand(string Token, IPAddress Ip) : IRequest<SignInResult>;
internal sealed record SignInByMicrosoftCommand(string Token, IPAddress Ip, string? UserAgent) : IRequest<SignInResult>;

internal sealed class SignInByMicrosoftCommandHandler : IRequestHandler<SignInByMicrosoftCommand, SignInResult>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net;
using MediatR;
using People.Api.Application.Models;
using People.Domain.Exceptions;
Expand All @@ -6,7 +7,8 @@

namespace People.Api.Application.Commands.SignUpByEmail;

internal sealed record SignUpByEmailCommand(string Token, string Code) : IRequest<SignUpResult>;
internal sealed record SignUpByEmailCommand(string Token, string Code, IPAddress Ip, string? UserAgent) :
IRequest<SignUpResult>;

internal sealed class SignUpByEmailCommandHandler : IRequestHandler<SignUpByEmailCommand, SignUpResult>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace People.Api.Application.Commands.SignUpByGoogle;

internal sealed record SignUpByGoogleCommand(string Token, Language Language, IPAddress Ip) : IRequest<SignUpResult>;
internal sealed record SignUpByGoogleCommand(string Token, Language Language, IPAddress Ip, string? UserAgent) :
IRequest<SignUpResult>;

internal sealed class SignUpByGoogleCommandHandler : IRequestHandler<SignUpByGoogleCommand, SignUpResult>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace People.Api.Application.Commands.SignUpByMicrosoft;

internal sealed record SignUpByMicrosoftCommand(string Token, Language Language, IPAddress Ip) : IRequest<SignUpResult>;
internal sealed record SignUpByMicrosoftCommand(string Token, Language Language, IPAddress Ip, string? UserAgent) :
IRequest<SignUpResult>;

internal sealed class SignUpByMicrosoftCommandHandler : IRequestHandler<SignUpByMicrosoftCommand, SignUpResult>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

namespace People.Api.Application.Commands.SigningUpByEmail;

internal sealed record SigningUpByEmailCommand(MailAddress Email, Language Language, IPAddress Ip) : IRequest<string>;
internal sealed record SigningUpByEmailCommand(MailAddress Email, Language Language, IPAddress Ip, string? UserAgent) :
IRequest<string>;

internal sealed class SigningUpByEmailCommandHandler : IRequestHandler<SigningUpByEmailCommand, string>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ public AccountUpdatedDomainEventHandler(IIntegrationEventBus bus, TimeProvider t

public Task Handle(AccountUpdatedDomainEvent notification, CancellationToken ct)
{
var evt = new AccountUpdatedIntegrationEvent(
Guid.NewGuid(),
_timeProvider.UtcNow(),
notification.Account.Id
);
var evt = new AccountUpdatedIntegrationEvent(Guid.NewGuid(), _timeProvider.UtcNow(), notification.Id);

return _bus.PublishAsync(evt, ct);
}
Expand Down
55 changes: 41 additions & 14 deletions src/People.Api/Grpc/PeopleService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Net.Mail;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using MediatR;
Expand All @@ -14,7 +13,6 @@
using People.Api.Application.Commands.SignUpByMicrosoft;
using People.Api.Application.Queries.GetAccountSummary;
using People.Api.Application.Queries.IsAccountActive;
using People.Domain.ValueObjects;
using People.Grpc.People;

namespace People.Api.Grpc;
Expand Down Expand Up @@ -46,9 +44,10 @@ public override async Task<EmailSigningUpReply> SigningUpByEmail(EmailSigningUpR
ServerCallContext context)
{
var command = new SigningUpByEmailCommand(
new MailAddress(request.Email),
Language.Parse(request.Language),
request.Ip.ToIpAddress()
request.Email.ToMailAddress(),
request.Language.ToLanguage(),
request.Ip.ToIpAddress(),
request.UserAgent.GetValue()
);

var token = await _mediator.Send(command, context.CancellationToken);
Expand All @@ -58,7 +57,13 @@ public override async Task<EmailSigningUpReply> SigningUpByEmail(EmailSigningUpR

public override async Task<SignUpReply> SignUpByEmail(EmailSignUpRequest request, ServerCallContext context)
{
var command = new SignUpByEmailCommand(request.Token, request.Code);
var command = new SignUpByEmailCommand(
request.Token,
request.Code,
request.Ip.ToIpAddress(),
request.UserAgent.GetValue()
);

var result = await _mediator.Send(command, context.CancellationToken);

return SignUpReply.Map(result);
Expand All @@ -67,15 +72,25 @@ public override async Task<SignUpReply> SignUpByEmail(EmailSignUpRequest request
public override async Task<EmailSigningInReply> SigningInByEmail(EmailSigningInRequest request,
ServerCallContext context)
{
var command = new SigningInByEmailCommand(new MailAddress(request.Email), Language.Parse(request.Language));
var command = new SigningInByEmailCommand(
request.Email.ToMailAddress(),
request.Language.ToLanguage()
);

var token = await _mediator.Send(command, context.CancellationToken);

return EmailSigningInReply.Map(token);
}

public override async Task<SignInReply> SignInByEmail(EmailSignInRequest request, ServerCallContext context)
{
var command = new SignInByEmailCommand(request.Token, request.Code);
var command = new SignInByEmailCommand(
request.Token,
request.Code,
request.Ip.ToIpAddress(),
request.UserAgent.GetValue()
);

var result = await _mediator.Send(command, context.CancellationToken);

return SignInReply.Map(result);
Expand All @@ -85,8 +100,9 @@ public override async Task<SignUpReply> SignUpByGoogle(ExternalSignUpRequest req
{
var command = new SignUpByGoogleCommand(
request.AccessToken,
Language.Parse(request.Language),
request.Ip.ToIpAddress()
request.Language.ToLanguage(),
request.Ip.ToIpAddress(),
request.UserAgent.GetValue()
);

var result = await _mediator.Send(command, context.CancellationToken);
Expand All @@ -96,7 +112,12 @@ public override async Task<SignUpReply> SignUpByGoogle(ExternalSignUpRequest req

public override async Task<SignInReply> SignInByGoogle(ExternalSignInRequest request, ServerCallContext context)
{
var command = new SignInByGoogleCommand(request.AccessToken, request.Ip.ToIpAddress());
var command = new SignInByGoogleCommand(
request.AccessToken,
request.Ip.ToIpAddress(),
request.UserAgent.GetValue()
);

var result = await _mediator.Send(command, context.CancellationToken);

return SignInReply.Map(result);
Expand All @@ -114,8 +135,9 @@ public override async Task<SignUpReply> SignUpByMicrosoft(ExternalSignUpRequest
{
var command = new SignUpByMicrosoftCommand(
request.AccessToken,
Language.Parse(request.Language),
request.Ip.ToIpAddress()
request.Language.ToLanguage(),
request.Ip.ToIpAddress(),
request.UserAgent.GetValue()
);

var result = await _mediator.Send(command, context.CancellationToken);
Expand All @@ -125,7 +147,12 @@ public override async Task<SignUpReply> SignUpByMicrosoft(ExternalSignUpRequest

public override async Task<SignInReply> SignInByMicrosoft(ExternalSignInRequest request, ServerCallContext context)
{
var command = new SignInByMicrosoftCommand(request.AccessToken, request.Ip.ToIpAddress());
var command = new SignInByMicrosoftCommand(
request.AccessToken,
request.Ip.ToIpAddress(),
request.UserAgent.GetValue()
);

var result = await _mediator.Send(command, context.CancellationToken);

return SignInReply.Map(result);
Expand Down
2 changes: 1 addition & 1 deletion src/People.Api/Messages/AccountReply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static AccountReply Map(AccountSummary account) =>
Picture = account.Picture,
CountryCode = account.CountryCode.ToString(),
TimeZone = account.TimeZone.ToString(),
Language = account.Language.ToString(),
Language = Language.Create(account.Language),
Ban = Types.Ban.Map(account.Ban),
Roles = { account.Roles }
};
Expand Down
11 changes: 11 additions & 0 deletions src/People.Api/Messages/Email.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Net.Mail;

// ReSharper disable CheckNamespace

namespace People.Grpc.People;

public partial class Email
{
public MailAddress ToMailAddress() =>
new(Value);
}
15 changes: 15 additions & 0 deletions src/People.Api/Messages/Language.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// ReSharper disable CheckNamespace

namespace People.Grpc.People;

public partial class Language
{
public Domain.ValueObjects.Language ToLanguage() =>
Domain.ValueObjects.Language.Parse(Value);

public static Language Create(Domain.ValueObjects.Language language) =>
new()
{
Value = language.ToString()
};
}
9 changes: 9 additions & 0 deletions src/People.Api/Messages/UserAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// ReSharper disable CheckNamespace

namespace People.Grpc.People;

public partial class UserAgent
{
public string? GetValue() =>
string.IsNullOrWhiteSpace(Value) ? null : Value;
}
2 changes: 2 additions & 0 deletions src/People.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Net;
using System.Text.Json;
using System.Text.Json.Serialization;
using FluentValidation;
Expand Down Expand Up @@ -230,6 +231,7 @@
.Destructure.AsScalar<TimeZone>()
.Destructure.AsScalar<DateFormat>()
.Destructure.AsScalar<TimeFormat>()
.Destructure.AsScalar<IPAddress>()
.Destructure.ByTransforming<Account>(x => new { x.Id, x.CountryCode, x.Name.Nickname })
.Destructure.ByTransforming<AccountSummary>(x => new { x.Id, x.CountryCode, x.Name.Nickname })
.ReadFrom.Configuration(context.Configuration)
Expand Down
31 changes: 25 additions & 6 deletions src/People.Api/Protos/people.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ message IpAddress {
string value = 1;
}

message UserAgent {
string value = 1;
}

message Language {
string value = 1;
}

message Email {
string value = 1;
}

message AccountRequest {
int64 id = 1;
}
Expand Down Expand Up @@ -57,15 +69,16 @@ message AccountReply {
string picture = 6;
string country_code = 7;
string time_zone = 8;
string language = 9;
Language language = 9;
Ban ban = 10;
repeated string roles = 11;
}

message EmailSigningUpRequest {
string email = 1;
string language = 2;
Email email = 1;
Language language = 2;
IpAddress ip = 3;
UserAgent user_agent = 4;
}

message EmailSigningUpReply {
Expand All @@ -75,11 +88,13 @@ message EmailSigningUpReply {
message EmailSignUpRequest {
string token = 1;
string code = 2;
IpAddress ip = 3;
UserAgent user_agent = 4;
}

message EmailSigningInRequest {
string email = 1;
string language = 2;
Email email = 1;
Language language = 2;
}

message EmailSigningInReply {
Expand All @@ -89,17 +104,21 @@ message EmailSigningInReply {
message EmailSignInRequest {
string token = 1;
string code = 2;
IpAddress ip = 3;
UserAgent user_agent = 4;
}

message ExternalSignInRequest {
string access_token = 1;
IpAddress ip = 2;
UserAgent user_agent = 3;
}

message ExternalSignUpRequest {
string access_token = 1;
string language = 2;
Language language = 2;
IpAddress ip = 3;
UserAgent user_agent = 4;
}

message ExternalAppendRequest {
Expand Down
2 changes: 1 addition & 1 deletion src/People.Domain/DomainEvents/AccountBannedDomainEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

namespace People.Domain.DomainEvents;

public sealed record AccountBannedDomainEvent(Account Account, string Reason, DateTime ExpiredAt) : INotification;
public sealed record AccountBannedDomainEvent(AccountId Id, string Reason, DateTime ExpiredAt) : INotification;
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

namespace People.Domain.DomainEvents;

public sealed record AccountUnbannedDomainEvent(Account Account) : INotification;
public sealed record AccountUnbannedDomainEvent(AccountId Id) : INotification;
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

namespace People.Domain.DomainEvents;

public sealed record AccountUpdatedDomainEvent(Account Account) : INotification;
public sealed record AccountUpdatedDomainEvent(AccountId Id) : INotification;
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

namespace People.Domain.DomainEvents;

public sealed record EmailConfirmedDomainEvent(Account Account, MailAddress Email) : INotification;
public sealed record EmailConfirmedDomainEvent(AccountId Id, MailAddress Email) : INotification;
Loading

0 comments on commit dbc92f6

Please sign in to comment.