aboutsummaryrefslogtreecommitdiffstats
path: root/code/api/src/Endpoints/Internal/PasswordResetRequests/FulfillResetRequestRoute.cs
blob: 9cd92bb97f6b6d26ef79b275b7b0309a6ce85d69 (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
namespace IOL.GreatOffice.Api.Endpoints.Internal.PasswordResetRequests;

public class FulfillResetRequestRoute : RouteBaseAsync.WithRequest<FulfillResetRequestRoute.Payload>.WithActionResult
{
    private readonly IStringLocalizer<SharedResources> _localizer;
    private readonly PasswordResetService _passwordResetService;

    public FulfillResetRequestRoute(PasswordResetService passwordResetService, IStringLocalizer<SharedResources> localizer) {
        _passwordResetService = passwordResetService;
        _localizer = localizer;
    }

    public class Payload
    {
        public Guid Id { get; set; }
        public string NewPassword { get; set; }
    }

    [AllowAnonymous]
    [HttpPost("~/_/password-reset-request/fulfill")]
    public override async Task<ActionResult> HandleAsync(Payload request, CancellationToken cancellationToken = default) {
        if (request.NewPassword.Length < 6) {
            return KnownProblem(_localizer["Invalid form"],
                _localizer["One or more fields is invalid"],
                new Dictionary<string, string[]> {{"newPassword", new string[] {_localizer["The new password needs to be atleast 6 characters"]}}}
            );
        }

        return await _passwordResetService.FulfillRequestAsync(request.Id, request.NewPassword, cancellationToken) switch {
            FulfillPasswordResetRequestResult.REQUEST_NOT_FOUND => NotFound(),
            FulfillPasswordResetRequestResult.USER_NOT_FOUND => NotFound(),
            FulfillPasswordResetRequestResult.FULFILLED => Ok(),
            _ => StatusCode(500)
        };
    }
}