Sitelet https://github.com/Altinn/app-lib-dotnet/pull/1872/files
Skip to content
Open
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
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageVersion Include="Altinn.Common.EFormidlingClient" Version="1.3.3" />
<PackageVersion Include="Altinn.Common.PEP" Version="4.2.3" />
<PackageVersion Include="Altinn.Platform.Models" Version="1.6.1" />
<PackageVersion Include="Altinn.Platform.Storage.Interface" Version="4.7.1" />
<PackageVersion Include="Altinn.Platform.Storage.Interface" Version="4.7.2" />
<PackageVersion Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.5.1" />
<PackageVersion Include="Azure.Identity" Version="1.21.0" />
<PackageVersion Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.8.1" />
Expand Down
122 changes: 88 additions & 34 deletions src/Altinn.App.Api/Controllers/InstancesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -626,12 +626,51 @@ public async Task<ActionResult<InstanceResponse>> PostSimplified(
);
}

Instance? source = null;

if (isCopyRequest)
{
string[] sourceSplit =
instansiationInstance.SourceInstanceId?.Split("/")
?? throw new ArgumentException("SourceInstanceId is null or not in the correct format");
Guid sourceInstanceGuid = Guid.Parse(sourceSplit[1]);

try
{
source = await _instanceClient.GetInstance(
app,
org,
party.PartyId,
sourceInstanceGuid,
authenticationMethod: null,
CancellationToken.None
);
}
catch (PlatformHttpException exception)
{
return StatusCode(
500,
$"Retrieving source instance failed with status code {exception.Response.StatusCode}"
);
}

if (source.Status?.IsArchived is not true)
{
return BadRequest("It is not possible to copy an instance that isn't archived.");
}
}

Instance instanceTemplate = new()
{
InstanceOwner = instansiationInstance.InstanceOwner,
VisibleAfter = instansiationInstance.VisibleAfter,
DueBefore = instansiationInstance.DueBefore,
DueBefore = instansiationInstance.DueBefore ?? GetDueBeforeFromSource(application, source),
Comment thread
ivarne marked this conversation as resolved.
Org = application.Org,
DataValues = SelectIncludedValues(application.CopyInstanceSettings?.IncludedDataValues, source?.DataValues),
PresentationTexts = SelectIncludedValues(
application.CopyInstanceSettings?.IncludedPresentationTexts,
source?.PresentationTexts
),
};

ConditionallySetReadStatus(instanceTemplate);
Expand Down Expand Up @@ -667,39 +706,8 @@ public async Task<ActionResult<InstanceResponse>> PostSimplified(

processResult = await _processEngine.GenerateProcessStartEvents(request);

Instance? source = null;

if (isCopyRequest)
if (source is not null)
{
string[] sourceSplit =
instansiationInstance.SourceInstanceId?.Split("/")
?? throw new ArgumentException("SourceInstanceId is null or not in the correct format");
Guid sourceInstanceGuid = Guid.Parse(sourceSplit[1]);

try
{
source = await _instanceClient.GetInstance(
app,
org,
party.PartyId,
sourceInstanceGuid,
authenticationMethod: null,
CancellationToken.None
);
}
catch (PlatformHttpException exception)
{
return StatusCode(
500,
$"Retrieving source instance failed with status code {exception.Response.StatusCode}"
);
}

if (source.Status?.IsArchived is not true)
{
return BadRequest("It is not possible to copy an instance that isn't archived.");
}

var copyInstanceValidator = _appImplementationFactory.Get<ICopyInstanceValidator>();
if (copyInstanceValidator is not null)
{
Expand Down Expand Up @@ -730,7 +738,7 @@ public async Task<ActionResult<InstanceResponse>> PostSimplified(
CancellationToken.None
);

if (isCopyRequest && source is not null)
if (source is not null)
{
await CopyDataFromSourceInstance(application, instance, source);
}
Expand Down Expand Up @@ -866,6 +874,15 @@ public async Task<ActionResult> CopyInstance(
{
InstanceOwner = sourceInstance.InstanceOwner,
VisibleAfter = sourceInstance.VisibleAfter,
DueBefore = GetDueBeforeFromSource(application, sourceInstance),
DataValues = SelectIncludedValues(
application.CopyInstanceSettings?.IncludedDataValues,
sourceInstance.DataValues
),
PresentationTexts = SelectIncludedValues(
application.CopyInstanceSettings?.IncludedPresentationTexts,
sourceInstance.PresentationTexts
),
Status = new() { ReadStatus = ReadStatus.Read },
};

Expand Down Expand Up @@ -1335,6 +1352,43 @@ await _dataClient.InsertBinaryData(
}
}

private static DateTime? GetDueBeforeFromSource(ApplicationMetadata application, Instance? sourceInstance)
{
if (sourceInstance is null || application.CopyInstanceSettings?.IncludeDueBefore is not true)
{
return null;
}

return sourceInstance.DueBefore;
}

/// <summary>
/// Picks the entries from <paramref name="sourceValues"/> whose keys are listed in <paramref name="includedKeys"/>.
/// Returns null when there is nothing to copy.
/// </summary>
private static Dictionary<string, string>? SelectIncludedValues(
List<string>? includedKeys,
Dictionary<string, string>? sourceValues
)
{
if (includedKeys is null || includedKeys.Count == 0 || sourceValues is null || sourceValues.Count == 0)
{
return null;
}

Dictionary<string, string>? selected = null;
foreach (string key in includedKeys)
{
if (sourceValues.TryGetValue(key, out string? value) && value is not null)
{
selected ??= new Dictionary<string, string>(includedKeys.Count);
selected[key] = value;
}
}
Comment thread
ivarne marked this conversation as resolved.
Dismissed

return selected;
}

private ActionResult ExceptionResponse(Exception exception, string message)
{
_logger.LogError(exception, message);
Expand Down
Loading
Loading