diff --git a/Source/Modules/TenantIdentity/Features/Aggregates/TenantAggregate/Application/Commands/CreateTenantWithAdmin.cs b/Source/Modules/TenantIdentity/Features/Aggregates/TenantAggregate/Application/Commands/CreateTenantWithAdmin.cs index e8d143cb..8782c1ab 100644 --- a/Source/Modules/TenantIdentity/Features/Aggregates/TenantAggregate/Application/Commands/CreateTenantWithAdmin.cs +++ b/Source/Modules/TenantIdentity/Features/Aggregates/TenantAggregate/Application/Commands/CreateTenantWithAdmin.cs @@ -22,7 +22,7 @@ public CreateTenantWithAdminCommandHandler(TenantIdentityDbContext tenantIdentit public async Task HandleAsync(CreateTenantWithAdmin createTenant, CancellationToken cancellationToken) { - var tenant = await Tenant.CreateTenantWithAdminAsync(createTenant.Name, Guid.Empty); + var tenant = Tenant.CreateTenantWithAdmin(createTenant.Name, Guid.Empty); tenantIdentityDbContext.Tenants.Add(tenant); await tenantIdentityDbContext.SaveChangesAsync(cancellationToken); diff --git a/Source/Modules/TenantIdentity/Features/Aggregates/TenantAggregate/Domain/Tenant.cs b/Source/Modules/TenantIdentity/Features/Aggregates/TenantAggregate/Domain/Tenant.cs index 57081397..df64141f 100644 --- a/Source/Modules/TenantIdentity/Features/Aggregates/TenantAggregate/Domain/Tenant.cs +++ b/Source/Modules/TenantIdentity/Features/Aggregates/TenantAggregate/Domain/Tenant.cs @@ -3,6 +3,7 @@ using Shared.Features.Domain; using Shared.Features.Domain.Exceptions; using Shared.Kernel.BuildingBlocks.Auth; +using StackExchange.Redis; namespace Modules.TenantIdentity.Features.Aggregates.TenantAggregate.Domain { @@ -22,22 +23,26 @@ public Tenant() public IReadOnlyCollection Invitations => invitations.AsReadOnly(); private List invitations = new List(); - public static async Task CreateTenantWithAdminAsync(string name, Guid adminUserId) + public static Tenant CreateTenantWithAdmin(string tenantName, Guid adminUserId) { return new Tenant { - - + Name = tenantName, + memberships = new List + { + new TenantMembership(adminUserId, TenantRole.Admin) + } }; } public void AddUser(Guid userId, TenantRole role) { + ThrowIfCallerIsNotInRole(TenantRole.Admin); TenantMembership tenantMembership; if ((tenantMembership = memberships.SingleOrDefault(m => m.UserId == userId)) is not null) { - tenantMembership.Role = role; + throw new DomainException(""); } else { @@ -45,11 +50,6 @@ public void AddUser(Guid userId, TenantRole role) } } - public void ChangeRoleOfUser(Guid userId, TenantRole role) - { - - } - public void ChangeRoleOfMember(Guid userId, TenantRole newRole) { ThrowIfCallerIsNotInRole(TenantRole.Admin); @@ -58,6 +58,9 @@ public void ChangeRoleOfMember(Guid userId, TenantRole newRole) { throw new MemberNotFoundException(); } + + TenantMembership tenantMembership = memberships.Single(m => m.UserId == userId); + tenantMembership.Role = newRole; } public void RemoveUser(Guid userId) @@ -72,25 +75,29 @@ public void RemoveUser(Guid userId) memberships.Remove(memberships.Single(m => m.UserId == userId)); } - public void InviteUserToRole(Guid userId, TenantRole role) + public void InviteUserToRole(string email, TenantRole role) { ThrowIfCallerIsNotInRole(TenantRole.Admin); - if (CheckIfMember(userId)) + if (invitations.Any(invitation => invitation.Email == email)) { - throw new UserIsAlreadyMemberException(); + throw new DomainException(""); } - //invitations.Add(new TenantInvitation { UserId = userId, Role = role }); + invitations.Add(new TenantInvitation { Email = email, Role = role }); } - public async void DeleteTenantMembership(Guid membershipId) + public void DeleteTenantMembership(Guid membershipId) { + ThrowIfCallerIsNotInRole(TenantRole.Admin); + var tenantMembership = Memberships.SingleOrDefault(t => t.Id == membershipId); if (tenantMembership == null) { throw new NotFoundException(); } + + memberships.Remove(tenantMembership); } public bool CheckIfMember(Guid userId) diff --git a/Source/Modules/TenantIdentity/Features/Aggregates/TenantAggregate/Domain/TenantInvitation.cs b/Source/Modules/TenantIdentity/Features/Aggregates/TenantAggregate/Domain/TenantInvitation.cs index b0f67e19..056bfa50 100644 --- a/Source/Modules/TenantIdentity/Features/Aggregates/TenantAggregate/Domain/TenantInvitation.cs +++ b/Source/Modules/TenantIdentity/Features/Aggregates/TenantAggregate/Domain/TenantInvitation.cs @@ -8,8 +8,7 @@ public class TenantInvitation : Entity { public Guid TenantId { get; set; } public Tenant Tenant { get; set; } - public Guid UserId { get; set; } - public ApplicationUser User { get; set; } + public string Email { get; set; } public TenantRole Role { get; set; } } }