Skip to content

Commit

Permalink
TierQuotaDefinitions are not deleted (#330)
Browse files Browse the repository at this point in the history
* feat: remove to delete tierQuotas. Depend on db cascade delete

* fix: bad param to metricStatusesService.RecalculateMetricStatuses

* feat: add migration for MS SQL Server

* feat: add migration for Postgres

* refactor: fix formatting

* refactor: fix naming violation

* refactor: fix naming violation

* fix: irrelevant tests must be removed

* fix: bad entityTypeConfig

* fix: not erroring when deleting a non-existing TQD

* refactor: rename variable

* fix: restore old DeleteTierQuotaDefinition handler and tests

* fix: space in file name

* refactor: remove needless RemoveTierQuotaDefinitionById method

* refactor: extract event handler body to private method

* refactor: use tierId instead of request.tierId

* refactor: no need to set definition as internal

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Dannyps and github-actions[bot] authored Oct 17, 2023
1 parent aa8dc1c commit cf8f081
Show file tree
Hide file tree
Showing 16 changed files with 933 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Backbone.Modules.Quotas.Application.Infrastructure.Persistence.Repository;
using Backbone.Modules.Quotas.Application.IntegrationEvents.Outgoing;
using Backbone.Modules.Quotas.Application.Metrics;
using Backbone.Modules.Quotas.Domain.Aggregates.Metrics;
using Backbone.Modules.Quotas.Domain.Aggregates.Tiers;
using Enmeshed.BuildingBlocks.Application.Abstractions.Infrastructure.EventBus;
using Microsoft.Extensions.Logging;
Expand All @@ -8,38 +10,30 @@ namespace Backbone.Modules.Quotas.Application.IntegrationEvents.Incoming.TierQuo
public class TierQuotaDefinitionDeletedIntegrationEventHandler : IIntegrationEventHandler<TierQuotaDefinitionDeletedIntegrationEvent>
{
private readonly IIdentitiesRepository _identitiesRepository;
private readonly ITiersRepository _tiersRepository;
private readonly IMetricStatusesService _metricStatusesService;
private readonly ILogger<TierQuotaDefinitionDeletedIntegrationEventHandler> _logger;

public TierQuotaDefinitionDeletedIntegrationEventHandler(IIdentitiesRepository identitiesRepository,
ITiersRepository tiersRepository, ILogger<TierQuotaDefinitionDeletedIntegrationEventHandler> logger)
public TierQuotaDefinitionDeletedIntegrationEventHandler(IIdentitiesRepository identitiesRepository, IMetricStatusesService metricStatusesService, ILogger<TierQuotaDefinitionDeletedIntegrationEventHandler> logger)
{
_metricStatusesService = metricStatusesService;
_identitiesRepository = identitiesRepository;
_tiersRepository = tiersRepository;
_logger = logger;
}

public async Task Handle(TierQuotaDefinitionDeletedIntegrationEvent @event)
{
_logger.LogTrace("Handling '{eventName}' ... ", nameof(TierQuotaDefinitionDeletedIntegrationEvent));
await RecalculateMetricStatuses(@event);
}

var identitiesWithTier = await _identitiesRepository.FindWithTier(new TierId(@event.TierId), CancellationToken.None, true);

if (!identitiesWithTier.Any())
{
_logger.LogTrace("No identities found with tier ID: '{tierId}'", @event.TierId);
return;
}

var tierQuotaDefinition = await _tiersRepository.FindTierQuotaDefinition(@event.TierQuotaDefinitionId, CancellationToken.None, true);

foreach (var identity in identitiesWithTier)
{
identity.DeleteTierQuotaFromDefinitionId(tierQuotaDefinition.Id);
}

await _identitiesRepository.Update(identitiesWithTier, CancellationToken.None);
private async Task RecalculateMetricStatuses(TierQuotaDefinitionDeletedIntegrationEvent @event)
{
var identitiesWithTier = await _identitiesRepository.FindWithTier(new TierId(@event.TierId), CancellationToken.None);

_logger.LogTrace("Successfully deleted quotas for Identities.");
await _metricStatusesService.RecalculateMetricStatuses(
identitiesWithTier.Select(i => i.Address).ToList(),
MetricKey.GetSupportedMetricKeyValues().ToList(),
CancellationToken.None
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public Handler(ITiersRepository tiersRepository, ILogger<Handler> logger, IEvent

public async Task Handle(DeleteTierQuotaDefinitionCommand request, CancellationToken cancellationToken)
{
_logger.LogTrace("Deleting tier quota definition with id: '{tierQuotaDefinitionId}'.", request.TierQuotaDefinitionId);

var tier = await _tiersRepository.Find(request.TierId, cancellationToken, true) ?? throw new NotFoundException(nameof(Tier));

var result = tier.DeleteQuota(request.TierQuotaDefinitionId);
Expand Down
1 change: 1 addition & 0 deletions Modules/Quotas/src/Quotas.Domain/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

[assembly: InternalsVisibleTo("Backbone.Modules.Quotas.Domain.Tests")]
[assembly: InternalsVisibleTo("Backbone.Modules.Quotas.Application.Tests")]
[assembly: InternalsVisibleTo("Backbone.Modules.Quotas.Infrastructure")]
Loading

0 comments on commit cf8f081

Please sign in to comment.