-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from thygesteffensen/issue/4/associate
Issue/4/associate
- Loading branch information
Showing
12 changed files
with
379 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
name: Build | ||
|
||
on: | ||
pull_request: | ||
types: [assigned, opened, synchronize, reopened] | ||
|
||
jobs: | ||
test: | ||
runs-on: windows-latest | ||
name: Testing | ||
steps: | ||
- name: Checkout code base | ||
uses: actions/checkout@v2 | ||
|
||
- name: Add nuget Source | ||
env: | ||
GH_TOKEN: ${{ secrets.GH_TOKEN }} | ||
run: nuget sources Add -Name Github -Source https://nuget.pkg.github.com/thygesteffensen/index.json -UserName thygesteffensen -Password $env:GH_TOKEN | ||
|
||
- name: Set Github nuget API | ||
env: | ||
GH_TOKEN: ${{ secrets.GH_TOKEN }} | ||
run: nuget setapikey $env:GH_TOKEN -Source https://nuget.pkg.github.com/thygesteffensen/index.json | ||
|
||
- name: Run tests | ||
run: dotnet test --verbosity normal | ||
|
||
- name: Run tests | ||
run: dotnet test --verbosity normal | ||
|
||
build: | ||
runs-on: windows-latest | ||
name: Building | ||
steps: | ||
- name: Checkout code base | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup MSBuild | ||
uses: microsoft/setup-msbuild@v1 | ||
|
||
- name: Add nuget Source | ||
env: | ||
GH_TOKEN: ${{ secrets.GH_TOKEN }} | ||
run: nuget sources Add -Name Github -Source https://nuget.pkg.github.com/thygesteffensen/index.json -UserName thygesteffensen -Password $env:GH_TOKEN | ||
|
||
- name: Set Github nuget API | ||
env: | ||
GH_TOKEN: ${{ secrets.GH_TOKEN }} | ||
run: nuget setapikey $env:GH_TOKEN -Source https://nuget.pkg.github.com/thygesteffensen/index.json | ||
|
||
- name: Run tests | ||
run: dotnet test --verbosity normal | ||
|
||
- name: Restore NuGet packages | ||
run: nuget restore PAMU_CDS.sln | ||
|
||
- name: Build solution | ||
run: msbuild /p:OutputPath=../build /p:Configuration=Release /p:RestorePackages=false | ||
|
||
- name: Archive build to artifacts | ||
uses: actions/upload-artifact@v2 | ||
with: | ||
name: build | ||
path: | | ||
build/PAMU_CDS.dll | ||
retention-days: 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Xrm.Sdk; | ||
using Microsoft.Xrm.Sdk.Messages; | ||
using PAMU_CDS.Auxiliary; | ||
using Parser.ExpressionParser; | ||
using Parser.FlowParser.ActionExecutors; | ||
|
||
namespace PAMU_CDS.Actions | ||
{ | ||
public class DisAndAssociateEntitiesAction : OpenApiConnectionActionExecutorBase | ||
{ | ||
private const string AssociateId = "AssociateEntities"; | ||
private const string DisassociateId = "DisassociateEntities"; | ||
public static readonly string[] OperationId = {AssociateId, DisassociateId}; | ||
|
||
private readonly IOrganizationService _organizationService; | ||
|
||
public DisAndAssociateEntitiesAction( | ||
IExpressionEngine expressionEngine, | ||
IOrganizationService organizationService) : base(expressionEngine) | ||
{ | ||
_organizationService = organizationService ?? throw new ArgumentNullException(nameof(organizationService)); | ||
} | ||
|
||
|
||
public override Task<ActionResult> Execute() | ||
{ | ||
var entity = new Entity(); | ||
entity = entity.CreateEntityFromParameters(Parameters); | ||
|
||
OrganizationRequest associateRequest; | ||
|
||
switch (Host.OperationId) | ||
{ | ||
case AssociateId: | ||
{ | ||
var relatedEntity = ExtractEntityReferenceFromOdataId("item/@odata.id"); | ||
associateRequest = new AssociateRequest | ||
{ | ||
Target = entity.ToEntityReference(), | ||
Relationship = new Relationship(Parameters["associationEntityRelationship"].GetValue<string>()), | ||
RelatedEntities = new EntityReferenceCollection {relatedEntity} | ||
}; | ||
break; | ||
} | ||
case DisassociateId: | ||
{ | ||
var relatedEntity = ExtractEntityReferenceFromOdataId("$id"); | ||
|
||
associateRequest = new DisassociateRequest | ||
{ | ||
Target = entity.ToEntityReference(), | ||
Relationship = new Relationship(Parameters["associationEntityRelationship"].GetValue<string>()), | ||
RelatedEntities = new EntityReferenceCollection {relatedEntity} | ||
}; | ||
break; | ||
} | ||
default: | ||
throw new PowerAutomateException( | ||
$"Action {nameof(DisAndAssociateEntitiesAction)} can only handle {AssociateId} and {DisassociateId} operations, not {Host.OperationId}."); | ||
} | ||
|
||
try | ||
{ | ||
// TODO: Figure out how this handle bad associations and error handling. | ||
// assignees: thygesteffensen | ||
_organizationService.Execute(associateRequest); | ||
} | ||
catch (InvalidPluginExecutionException) | ||
{ | ||
// We need to do some experiments on how the error handling works. Take a look at one of your customers. | ||
return Task.FromResult(new ActionResult {ActionStatus = ActionStatus.Failed}); | ||
} | ||
|
||
return Task.FromResult(new ActionResult {ActionStatus = ActionStatus.Succeeded}); | ||
} | ||
|
||
private EntityReference ExtractEntityReferenceFromOdataId(string itemKey) | ||
{ | ||
// https://dglab6.crm4.dynamics.com/api/data/v9.1/contacts(8c711383-b933-eb11-a813-000d3ab11761) | ||
|
||
var oDataId = Parameters[itemKey].GetValue<string>(); | ||
var entityName = | ||
oDataId.Substring(oDataId.LastIndexOf('/') + 1, oDataId.IndexOf('(') - oDataId.LastIndexOf('/') - 2); | ||
var entityId = oDataId.Substring(oDataId.IndexOf('(') + 1, oDataId.IndexOf(')') - oDataId.IndexOf('(') - 1); | ||
|
||
return new EntityReference(entityName, new Guid(entityId)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.