Skip to content

Commit

Permalink
Add logic for downloading attachment for A2 GUI (#421)
Browse files Browse the repository at this point in the history
* Add logic for downloading attachment for A2 GUI

* null check fileName

* Checkout before using action (#423)

* Fix deploy permission (#425)

* Fix: Ensure correspondence is fetched before updating to Read or Confirmed (#427)

* fix: validate correspondence has been fetched before it can be read and confirmed

* update tests

* typo fix

* Handle potential null value for IsConfirmationNeeded

* reuse factory for client initialization

* rename testname

* add Fetched to statuses available for Recipient

* Legacy/overview auth! (#430)

* Initial functionality for legacy correspondenceOverview

* improvements

* better naming

* better text for api route

* add auth for legacy overview

* better error message

* fix variable names

* improve minAuthLevel function

* Better naming

---------

Co-authored-by: Hammerbeck <[email protected]>

* Missing reference to Github environment in CI/CD (#436)

* Add logic to check if user is recipient

* Fix deploy permission (#425)

* Fix: Ensure correspondence is fetched before updating to Read or Confirmed (#427)

* fix: validate correspondence has been fetched before it can be read and confirmed

* update tests

* typo fix

* Handle potential null value for IsConfirmationNeeded

* reuse factory for client initialization

* rename testname

* add Fetched to statuses available for Recipient

* Missing reference to Github environment in CI/CD (#436)

* remove duplicate and rewrite userClaim logic

* remove unused usings

* add norwegian org prefix to recipient check

---------

Co-authored-by: Roar Mjelde <[email protected]>
Co-authored-by: Andreas Hammerbeck <[email protected]>
Co-authored-by: Hammerbeck <[email protected]>
  • Loading branch information
4 people authored Nov 6, 2024
1 parent e6e2d6e commit 4cb90ac
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 8 deletions.
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)
{
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
);
}

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));
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

0 comments on commit 4cb90ac

Please sign in to comment.