Skip to content

Commit

Permalink
Fix - Suppression de la vérification de déclaration pour les producers
Browse files Browse the repository at this point in the history
  • Loading branch information
noelmugnier committed Nov 30, 2020
1 parent a605644 commit efef745
Show file tree
Hide file tree
Showing 10 changed files with 3,317 additions and 116 deletions.

This file was deleted.

39 changes: 1 addition & 38 deletions Sheaft.Application.Handlers/Commands/LegalCommandsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
using System.Threading.Tasks;
using System.Threading;
using Sheaft.Domain.Models;
using Sheaft.Domain.Enums;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Sheaft.Application.Events;

namespace Sheaft.Application.Handlers
{
Expand All @@ -20,8 +16,7 @@ public class LegalCommandsHandler : ResultsHandler,
IRequestHandler<UpdateBusinessLegalCommand, Result<bool>>,
IRequestHandler<UpdateConsumerLegalCommand, Result<bool>>,
IRequestHandler<CheckBusinessLegalConfigurationCommand, Result<bool>>,
IRequestHandler<CheckConsumerLegalConfigurationCommand, Result<bool>>,
IRequestHandler<CheckLegalsDeclarationRequiredCommand, Result<bool>>
IRequestHandler<CheckConsumerLegalConfigurationCommand, Result<bool>>
{
private readonly IPspService _pspService;

Expand Down Expand Up @@ -257,37 +252,5 @@ public async Task<Result<bool>> Handle(CheckConsumerLegalConfigurationCommand re
return Ok(true);
});
}

public async Task<Result<bool>> Handle(CheckLegalsDeclarationRequiredCommand request, CancellationToken token)
{
return await ExecuteAsync(request, async () =>
{
var legal = await _context.GetSingleAsync<BusinessLegal>(b => b.User.Id == request.UserId, token);
if (legal.User.Kind != ProfileKind.Producer)
return Ok(false);
if (legal.DeclarationRequired)
return Ok(legal.DeclarationRequired);
var currentMonth = DateTimeOffset.UtcNow.Month;
var cumulatedMonthAmount = await _context.PurchaseOrders
.Get(po =>
po.Vendor.Id == request.UserId &&
po.Sender.Kind == ProfileKind.Consumer &&
po.Status == PurchaseOrderStatus.Delivered &&
po.DeliveredOn.HasValue &&
po.DeliveredOn.Value.Month == currentMonth)
.SumAsync(po => po.TotalOnSalePrice, token);
if (cumulatedMonthAmount >= 150)
{
legal.SetDeclarationRequired(true);
await _context.SaveChangesAsync(token);
_mediatr.Post(new ProducerDeclarationRequiredEvent(request.RequestUser) { ProducerId = request.UserId });
}
return Ok(legal.DeclarationRequired);
});
}
}
}
33 changes: 15 additions & 18 deletions Sheaft.Application.Handlers/Commands/PayoutCommandsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ public async Task<Result<Guid>> Handle(CreatePayoutCommand request, Cancellation
if (!checkConfigurationResult.Success)
return Failed<Guid>(checkConfigurationResult.Exception);
var producerLegals = await _context.GetSingleAsync<BusinessLegal>(c => c.User.Id == request.ProducerId, token);
if (producerLegals.DeclarationRequired)
{
var checkDocumentsResult = await _mediatr.Process(new EnsureProducerDocumentsValidatedCommand(request.RequestUser) { ProducerId = request.ProducerId }, token);
if (!checkDocumentsResult.Success)
return Failed<Guid>(checkDocumentsResult.Exception);
var producerLegals = await _context.GetSingleAsync<BusinessLegal>(c => c.User.Id == request.ProducerId, token);
var checkDocumentsResult = await _mediatr.Process(new EnsureProducerDocumentsValidatedCommand(request.RequestUser) { ProducerId = request.ProducerId }, token);
if (!checkDocumentsResult.Success)
return Failed<Guid>(checkDocumentsResult.Exception);
if (producerLegals.Kind == LegalKind.Business)
{
var checkDeclarationResult = await _mediatr.Process(new EnsureDeclarationIsValidatedCommand(request.RequestUser) { ProducerId = request.ProducerId }, token);
if (!checkDeclarationResult.Success)
return Failed<Guid>(checkDeclarationResult.Exception);
Expand All @@ -175,19 +175,16 @@ public async Task<Result<Guid>> Handle(CreatePayoutCommand request, Cancellation
token);
var amount = transfers.Sum(t => t.Credited);
var fees = 0m;
if (producerLegals.DeclarationRequired)
{
var hasAlreadyPaidComission = await _context.AnyAsync<Payout>(
p => p.Fees > 0
&& p.DebitedWallet.User.Id == request.ProducerId
&& p.Status != TransactionStatus.Failed,
token);
var fees = 0m;
var hasAlreadyPaidComission = await _context.AnyAsync<Payout>(
p => p.Fees > 0
&& p.DebitedWallet.User.Id == request.ProducerId
&& p.Status != TransactionStatus.Failed,
token);
fees = hasAlreadyPaidComission || amount < _pspOptions.ProducerFees ? 0m : _pspOptions.ProducerFees;
if (!hasAlreadyPaidComission && fees == 0m)
return Failed<Guid>(new Exception("Invalid fees for payout without paid commission."));
}
fees = hasAlreadyPaidComission || amount < _pspOptions.ProducerFees ? 0m : _pspOptions.ProducerFees;
if (!hasAlreadyPaidComission && fees == 0m)
return Failed<Guid>(new Exception("Invalid fees for payout without paid commission."));
using (var transaction = await _context.BeginTransactionAsync(token))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,8 @@ public async Task<Result<bool>> Handle(DeliverPurchaseOrderCommand request, Canc
await _context.SaveChangesAsync(token);
var producerLegals = await _context.GetSingleAsync<BusinessLegal>(c => c.User.Id == purchaseOrder.Vendor.Id, token);
if(!producerLegals.DeclarationRequired)
_mediatr.Post(new CheckLegalsDeclarationRequiredCommand(request.RequestUser) { UserId = purchaseOrder.Vendor.Id });
_mediatr.Schedule(new CreateTransferCommand(request.RequestUser) { PurchaseOrderId = purchaseOrder.Id }, TimeSpan.FromDays(7));
return Ok(true);
});
}
Expand Down
12 changes: 0 additions & 12 deletions Sheaft.Domain/BusinessLegal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public BusinessLegal(Guid id, Business business, LegalKind kind, string name, st
SetAddress(address);
SetSiret(siret);
SetVatIdentifier(vatIdentifier);
DeclarationRequired = false;
}

public string Name { get; set; }
Expand All @@ -29,8 +28,6 @@ public BusinessLegal(Guid id, Business business, LegalKind kind, string name, st
public string VatIdentifier { get; private set; }
public virtual LegalAddress Address { get; private set; }
public virtual Declaration Declaration { get; private set; }
public bool DeclarationRequired { get; private set; }
public bool IsComplete => !DeclarationRequired || (DeclarationRequired && Declaration?.Status == DeclarationStatus.Validated && Documents.All(d => d.Status == DocumentStatus.Validated));

public void SetDeclaration()
{
Expand All @@ -40,15 +37,6 @@ public void SetDeclaration()
Declaration = new Declaration(Guid.NewGuid());
}


public void SetDeclarationRequired(bool validationRequired)
{
if (DeclarationRequired)
throw new ValidationException(MessageKind.Legal_Cannot_Unrequire_Declaration);

DeclarationRequired = validationRequired;
}

public void SetSiret(string siret)
{
if (siret.IsNotNullAndIsEmptyOrWhiteSpace())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public void Configure(EntityTypeBuilder<BusinessLegal> entity)
{
entity.Property(c => c.Siret).IsRequired();
entity.Property(c => c.VatIdentifier);
entity.Ignore(c => c.IsComplete);

entity.OwnsOne(c => c.Address, e => {
e.ToTable("BusinessLegalAddresses");
Expand Down
Loading

0 comments on commit efef745

Please sign in to comment.