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

Feature/10108 remove shadow fields #233

Merged
merged 7 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
36 changes: 36 additions & 0 deletions src/Altinn.App.Core/Helpers/ShadowFieldsConverter.cs
nkylstad marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;

namespace Altinn.App.Core.Helpers
{
public class IgnorePropertiesWithPrefix
{
private readonly string _ignorePrefix;

public IgnorePropertiesWithPrefix(string prefix)
=> _ignorePrefix = prefix;

public void ModifyPrefixInfo(JsonTypeInfo ti)
{
if (ti.Kind != JsonTypeInfoKind.Object)
return;

ti.Properties.RemoveAll(prop => prop.Name.StartsWith(_ignorePrefix));
}
}

public static class ListHelpers
{
// IList<T> implementation of List<T>.RemoveAll method.
public static void RemoveAll<T>(this IList<T> list, Predicate<T> predicate)
{
for (int i = 0; i < list.Count; i++)
{
if (predicate(list[i]))
{
list.RemoveAt(i--);
}
}
}
}
}
50 changes: 50 additions & 0 deletions src/Altinn.App.Core/Implementation/DefaultTaskEvents.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using Altinn.App.Core.Configuration;
using Altinn.App.Core.EFormidling.Interface;
using Altinn.App.Core.Features;
Expand Down Expand Up @@ -144,6 +147,8 @@ public async Task OnEndProcessTask(string endEvent, Instance instance)

await RunRemoveHiddenData(instance, instanceGuid, dataTypesToLock);

await RunRemoveShadowFields(instance, instanceGuid, dataTypesToLock);

await RunAppDefinedOnTaskEnd(endEvent, instance);

await RunLockDataAndGeneratePdf(endEvent, instance, dataTypesToLock);
Expand All @@ -161,6 +166,13 @@ private async Task RunRemoveHiddenData(Instance instance, Guid instanceGuid, Lis
}
}

private async Task RunRemoveShadowFields(Instance instance, Guid instanceGuid, List<DataType> dataTypesToLock) {
if (dataTypesToLock.Find(dt => dt.AppLogic?.ShadowFields?.Prefix != null) != null)
{
await RemoveShadowFields(instance, instanceGuid, dataTypesToLock);
}
}

private async Task RunAppDefinedOnTaskEnd(string endEvent, Instance instance)
{
foreach (var taskEnd in _taskEnds)
Expand Down Expand Up @@ -256,6 +268,44 @@ private async Task RemoveHiddenData(Instance instance, Guid instanceGuid, List<D
}
}

private async Task RemoveShadowFields(Instance instance, Guid instanceGuid, List<DataType> dataTypesToLock)
{
foreach (var dataType in dataTypesToLock.Where(dt => dt.AppLogic?.ShadowFields != null))
{
foreach (Guid dataElementId in instance.Data.Where(de => de.DataType == dataType.Id).Select(dataElement => Guid.Parse(dataElement.Id)))
{
Type modelType = _appModel.GetModelType(dataType.AppLogic.ClassRef);
string app = instance.AppId.Split("/")[1];
int instanceOwnerPartyId = int.Parse(instance.InstanceOwner.PartyId);
dynamic data = await _dataClient.GetFormData(
instanceGuid, modelType, instance.Org, app, instanceOwnerPartyId, dataElementId);

var modifier = new IgnorePropertiesWithPrefix(dataType.AppLogic.ShadowFields.Prefix);
JsonSerializerOptions options = new ()
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver
{
Modifiers = { modifier.ModifyPrefixInfo }
},
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};

string serializedData = JsonSerializer.Serialize(data, options);
if (dataType.AppLogic.ShadowFields.SaveToDataType != null) {
var saveToDataType = dataTypesToLock.Find(dt => dt.Id == dataType.AppLogic.ShadowFields.SaveToDataType);
Type saveToModelType = _appModel.GetModelType(saveToDataType.AppLogic.ClassRef);
var updatedData = Newtonsoft.Json.JsonConvert.DeserializeObject(serializedData, saveToModelType ?? modelType);
nkylstad marked this conversation as resolved.
Show resolved Hide resolved
await _dataClient.InsertFormData(updatedData, instanceGuid, saveToModelType ?? modelType, instance.Org, app, instanceOwnerPartyId, saveToDataType.Id);
}
else
{
var updatedData = Newtonsoft.Json.JsonConvert.DeserializeObject(serializedData, modelType);
await _dataClient.UpdateData(updatedData, instanceGuid, modelType, instance.Org, app, instanceOwnerPartyId, dataElementId);
}
}
}
}

/// <inheritdoc />
public async Task OnAbandonProcessTask(string taskId, Instance instance)
{
Expand Down