44
55namespace DocuSign . CodeExamples . Controllers
66{
7+ using System ;
78 using System . Linq ;
89 using System . Threading . Tasks ;
910 using DocuSign . CodeExamples . Common ;
@@ -17,6 +18,8 @@ namespace DocuSign.CodeExamples.Controllers
1718 [ Route ( "mae001" ) ]
1819 public class Mae001TriggerWorkflowController : EgController
1920 {
21+ private const string WorkflowName = "Example workflow - send invite to signer" ;
22+
2023 public Mae001TriggerWorkflowController ( DsConfiguration dsConfig ,
2124 LauncherTexts launcherTexts ,
2225 IRequestItemsService requestItemsService )
@@ -28,6 +31,96 @@ public Mae001TriggerWorkflowController(DsConfiguration dsConfig,
2831
2932 public override string EgName => "mae001" ;
3033
34+ [ MustAuthenticate ]
35+ [ HttpGet ]
36+ public override IActionResult Get ( )
37+ {
38+ var actionResult = base . Get ( ) ;
39+ if ( this . RequestItemsService . EgName == this . EgName )
40+ {
41+ return actionResult ;
42+ }
43+
44+ var basePath = this . RequestItemsService . Session . IamBasePath ;
45+ var accessToken = this . RequestItemsService . User . AccessToken ;
46+ var accountId = this . RequestItemsService . Session . AccountId ;
47+
48+ try
49+ {
50+ if ( this . RequestItemsService . WorkflowId == null )
51+ {
52+ WorkflowsListSuccess workflowsList = TriggerMaestroWorkflow . GetMaestroWorkflow (
53+ basePath ,
54+ accessToken ,
55+ accountId ) . GetAwaiter ( ) . GetResult ( ) ;
56+
57+ if ( workflowsList . Data != null || workflowsList . Data . Count > 0 )
58+ {
59+ var maestroWorkflow = workflowsList . Data . FirstOrDefault ( workflow => workflow . Name == WorkflowName ) ;
60+
61+ this . RequestItemsService . WorkflowId = maestroWorkflow ? . Id ;
62+ }
63+ }
64+
65+ if ( this . RequestItemsService . WorkflowId == null && this . RequestItemsService . TemplateId != null )
66+ {
67+ var workflowId = TriggerMaestroWorkflow . CreateWorkflowAsync (
68+ accessToken ,
69+ accountId ,
70+ this . RequestItemsService . TemplateId ,
71+ this . Config . MaestroWorkflowConfig ) . GetAwaiter ( ) . GetResult ( ) ;
72+ this . RequestItemsService . WorkflowId = workflowId ;
73+
74+ var publishLink = TriggerMaestroWorkflow . PublishWorkflowAsync (
75+ accountId ,
76+ workflowId ,
77+ accessToken ) . GetAwaiter ( ) . GetResult ( ) ;
78+
79+ this . RequestItemsService . IsWorkflowPublished = true ;
80+ this . ViewBag . ConsentLink = this . CodeExampleText . AdditionalPages [ 0 ] . ResultsPageText
81+ . Replace ( "{0}" , publishLink ) ;
82+ this . ViewBag . WorkflowId = this . RequestItemsService . WorkflowId ;
83+ this . ViewBag . TemplateId = this . RequestItemsService . TemplateId ;
84+
85+ return this . View ( "publishWorkflow" ) ;
86+ }
87+
88+ if ( this . RequestItemsService . IsWorkflowPublished )
89+ {
90+ var publishLink = TriggerMaestroWorkflow . PublishWorkflowAsync (
91+ accountId ,
92+ this . RequestItemsService . WorkflowId ,
93+ accessToken ) . GetAwaiter ( ) . GetResult ( ) ;
94+
95+ if ( publishLink != string . Empty )
96+ {
97+ this . ViewBag . ConsentLink = this . CodeExampleText . AdditionalPages [ 0 ] . ResultsPageText
98+ . Replace ( "{0}" , publishLink ) ;
99+
100+ this . ViewBag . WorkflowId = this . RequestItemsService . WorkflowId ;
101+ this . ViewBag . TemplateId = this . RequestItemsService . TemplateId ;
102+ return this . View ( "publishWorkflow" ) ;
103+ }
104+
105+ this . RequestItemsService . IsWorkflowPublished = false ;
106+ }
107+
108+ this . ViewBag . WorkflowId = this . RequestItemsService . WorkflowId ;
109+ this . ViewBag . TemplateId = this . RequestItemsService . TemplateId ;
110+
111+ return this . View ( "mae001" ) ;
112+ }
113+ catch ( Exception apiException )
114+ {
115+ this . ViewBag . errorCode = string . Empty ;
116+ this . ViewBag . errorMessage = apiException . Message ;
117+ this . ViewBag . SupportingTexts = this . LauncherTexts . ManifestStructure . SupportingTexts ;
118+ this . ViewBag . SupportMessage = this . CodeExampleText . CustomErrorTexts [ 0 ] . ErrorMessage ;
119+
120+ return this . View ( "Error" ) ;
121+ }
122+ }
123+
31124 [ HttpPost ]
32125 [ SetViewBag ]
33126 public async Task < IActionResult > Create (
@@ -53,48 +146,29 @@ public async Task<IActionResult> Create(
53146 string accessToken = this . RequestItemsService . User . AccessToken ;
54147 string basePath = this . RequestItemsService . Session . IamBasePath ;
55148 string accountId = this . RequestItemsService . Session . AccountId ;
149+ string workflowId = this . RequestItemsService . WorkflowId ;
56150
57151 try
58152 {
59- WorkflowsListSuccess workflowsList = await TriggerMaestroWorkflow . GetMaestroWorkflow (
153+ var instance = await TriggerMaestroWorkflow . TriggerWorkflowInstance (
60154 basePath ,
61155 accessToken ,
62- accountId ) ;
156+ accountId ,
157+ workflowId ,
158+ signerEmail ,
159+ signerName ,
160+ ccEmail ,
161+ ccName ,
162+ instanceName ) ;
63163
64- if ( workflowsList . Data != null || workflowsList . Data . Count > 0 )
65- {
66- var maestroWorkflow = workflowsList . Data . FirstOrDefault ( workflow =>
67- workflow . Status == "active" && workflow . Name == "Example workflow - send invite to signer" ) ;
68-
69- if ( maestroWorkflow != null )
70- {
71- var instance = await TriggerMaestroWorkflow . TriggerWorkflowInstance (
72- basePath ,
73- accessToken ,
74- accountId ,
75- maestroWorkflow . Id ,
76- signerEmail ,
77- signerName ,
78- ccEmail ,
79- ccName ,
80- instanceName ) ;
81-
82- this . RequestItemsService . WorkflowId = maestroWorkflow . Id ;
83- this . RequestItemsService . InstanceId = instance . InstanceId ;
84-
85- this . ViewBag . h1 = this . CodeExampleText . ExampleName ;
86- this . ViewBag . Url = instance . InstanceUrl ;
87- this . ViewBag . message = string . Format ( this . CodeExampleText . ResultsPageText ) ;
88-
89- return this . View ( "embed" ) ;
90- }
91- }
164+ this . RequestItemsService . WorkflowId = workflowId ;
165+ this . RequestItemsService . InstanceId = instance . InstanceId ;
92166
93- this . ViewBag . errorCode = string . Empty ;
94- this . ViewBag . SupportingTexts = this . LauncherTexts . ManifestStructure . SupportingTexts ;
95- this . ViewBag . SupportMessage = this . CodeExampleText . CustomErrorTexts [ 0 ] . ErrorMessage ;
167+ this . ViewBag . h1 = this . CodeExampleText . ExampleName ;
168+ this . ViewBag . Url = instance . InstanceUrl ;
169+ this . ViewBag . message = string . Format ( this . CodeExampleText . ResultsPageText ) ;
96170
97- return this . View ( "Error " ) ;
171+ return this . View ( "embed " ) ;
98172 }
99173 catch ( APIException exception )
100174 {
0 commit comments