Skip to content

Commit

Permalink
feat(webapi): Return 410 GONE for notification checks on deleted dial…
Browse files Browse the repository at this point in the history
…ogs (#1387)

<!--- Provide a general summary of your changes in the Title above -->

## Description

<!--- Describe your changes in detail -->

## Related Issue(s)

- #1386 

## Verification

- [ ] **Your** code builds clean without any errors or warnings
- [ ] Manual testing done (required)
- [ ] Relevant automated test added (if you find this hard, leave it and
we'll help out)

## Documentation

- [ ] Documentation is updated (either in `docs`-directory, Altinnpedia
or a separate linked PR in
[altinn-studio-docs.](https://github.com/Altinn/altinn-studio-docs), if
applicable)


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Enhanced error handling for deleted entities in the notification
condition query.
- Added a new response type for deleted dialogs, improving clarity in
error reporting.

- **Bug Fixes**
- Improved response handling for scenarios where a requested dialog has
been deleted.

- **Tests**
- Introduced a new test case to verify correct behavior when querying a
deleted dialog.
	- Refactored test methods for improved readability and maintainability.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
oskogstad authored Nov 4, 2024
1 parent 431fe16 commit 198bebd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public enum NotificationConditionType
}

[GenerateOneOf]
public sealed partial class NotificationConditionResult : OneOfBase<NotificationConditionDto, ValidationError, EntityNotFound>;
public sealed partial class NotificationConditionResult : OneOfBase<NotificationConditionDto, ValidationError, EntityNotFound, EntityDeleted>;

internal sealed class NotificationConditionQueryHandler : IRequestHandler<NotificationConditionQuery, NotificationConditionResult>
{
Expand All @@ -41,6 +41,7 @@ public async Task<NotificationConditionResult> Handle(NotificationConditionQuery
.Include(x => x.Activities
.Where(x => request.TransmissionId == null || x.TransmissionId == request.TransmissionId)
.Where(x => x.TypeId == request.ActivityType))
.IgnoreQueryFilters()
.FirstOrDefaultAsync(x => x.Id == request.DialogId,
cancellationToken: cancellationToken);

Expand All @@ -49,6 +50,11 @@ public async Task<NotificationConditionResult> Handle(NotificationConditionQuery
return new EntityNotFound<DialogEntity>(request.DialogId);
}

if (dialog.Deleted)
{
return new EntityDeleted<DialogEntity>(request.DialogId);
}

var conditionMet = dialog.Activities.Count == 0
? request.ConditionType == NotificationConditionType.NotExists
: request.ConditionType == NotificationConditionType.Exists;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public override async Task HandleAsync(NotificationConditionQuery req, Cancellat
await result.Match(
dto => SendOkAsync(dto, ct),
validationError => this.BadRequestAsync(validationError, ct),
notFound => this.NotFoundAsync(notFound, ct));
notFound => this.NotFoundAsync(notFound, ct),
deleted => this.GoneAsync(deleted, ct));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.DialogActivities.Queries.NotificationCondition;
using Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Create;
using Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Delete;
using Digdir.Domain.Dialogporten.Application.Integration.Tests.Common;
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Activities;
using Digdir.Domain.Dialogporten.Domain.Dialogs.Entities.Transmissions;
Expand Down Expand Up @@ -39,12 +40,7 @@ public async Task SendNotification_Should_Be_True_When_Conditions_Are_Met(
var response = await Application.Send(createDialogCommand);
response.TryPickT0(out var dialogId, out _);

var notificationConditionQuery = new NotificationConditionQuery
{
DialogId = dialogId.Value,
ActivityType = activityType,
ConditionType = conditionType,
};
var notificationConditionQuery = CreateNotificationConditionQuery(dialogId.Value, activityType, conditionType);

if (activityType is DialogActivityType.Values.TransmissionOpened)
{
Expand Down Expand Up @@ -79,12 +75,7 @@ private static void AddActivityRequirements(
public async Task NotFound_Should_Be_Returned_When_Dialog_Does_Not_Exist()
{
// Arrange
var notificationConditionQuery = new NotificationConditionQuery
{
DialogId = Guid.NewGuid(),
ActivityType = DialogActivityType.Values.Information,
ConditionType = NotificationConditionType.Exists,
};
var notificationConditionQuery = CreateNotificationConditionQuery(Guid.NewGuid());

// Act
var queryResult = await Application.Send(notificationConditionQuery);
Expand All @@ -94,4 +85,37 @@ public async Task NotFound_Should_Be_Returned_When_Dialog_Does_Not_Exist()
queryResult.IsT2.Should().BeTrue();
notFound.Should().NotBeNull();
}

[Fact]
public async Task Gone_Should_Be_Returned_When_Dialog_Is_Deleted()
{
// Arrange
var createDialogCommand = DialogGenerator.GenerateSimpleFakeDialog();

var response = await Application.Send(createDialogCommand);
response.TryPickT0(out var dialogId, out _);

await Application.Send(new DeleteDialogCommand { Id = dialogId.Value });

var notificationConditionQuery = CreateNotificationConditionQuery(dialogId.Value);

// Act
var queryResult = await Application.Send(notificationConditionQuery);

// Assert
queryResult.TryPickT3(out var deleted, out _);
queryResult.IsT3.Should().BeTrue();
deleted.Should().NotBeNull();
deleted.Message.Should().Contain(dialogId.Value.ToString());
}

private static NotificationConditionQuery CreateNotificationConditionQuery(Guid dialogId,
DialogActivityType.Values activityType = DialogActivityType.Values.Information,
NotificationConditionType conditionType = NotificationConditionType.Exists)
=> new()
{
DialogId = dialogId,
ActivityType = activityType,
ConditionType = conditionType,
};
}

0 comments on commit 198bebd

Please sign in to comment.