Skip to content

Commit

Permalink
Tenant Domain logic
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidEggenberger committed Feb 21, 2024
1 parent d201593 commit 7b27863
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public CreateTenantWithAdminCommandHandler(TenantIdentityDbContext tenantIdentit

public async Task<TenantDTO> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -22,34 +23,33 @@ public Tenant()
public IReadOnlyCollection<TenantInvitation> Invitations => invitations.AsReadOnly();
private List<TenantInvitation> invitations = new List<TenantInvitation>();

public static async Task<Tenant> CreateTenantWithAdminAsync(string name, Guid adminUserId)
public static Tenant CreateTenantWithAdmin(string tenantName, Guid adminUserId)
{
return new Tenant
{


Name = tenantName,
memberships = new List<TenantMembership>
{
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
{
memberships.Add(new TenantMembership(userId, role));
}
}

public void ChangeRoleOfUser(Guid userId, TenantRole role)
{

}

public void ChangeRoleOfMember(Guid userId, TenantRole newRole)
{
ThrowIfCallerIsNotInRole(TenantRole.Admin);
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}

0 comments on commit 7b27863

Please sign in to comment.