|
4 | 4 |
|
5 | 5 | namespace Altinn.DialogportenAdapter.WebApi.Features.Command.Delete;
|
6 | 6 |
|
7 |
| -public record DeleteDialogDto(int PartyId, Guid InstanceGuid, bool Hard, string DialogToken); |
| 7 | +internal sealed record DeleteInstanceDto(int PartyId, Guid InstanceGuid, bool Hard, string DialogToken); |
8 | 8 |
|
9 |
| -public enum DeleteDialogResult |
| 9 | +internal enum DeleteInstanceResult |
10 | 10 | {
|
11 | 11 | Success,
|
12 | 12 | InstanceNotFound,
|
13 | 13 | Unauthorized
|
14 | 14 | }
|
15 | 15 |
|
16 |
| -internal sealed class DeleteDialogService |
| 16 | +internal sealed class InstanceService |
17 | 17 | {
|
18 | 18 | private readonly IStorageApi _storageApi;
|
19 | 19 | private readonly IDialogTokenValidator _dialogTokenValidator;
|
20 | 20 |
|
21 |
| - public DeleteDialogService(IStorageApi storageApi, IDialogTokenValidator dialogTokenValidator) |
| 21 | + public InstanceService(IStorageApi storageApi, IDialogTokenValidator dialogTokenValidator) |
22 | 22 | {
|
23 | 23 | _storageApi = storageApi ?? throw new ArgumentNullException(nameof(storageApi));
|
24 | 24 | _dialogTokenValidator = dialogTokenValidator ?? throw new ArgumentNullException(nameof(dialogTokenValidator));
|
25 | 25 | }
|
26 | 26 |
|
27 |
| - public async Task<DeleteDialogResult> DeleteDialog(DeleteDialogDto request, CancellationToken cancellationToken) |
| 27 | + public async Task<DeleteInstanceResult> Delete(DeleteInstanceDto request, CancellationToken cancellationToken) |
28 | 28 | {
|
29 | 29 | var instance = await _storageApi
|
30 | 30 | .GetInstance(request.PartyId, request.InstanceGuid, cancellationToken)
|
31 | 31 | .ContentOrDefault();
|
32 | 32 |
|
33 | 33 | if (instance is null)
|
34 | 34 | {
|
35 |
| - return DeleteDialogResult.InstanceNotFound; |
| 35 | + return DeleteInstanceResult.InstanceNotFound; |
36 | 36 | }
|
37 | 37 |
|
38 | 38 | var dialogId = request.InstanceGuid.ToVersion7(instance.Created!.Value);
|
39 |
| - var result = ValidateDialogToken(request.DialogToken, dialogId); |
40 |
| - if (!result.IsValid) |
| 39 | + if (!ValidateDialogToken(request.DialogToken, dialogId)) |
41 | 40 | {
|
42 |
| - return DeleteDialogResult.Unauthorized; |
| 41 | + return DeleteInstanceResult.Unauthorized; |
43 | 42 | }
|
44 | 43 |
|
45 | 44 | await _storageApi.DeleteInstance(request.PartyId, request.InstanceGuid, request.Hard, cancellationToken);
|
46 |
| - return DeleteDialogResult.Success; |
| 45 | + return DeleteInstanceResult.Success; |
47 | 46 | }
|
48 | 47 |
|
49 |
| - private IValidationResult ValidateDialogToken(ReadOnlySpan<char> token, Guid dialogId) |
| 48 | + private bool ValidateDialogToken(ReadOnlySpan<char> token, Guid dialogId) |
50 | 49 | {
|
51 | 50 | const string bearerPrefix = "Bearer ";
|
52 | 51 | token = token.StartsWith(bearerPrefix, StringComparison.OrdinalIgnoreCase)
|
53 | 52 | ? token[bearerPrefix.Length..]
|
54 | 53 | : token;
|
55 |
| - var result = _dialogTokenValidator.Validate(token); |
56 |
| - // TODO: Validate dialog id |
57 |
| - // TODO: Validate action |
58 |
| - return result; |
| 54 | + var result = _dialogTokenValidator.Validate(token, dialogId, ["delete"]); |
| 55 | + return result.IsValid; |
59 | 56 | }
|
60 | 57 | }
|
0 commit comments