Skip to content

Commit

Permalink
Merge pull request #235 from delegateas/case-resolution
Browse files Browse the repository at this point in the history
Resolve incident fixes
  • Loading branch information
mkholt authored Sep 10, 2024
2 parents fddb8fb + fd22a6e commit f5715d2
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 12 deletions.
23 changes: 12 additions & 11 deletions src/XrmMockupShared/Requests/CloseIncidentRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using System.Linq;
using Microsoft.Crm.Sdk.Messages;
using System.ServiceModel;
Expand Down Expand Up @@ -80,12 +76,6 @@ internal override OrganizationResponse Execute(OrganizationRequest orgRequest, E
throw new FaultException($"incident With Id = {incidentRef.Id} is allready cancelled and connot be closed");
}

var setStaterequest = new SetStateRequest();
setStaterequest.EntityMoniker = incidentRef;
setStaterequest.State = new OptionSetValue(1);
setStaterequest.Status = request.Status;
core.Execute(setStaterequest, userRef);

var incidentResolution = db.GetDbRowOrNull(request.IncidentResolution.ToEntityReference());

if (incidentResolution != null)
Expand All @@ -95,6 +85,17 @@ internal override OrganizationResponse Execute(OrganizationRequest orgRequest, E

db.Add(request.IncidentResolution);

incident["statecode"] = new OptionSetValue(1);
incident["statuscode"] = request.Status;

var updateRequest = new UpdateRequest
{
Target = incident,
["CloseIncidentRequestHandler"] = true,
};

core.Execute(updateRequest, userRef);

return new CloseIncidentResponse();
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/XrmMockupShared/Requests/UpdateRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ internal override OrganizationResponse Execute(OrganizationRequest orgRequest, E
var request = MakeRequest<UpdateRequest>(orgRequest);
var settings = MockupExecutionContext.GetSettings(request);

if (request.Target.LogicalName is "incident" &&
request.Target.GetAttributeValue<OptionSetValue>("statecode").Value is 1 &&
!request.Parameters.ContainsKey("CloseIncidentRequestHandler"))
{
throw new FaultException("This message can not be used to set the state of incident to Resolved. In order to set state of incident to Resolved, use the CloseIncidentRequest message instead.");
}

var entRef = request.Target.ToEntityReferenceWithKeyAttributes();
var entity = request.Target;
var row = db.GetDbRow(entRef);

if (settings.ServiceRole == MockupServiceSettings.Role.UI &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using DG.XrmFramework.BusinessDomain.ServiceContext;
using System.Linq;

namespace DG.Some.Namespace
{
public class IncidentDeleteAllRelatedResolutionsOnClose : Plugin
{
public IncidentDeleteAllRelatedResolutionsOnClose() : base(typeof(IncidentDeleteAllRelatedResolutionsOnClose))
{
RegisterPluginStep<Incident>(
EventOperation.Update,
ExecutionStage.PostOperation,
ExecuteDeleteAllRelatedResolutionsOnClose)
.AddFilteredAttributes(x => x.StateCode)
.AddImage(ImageType.PreImage, x => x.StateCode, x => x.Title)
.AddImage(ImageType.PostImage, x => x.StateCode)
.SetExecutionOrder(10);
}

protected void ExecuteDeleteAllRelatedResolutionsOnClose(LocalPluginContext localContext)
{
var preImage = localContext.PluginExecutionContext.PreEntityImages["PreImage"].ToEntity<Incident>();
if (preImage.Title != "TestRemovalOfResolutionsAfterClose") return;

var postImage = localContext.PluginExecutionContext.PostEntityImages["PostImage"].ToEntity<Incident>();

if (preImage.StateCode == IncidentState.Active && postImage.StateCode != IncidentState.Active)
{
using (var context = new Xrm(localContext.OrganizationService))
{
context.IncidentResolutionSet
.Where(x => x.IncidentId.Id == localContext.PluginExecutionContext.PrimaryEntityId)
.ToList()
.ForEach(x => localContext.OrganizationAdminService.Delete(x.LogicalName, x.Id));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Closeincidentplugin.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IncidentDeleteAllRelatedResolutionsOnClose.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ParentPostCreatePlugin.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ChildPreCreatePlugin.cs" />
<Compile Include="$(MSBuildThisFileDirectory)NotePostOperationPlugin.cs" />
Expand Down
40 changes: 40 additions & 0 deletions tests/SharedTests/TestIncident.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,5 +539,45 @@ public void TestUpdateResolvedIncidentSucceedsWhenFieldModificationIsAllowed()
var retrievedIncident = Incident.Retrieve(orgAdminService, incident.Id);
Assert.Equal(IncidentState.Active, retrievedIncident.StateCode);
}

[Fact]
public void TestRemovalOfResolutionsAfterClose()
{
var incident = new Incident
{
Title = "TestRemovalOfResolutionsAfterClose"
};
incident.Id = orgAdminUIService.Create(incident);

var incidentResolution = new IncidentResolution
{
Subject = "Resolved Sample Incident",
IncidentId = incident.ToEntityReference()
};
var closeIncidentRequest = new CloseIncidentRequest
{
IncidentResolution = incidentResolution,
Status = new OptionSetValue((int)Incident_StatusCode.ProblemSolved)
};
orgAdminUIService.Execute(closeIncidentRequest);

using (var context = new Xrm(orgAdminUIService))
{
var retrievedIncidentResolutions = context.IncidentResolutionSet.Where(x => x.IncidentId.Id == incident.Id);
Assert.Empty(retrievedIncidentResolutions);
}
}

[Fact]
public void TestUpdateIncidentAsResolvedFails()
{
var incident = new Incident();
incident.Id = orgAdminUIService.Create(incident);

incident.StateCode = IncidentState.Resolved;
incident.StatusCode = Incident_StatusCode.ProblemSolved;

Assert.Throws<FaultException>(() => orgAdminService.Update(incident));
}
}
}

0 comments on commit f5715d2

Please sign in to comment.