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

initialize correspondence #54

Merged
merged 6 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# altinn-correspondence

## Entity Framework
Correspondence uses Entity Framework.
When run locally, it applies migrations automaticly on startup

## Local Development

The start.ps1 script runs all neccassary commands to run the project.

# Entity Framework
Correspondence uses Entity Framework.
When run locally, it applies migrations automaticly on startup
Installing Dotnet 8.0 is a pre-requisite.

## Adding migrations:
To add a new migration you can run the following command:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<ActionResult<AttachmentOverviewExt>> InitializeAttachment(Init
_logger.LogInformation("Initialize attachment");

return commandResult.Match(
fileTransferId => Ok(fileTransferId.ToString()),
id => Ok(id.ToString()),
Problem
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.API.Models.Enums;
using Altinn.Correspondence.Application;
using Altinn.Correspondence.Application.InitializeCorrespondenceCommand;
using Altinn.Correspondence.Helpers;
using Microsoft.ApplicationInsights.AspNetCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Altinn.Correspondence.Mappers;
using Microsoft.AspNetCore.Mvc;

namespace Altinn.Correspondence.API.Controllers
Expand All @@ -27,27 +28,17 @@ public CorrespondenceController(ILogger<CorrespondenceController> logger)
/// </remarks>
/// <returns></returns>
[HttpPost]
public async Task<ActionResult<CorrespondenceOverviewExt>> InitializeCorrespondence(InitializeCorrespondenceExt initializeCorrespondence)
public async Task<ActionResult<CorrespondenceOverviewExt>> InitializeCorrespondence(InitializeCorrespondenceExt initializeCorrespondence, [FromServices] InitializeCorrespondenceCommandHandler handler, CancellationToken cancellationToken)
{
LogContextHelpers.EnrichLogsWithInsertCorrespondence(initializeCorrespondence);
_logger.LogInformation("Initialize correspondence");

// Hack return for now
return Ok(
new CorrespondenceOverviewExt
{
CorrespondenceId = Guid.NewGuid(),
Recipient = initializeCorrespondence.Recipient,
Content = (CorrespondenceContentExt)initializeCorrespondence.Content,
ResourceId = initializeCorrespondence.ResourceId,
Sender = initializeCorrespondence.Sender,
SendersReference = initializeCorrespondence.SendersReference,
Created = DateTime.Now,
VisibleFrom = initializeCorrespondence.VisibleFrom,
Status = CorrespondenceStatusExt.Initialized,
StatusText = "Initialized Successfully - waiting for attachment upload",
StatusChanged = DateTime.Now
}
var commandRequest = InitializeCorrespondenceMapper.MapToRequest(initializeCorrespondence);
var commandResult = await handler.Process(commandRequest, cancellationToken);

return commandResult.Match(
id => Ok(id.ToString()),
Problem
);
}

Expand Down Expand Up @@ -96,7 +87,7 @@ public async Task<ActionResult<CorrespondenceOverviewExt>> InitializeCorresponde
[Route("{correspondenceId}")]
public async Task<ActionResult<CorrespondenceOverviewExt>> GetCorrespondenceOverview(
Guid correspondenceId)
{
{
_logger.LogInformation("Getting Correspondence overview for {correspondenceId}", correspondenceId.ToString());

// Hack return for now
Expand Down Expand Up @@ -149,11 +140,12 @@ public async Task<ActionResult<CorrespondenceDetailsExt>> GetCorrespondenceDetai
new CorrespondenceNotificationOverviewExt { NotificationId = Guid.NewGuid(), NotificationTemplate = "EmailReminder", Created = DateTime.Now.AddDays(-1), RequestedSendTime = DateTime.Now.AddDays(13), NotificationChannel = NotificationChannelExt.Sms }
},
StatusHistory = new List<CorrespondenceStatusEventExt>() {
new CorrespondenceStatusEventExt { Status = CorrespondenceStatusExt.Initialized, StatusChanged = DateTime.Now.AddDays(-1), StatusText = "Initialized - awaiting upload" },
new CorrespondenceStatusEventExt { Status = CorrespondenceStatusExt.Initialized, StatusChanged = DateTime.Now.AddDays(-1), StatusText = "Initialized - awaiting upload" },
new CorrespondenceStatusEventExt { Status = CorrespondenceStatusExt.Published, StatusChanged = DateTime.Now.AddDays(-1).AddMinutes(2), StatusText = "Published - Ready for use" }
}
}
);
}
private ObjectResult Problem(Error error) => Problem(detail: error.Message, statusCode: (int)error.StatusCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.Core.Models;

namespace Altinn.Correspondence.Mappers;

internal static class CorrespondenceNotificationMapper
{
internal static CorrespondenceNotificationEntity MapToEntity(InitializeCorrespondenceNotificationExt correspondenceNotificationExt)
{
var notification = new CorrespondenceNotificationEntity
{
NotificationTemplate = correspondenceNotificationExt.NotificationTemplate,
RequestedSendTime = correspondenceNotificationExt.RequestedSendTime,
SendersReference = correspondenceNotificationExt.SendersReference,
CustomTextToken = correspondenceNotificationExt.CustomTextToken
};
return notification;
}

internal static List<CorrespondenceNotificationEntity> MapListToEntities(List<InitializeCorrespondenceNotificationExt> notificationsExt)
{
var notifications = new List<CorrespondenceNotificationEntity>();
foreach (var not in notificationsExt)
{
notifications.Add(MapToEntity(not));
}
return notifications;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.Core.Models;

namespace Altinn.Correspondence.Mappers;

internal static class CorrespondenceReplyOptionsMapper
{
internal static CorrespondenceReplyOptionEntity MapToEntity(CorrespondenceReplyOptionExt correspondenceReplyOptionExt)
{
var ReplyOptions = new CorrespondenceReplyOptionEntity
{
LinkText = correspondenceReplyOptionExt.LinkText,
LinkURL = correspondenceReplyOptionExt.LinkURL
};
return ReplyOptions;
}

internal static List<CorrespondenceReplyOptionEntity> MapListToEntities(List<CorrespondenceReplyOptionExt> replyOptionsExt)
{
var replyOptions = new List<CorrespondenceReplyOptionEntity>();
foreach (var replyOption in replyOptionsExt)
{
replyOptions.Add(MapToEntity(replyOption));
}
return replyOptions;
}
}
27 changes: 27 additions & 0 deletions src/Altinn.Correspondence.API/Mappers/ExternalReferenceMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.Core.Models;
using Altinn.Correspondence.Core.Models.Enums;

namespace Altinn.Correspondence.Mappers;

internal static class ExternalReferenceMapper
{
internal static ExternalReferenceEntity MapToEntity(ExternalReferenceExt externalReferenceExt)
{
var externalReference = new ExternalReferenceEntity
{
ReferenceValue = externalReferenceExt.ReferenceValue,
ReferenceType = (ReferenceType)externalReferenceExt.ReferenceType
};
return externalReference;
}
internal static List<ExternalReferenceEntity> MapListToEntities(List<ExternalReferenceExt> externalReferencesExt)
{
var externalReferences = new List<ExternalReferenceEntity>();
foreach (var extRef in externalReferencesExt)
{
externalReferences.Add(MapToEntity(extRef));
}
return externalReferences;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ internal static InitializeAttachmentCommandRequest MapToRequest(InitializeAttach
{
var attachment = new AttachmentEntity
{
Name = initializeAttachmentExt.Name,
FileName = initializeAttachmentExt.FileName,
SendersReference = initializeAttachmentExt.SendersReference,
DataType = initializeAttachmentExt.DataType,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.Core.Models;
using Altinn.Correspondence.Core.Models.Enums;

namespace Altinn.Correspondence.Mappers;

internal static class InitializeCorrespondenceAttachmentMapper
{
internal static List<CorrespondenceAttachmentEntity> MapListToEntities(List<InitializeCorrespondenceAttachmentExt> initializeAttachmentExt)
{
var attachments = new List<CorrespondenceAttachmentEntity>();
foreach (var attachment in initializeAttachmentExt)
{
attachments.Add(MapToEntity(attachment));
}
return attachments;
}
internal static CorrespondenceAttachmentEntity MapToEntity(InitializeCorrespondenceAttachmentExt initializeAttachmentExt)
{
return new CorrespondenceAttachmentEntity
{
Name = initializeAttachmentExt.Name,
SendersReference = initializeAttachmentExt.SendersReference,
DataType = initializeAttachmentExt.DataType,
IntendedPresentation = (IntendedPresentationType)initializeAttachmentExt.IntendedPresentation,
Checksum = initializeAttachmentExt.Checksum,
IsEncrypted = initializeAttachmentExt.IsEncrypted
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.Application.InitializeCorrespondenceCommand;
using Altinn.Correspondence.Core.Models;
using Altinn.Correspondence.Core.Models.Enums;

namespace Altinn.Correspondence.Mappers;

internal static class InitializeCorrespondenceMapper
{
internal static InitializeCorrespondenceCommandRequest MapToRequest(InitializeCorrespondenceExt initializeCorrespondenceExt)
{
var correspondence = new CorrespondenceEntity
{
SendersReference = initializeCorrespondenceExt.SendersReference,
Recipient = initializeCorrespondenceExt.Recipient,
ResourceId = initializeCorrespondenceExt.ResourceId,
Sender = initializeCorrespondenceExt.Sender,
VisibleFrom = initializeCorrespondenceExt.VisibleFrom,
AllowSystemDeleteAfter = initializeCorrespondenceExt.AllowSystemDeleteAfter,
DueDateTime = initializeCorrespondenceExt.DueDateTime,
PropertyList = initializeCorrespondenceExt.PropertyList,
ReplyOptions = CorrespondenceReplyOptionsMapper.MapListToEntities(initializeCorrespondenceExt.ReplyOptions),
IsReservable = initializeCorrespondenceExt.IsReservable,
Notifications = CorrespondenceNotificationMapper.MapListToEntities(initializeCorrespondenceExt.Notifications),
Content = new CorrespondenceContentEntity
{
Language = initializeCorrespondenceExt.Content.Language,
MessageTitle = initializeCorrespondenceExt.Content.MessageTitle,
MessageSummary = initializeCorrespondenceExt.Content.MessageSummary,
Attachments = InitializeCorrespondenceAttachmentMapper.MapListToEntities(initializeCorrespondenceExt.Content.Attachments)
}
};
return new InitializeCorrespondenceCommandRequest()
{
correspondence = correspondence,
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Altinn.Correspondence.API.Models.Enums;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization;

namespace Altinn.Correspondence.API.Models
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public enum InitializeAttachmentDataLocationTypeExt : int
/// Specifies that the attachment data will need to be uploaded to Altinn Correspondence via the Upload Attachment operation
/// </summary>
NewCorrespondenceAttachment = 0,

/// <summary>
/// Specifies that the attachment already exist in Altinn Correpondence storage
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public enum IntendedPresentationTypeExt : int
/// <summary>
/// Human-readable content to be displayed in GUI, such as Message Body
/// </summary>
HumanReadable = 0,
HumanReadable = 0,

/// <summary>
/// Machine-readable content not to be displayed in GUI, but intended for system consumption or download to disk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public class InitializeCorrespondenceContentExt
/// </remarks>
[JsonPropertyName("attachments")]
public List<InitializeCorrespondenceAttachmentExt> Attachments { get; set; }

/// <summary>
/// Ids of the attachments that are to be included in the correspondence.
/// </summary>
[JsonPropertyName("attachmentIds")]
public List<Guid>? AttachmentIds { get; set; }
}

[AttributeUsage(AttributeTargets.Property)]
Expand Down
2 changes: 2 additions & 0 deletions src/Altinn.Correspondence.Application/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Altinn.Correspondence.Application.InitializeAttachmentCommand;
using Altinn.Correspondence.Application.InitializeCorrespondenceCommand;
using Microsoft.Extensions.DependencyInjection;

namespace Altinn.Correspondence.Application;
Expand All @@ -7,5 +8,6 @@ public static class DependencyInjection
public static void AddApplicationHandlers(this IServiceCollection services)
{
services.AddScoped<InitializeAttachmentCommandHandler>();
services.AddScoped<InitializeCorrespondenceCommandHandler>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

namespace Altinn.Correspondence.Application.InitializeAttachmentCommand;

public class InitializeAttachmentCommandHandler : IHandler<InitializeAttachmentCommandRequest, int>
public class InitializeAttachmentCommandHandler : IHandler<InitializeAttachmentCommandRequest, Guid>
{

private readonly IAttachmentRepository _attachmentRepository;
private readonly IAttachmentStatusRepository _attachmentStatusRepository;
public InitializeAttachmentCommandHandler(IAttachmentRepository attachmentRepository, IAttachmentStatusRepository attachmentStatusRepository)
Expand All @@ -15,7 +14,7 @@ public InitializeAttachmentCommandHandler(IAttachmentRepository attachmentReposi
_attachmentStatusRepository = attachmentStatusRepository;
}

public async Task<OneOf<int, Error>> Process(InitializeAttachmentCommandRequest request, CancellationToken cancellationToken)
public async Task<OneOf<Guid, Error>> Process(InitializeAttachmentCommandRequest request, CancellationToken cancellationToken)
{
var attachmentId = await _attachmentRepository.InitializeAttachment(request.Attachment, cancellationToken);
var status = new AttachmentStatusEntity
Expand Down
Loading