Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webapi): Add option to disable event generation #1633

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions docs/schema/V1/swagger.verified.json
Original file line number Diff line number Diff line change
Expand Up @@ -2255,7 +2255,7 @@
},
"type": "object"
},
"V1ServiceOwnerDialogsCommandsCreate_DialogCommand": {
"V1ServiceOwnerDialogsCommandsCreate_Dialog": {
"additionalProperties": false,
"properties": {
"activities": {
Expand Down Expand Up @@ -3233,6 +3233,15 @@
},
"type": "object"
},
"V1ServiceOwnerDialogsCreate_DialogRequest": {
"additionalProperties": false,
"properties": {
"dto": {
"$ref": "#/components/schemas/V1ServiceOwnerDialogsCommandsCreate_Dialog"
}
},
"type": "object"
},
"V1ServiceOwnerDialogSeenLogsQueriesGet_SeenLog": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -5844,14 +5853,12 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/V1ServiceOwnerDialogsCommandsCreate_DialogCommand"
"$ref": "#/components/schemas/V1ServiceOwnerDialogsCommandsCreate_Dialog"
}
}
},
"description": "",
"required": true,
"x-name": "CreateDialogCommand",
"x-position": 1
"x-name": "dto"
},
"responses": {
"201": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.Hosting;
using System.Reflection;
using Digdir.Domain.Dialogporten.Application.Common.Authorization;
using Digdir.Domain.Dialogporten.Domain;
using MediatR.NotificationPublishers;

namespace Digdir.Domain.Dialogporten.Application;
Expand All @@ -31,6 +32,9 @@ public static IServiceCollection AddApplication(this IServiceCollection services
// 'CreatedAt', not 'Created At'.
ValidatorOptions.Global.DisplayNameResolver = (_, member, _) => member?.Name;

// AddDomain
// Lag lik extension i domain

services
// Framework
.AddAutoMapper(thisAssembly)
Expand All @@ -47,6 +51,7 @@ public static IServiceCollection AddApplication(this IServiceCollection services
.AddSingleton<ICompactJwsGenerator, Ed25519Generator>()

// Scoped
.AddDomain()
.AddScoped<IDomainContext, DomainContext>()
.AddScoped<ITransactionTime, TransactionTime>()
.AddScoped<IDialogTokenGenerator, DialogTokenGenerator>()
Expand All @@ -59,7 +64,8 @@ public static IServiceCollection AddApplication(this IServiceCollection services
.AddTransient<IUserParties, UserParties>()
.AddTransient<IClock, Clock>()
.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>))
.AddTransient(typeof(IPipelineBehavior<,>), typeof(DomainContextBehaviour<,>));
.AddTransient(typeof(IPipelineBehavior<,>), typeof(DomainContextBehaviour<,>))
.AddTransient(typeof(IPipelineBehavior<,>), typeof(DomainAltinnEventOptOutBehaviour<,>));

if (!environment.IsDevelopment())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public async Task<SetResourceTypeResult> SetResourceType(DialogEntity dialog, Ca
if (serviceResourceInformation is null)
{
var supportedResourceTypes = string.Join(", ", Constants.SupportedResourceTypes);
_domainContext.AddError(nameof(CreateDialogCommand.ServiceResource),
_domainContext.AddError(nameof(CreateDialogDto.ServiceResource),
$"Service resource '{dialog.ServiceResource}' does not exist in the resource " +
$"registry, or is not of the following supported resource types: " +
$"[{supportedResourceTypes}].");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Digdir.Domain.Dialogporten.Domain.Common.DomainEvents;
using MediatR;

namespace Digdir.Domain.Dialogporten.Application.Common.Behaviours;

internal sealed class DomainAltinnEventOptOutBehaviour<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>, IAltinnEventDisabler
{
private readonly IDomainEventContext _domainEventContext;

public DomainAltinnEventOptOutBehaviour(IDomainEventContext domainEventContext)
{
_domainEventContext = domainEventContext;
}

public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
if (request.DisableAltinnEvents)
{
_domainEventContext.AddMetadata("DisableEvents", "true");
}

return next();
}
}

public interface IAltinnEventDisabler
{
bool DisableAltinnEvents { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public DialogEventToAltinnForwarder(ICloudEventBus cloudEventBus, IOptions<Appli
[EndpointName("DialogEventToAltinnForwarder_DialogCreatedDomainEvent")]
public async Task Handle(DialogCreatedDomainEvent domainEvent, CancellationToken cancellationToken)
{
domainEvent.Metadata.TryGetValue("DisableEvents", out var value);
var cloudEvent = new CloudEvent
{
Id = domainEvent.EventId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task<SetSystemLabelResult> Handle(

var saveResult = await _unitOfWork
.EnableConcurrencyCheck(dialog.DialogEndUserContext, request.IfMatchDialogRevision)
.SaveChangesAsync(cancellationToken);
.SaveChangesAsync(cancellationToken: cancellationToken);
return saveResult.Match<SetSystemLabelResult>(
success => success,
domainError => domainError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public async Task<GetDialogResult> Handle(GetDialogQuery request, CancellationTo

var saveResult = await _unitOfWork
.WithoutAggregateSideEffects()
.SaveChangesAsync(cancellationToken);
.SaveChangesAsync(cancellationToken: cancellationToken);

saveResult.Switch(
success => { },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public async Task<SyncSubjectMapResult> Handle(SyncSubjectMapCommand request, Ca
mergeCount += batchMergeCount;
}

await _unitOfWork.SaveChangesAsync(cancellationToken);
await _unitOfWork.SaveChangesAsync(cancellationToken: cancellationToken);
if (mergeCount > 0)
{
_logger.LogInformation("Successfully synced {UpdatedAmount} total subject-resources. Changes committed.", mergeCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using AutoMapper;
using Digdir.Domain.Dialogporten.Application.Common;
using Digdir.Domain.Dialogporten.Application.Common.Authorization;
using Digdir.Domain.Dialogporten.Application.Common.Behaviours;
using Digdir.Domain.Dialogporten.Application.Common.Extensions;
using Digdir.Domain.Dialogporten.Application.Common.ReturnTypes;
using Digdir.Domain.Dialogporten.Application.Externals;
Expand All @@ -15,11 +16,14 @@
using Digdir.Library.Entity.Abstractions.Features.Identifiable;
using MediatR;
using OneOf;
using OneOf.Types;

namespace Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Create;

public sealed class CreateDialogCommand : CreateDialogDto, IRequest<CreateDialogResult>;
public sealed class CreateDialogCommand : IRequest<CreateDialogResult>, IAltinnEventDisabler
{
public bool DisableAltinnEvents { get; init; }
public CreateDialogDto Dto { get; set; } = null!;
}

public sealed record CreateDialogSuccess(Guid DialogId, Guid Revision);

Expand Down Expand Up @@ -56,7 +60,7 @@ public CreateDialogCommandHandler(

public async Task<CreateDialogResult> Handle(CreateDialogCommand request, CancellationToken cancellationToken)
{
var dialog = _mapper.Map<DialogEntity>(request);
var dialog = _mapper.Map<DialogEntity>(request.Dto);

await _serviceResourceAuthorizer.SetResourceType(dialog, cancellationToken);
var serviceResourceAuthorizationResult = await _serviceResourceAuthorizer.AuthorizeServiceResources(dialog, cancellationToken);
Expand Down Expand Up @@ -88,7 +92,7 @@ public async Task<CreateDialogResult> Handle(CreateDialogCommand request, Cancel
_domainContext.AddErrors(dialog.Transmissions.ValidateReferenceHierarchy(
keySelector: x => x.Id,
parentKeySelector: x => x.RelatedTransmissionId,
propertyName: nameof(CreateDialogCommand.Transmissions),
propertyName: nameof(CreateDialogDto.Transmissions),
maxDepth: 100,
maxWidth: 1));

Expand All @@ -103,7 +107,7 @@ public async Task<CreateDialogResult> Handle(CreateDialogCommand request, Cancel
private void CreateDialogEndUserContext(CreateDialogCommand request, DialogEntity dialog)
{
dialog.DialogEndUserContext = new();
if (!request.SystemLabel.HasValue)
if (!request.Dto.SystemLabel.HasValue)
{
return;
}
Expand All @@ -115,7 +119,7 @@ private void CreateDialogEndUserContext(CreateDialogCommand request, DialogEntit
}

dialog.DialogEndUserContext.UpdateLabel(
request.SystemLabel.Value,
request.Dto.SystemLabel.Value,
$"{NorwegianOrganizationIdentifier.PrefixWithSeparator}{organizationNumber}",
ActorType.Values.ServiceOwner);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ namespace Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialog

internal sealed class CreateDialogCommandValidator : AbstractValidator<CreateDialogCommand>
{
public CreateDialogCommandValidator(
public CreateDialogCommandValidator(IValidator<CreateDialogDto> createDialogDtoValidator)
{
RuleFor(x => x.Dto)
.NotEmpty()
.SetValidator(createDialogDtoValidator);
}
}

internal sealed class CreateDialogDtoValidator : AbstractValidator<CreateDialogDto>
{
public CreateDialogDtoValidator(
IValidator<TransmissionDto> transmissionValidator,
IValidator<AttachmentDto> attachmentValidator,
IValidator<GuiActionDto> guiActionValidator,
Expand All @@ -35,13 +45,13 @@ public CreateDialogCommandValidator(

RuleFor(x => x.CreatedAt)
.NotEmpty()
.WithMessage($"{{PropertyName}} must not be empty when '{nameof(CreateDialogCommand.UpdatedAt)} is set.")
.WithMessage($"{{PropertyName}} must not be empty when '{nameof(CreateDialogDto.UpdatedAt)} is set.")
.When(x => x.UpdatedAt != default);

RuleFor(x => x.UpdatedAt)
.IsInPast()
.GreaterThanOrEqualTo(x => x.CreatedAt)
.WithMessage($"'{{PropertyName}}' must be greater than or equal to '{nameof(CreateDialogCommand.CreatedAt)}'.")
.WithMessage($"'{{PropertyName}}' must be greater than or equal to '{nameof(CreateDialogDto.CreatedAt)}'.")
.When(x => x.CreatedAt != default && x.UpdatedAt != default);

RuleFor(x => x.ServiceResource)
Expand Down Expand Up @@ -136,7 +146,7 @@ public CreateDialogCommandValidator(

RuleFor(x => x.Process)
.NotEmpty()
.WithMessage($"{{PropertyName}} must not be empty when {nameof(CreateDialogCommand.PrecedingProcess)} is set.")
.WithMessage($"{{PropertyName}} must not be empty when {nameof(CreateDialogDto.PrecedingProcess)} is set.")
.When(x => x.PrecedingProcess is not null);

RuleFor(x => x.PrecedingProcess)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Create;

public class CreateDialogDto
public sealed class CreateDialogDto
{
/// <summary>
/// A self-defined UUIDv7 may be provided to support idempotent creation of dialogs. If not provided, a new UUIDv7 will be generated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task<DeleteDialogResult> Handle(DeleteDialogCommand request, Cancel
_db.Dialogs.SoftRemove(dialog);
var saveResult = await _unitOfWork
.EnableConcurrencyCheck(dialog, request.IfMatchDialogRevision)
.SaveChangesAsync(cancellationToken);
.SaveChangesAsync(cancellationToken: cancellationToken);

return saveResult.Match<DeleteDialogResult>(
success => new DeleteDialogSuccess(dialog.Revision),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task<PurgeDialogResult> Handle(PurgeDialogCommand request, Cancella
_db.Dialogs.HardRemove(dialog);
var saveResult = await _unitOfWork
.EnableConcurrencyCheck(dialog, request.IfMatchDialogRevision)
.SaveChangesAsync(cancellationToken);
.SaveChangesAsync(cancellationToken: cancellationToken);

return saveResult.Match<PurgeDialogResult>(
success => success,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AutoMapper;
using Digdir.Domain.Dialogporten.Application.Common;
using Digdir.Domain.Dialogporten.Application.Common.Authorization;
using Digdir.Domain.Dialogporten.Application.Common.Behaviours;
using Digdir.Domain.Dialogporten.Application.Common.Extensions;
using Digdir.Domain.Dialogporten.Application.Common.Extensions.Enumerables;
using Digdir.Domain.Dialogporten.Application.Common.ReturnTypes;
Expand All @@ -22,11 +23,12 @@

namespace Digdir.Domain.Dialogporten.Application.Features.V1.ServiceOwner.Dialogs.Commands.Update;

public sealed class UpdateDialogCommand : IRequest<UpdateDialogResult>
public sealed class UpdateDialogCommand : IRequest<UpdateDialogResult>, IAltinnEventDisabler
{
public Guid Id { get; set; }
public Guid? IfMatchDialogRevision { get; set; }
public UpdateDialogDto Dto { get; set; } = null!;
public bool DisableAltinnEvents { get; set; }
}

[GenerateOneOf]
Expand Down Expand Up @@ -163,6 +165,7 @@ public async Task<UpdateDialogResult> Handle(UpdateDialogCommand request, Cancel
}

UpdateLabel(dialog);

var saveResult = await _unitOfWork
.EnableConcurrencyCheck(dialog, request.IfMatchDialogRevision)
.SaveChangesAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public async Task<GetDialogResult> Handle(GetDialogQuery request, CancellationTo

var saveResult = await _unitOfWork
.WithoutAggregateSideEffects()
.SaveChangesAsync(cancellationToken);
.SaveChangesAsync(cancellationToken: cancellationToken);

saveResult.Switch(
success => { },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System.Text.Json.Serialization;
using System.Collections.Frozen;
using System.Text.Json.Serialization;
using Digdir.Domain.Dialogporten.Domain.Common.EventPublisher;

namespace Digdir.Domain.Dialogporten.Domain.Common;
namespace Digdir.Domain.Dialogporten.Domain.Common.DomainEvents;

public abstract record DomainEvent : IDomainEvent
{
Expand All @@ -10,4 +11,7 @@ public abstract record DomainEvent : IDomainEvent

[JsonInclude]
public DateTimeOffset OccuredAt { get; set; }

[JsonInclude]
public FrozenDictionary<string, string> Metadata { get; set; } = FrozenDictionary<string, string>.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Frozen;

namespace Digdir.Domain.Dialogporten.Domain.Common.DomainEvents;

public sealed class DomainEventContext : IDomainEventContext
{
private readonly Dictionary<string, string> _metadata = new();

public FrozenDictionary<string, string> GetMetadata() => _metadata.ToFrozenDictionary();

public void AddMetadata(string key, string value) => _metadata[key] = value;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Frozen;

namespace Digdir.Domain.Dialogporten.Domain.Common.DomainEvents;

public interface IDomainEventContext
{
FrozenDictionary<string, string> GetMetadata();
void AddMetadata(string key, string value);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Frozen;
using MediatR;

namespace Digdir.Domain.Dialogporten.Domain.Common.EventPublisher;
Expand All @@ -16,4 +17,9 @@ public interface IDomainEvent : INotification
/// The time in which the event, as well as all the actual changes occured.
/// </summary>
DateTimeOffset OccuredAt { get; set; }

/// <summary>
/// Read-only dictionary of metadata.
/// </summary>
public FrozenDictionary<string, string> Metadata { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public sealed class DialogActivity : IImmutableEntity, IAggregateCreatedHandler,
[AggregateChild]
public DialogActivityPerformedByActor PerformedBy { get; set; } = null!;

public void OnCreate(AggregateNode self, DateTimeOffset utcNow)
public void OnCreate(AggregateNode self, DateTimeOffset utcNow, bool disableEvents = false)
{
_domainEvents.Add(new DialogActivityCreatedDomainEvent(
DialogId, Id, TypeId, Dialog.Party,
Expand Down
Loading
Loading