Skip to content

Commit

Permalink
composing lastChanged/lastChangedBy in instanceRepository (#4446)
Browse files Browse the repository at this point in the history
* composing lastChanged/lastChangedBy in instanceRepository

* indentation

* syntax fix

* updated mock

* updated local test and mocks

* unit tests for findlastchangeby instance helper

* updated testdata

* fixed bug in mock

Co-authored-by: Stephanie Buadu <[email protected]>
Co-authored-by: jeevananthank <[email protected]>
  • Loading branch information
3 people authored Jun 30, 2020
1 parent 867742b commit 9932ed0
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"org": "tdd",
"created": "2019-07-31T09:57:23.4729995Z",
"lastChanged": "2019-07-31T09:57:23.4729995Z",
"lastChangedBy": "12345",
"dueBefore": "2019-07-05T00:00:00Z",
"visibleAfter": "2019-06-05T00:00:00Z",
"process": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public Task<Instance> GetInstance(string app, string org, int instanceOwnerId, G
if (instance != null)
{
instance.Data = GetDataElements(org, app, instanceOwnerId, instanceId);
(instance.LastChangedBy, instance.LastChanged) = FindLastChanged(instance);
}

return Task.FromResult(instance);
}

Expand Down Expand Up @@ -198,7 +200,7 @@ public async Task<Instance> UpdateReadStatus(int instanceOwnerPartyId, Guid inst
return await Task.FromResult(storedInstance);
}


return null;

}
Expand All @@ -215,5 +217,37 @@ private string GetInstancePath(int instanceOwnerPartyId, Guid instanceGuid)

return string.Empty;
}

private static (string LastChangedBy, DateTime? LastChanged) FindLastChanged(Instance instance)
{
string lastChangedBy = instance.LastChangedBy;
DateTime? lastChanged = instance.LastChanged;
if (instance.Data == null || instance.Data.Count == 0)
{
return (lastChangedBy, lastChanged);
}

List<DataElement> newerDataElements = instance.Data.FindAll(dataElement =>
dataElement.LastChanged != null
&& dataElement.LastChangedBy != null
&& dataElement.LastChanged > instance.LastChanged);

if (newerDataElements.Count == 0)
{
return (lastChangedBy, lastChanged);
}

lastChanged = (DateTime)instance.LastChanged;
newerDataElements.ForEach((DataElement dataElement) =>
{
if (dataElement.LastChanged > lastChanged)
{
lastChangedBy = dataElement.LastChangedBy;
lastChanged = (DateTime)dataElement.LastChanged;
}
});

return (lastChangedBy, lastChanged);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static MessageBoxInstance ConvertToMessageBoxInstance(Instance instance)
DueDateTime = instance.DueBefore,
Id = instanceGuid,
InstanceOwnerId = instance.InstanceOwner.PartyId,
LastChangedBy = FindLastChangedBy(instance),
LastChangedBy = FindLastChanged(instance).LastChangedBy,
Org = instance.Org,
AppName = instance.AppId.Split('/')[1],
ProcessCurrentTask = GetSBLStatusForCurrentTask(instance),
Expand Down Expand Up @@ -119,12 +119,18 @@ public static string GetSBLStatusForCurrentTask(Instance instance)
}
}

private static string FindLastChangedBy(Instance instance)
/// <summary>
/// Finds last changed by for an instance and its listed data elements
/// </summary>
/// <param name="instance">The instance</param>
/// <returns>Last changed by</returns>
public static (string LastChangedBy, DateTime? LastChanged) FindLastChanged(Instance instance)
{
string result = instance.LastChangedBy;
string lastChangedBy = instance.LastChangedBy;
DateTime? lastChanged = instance.LastChanged;
if (instance.Data == null || instance.Data.Count == 0)
{
return result;
return (lastChangedBy, lastChanged);
}

List<DataElement> newerDataElements = instance.Data.FindAll(dataElement =>
Expand All @@ -134,20 +140,20 @@ private static string FindLastChangedBy(Instance instance)

if (newerDataElements.Count == 0)
{
return result;
return (lastChangedBy, lastChanged);
}

DateTime lastChanged = (DateTime)instance.LastChanged;
lastChanged = (DateTime)instance.LastChanged;
newerDataElements.ForEach((DataElement dataElement) =>
{
if (dataElement.LastChanged > lastChanged)
{
result = dataElement.LastChangedBy;
lastChangedBy = dataElement.LastChangedBy;
lastChanged = (DateTime)dataElement.LastChanged;
}
});

return result;
return (lastChangedBy, lastChanged);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Altinn.Platform.Storage.Repository
/// Repository operations for application instances.
/// </summary>
public class InstanceRepository : IInstanceRepository
{
{
private const string CollectionId = "instances";
private const string PartitionKey = "/instanceOwner/partyId";

Expand Down Expand Up @@ -59,7 +59,7 @@ public InstanceRepository(
databaseUri,
documentCollection).GetAwaiter().GetResult();

_client.OpenAsync();
_client.OpenAsync();
}

/// <inheritdoc/>
Expand Down Expand Up @@ -542,8 +542,13 @@ private void PreProcess(Instance instance)
}

/// <summary>
/// Converts the instanceId (id) of the instance from {instanceGuid} to {instanceOwnerPartyId}/{instanceGuid} to be used outside cosmos.
/// Prepares the instance for exposure to end users and app owners.
/// </summary>
/// <remarks>
/// - Converts the instanceId (id) of the instance from {instanceGuid} to {instanceOwnerPartyId}/{instanceGuid} to be used outside cosmos.
/// - Retrieves all dataelements from data repository
/// - Sets correct LastChanged/LastChangedBy by comparing instance and data elements
/// </remarks>
/// <param name="instance">the instance to preprocess</param>
private async Task PostProcess(Instance instance)
{
Expand All @@ -552,6 +557,10 @@ private async Task PostProcess(Instance instance)

instance.Id = instanceId;
instance.Data = await _dataRepository.ReadAll(instanceGuid);

(string lastChangedBy, DateTime? lastChanged) = InstanceHelper.FindLastChanged(instance);
instance.LastChanged = lastChanged;
instance.LastChangedBy = lastChangedBy;
}

/// <summary>
Expand All @@ -563,7 +572,7 @@ private async Task PostProcess(List<Instance> instances)
foreach (Instance item in instances)
{
await PostProcess(item);
}
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void ConvertToMessageBoxSingleInstance_TC03()
string actualLastChangedBy = actual.LastChangedBy;

// Assert
Assert.Equal(lastChangedBy, actualLastChangedBy);
Assert.Equal(lastChangedBy, actualLastChangedBy);
}

/// <summary>
Expand Down Expand Up @@ -140,5 +140,69 @@ public void GetSBLStatusForCurrentTask_TC04()
string sblStatus = InstanceHelper.GetSBLStatusForCurrentTask(instance);
Assert.Equal("default", sblStatus);
}

/// <summary>
/// Scenario: Find last changed by from the instance and date elements
/// Expected: lastChangedBy is an user id is from the instance without dateelements
/// Success: lastChangedBy equals {expectedlastChangedBy} and lastchanged equals {expectedlastChanged}
/// </summary>
[Fact]
public void FindLastChangedBy_TC01()
{
// Arrange
Instance instance = TestData.Instance_2_2;
string expectedlastChangedBy = "20000000";
DateTime expectedlastChanged = Convert.ToDateTime("2019-08-20T19:19:22.2135489Z");

// Act
(string lastChangedBy, DateTime? lastChanged) = InstanceHelper.FindLastChanged(instance);

// Assert
Assert.Equal(expectedlastChangedBy, lastChangedBy);
Assert.Equal(expectedlastChanged, lastChanged);
}

/// <summary>
/// Scenario: Find last changed by from the instance and date elements
/// Expected: lastChangedBy is an user id from one dataelement
/// Success: lastChangedBy equals {expectedlastChangedBy} and lastchanged equals {expectedlastChanged}
/// </summary>
[Fact]
public void FindLastChangedBy_TC02()
{
// Arrange
Instance instance = TestData.Instance_1_2;
string expectedlastChangedBy = "20000001";
DateTime expectedlastChanged = Convert.ToDateTime("2019-09-20T21:19:22.2135489Z");

// Act
(string lastChangedBy, DateTime? lastChanged) = InstanceHelper.FindLastChanged(instance);

// Assert
Assert.Equal(expectedlastChangedBy, lastChangedBy);
Assert.Equal(expectedlastChanged, lastChanged);
}

/// <summary>
/// Scenario: Find last changed by from the instance and date elements
/// Expected: lastChangedBy is an user id from the dataelements list that has the latest lastChanged datetime
/// Success: lastChangedBy equals {expectedlastChangedBy} and lastchanged equals {expectedlastChanged}
/// </summary>
[Fact]
public void FindLastChangedBy_TC03()
{
// Arrange
Instance instance = TestData.Instance_2_1;
string expectedlastChangedBy = "20000001";
DateTime expectedlastChanged = Convert.ToDateTime("2019-10-20T21:19:22.2135489Z");

// Act
(string lastChangedBy, DateTime? lastChanged) = InstanceHelper.FindLastChanged(instance);

// Assert
Assert.Equal(expectedlastChangedBy, lastChangedBy);
Assert.Equal(expectedlastChanged, lastChanged);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Net;
using System.Reflection;
using System.Threading.Tasks;

using Altinn.Platform.Storage.Helpers;
using Altinn.Platform.Storage.Interface.Models;
using Altinn.Platform.Storage.Repository;
using Altinn.Platform.Storage.UnitTest.Utils;
Expand Down Expand Up @@ -274,6 +274,11 @@ private void PostProcess(Instance instance)
string instanceId = $"{instance.InstanceOwner.PartyId}/{instance.Id}";

instance.Id = instanceId;
// Should instanceData be included here as well?

(string lastChangedBy, DateTime? lastChanged) = InstanceHelper.FindLastChanged(instance);
instance.LastChanged = lastChanged;
instance.LastChangedBy = lastChangedBy;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static class TestData
{
public static string InstanceOwnerPartyId_1 = "50000000";
public static string UserId_1 = "20000000";
public static string UserId_2 = "20000001";

public static string Org_1 = "TDD";
public static string Org_2 = "SPF";
Expand Down Expand Up @@ -102,7 +103,10 @@ private static ProcessState CreateProcessState()
Created = Convert.ToDateTime("2019-08-20T19:20:21.7920255Z"),
InstanceOwner = new InstanceOwner { PartyId = InstanceOwnerPartyId_1 },
Status = new InstanceStatus
{
{
},
Data = new List<DataElement>() {
new DataElement() { LastChangedBy= UserId_2, LastChanged = Convert.ToDateTime("2019-09-20T21:19:22.2135489Z") }
},
LastChangedBy = UserId_1,
LastChanged = Convert.ToDateTime("2019-08-20T21:19:22.2135489Z"),
Expand All @@ -121,6 +125,10 @@ private static ProcessState CreateProcessState()
Status = new InstanceStatus
{
},
Data = new List<DataElement>() {
new DataElement() { LastChangedBy = UserId_2, LastChanged = Convert.ToDateTime("2019-09-20T21:19:22.2135489Z") },
new DataElement() { LastChangedBy = UserId_2, LastChanged = Convert.ToDateTime("2019-10-20T21:19:22.2135489Z") }
},
LastChangedBy = UserId_1,
LastChanged = Convert.ToDateTime("2019-08-20T23:19:22.2135489Z"),
Org = Org_1,
Expand Down
33 changes: 17 additions & 16 deletions src/development/LocalTest/Helpers/Storage/InstanceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static MessageBoxInstance ConvertToMessageBoxInstance(Instance instance)
DueDateTime = instance.DueBefore,
Id = instanceGuid,
InstanceOwnerId = instance.InstanceOwner.PartyId,
LastChangedBy = FindLastChangedBy(instance),
LastChangedBy = FindLastChanged(instance).LastChangedBy,
Org = instance.Org,
AppName = instance.AppId.Split('/')[1],
ProcessCurrentTask = GetSBLStatusForCurrentTask(instance),
Expand Down Expand Up @@ -100,22 +100,17 @@ public static string GetSBLStatusForCurrentTask(Instance instance)
{
if (instance.Process != null)
{
string currentTask = instance.Process.CurrentTask?.ElementId;
if (currentTask != null)
{
return "FormFilling";
}
else if (string.IsNullOrEmpty(currentTask) && instance.Process.Ended != null && instance.Status?.Archived == null)
if (instance.Process.Ended != null && instance.Status?.Archived == null)
{
return "Submit";
}
else if (string.IsNullOrEmpty(currentTask) && instance.Process.Ended != null && instance.Status?.Archived != null)
else if (instance.Process.Ended != null && instance.Status?.Archived != null)
{
return "Archived";
}
else
{
return instance.Process.CurrentTask?.ElementId;
return "FormFilling";
}
}
else
Expand All @@ -124,12 +119,18 @@ public static string GetSBLStatusForCurrentTask(Instance instance)
}
}

private static string FindLastChangedBy(Instance instance)
/// <summary>
/// Finds last changed by for an instance and its listed data elements
/// </summary>
/// <param name="instance">The instance</param>
/// <returns>Last changed by</returns>
public static (string LastChangedBy, DateTime? LastChanged) FindLastChanged(Instance instance)
{
string result = instance.LastChangedBy;
string lastChangedBy = instance.LastChangedBy;
DateTime? lastChanged = instance.LastChanged;
if (instance.Data == null || instance.Data.Count == 0)
{
return result;
return (lastChangedBy, lastChanged);
}

List<DataElement> newerDataElements = instance.Data.FindAll(dataElement =>
Expand All @@ -139,20 +140,20 @@ private static string FindLastChangedBy(Instance instance)

if (newerDataElements.Count == 0)
{
return result;
return (lastChangedBy, lastChanged);
}

DateTime lastChanged = (DateTime)instance.LastChanged;
lastChanged = (DateTime)instance.LastChanged;
newerDataElements.ForEach((DataElement dataElement) =>
{
if (dataElement.LastChanged > lastChanged)
{
result = dataElement.LastChangedBy;
lastChangedBy = dataElement.LastChangedBy;
lastChanged = (DateTime)dataElement.LastChanged;
}
});

return result;
return (lastChangedBy, lastChanged);
}
}
}
Loading

0 comments on commit 9932ed0

Please sign in to comment.