Skip to content

Commit

Permalink
(#87) add endpoint for removing organization
Browse files Browse the repository at this point in the history
  • Loading branch information
eggwhat committed May 25, 2024
1 parent 1833ea8 commit 1a8e0ec
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static async Task Main(string[] args)
afterDispatch: (cmd, ctx) => ctx.Response.Created($"organizations/root"))
.Post<CreateOrganization>("organizations/{organizationId}/children",
afterDispatch: (cmd, ctx) => ctx.Response.Created($"organizations/{cmd.OrganizationId}"))
.Delete<DeleteOrganization>("organizations/{organizationId}")
.Post<AddOrganizerToOrganization>("organizations/{organizationId}/organizer")
.Delete<RemoveOrganizerFromOrganization>("organizations/{organizationId}/organizer/{organizerId}")
))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Convey.CQRS.Commands;

namespace MiniSpace.Services.Organizations.Application.Commands
{
public class DeleteOrganization: ICommand
{
public Guid OrganizationId { get; set; }
public Guid RootId { get; }

public DeleteOrganization(Guid organizationId, Guid rootId)
{
OrganizationId = organizationId;
RootId = rootId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Convey.CQRS.Commands;
using MiniSpace.Services.Organizations.Application.Events;
using MiniSpace.Services.Organizations.Application.Exceptions;
using MiniSpace.Services.Organizations.Application.Services;
using MiniSpace.Services.Organizations.Core.Repositories;

namespace MiniSpace.Services.Organizations.Application.Commands.Handlers
{
public class DeleteOrganizationHandler:ICommandHandler<DeleteOrganization>
{
private readonly IOrganizationRepository _organizationRepository;
private readonly IAppContext _appContext;
private readonly IMessageBroker _messageBroker;

public DeleteOrganizationHandler(IOrganizationRepository organizationRepository, IAppContext appContext, IMessageBroker messageBroker)
{
_organizationRepository = organizationRepository;
_appContext = appContext;
_messageBroker = messageBroker;
}

public async Task HandleAsync(DeleteOrganization command, CancellationToken cancellationToken)
{
var identity = _appContext.Identity;
if(identity.IsAuthenticated && !identity.IsAdmin)
{
throw new Exceptions.UnauthorizedAccessException("admin");
}

var root = await _organizationRepository.GetAsync(command.RootId);
if(root is null)
{
throw new RootOrganizationNotFoundException(command.RootId);
}

var organization = root.GetSubOrganization(command.OrganizationId);
if(organization is null)
{
throw new OrganizationNotFoundException(command.OrganizationId);
}

if (root.Id.Equals(organization.Id))
{
await _organizationRepository.DeleteAsync(root.Id);
}
else
{
root.RemoveChildOrganization(organization);
await _organizationRepository.UpdateAsync(root);
}

await _messageBroker.PublishAsync(new OrganizationDeleted(organization.Id));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Convey.CQRS.Events;

namespace MiniSpace.Services.Organizations.Application.Events
{
public class OrganizationDeleted:IEvent
{
public Guid OrganizationId { get; }

public OrganizationDeleted(Guid organizationId)
{
OrganizationId = organizationId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,34 @@ private static void FindAllChildrenOrganizationsRecursive(Organization currentOr
FindAllChildrenOrganizationsRecursive(subOrg, organizations);
}
}

private Organization GetParentOrganization(Guid id)
{
foreach (var subOrg in SubOrganizations)
{
if (subOrg.Id == id)
{
return this;
}

var result = subOrg.GetParentOrganization(id);
if (result != null)
{
return result;
}
}

return null;
}

public void RemoveChildOrganization(Organization organization)
{
var parent = GetParentOrganization(organization.Id);
if(parent is null)
{
throw new ParentOfOrganizationNotFoundException(organization.Id);
}
parent._subOrganizations.Remove(organization);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace MiniSpace.Services.Organizations.Core.Exceptions
{
public class ParentOfOrganizationNotFoundException : DomainException
{
public override string Code { get; } = "parent_of_organization_not_found";
public Guid ChildId { get; }

public ParentOfOrganizationNotFoundException(Guid childId) : base(
$"Parent organization was not found for child organization with ID: '{childId}'.")
{
ChildId = childId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public static IApplicationBuilder UseInfrastructure(this IApplicationBuilder app
.UseCertificateAuthentication()
.UseRabbitMq()
.SubscribeCommand<CreateOrganization>()
.SubscribeCommand<DeleteOrganization>()
.SubscribeCommand<AddOrganizerToOrganization>()
.SubscribeCommand<RemoveOrganizerFromOrganization>()
.SubscribeEvent<OrganizerRightsGranted>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ private static IReadOnlyDictionary<Type, HandlerLogTemplate> MessageTemplates
After = "Added a new child organization with id: {OrganizationId} for parent with id: {ParentId}."
}
},
{
typeof(DeleteOrganization), new HandlerLogTemplate
{
After = "Deleted an organization with id: {OrganizationId} and its children."
}
},
{
typeof(AddOrganizerToOrganization), new HandlerLogTemplate
{
Expand Down

0 comments on commit 1a8e0ec

Please sign in to comment.