Skip to content

Commit

Permalink
Add Console log event bus (#95)
Browse files Browse the repository at this point in the history
* Add Console log event bus

* cleanup

---------

Co-authored-by: Hammerbeck <[email protected]>
  • Loading branch information
Andreass2 and Hammerbeck authored Jun 4, 2024
1 parent c08fb0a commit f391a5f
Show file tree
Hide file tree
Showing 19 changed files with 238 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Altinn.Correspondence.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{DC791FEF-1
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Altinn.Correspondence.Tests", "Test\Altinn.Correspondence.Tests\Altinn.Correspondence.Tests.csproj", "{D78A36B1-D457-4FF7-B01E-82A0C3C64EDC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Altinn.Correspondence.Integrations", "src\Altinn.Correspondence.Integrations\Altinn.Correspondence.Integrations.csproj", "{5C88BAAD-CF82-4D7A-8FB6-879605EF4E03}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -41,6 +43,10 @@ Global
{D78A36B1-D457-4FF7-B01E-82A0C3C64EDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D78A36B1-D457-4FF7-B01E-82A0C3C64EDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D78A36B1-D457-4FF7-B01E-82A0C3C64EDC}.Release|Any CPU.Build.0 = Release|Any CPU
{5C88BAAD-CF82-4D7A-8FB6-879605EF4E03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5C88BAAD-CF82-4D7A-8FB6-879605EF4E03}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C88BAAD-CF82-4D7A-8FB6-879605EF4E03}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C88BAAD-CF82-4D7A-8FB6-879605EF4E03}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<ItemGroup>
<ProjectReference Include="..\Altinn.Correspondence.Core\Altinn.Correspondence.Core.csproj" />
<ProjectReference Include="..\Altinn.Correspondence.Application\Altinn.Correspondence.Application.csproj" />
<ProjectReference Include="..\Altinn.Correspondence.Integrations\Altinn.Correspondence.Integrations.csproj" />
<ProjectReference Include="..\Altinn.Correspondence.Persistence\Altinn.Correspondence.Persistence.csproj" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions src/Altinn.Correspondence.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Altinn.Correspondence.Application;
using Altinn.Correspondence.Core.Options;
using Altinn.Correspondence.Integrations;
using Altinn.Correspondence.Persistence;
using Azure.Identity;
using Microsoft.AspNetCore.Http.Features;
Expand Down Expand Up @@ -61,6 +62,7 @@ static void ConfigureServices(IServiceCollection services, IConfiguration config

services.AddApplicationHandlers();
services.AddPersistence();
services.AddIntegrations();

services.AddHttpClient();
services.AddProblemDetails();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="OneOf" Version="3.0.263" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Altinn.Correspondence.Application.GetCorrespondenceOverviewCommand;
using Altinn.Correspondence.Application.GetCorrespondencesCommand;
using Altinn.Correspondence.Application.DownloadAttachmentQuery;
using Altinn.Correspondence.Application.GetCorrespondencesCommand;
using Altinn.Correspondence.Application.InitializeAttachmentCommand;
using Altinn.Correspondence.Application.InitializeCorrespondenceCommand;
using Altinn.Correspondence.Application.UploadAttachmentCommand;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Altinn.Correspondence.Core.Models;
using Altinn.Correspondence.Core.Models.Enums;
using Altinn.Correspondence.Core.Repositories;
using Altinn.Correspondence.Core.Services;
using Altinn.Correspondence.Core.Services.Enums;
using OneOf;

namespace Altinn.Correspondence.Application.InitializeAttachmentCommand;
Expand All @@ -9,10 +11,12 @@ public class InitializeAttachmentCommandHandler : IHandler<InitializeAttachmentC
{
private readonly IAttachmentRepository _attachmentRepository;
private readonly IAttachmentStatusRepository _attachmentStatusRepository;
public InitializeAttachmentCommandHandler(IAttachmentRepository attachmentRepository, IAttachmentStatusRepository attachmentStatusRepository)
private readonly IEventBus _eventBus;
public InitializeAttachmentCommandHandler(IAttachmentRepository attachmentRepository, IAttachmentStatusRepository attachmentStatusRepository, IEventBus eventBus)
{
_attachmentRepository = attachmentRepository;
_attachmentStatusRepository = attachmentStatusRepository;
_eventBus = eventBus;
}

public async Task<OneOf<Guid, Error>> Process(InitializeAttachmentCommandRequest request, CancellationToken cancellationToken)
Expand All @@ -26,6 +30,7 @@ public async Task<OneOf<Guid, Error>> Process(InitializeAttachmentCommandRequest
StatusText = AttachmentStatus.Initialized.ToString()
};
await _attachmentStatusRepository.AddAttachmentStatus(status, cancellationToken);
await _eventBus.Publish(AltinnEventType.AttachmentInitialized, null, attachmentId.ToString(), "attachment", null, cancellationToken);
return attachmentId;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Altinn.Correspondence.Core.Models;
using Altinn.Correspondence.Core.Models.Enums;
using Altinn.Correspondence.Core.Repositories;
using Altinn.Correspondence.Core.Services;
using Altinn.Correspondence.Core.Services.Enums;
using OneOf;

namespace Altinn.Correspondence.Application.InitializeCorrespondenceCommand;
Expand All @@ -9,10 +11,12 @@ public class InitializeCorrespondenceCommandHandler : IHandler<InitializeCorresp
{
private readonly ICorrespondenceRepository _correspondenceRepository;
private readonly IAttachmentRepository _attachmentRepository;
public InitializeCorrespondenceCommandHandler(ICorrespondenceRepository correspondenceRepository, IAttachmentRepository attachmentRepository)
private readonly IEventBus _eventBus;
public InitializeCorrespondenceCommandHandler(ICorrespondenceRepository correspondenceRepository, IAttachmentRepository attachmentRepository, IEventBus eventBus)
{
_correspondenceRepository = correspondenceRepository;
_attachmentRepository = attachmentRepository;
_eventBus = eventBus;
}

public async Task<OneOf<InitializeCorrespondenceCommandResponse, Error>> Process(InitializeCorrespondenceCommandRequest request, CancellationToken cancellationToken)
Expand All @@ -31,6 +35,7 @@ public async Task<OneOf<InitializeCorrespondenceCommandResponse, Error>> Process
};
request.Correspondence.Statuses = statuses;
var correspondence = await _correspondenceRepository.InitializeCorrespondence(request.Correspondence, cancellationToken);
await _eventBus.Publish(AltinnEventType.CorrespondenceInitialized, null, correspondence.Id.ToString(), "correspondence", null, cancellationToken);
return new InitializeCorrespondenceCommandResponse()
{
CorrespondenceId = correspondence.Id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Altinn.Correspondence.Core.Models;
using Altinn.Correspondence.Core.Models.Enums;
using Altinn.Correspondence.Core.Repositories;
using Altinn.Correspondence.Core.Services;
using Altinn.Correspondence.Core.Services.Enums;
using OneOf;

namespace Altinn.Correspondence.Application.UpdateCorrespondenceStatusCommand;
Expand All @@ -9,10 +11,12 @@ public class UpdateCorrespondenceStatusCommandHandler : IHandler<UpdateCorrespon
{
private readonly ICorrespondenceRepository _correspondenceRepository;
private readonly ICorrespondenceStatusRepository _correspondenceStatusRepository;
public UpdateCorrespondenceStatusCommandHandler(ICorrespondenceRepository correspondenceRepository, ICorrespondenceStatusRepository correspondenceStatusRepository)
private readonly IEventBus _eventBus;
public UpdateCorrespondenceStatusCommandHandler(ICorrespondenceRepository correspondenceRepository, ICorrespondenceStatusRepository correspondenceStatusRepository, IEventBus eventBus)
{
_correspondenceRepository = correspondenceRepository;
_correspondenceStatusRepository = correspondenceStatusRepository;
_eventBus = eventBus;
}

public async Task<OneOf<Guid, Error>> Process(UpdateCorrespondenceStatusCommandRequest request, CancellationToken cancellationToken)
Expand All @@ -33,13 +37,27 @@ public async Task<OneOf<Guid, Error>> Process(UpdateCorrespondenceStatusCommandR
return request.CorrespondenceId;
}


await _correspondenceStatusRepository.AddCorrespondenceStatus(new CorrespondenceStatusEntity
{
CorrespondenceId = request.CorrespondenceId,
Status = request.Status,
StatusChanged = DateTimeOffset.UtcNow,
StatusText = request.Status.ToString(),
}, cancellationToken);
await PublishEvent(request.CorrespondenceId, request.Status, cancellationToken);
return request.CorrespondenceId;
}

private async Task PublishEvent(Guid correspondenceId, CorrespondenceStatus status, CancellationToken cancellationToken)
{
if (status == CorrespondenceStatus.Confirmed)
{
await _eventBus.Publish(AltinnEventType.CorrespondenceReceiverConfirmed, null, correspondenceId.ToString(), "correspondence", null, cancellationToken);
}
else if (status == CorrespondenceStatus.Read)
{
await _eventBus.Publish(AltinnEventType.CorrespondenceReceiverRead, null, correspondenceId.ToString(), "correspondence", null, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using Altinn.Correspondence.Core.Models;
using Altinn.Correspondence.Core.Models.Enums;
using Altinn.Correspondence.Core.Repositories;
using Altinn.Correspondence.Core.Services;
using Altinn.Correspondence.Core.Services.Enums;
using OneOf;

namespace Altinn.Correspondence.Application.UploadAttachmentCommand;

public class UploadAttachmentCommandHandler(IAttachmentRepository attachmentRepository, IAttachmentStatusRepository attachmentStatusRepository, IStorageRepository storageRepository, ICorrespondenceRepository correspondenceRepository, ICorrespondenceStatusRepository correspondenceStatusRepository) : IHandler<UploadAttachmentCommandRequest, UploadAttachmentCommandResponse>
public class UploadAttachmentCommandHandler(IAttachmentRepository attachmentRepository, IAttachmentStatusRepository attachmentStatusRepository, IStorageRepository storageRepository, ICorrespondenceRepository correspondenceRepository, ICorrespondenceStatusRepository correspondenceStatusRepository, IEventBus eventBus) : IHandler<UploadAttachmentCommandRequest, UploadAttachmentCommandResponse>
{
private readonly IAttachmentRepository _attachmentRepository = attachmentRepository;
private readonly IAttachmentStatusRepository _attachmentStatusRepository = attachmentStatusRepository;
private readonly ICorrespondenceRepository _correspondenceRepository = correspondenceRepository;
private readonly ICorrespondenceStatusRepository _correspondenceStatusRepository = correspondenceStatusRepository;
private readonly IStorageRepository _storageRepository = storageRepository;
private readonly IEventBus _eventBus = eventBus;

public async Task<OneOf<UploadAttachmentCommandResponse, Error>> Process(UploadAttachmentCommandRequest request, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -60,6 +63,7 @@ await _attachmentStatusRepository.AddAttachmentStatus(new AttachmentStatusEntity
await _attachmentStatusRepository.AddAttachmentStatus(publishStatus, cancellationToken);
await CheckCorrespondenceStatusesAfterUploadAndPublish(attachment.Id, cancellationToken);

await _eventBus.Publish(AltinnEventType.AttachmentPublished, null, request.AttachmentId.ToString(), "attachment", null, cancellationToken);
return new UploadAttachmentCommandResponse()
{
AttachmentId = attachment.Id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions src/Altinn.Correspondence.Core/Options/AltinnOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Altinn.Correspondence.Core.Options;

public class AltinnOptions
{
public string OpenIdWellKnown { get; set; } = string.Empty;
public string PlatformGatewayUrl { get; set; } = string.Empty;
}
24 changes: 24 additions & 0 deletions src/Altinn.Correspondence.Core/Services/Enums/AltinnEventType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Altinn.Correspondence.Core.Services.Enums;

public enum AltinnEventType
{
AttachmentInitialized,
AttachmentUploadProcessing,
AttachmentPublished,
AttachmentUploadFailed,
AttachmentPurged,
AttachmentDownloaded,

CorrespondenceInitialized,
CorrespondencePublished,
CorrespondenceArchived,
CorrespondencePurged,

CorrespondenceReceiverRead,
CorrespondenceReceiverConfirmed,
CorrespondenceReceiverReplied,
CorrespondenceReceiverNeverConfirmed,
CorrespondenceReceiverReserved,


}
8 changes: 8 additions & 0 deletions src/Altinn.Correspondence.Core/Services/IEventBus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Altinn.Correspondence.Core.Services.Enums;

namespace Altinn.Correspondence.Core.Services;

public interface IEventBus
{
Task Publish(AltinnEventType type, string resourceId, string itemId, string eventSource, string? organizationId = null, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Altinn.Correspondence.Core\Altinn.Correspondence.Core.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Net.Http.Json;
using System.Text.Json;

using Altinn.Correspondence.Core.Options;
using Altinn.Correspondence.Core.Repositories;
using Altinn.Correspondence.Core.Services;
using Altinn.Correspondence.Core.Services.Enums;
using Altinn.Correspondence.Integrations.Altinn.Events.Helpers;

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Altinn.Correspondence.Integrations.Altinn.Events;
public class AltinnEventBus : IEventBus
{
private readonly AltinnOptions _altinnOptions;
private readonly HttpClient _httpClient;
private readonly ILogger<AltinnEventBus> _logger;

public AltinnEventBus(HttpClient httpClient, IOptions<AltinnOptions> altinnOptions, ILogger<AltinnEventBus> logger)
{
_httpClient = httpClient;
_altinnOptions = altinnOptions.Value;
_logger = logger;
}

public async Task Publish(AltinnEventType type, string resourceId, string itemId, string eventSource, string? organizationId = null, CancellationToken cancellationToken = default)
{
string? partyId = null;
// TODO: Get party id

var cloudEvent = CreateCloudEvent(type, resourceId, itemId, partyId, organizationId, eventSource);
var serializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = new LowerCaseNamingPolicy()
};
var response = await _httpClient.PostAsync("events/api/v1/events", JsonContent.Create(cloudEvent, options: serializerOptions, mediaType: new System.Net.Http.Headers.MediaTypeHeaderValue("application/cloudevents+json")), cancellationToken);
if (!response.IsSuccessStatusCode)
{
_logger.LogError("Unexpected null or invalid json response when posting cloud event {type} of {resourceId} with {eventSource} id {itemId}.", type, resourceId, eventSource, itemId);
_logger.LogError("Statuscode was: {}, error was: {error}", response.StatusCode, await response.Content.ReadAsStringAsync(cancellationToken));
}
}

private CloudEvent CreateCloudEvent(AltinnEventType type, string resourceId, string itemId, string? partyId, string? alternativeSubject, string eventSource)
{
CloudEvent cloudEvent = new CloudEvent()
{
Id = Guid.NewGuid(),
SpecVersion = "1.0",
Time = DateTime.UtcNow,
Resource = "urn:altinn:resource:" + resourceId,
ResourceInstance = itemId,
Type = "no.altinn.correspondence." + type.ToString().ToLowerInvariant(),
Source = _altinnOptions.PlatformGatewayUrl + "correspondence/api/v1/" + eventSource,
Subject = !string.IsNullOrWhiteSpace(partyId) ? "/party/" + partyId : null,
AlternativeSubject = !string.IsNullOrWhiteSpace(alternativeSubject) ? "/organisation/" + alternativeSubject : null,
};

return cloudEvent;
}
}

15 changes: 15 additions & 0 deletions src/Altinn.Correspondence.Integrations/Altinn/Events/CloudEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Altinn.Correspondence.Integrations.Altinn.Events;

public class CloudEvent
{
public string SpecVersion { get; set; } = "1.0";
public Guid Id { get; set; }
public string Type { get; set; } = null!;
public DateTimeOffset Time { get; set; }
public string Resource { get; set; } = null!;
public string ResourceInstance { get; set; } = null!;
public string? Subject { get; set; }
public string? AlternativeSubject { get; set; }
public string Source { get; set; } = null!;
public Dictionary<string, object>? Data { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Altinn.Correspondence.Core.Services;
using Altinn.Correspondence.Core.Services.Enums;

using Microsoft.Extensions.Logging;

namespace Altinn.Correspondence.Integrations.Altinn.Events;
public class ConsoleLogEventBus : IEventBus
{
private readonly ILogger<ConsoleLogEventBus> _logger;

public ConsoleLogEventBus(ILogger<ConsoleLogEventBus> logger)
{
_logger = logger;
}

public Task Publish(AltinnEventType type, string resourceId, string itemId, string eventSource, string? organizationId = null, CancellationToken cancellationToken = default)
{
_logger.LogInformation("{CloudEventType} event raised on instance {eventSource} {itemId} for party with organization number {organizationId}", type.ToString(), eventSource, itemId, organizationId);
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

using System.Text.Json;

namespace Altinn.Correspondence.Integrations.Altinn.Events.Helpers;
internal class LowerCaseNamingPolicy : JsonNamingPolicy
{
public override string ConvertName(string name)
{
return name.ToLower();
}
}
Loading

0 comments on commit f391a5f

Please sign in to comment.