From ebb2e6e7fc44383cdc4855b2a5ce5ea7b61f628f Mon Sep 17 00:00:00 2001 From: gamer147 Date: Mon, 29 Dec 2025 14:33:08 -0500 Subject: [PATCH] [FA-55] User service should be done --- .../UserManagementServiceTests.cs | 7 +++++-- .../IntegrationEvents/UserInvitedEvent.cs | 17 +++++++++++++++++ .../Services/UserManagementService.cs | 18 +++++++++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 FictionArchive.Service.UserService/Models/IntegrationEvents/UserInvitedEvent.cs diff --git a/FictionArchive.Service.UserService.Tests/UserManagementServiceTests.cs b/FictionArchive.Service.UserService.Tests/UserManagementServiceTests.cs index e9f8dfd..c7c804d 100644 --- a/FictionArchive.Service.UserService.Tests/UserManagementServiceTests.cs +++ b/FictionArchive.Service.UserService.Tests/UserManagementServiceTests.cs @@ -1,3 +1,4 @@ +using FictionArchive.Service.Shared.Services.EventBus; using FictionArchive.Service.UserService.Models.Database; using FictionArchive.Service.UserService.Services; using FictionArchive.Service.UserService.Services.AuthenticationClient; @@ -25,12 +26,14 @@ public class UserManagementServiceTests private static UserManagementService CreateService( UserServiceDbContext dbContext, - IAuthenticationServiceClient authClient) + IAuthenticationServiceClient authClient, + IEventBus? eventBus = null) { return new UserManagementService( dbContext, NullLogger.Instance, - authClient); + authClient, + eventBus ?? Substitute.For()); } private static User CreateTestUser(string username, string email, int availableInvites = 5) diff --git a/FictionArchive.Service.UserService/Models/IntegrationEvents/UserInvitedEvent.cs b/FictionArchive.Service.UserService/Models/IntegrationEvents/UserInvitedEvent.cs new file mode 100644 index 0000000..5039e75 --- /dev/null +++ b/FictionArchive.Service.UserService/Models/IntegrationEvents/UserInvitedEvent.cs @@ -0,0 +1,17 @@ +using FictionArchive.Service.Shared.Services.EventBus; + +namespace FictionArchive.Service.UserService.Models.IntegrationEvents; + +public class UserInvitedEvent : IIntegrationEvent +{ + // Invited user info + public Guid InvitedUserId { get; set; } + public required string InvitedUsername { get; set; } + public required string InvitedEmail { get; set; } + public required string InvitedOAuthProviderId { get; set; } + + // Inviter info + public Guid InviterId { get; set; } + public required string InviterUsername { get; set; } + public required string InviterOAuthProviderId { get; set; } +} diff --git a/FictionArchive.Service.UserService/Services/UserManagementService.cs b/FictionArchive.Service.UserService/Services/UserManagementService.cs index a6b409a..9e7a6cb 100644 --- a/FictionArchive.Service.UserService/Services/UserManagementService.cs +++ b/FictionArchive.Service.UserService/Services/UserManagementService.cs @@ -1,4 +1,6 @@ +using FictionArchive.Service.Shared.Services.EventBus; using FictionArchive.Service.UserService.Models.Database; +using FictionArchive.Service.UserService.Models.IntegrationEvents; using FictionArchive.Service.UserService.Services.AuthenticationClient; using Microsoft.EntityFrameworkCore; @@ -9,15 +11,18 @@ public class UserManagementService private readonly ILogger _logger; private readonly UserServiceDbContext _dbContext; private readonly IAuthenticationServiceClient _authClient; + private readonly IEventBus _eventBus; public UserManagementService( UserServiceDbContext dbContext, ILogger logger, - IAuthenticationServiceClient authClient) + IAuthenticationServiceClient authClient, + IEventBus eventBus) { _dbContext = dbContext; _logger = logger; _authClient = authClient; + _eventBus = eventBus; } /// @@ -94,6 +99,17 @@ public class UserManagementService await _dbContext.SaveChangesAsync(); + await _eventBus.Publish(new UserInvitedEvent + { + InvitedUserId = newUser.Id, + InvitedUsername = newUser.Username, + InvitedEmail = newUser.Email, + InvitedOAuthProviderId = newUser.OAuthProviderId, + InviterId = inviter.Id, + InviterUsername = inviter.Username, + InviterOAuthProviderId = inviter.OAuthProviderId + }); + _logger.LogInformation( "User {Username} was successfully invited by {InviterId}. New user id: {NewUserId}", username, inviter.Id, newUser.Id);