From 596fe4dbaf8d1d1f05e5c9bad6fa68d34b8485e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=ABl=20Mugnier?= <46538060+noelmugnier@users.noreply.github.com> Date: Sun, 31 Jan 2021 10:51:42 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20-=20Correction=20de=20la=20suppression=20?= =?UTF-8?q?du=20compte=20(d=C3=A9calage=20de=2014j=20pour=20la=20suppressi?= =?UTF-8?q?on=20effective)=20(#8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../User/RemoveUserDataCommand.cs | 1 + .../User/RestoreUserCommand.cs | 16 +++++ .../Commands/UserCommandsHandler.cs | 66 +++++++++++++------ Sheaft.Domain/Base/User.cs | 16 +++-- .../Controllers/ProducersController.cs | 15 +++++ .../Views/Producers/Index.cshtml | 2 + 6 files changed, 91 insertions(+), 25 deletions(-) create mode 100644 Sheaft.Application.Commands/User/RestoreUserCommand.cs diff --git a/Sheaft.Application.Commands/User/RemoveUserDataCommand.cs b/Sheaft.Application.Commands/User/RemoveUserDataCommand.cs index 652dca85b..cb571e5c6 100644 --- a/Sheaft.Application.Commands/User/RemoveUserDataCommand.cs +++ b/Sheaft.Application.Commands/User/RemoveUserDataCommand.cs @@ -13,5 +13,6 @@ public RemoveUserDataCommand(RequestUser requestUser) : base(requestUser) public Guid Id { get; set; } public string Email { get; set; } + public string Reason { get; set; } } } diff --git a/Sheaft.Application.Commands/User/RestoreUserCommand.cs b/Sheaft.Application.Commands/User/RestoreUserCommand.cs new file mode 100644 index 000000000..f123c3cda --- /dev/null +++ b/Sheaft.Application.Commands/User/RestoreUserCommand.cs @@ -0,0 +1,16 @@ +using System; +using Newtonsoft.Json; +using Sheaft.Core; + +namespace Sheaft.Application.Commands +{ + public class RestoreUserCommand : Command + { + [JsonConstructor] + public RestoreUserCommand(RequestUser requestUser) : base(requestUser) + { + } + + public Guid Id { get; set; } + } +} diff --git a/Sheaft.Application.Handlers/Commands/UserCommandsHandler.cs b/Sheaft.Application.Handlers/Commands/UserCommandsHandler.cs index 76c8974cf..0d2fe6a96 100644 --- a/Sheaft.Application.Handlers/Commands/UserCommandsHandler.cs +++ b/Sheaft.Application.Handlers/Commands/UserCommandsHandler.cs @@ -16,6 +16,7 @@ using Sheaft.Options; using Microsoft.Extensions.Options; using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; namespace Sheaft.Application.Handlers { @@ -26,6 +27,7 @@ public class UserCommandsHandler : ResultsHandler, IRequestHandler>, IRequestHandler>, IRequestHandler>, + IRequestHandler>, IRequestHandler> { private readonly IBlobService _blobService; @@ -115,10 +117,27 @@ public async Task> Handle(RemoveUserDataCommand request, Cancella { return await ExecuteAsync(request, async () => { - var entity = await _context.GetByIdAsync(request.Id, token); + var entity = await _context.Users.FirstOrDefaultAsync(c => c.Id == request.Id, token); + if (entity == null) + return NotFound(); + + if (!entity.RemovedOn.HasValue) + return Ok(request.Reason); + await _blobService.CleanUserStorageAsync(request.Id, token); - return Ok(request.Email); + var result = await _mediatr.Process(new RemoveAuthUserCommand(request.RequestUser) + { + UserId = entity.Id + }, token); + + if (!result.Success) + return Failed(result.Exception); + + entity.Close(); + await _context.SaveChangesAsync(token); + + return Ok(request.Reason); }); } @@ -202,30 +221,35 @@ public async Task> Handle(RemoveUserCommand request, CancellationTo { return await ExecuteAsync(request, async () => { - using (var transaction = await _context.BeginTransactionAsync(token)) - { - var entity = await _context.GetByIdAsync(request.Id, token); - - var hasActiveOrders = await _context.AnyAsync(o => (o.Vendor.Id == entity.Id || o.Sender.Id == entity.Id) && (int)o.Status < 6, token); - if (hasActiveOrders) - return ValidationError(MessageKind.Consumer_CannotBeDeleted_HasActiveOrders); + var entity = await _context.GetByIdAsync(request.Id, token); - var result = await _mediatr.Process(new RemoveAuthUserCommand(request.RequestUser) - { - UserId = entity.Id - }, token); + var hasActiveOrders = await _context.AnyAsync(o => (o.Vendor.Id == entity.Id || o.Sender.Id == entity.Id) && (int)o.Status < 6, token); + if (hasActiveOrders) + return ValidationError(MessageKind.Consumer_CannotBeDeleted_HasActiveOrders); - if (!result.Success) - return Failed(result.Exception); + _context.Remove(entity); + await _context.SaveChangesAsync(token); - entity.Close(request.Reason); + _mediatr.Schedule(new RemoveUserDataCommand(request.RequestUser) { Id = request.Id, Email = entity.Email, Reason = request.Reason }, TimeSpan.FromDays(14)); + return Ok(true); + }); + } - await _context.SaveChangesAsync(token); - await transaction.CommitAsync(token); + public async Task> Handle(RestoreUserCommand request, CancellationToken token) + { + return await ExecuteAsync(request, async () => + { + var entity = await _context.Users.FirstOrDefaultAsync(c => c.Id == request.Id, token); + if (entity == null) + return NotFound(); + + if (!entity.RemovedOn.HasValue) + return BadRequest(); + + entity.Restore(); + await _context.SaveChangesAsync(token); - _mediatr.Post(new RemoveUserDataCommand(request.RequestUser) { Id = request.Id, Email = entity.Email }); - return Ok(true); - } + return Ok(true); }); } diff --git a/Sheaft.Domain/Base/User.cs b/Sheaft.Domain/Base/User.cs index b8a9c7349..e0d2e8f4a 100644 --- a/Sheaft.Domain/Base/User.cs +++ b/Sheaft.Domain/Base/User.cs @@ -138,14 +138,17 @@ public void SetIdentifier(string identifier) Identifier = identifier; } - public virtual void Close(string reason) + public virtual void Close() { + if(Kind != ProfileKind.Consumer) + return; + FirstName = string.Empty; LastName = string.Empty; - Email = $"{Guid.NewGuid():N}@a.c"; + Name = string.Empty; + Email = $"{Guid.NewGuid():N}@ano.nym"; Phone = string.Empty; - RemovedOn = DateTime.UtcNow; - SetAddress("", "", Address.Zipcode, "", Address.Country, Address.Department); + SetAddress("Anonymous", null, Address.Zipcode, "Anonymous", Address.Country, Address.Department); } public Points AddPoints(PointKind kind, int quantity, DateTimeOffset? createdOn = null) @@ -164,6 +167,11 @@ private void RefreshPoints() { TotalPoints = _points.Sum(c => c.Quantity); } + + public void Restore() + { + RemovedOn = null; + } } public class Admin : User diff --git a/Sheaft.Web.Manage/Controllers/ProducersController.cs b/Sheaft.Web.Manage/Controllers/ProducersController.cs index c14748b0e..1b59373bd 100644 --- a/Sheaft.Web.Manage/Controllers/ProducersController.cs +++ b/Sheaft.Web.Manage/Controllers/ProducersController.cs @@ -136,6 +136,21 @@ public async Task Delete(Guid id, CancellationToken token) throw result.Exception; return RedirectToAction("Index"); + } + + [HttpPost] + [ValidateAntiForgeryToken] + public async Task Restore(Guid id, CancellationToken token) + { + var result = await _mediatr.Process(new RestoreUserCommand(await GetRequestUser(token)) + { + Id = id + }, token); + + if (!result.Success) + throw result.Exception; + + return RedirectToAction("Edit", new {id}); } } } diff --git a/Sheaft.Web.Manage/Views/Producers/Index.cshtml b/Sheaft.Web.Manage/Views/Producers/Index.cshtml index 107cd707c..daad25bc0 100644 --- a/Sheaft.Web.Manage/Views/Producers/Index.cshtml +++ b/Sheaft.Web.Manage/Views/Producers/Index.cshtml @@ -15,6 +15,7 @@ Name Email CreatedOn + Removed @@ -25,6 +26,7 @@ @entity.Name @entity.Email @entity.CreatedOn.ToString("dd/MM/yy hh:mm") + @entity.RemovedOn.HasValue