Skip to content

Commit

Permalink
Merge pull request #93 from DougSchmidt-AI/feature/Issue-91-DeleteTim…
Browse files Browse the repository at this point in the history
…eSeriesWithLockedApprovals

Issue-91 Allow a locked visit to be deleted after confirmation
  • Loading branch information
Doug Schmidt authored Nov 15, 2018
2 parents b1fd683 + 14d93aa commit 380194e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
40 changes: 36 additions & 4 deletions TimeSeries/PublicApis/SdkExamples/LocationDeleter/Deleter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using LocationDeleter.PrivateApis.SiteVisit;
using ServiceStack;
using ServiceStack.Logging;
using ApprovalLevel = LocationDeleter.PrivateApis.SiteVisit.ApprovalLevel;
using Publish3x = Aquarius.TimeSeries.Client.ServiceModels.Legacy.Publish3x;

namespace LocationDeleter
Expand Down Expand Up @@ -138,6 +139,22 @@ private int DeleteVisitsAtLocation(LocationInfo locationInfo)
.Where(v => v.IsLocked)
.ToList();

var lockedVisitSummary = $"{"locked visit".ToQuantity(lockedVisits.Count)} in '{locationInfo.Identifier}'";

if (lockedVisits.Any() && ConfirmAction(
$"unlocking of {lockedVisitSummary}",
$"unlock {lockedVisitSummary}",
() => lockedVisitSummary,
$"the identifier of the location",
locationInfo.Identifier))
{
UnlockVisits(siteVisitLocation.Id, lockedVisits);

lockedVisits.Clear();

Log.Info($"Unlocked {lockedVisitSummary}.");
}

if (lockedVisits.Any())
{
Log.Warn($"Skipping deletion of {"locked field visit".ToQuantity(lockedVisits.Count)} in location '{locationInfo.Identifier}'.");
Expand Down Expand Up @@ -182,6 +199,16 @@ private int DeleteVisitsAtLocation(LocationInfo locationInfo)
return visits.Count;
}

private void UnlockVisits(long locationId, List<Visit> visits)
{
var lowestApprovalLevel = GetLowestApprovalLevel(locationId);

foreach (var visit in visits)
{
_siteVisit.Put(new PutVisitApprovalLevel {Id = visit.Id, ApprovalLevelId = lowestApprovalLevel.Id});
}
}

private int LockedTimeSeriesCount { get; set; }

private void DeleteSpecifiedTimeSeries()
Expand Down Expand Up @@ -287,10 +314,7 @@ private bool UnlockTimeSeries(TimeSeriesDescription timeSeriesDescription)

var siteVisitLocation = GetSiteVisitLocation(timeSeriesDescription);

var lowestApprovalLevel = _siteVisit.Get(new GetLocationApprovalLevels {Id = siteVisitLocation.Id})
.ApprovalLevels
.OrderBy(a => a.Level)
.First();
var lowestApprovalLevel = GetLowestApprovalLevel(siteVisitLocation.Id);

var timeSeriesInfo = GetTimeSeriesInfo(timeSeriesDescription);

Expand Down Expand Up @@ -327,6 +351,14 @@ private bool UnlockTimeSeries(TimeSeriesDescription timeSeriesDescription)

private static readonly AquariusServerVersion MinimumApprovalUnlockVersion = AquariusServerVersion.Create("17.3.75");

private ApprovalLevel GetLowestApprovalLevel(long locationId)
{
return _siteVisit.Get(new GetLocationApprovalLevels {Id = locationId})
.ApprovalLevels
.OrderBy(a => a.Level)
.First();
}

private void DeleteTimeSeries(TimeSeriesDescription timeSeriesDescription)
{
if (timeSeriesDescription.TimeSeriesType == "Reflected")
Expand Down
11 changes: 9 additions & 2 deletions TimeSeries/PublicApis/SdkExamples/LocationDeleter/PrivateApis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ public class DeleteVisit : IReturnVoid
public long Id { get; set; }
}

[Route("/visits/{Id}/approvallevel", HttpMethods.Put)]
public class PutVisitApprovalLevel : IReturnVoid
{
public long Id { get; set; }
public long ApprovalLevelId { get; set; }
}

// Approval DTOs lifted from 17.3 SiteVisit and trimmed down to minimum required properties
[Route("/locations/{Id}/approvallevels", HttpMethods.Get)]
public class GetLocationApprovalLevels : IReturn<ResolvedLocationRole>
Expand Down Expand Up @@ -118,12 +125,12 @@ public class PostDatasetApproval : IReturn<DatasetApprovalSaveResult>
[Route("/approvaljobs/{Id}", HttpMethods.Get)]
public class GetApprovalJob : IReturn<DatasetApprovalSaveResult>
{
public Int64 Id { get; set; }
public long Id { get; set; }
}

public class DatasetApprovalSaveResult
{
public Int64 Id { get; set; }
public long Id { get; set; }
public bool Complete { get; set; }
public bool Success { get; set; }
public List<RelatedDataset> RelatedDatasets { get; set; }
Expand Down

0 comments on commit 380194e

Please sign in to comment.