Skip to content

Commit

Permalink
fix(graphQL): GraphQL subscription not notified on DialogActivityCrea…
Browse files Browse the repository at this point in the history
…ted (#1187)

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

## Description
Removes limitations on creating DialogUpdatedEvent
(They where not created if the only thing changed in the aggregate was
added activities)

We now create DialogUpdatedEvent for any change in the aggregate. When
processing events after a successful DB save we check for either a
DialogUpdated or DialogDeleted per dialogID, and notify each
dialogEvents GraphQL topic.
<!--- Describe your changes in detail -->

## Related Issue(s)

- #1165 

## 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 event handling for dialog updates, streamlining the process
and reducing redundant event processing.
- Introduced a mechanism to prevent multiple sends for the same dialog
within a short time frame, improving efficiency.
- **Bug Fixes**
- Improved logic for processing domain events, ensuring clearer and more
efficient handling.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: Magnus Sandgren <5285192+MagnusSandgren@users.noreply.github.com>
Co-authored-by: Knut Haug <knut.espen.haug@digdir.no>
  • Loading branch information
3 people authored Oct 2, 2024
1 parent dc96c94 commit f28e291
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,8 @@ public sealed class DialogEntity :
public void OnCreate(AggregateNode self, DateTimeOffset utcNow)
=> _domainEvents.Add(new DialogCreatedDomainEvent(Id, ServiceResource, Party, Process, PrecedingProcess));

public void OnUpdate(AggregateNode self, DateTimeOffset utcNow)
{
var changedChildren = self.Children.Where(x =>
x.State != AggregateNodeState.Unchanged &&
x.Entity is not DialogSearchTag &&
x.Entity is not DialogActivity);

var shouldProduceEvent = self.IsDirectlyModified() || changedChildren.Any();
if (shouldProduceEvent)
{
_domainEvents.Add(new DialogUpdatedDomainEvent(Id, ServiceResource, Party, Process, PrecedingProcess));
}
}
public void OnUpdate(AggregateNode self, DateTimeOffset utcNow) =>
_domainEvents.Add(new DialogUpdatedDomainEvent(Id, ServiceResource, Party, Process, PrecedingProcess));

public void OnDelete(AggregateNode self, DateTimeOffset utcNow)
=> _domainEvents.Add(new DialogDeletedDomainEvent(Id, ServiceResource, Party, Process, PrecedingProcess));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Digdir.Domain.Dialogporten.Application.Common;
using Digdir.Domain.Dialogporten.Domain.Dialogs.Events;
using Digdir.Domain.Dialogporten.Domain.Dialogs.Events.Activities;
using Digdir.Domain.Dialogporten.Domain.Outboxes;
using Digdir.Domain.Dialogporten.Infrastructure.GraphQl;
using Digdir.Library.Entity.Abstractions.Features.EventPublisher;
Expand Down Expand Up @@ -65,47 +64,35 @@ x.Entity is IEventPublisher publisher
public override async ValueTask<int> SavedChangesAsync(SaveChangesCompletedEventData eventData, int result,
CancellationToken cancellationToken = default)
{
foreach (var domainEvent in _domainEvents)
try
{
try
{
// If you are adding any additional cases to this switch,
// please consider making the running of the tasks parallel
var task = domainEvent switch
var tasks = _domainEvents
.Select(x => x switch
{
DialogUpdatedDomainEvent dialogUpdatedDomainEvent => _topicEventSender.SendAsync(
$"{Constants.DialogEventsTopic}{dialogUpdatedDomainEvent.DialogId}",
new DialogEventPayload
{
Id = dialogUpdatedDomainEvent.DialogId,
Type = DialogEventType.DialogUpdated
},
cancellationToken),
DialogDeletedDomainEvent dialogDeletedDomainEvent => _topicEventSender.SendAsync(
$"{Constants.DialogEventsTopic}{dialogDeletedDomainEvent.DialogId}",
new DialogEventPayload
{
Id = dialogDeletedDomainEvent.DialogId,
Type = DialogEventType.DialogDeleted
},
cancellationToken),
DialogActivityCreatedDomainEvent dialogActivityCreatedDomainEvent => _topicEventSender.SendAsync(
$"{Constants.DialogEventsTopic}{dialogActivityCreatedDomainEvent.DialogId}",
new DialogEventPayload
{
Id = dialogActivityCreatedDomainEvent.DialogId,
Type = DialogEventType.DialogUpdated
},
cancellationToken),
_ => ValueTask.CompletedTask
};

await task;
}
catch (Exception e)
{
_logger.LogError(e, "Failed to send domain event to graphQL subscription");
}
DialogUpdatedDomainEvent dialogUpdatedDomainEvent => new DialogEventPayload
{
Id = dialogUpdatedDomainEvent.DialogId,
Type = DialogEventType.DialogUpdated
},
DialogDeletedDomainEvent dialogDeletedDomainEvent => new DialogEventPayload
{
Id = dialogDeletedDomainEvent.DialogId,
Type = DialogEventType.DialogDeleted
},
_ => (DialogEventPayload?)null
})
.Where(x => x is not null)
.Cast<DialogEventPayload>()
.Select(x => _topicEventSender.SendAsync(
$"{Constants.DialogEventsTopic}{x.Id}",
x,
cancellationToken)
.AsTask());
await Task.WhenAll(tasks);
}
catch (Exception e)
{
_logger.LogError(e, "Failed to send domain events to graphQL subscription");
}

return await base.SavedChangesAsync(eventData, result, cancellationToken);
Expand Down

0 comments on commit f28e291

Please sign in to comment.