aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Endpoints/Internal/Account/DeleteAccountRoute.cs
blob: e5bbb10de2cc098b8391fd72c81f558348d1cf61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace IOL.GreatOffice.Api.Endpoints.Internal.Account;

public class DeleteAccountRoute : RouteBaseAsync.WithoutRequest.WithActionResult
{
    private readonly MainAppDatabase _database;
    private readonly UserService _userService;

    public DeleteAccountRoute(MainAppDatabase database, UserService userService) {
        _database = database;
        _userService = userService;
    }

    /// <summary>
    /// Delete the logged on user's account.
    /// </summary>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [HttpDelete("~/_/account/delete")]
    public override async Task<ActionResult> HandleAsync(CancellationToken cancellationToken = default) {
        var user = _database.Users.SingleOrDefault(c => c.Id == LoggedInUser.Id);
        if (user == default) {
            await _userService.LogOutUser(HttpContext);
            return Unauthorized();
        }

        if (user.Username == "demo@demo.demo") {
            await _userService.LogOutUser(HttpContext);
            return Ok();
        }

        var passwordResets = _database.ForgotPasswordRequests.Where(c => c.UserId == user.Id);

        _database.ForgotPasswordRequests.RemoveRange(passwordResets);
        _database.Users.Remove(user);

        await _database.SaveChangesAsync(cancellationToken);
        await _userService.LogOutUser(HttpContext);
        return Ok();
    }
}