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

Add logic for downloading attachment for A2 GUI #421

Merged
merged 15 commits into from
Nov 6, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
using System.Runtime.CompilerServices;
using Altinn.Correspondence.API.Models;
using Altinn.Correspondence.API.Models.Enums;
using Altinn.Correspondence.Application;
using Altinn.Correspondence.Application.Configuration;
using Altinn.Correspondence.Application.DownloadCorrespondenceAttachment;
using Altinn.Correspondence.Application.GetCorrespondenceDetails;
using Altinn.Correspondence.Application.GetCorrespondenceHistory;
using Altinn.Correspondence.Application.GetCorrespondenceOverview;
using Altinn.Correspondence.Application.GetCorrespondences;
using Altinn.Correspondence.Application.InitializeCorrespondences;
using Altinn.Correspondence.Application.PurgeCorrespondence;
using Altinn.Correspondence.Application.UpdateCorrespondenceStatus;
using Altinn.Correspondence.Application.UpdateMarkAsUnread;
using Altinn.Correspondence.Core.Models.Enums;
using Altinn.Correspondence.Helpers;
using Altinn.Correspondence.Mappers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand Down Expand Up @@ -115,6 +108,30 @@ public async Task<ActionResult<CorrespondencesExt>> GetCorrespondences(
);
}

/// <summary>
/// Download an attachment from a Correspondence
/// </summary>
[HttpGet]
[Route("{correspondenceId}/attachment/{attachmentId}/download")]
[ApiExplorerSettings(IgnoreApi = true)]
public async Task<ActionResult> DownloadCorrespondenceAttachment(
Guid correspondenceId,
Guid attachmentId,
[FromServices] LegacyDownloadCorrespondenceAttachmentHandler handler,
CancellationToken cancellationToken)
{
CelineTrammi marked this conversation as resolved.
Show resolved Hide resolved
var commandResult = await handler.Process(new DownloadCorrespondenceAttachmentRequest()
{
CorrespondenceId = correspondenceId,
AttachmentId = attachmentId
}, cancellationToken);

return commandResult.Match<ActionResult>(
result => File(result.Stream, "application/octet-stream", result.FileName),
Problem
CelineTrammi marked this conversation as resolved.
Show resolved Hide resolved
);
}

private ActionResult Problem(Error error) => Problem(detail: error.Message, statusCode: (int)error.StatusCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static void AddApplicationHandlers(this IServiceCollection services)
services.AddScoped<LegacyGetCorrespondencesHandler>();
services.AddScoped<LegacyGetCorrespondenceOverviewHandler>();
services.AddScoped<LegacyGetCorrespondenceHistoryHandler>();
services.AddScoped<LegacyDownloadCorrespondenceAttachmentHandler>();

// Migration
services.AddScoped<MigrateInitializeAttachmentHandler>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Altinn.Correspondence.Application.Helpers;
using Altinn.Correspondence.Core.Repositories;
using Altinn.Correspondence.Core.Services;
using Altinn.Correspondence.Core.Services.Enums;
using Hangfire;
using OneOf;

namespace Altinn.Correspondence.Application.DownloadCorrespondenceAttachment;

public class LegacyDownloadCorrespondenceAttachmentHandler : IHandler<DownloadCorrespondenceAttachmentRequest, DownloadCorrespondenceAttachmentResponse>
{
private readonly ICorrespondenceRepository _correspondenceRepository;
private readonly IStorageRepository _storageRepository;
private readonly IAttachmentRepository _attachmentRepository;
private readonly IAltinnRegisterService _altinnRegisterService;
private readonly UserClaimsHelper _userClaimsHelper;
private readonly IBackgroundJobClient _backgroundJobClient;

public LegacyDownloadCorrespondenceAttachmentHandler(IStorageRepository storageRepository, IAttachmentRepository attachmentRepository, ICorrespondenceRepository correspondenceRepository, UserClaimsHelper userClaimsHelper, IBackgroundJobClient backgroundJobClient, IAltinnRegisterService altinnRegisterService)
{
_correspondenceRepository = correspondenceRepository;
_storageRepository = storageRepository;
_attachmentRepository = attachmentRepository;
_altinnRegisterService = altinnRegisterService;
_userClaimsHelper = userClaimsHelper;
_backgroundJobClient = backgroundJobClient;
}

public async Task<OneOf<DownloadCorrespondenceAttachmentResponse, Error>> Process(DownloadCorrespondenceAttachmentRequest request, CancellationToken cancellationToken)
{
var partyId = _userClaimsHelper.GetPartyId();
if (partyId is null)
{
return Errors.InvalidPartyId;
}
var party = await _altinnRegisterService.LookUpPartyByPartyId(partyId.Value, cancellationToken);
if (party is null || (string.IsNullOrEmpty(party.SSN) && string.IsNullOrEmpty(party.OrgNumber)))
{
return Errors.CouldNotFindOrgNo;
}

var correspondence = await _correspondenceRepository.GetCorrespondenceById(request.CorrespondenceId, true, false, cancellationToken);
if (correspondence is null)
{
return Errors.CorrespondenceNotFound;
}
var attachment = await _attachmentRepository.GetAttachmentByCorrespondenceIdAndAttachmentId(request.CorrespondenceId, request.AttachmentId, cancellationToken);
if (attachment is null)
{
return Errors.AttachmentNotFound;
}
bool isRecipient = correspondence.Recipient == ("0192:"+party.OrgNumber) || correspondence.Recipient == party.SSN;
if (!isRecipient)
{
return Errors.CorrespondenceNotFound;
}
var latestStatus = correspondence.GetLatestStatus();
if (!latestStatus.Status.IsAvailableForRecipient())
{
return Errors.CorrespondenceNotFound;
}
var attachmentStream = await _storageRepository.DownloadAttachment(attachment.Id, cancellationToken);
_backgroundJobClient.Enqueue<IDialogportenService>((dialogportenService) => dialogportenService.CreateInformationActivity(request.CorrespondenceId, DialogportenActorType.Recipient, DialogportenTextType.DownloadStarted, attachment.FileName ?? attachment.Name));
CelineTrammi marked this conversation as resolved.
Show resolved Hide resolved
return new DownloadCorrespondenceAttachmentResponse(){
FileName = attachment.FileName ?? attachment.Name,
Stream = attachmentStream
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class UserClaimsHelper
private const string _consumerClaim = "consumer";
private const string _IdProperty = "ID";
private const string _dialogportenOrgClaim = "p";
private const string _partyIdClaim = "urn:altinn:partyid";

public UserClaimsHelper(IHttpContextAccessor httpContextAccessor, IOptions<DialogportenSettings> dialogportenSettings, IOptions<IdportenSettings> idportenSettings)
{
Expand All @@ -26,6 +27,13 @@ public UserClaimsHelper(IHttpContextAccessor httpContextAccessor, IOptions<Dialo
_dialogportenSettings = dialogportenSettings.Value;
_idportenSettings = idportenSettings.Value;
}
public int? GetPartyId()
{
var partyId = _claims.FirstOrDefault(c => c.Type == _partyIdClaim)?.Value;
if (partyId is null) return null;
if (int.TryParse(partyId, out int id)) return id;
return null;
}
public bool IsAffiliatedWithCorrespondence(string recipientId, string senderId)
{
return IsRecipient(recipientId) || IsSender(senderId);
Expand Down
Loading