Skip to content

Commit

Permalink
Fix - Correction de la suppression du compte (décalage de 14j pour la…
Browse files Browse the repository at this point in the history
… suppression effective) (#8)
  • Loading branch information
noelmugnier authored Jan 31, 2021
1 parent 2a1835c commit 596fe4d
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 25 deletions.
1 change: 1 addition & 0 deletions Sheaft.Application.Commands/User/RemoveUserDataCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
16 changes: 16 additions & 0 deletions Sheaft.Application.Commands/User/RestoreUserCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using Newtonsoft.Json;
using Sheaft.Core;

namespace Sheaft.Application.Commands
{
public class RestoreUserCommand : Command<bool>
{
[JsonConstructor]
public RestoreUserCommand(RequestUser requestUser) : base(requestUser)
{
}

public Guid Id { get; set; }
}
}
66 changes: 45 additions & 21 deletions Sheaft.Application.Handlers/Commands/UserCommandsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Sheaft.Options;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace Sheaft.Application.Handlers
{
Expand All @@ -26,6 +27,7 @@ public class UserCommandsHandler : ResultsHandler,
IRequestHandler<CreateUserPointsCommand, Result<bool>>,
IRequestHandler<ChangeUserRolesCommand, Result<bool>>,
IRequestHandler<RemoveUserCommand, Result<bool>>,
IRequestHandler<RestoreUserCommand, Result<bool>>,
IRequestHandler<RemoveUserDataCommand, Result<string>>
{
private readonly IBlobService _blobService;
Expand Down Expand Up @@ -115,10 +117,27 @@ public async Task<Result<string>> Handle(RemoveUserDataCommand request, Cancella
{
return await ExecuteAsync(request, async () =>
{
var entity = await _context.GetByIdAsync<User>(request.Id, token);
var entity = await _context.Users.FirstOrDefaultAsync(c => c.Id == request.Id, token);
if (entity == null)
return NotFound<string>();
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<string>(result.Exception);
entity.Close();
await _context.SaveChangesAsync(token);
return Ok(request.Reason);
});
}

Expand Down Expand Up @@ -202,30 +221,35 @@ public async Task<Result<bool>> Handle(RemoveUserCommand request, CancellationTo
{
return await ExecuteAsync(request, async () =>
{
using (var transaction = await _context.BeginTransactionAsync(token))
{
var entity = await _context.GetByIdAsync<User>(request.Id, token);
var hasActiveOrders = await _context.AnyAsync<PurchaseOrder>(o => (o.Vendor.Id == entity.Id || o.Sender.Id == entity.Id) && (int)o.Status < 6, token);
if (hasActiveOrders)
return ValidationError<bool>(MessageKind.Consumer_CannotBeDeleted_HasActiveOrders);
var entity = await _context.GetByIdAsync<User>(request.Id, token);
var result = await _mediatr.Process(new RemoveAuthUserCommand(request.RequestUser)
{
UserId = entity.Id
}, token);
var hasActiveOrders = await _context.AnyAsync<PurchaseOrder>(o => (o.Vendor.Id == entity.Id || o.Sender.Id == entity.Id) && (int)o.Status < 6, token);
if (hasActiveOrders)
return ValidationError<bool>(MessageKind.Consumer_CannotBeDeleted_HasActiveOrders);
if (!result.Success)
return Failed<bool>(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<Result<bool>> 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<bool>();
if (!entity.RemovedOn.HasValue)
return BadRequest<bool>();
entity.Restore();
await _context.SaveChangesAsync(token);
_mediatr.Post(new RemoveUserDataCommand(request.RequestUser) { Id = request.Id, Email = entity.Email });
return Ok(true);
}
return Ok(true);
});
}

Expand Down
16 changes: 12 additions & 4 deletions Sheaft.Domain/Base/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -164,6 +167,11 @@ private void RefreshPoints()
{
TotalPoints = _points.Sum(c => c.Quantity);
}

public void Restore()
{
RemovedOn = null;
}
}

public class Admin : User
Expand Down
15 changes: 15 additions & 0 deletions Sheaft.Web.Manage/Controllers/ProducersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ public async Task<IActionResult> Delete(Guid id, CancellationToken token)
throw result.Exception;

return RedirectToAction("Index");
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> 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});
}
}
}
2 changes: 2 additions & 0 deletions Sheaft.Web.Manage/Views/Producers/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">CreatedOn</th>
<th scope="col">Removed</th>
<th scope="col"></th>
</tr>
</thead>
Expand All @@ -25,6 +26,7 @@
<th scope="row">@entity.Name</th>
<td>@entity.Email</td>
<td>@entity.CreatedOn.ToString("dd/MM/yy hh:mm")</td>
<td>@entity.RemovedOn.HasValue</td>
<td>
<a asp-controller="Producers" asp-action="Edit" asp-route-id="@entity.Id" class="btn btn-info">
<i class="fa fa-pen"></i>
Expand Down

0 comments on commit 596fe4d

Please sign in to comment.