55namespace DocuSign . Maestro . Controllers
66{
77 using System ;
8+ using System . Linq ;
89 using DocuSign . CodeExamples ;
910 using DocuSign . CodeExamples . Common ;
1011 using DocuSign . CodeExamples . Controllers ;
1112 using DocuSign . CodeExamples . Maestro . Models ;
1213 using DocuSign . CodeExamples . Models ;
1314 using DocuSign . Maestro . Client ;
15+ using DocuSign . Maestro . Model ;
1416 using DocuSign . WebForms . Examples ;
1517 using Microsoft . AspNetCore . Mvc ;
1618 using Microsoft . Extensions . Configuration ;
@@ -20,6 +22,8 @@ namespace DocuSign.Maestro.Controllers
2022 [ Route ( "mae001" ) ]
2123 public class TriggerWorkflow : EgController
2224 {
25+ private const string WORKFLOWNAME = "Example workflow - send invite to signer" ;
26+
2327 private IConfiguration configuration ;
2428
2529 public TriggerWorkflow (
@@ -32,6 +36,7 @@ public TriggerWorkflow(
3236 this . configuration = configuration ;
3337 this . CodeExampleText = this . GetExampleText ( this . EgName , ExamplesApiType . Maestro ) ;
3438 this . ViewBag . title = this . CodeExampleText . ExampleName ;
39+ this . ViewBag . SupportingTexts = this . LauncherTexts . ManifestStructure . SupportingTexts ;
3540 }
3641
3742 public override string EgName => "mae001" ;
@@ -40,15 +45,70 @@ public TriggerWorkflow(
4045 [ HttpGet ]
4146 public override IActionResult Get ( )
4247 {
48+ var actionResult = base . Get ( ) ;
49+ if ( this . RequestItemsService . EgName == this . EgName )
50+ {
51+ return actionResult ;
52+ }
53+
54+ var accessToken = this . RequestItemsService . User . AccessToken ;
55+ var accountId = this . RequestItemsService . Session . AccountId ;
56+
4357 try
4458 {
45- var actionResult = base . Get ( ) ;
46- if ( this . RequestItemsService . EgName == this . EgName )
59+ var docuSignManageClient = new DocuSignClient ( this . RequestItemsService . Session . MaestroApiBasePath ) ;
60+ docuSignManageClient . Configuration . DefaultHeader . Add ( "Authorization" , "Bearer " + accessToken ) ;
61+ var workflows = TriggerWorkflowService . GetWorkFlowDefinitions ( docuSignManageClient , accountId ) ;
62+
63+ if ( workflows . Count > 0 )
64+ {
65+ var workflow = workflows . Value
66+ . Where ( x => x . Name == WORKFLOWNAME )
67+ . OrderByDescending ( x => x . LastUpdatedDate )
68+ . FirstOrDefault ( ) ;
69+ this . RequestItemsService . WorkflowId = workflow != null ? workflow . Id : null ;
70+ }
71+
72+ if ( this . RequestItemsService . WorkflowId == null && this . RequestItemsService . TemplateId != null )
4773 {
48- return actionResult ;
74+ NewOrUpdatedWorkflowDefinitionResponse workflow = CreateWorkflowService . CreateWorkflowDefinition (
75+ docuSignManageClient ,
76+ accountId ,
77+ this . RequestItemsService . TemplateId ) ;
78+ this . RequestItemsService . WorkflowId = workflow . WorkflowDefinitionId ;
79+
80+ string publishLink = TriggerWorkflowService . PublishWorkFlow (
81+ docuSignManageClient ,
82+ accountId ,
83+ workflow . WorkflowDefinitionId ) ;
84+ this . RequestItemsService . IsWorkflowPublished = true ;
85+ this . ViewBag . ConsentLink = this . CodeExampleText . AdditionalPages [ 0 ] . ResultsPageText
86+ . Replace ( "{0}" , publishLink ) ;
87+
88+ return this . View ( "publishWorkflow" ) ;
4989 }
5090
51- this . RequestItemsService . WorkflowId = this . configuration [ "DocuSign:WorkflowId" ] ;
91+ if ( this . RequestItemsService . IsWorkflowPublished )
92+ {
93+ string publishLink = TriggerWorkflowService . PublishWorkFlow (
94+ docuSignManageClient ,
95+ accountId ,
96+ this . RequestItemsService . WorkflowId ) ;
97+ if ( publishLink != string . Empty )
98+ {
99+ this . ViewBag . ConsentLink = this . CodeExampleText . AdditionalPages [ 0 ] . ResultsPageText
100+ . Replace ( "{0}" , publishLink ) ;
101+
102+ return this . View ( "publishWorkflow" ) ;
103+ }
104+ else
105+ {
106+ this . RequestItemsService . IsWorkflowPublished = false ;
107+ }
108+ }
109+
110+ this . ViewBag . WorkflowId = this . RequestItemsService . WorkflowId ;
111+ this . ViewBag . TemplateId = this . RequestItemsService . TemplateId ;
52112 this . ViewBag . Config = this . Config ;
53113 var workflowTriggerModel = new WorkflowTriggerModel ( ) ;
54114
@@ -57,7 +117,9 @@ public override IActionResult Get()
57117 catch ( ApiException apiException )
58118 {
59119 this . ViewBag . errorCode = apiException . ErrorCode ;
60- this . ViewBag . errorMessage = apiException . Message ;
120+ this . ViewBag . errorMessage = apiException . ErrorCode == 403 ?
121+ this . LauncherTexts . ManifestStructure . SupportingTexts . ContactSupportToEnableFeature . Replace ( "{0}" , "Maestro" )
122+ : apiException . Message ;
61123 this . ViewBag . SupportingTexts = this . LauncherTexts . ManifestStructure . SupportingTexts ;
62124
63125 return this . View ( "Error" ) ;
@@ -74,13 +136,19 @@ public ActionResult SubmitForm(WorkflowTriggerModel model)
74136 {
75137 var accessToken = this . RequestItemsService . User . AccessToken ;
76138 var accountId = this . RequestItemsService . Session . AccountId ;
77- var docuSignManageClient = new DocuSignClient ( this . RequestItemsService . Session . MaestroManageApiBasePath ) ;
139+ var docuSignManageClient = new DocuSignClient ( this . RequestItemsService . Session . MaestroApiBasePath ) ;
78140 docuSignManageClient . Configuration . DefaultHeader . Add ( "Authorization" , "Bearer " + accessToken ) ;
79- var workflow = TriggerWorkflowService . GetWorkFlowDefinition ( docuSignManageClient , accountId , this . RequestItemsService . WorkflowId ) ;
80141
81- var docuSignAuthClient = new DocuSignClient ( this . RequestItemsService . Session . MaestroAuthApiBasePath ) ;
82- docuSignAuthClient . Configuration . DefaultHeader . Add ( "Authorization" , "Bearer " + accessToken ) ;
83- var result = TriggerWorkflowService . TriggerWorkflow ( docuSignAuthClient , accountId , new Uri ( workflow . TriggerUrl ) , model ) ;
142+ var workflow = TriggerWorkflowService . GetWorkFlowDefinition (
143+ docuSignManageClient ,
144+ accountId ,
145+ this . RequestItemsService . WorkflowId ) ;
146+
147+ var result = TriggerWorkflowService . TriggerWorkflow (
148+ docuSignManageClient ,
149+ accountId ,
150+ new Uri ( workflow . TriggerUrl ) ,
151+ model ) ;
84152
85153 this . RequestItemsService . InstanceId = result . InstanceId ;
86154
0 commit comments