| 
 | 1 | +// <copyright file="Mae001TriggerWorkflowController.cs" company="DocuSign">  | 
 | 2 | +// Copyright (c) DocuSign. All rights reserved.  | 
 | 3 | +// </copyright>  | 
 | 4 | + | 
 | 5 | +namespace DocuSign.CodeExamples.Controllers  | 
 | 6 | +{  | 
 | 7 | +    using System;  | 
 | 8 | +    using System.Linq;  | 
 | 9 | +    using System.Threading.Tasks;  | 
 | 10 | +    using DocuSign.CodeExamples.Common;  | 
 | 11 | +    using DocuSign.CodeExamples.Examples;  | 
 | 12 | +    using DocuSign.CodeExamples.Models;  | 
 | 13 | +    using Docusign.IAM.SDK.Models.Components;  | 
 | 14 | +    using Docusign.IAM.SDK.Models.Errors;  | 
 | 15 | +    using Microsoft.AspNetCore.Mvc;  | 
 | 16 | + | 
 | 17 | +    [Area("Maestro")]  | 
 | 18 | +    [Route("mae001")]  | 
 | 19 | +    public class Mae001TriggerWorkflowController : EgController  | 
 | 20 | +    {  | 
 | 21 | +        private const string WorkflowName = "Example workflow - send invite to signer";  | 
 | 22 | + | 
 | 23 | +        public Mae001TriggerWorkflowController(DsConfiguration dsConfig,  | 
 | 24 | +            LauncherTexts launcherTexts,  | 
 | 25 | +            IRequestItemsService requestItemsService)  | 
 | 26 | +            : base(dsConfig, launcherTexts, requestItemsService)  | 
 | 27 | +        {  | 
 | 28 | +            this.CodeExampleText = this.GetExampleText(this.EgName, ExamplesApiType.Maestro);  | 
 | 29 | +            this.ViewBag.title = this.CodeExampleText.ExampleName;  | 
 | 30 | +        }  | 
 | 31 | + | 
 | 32 | +        public override string EgName => "mae001";  | 
 | 33 | + | 
 | 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 | + | 
 | 124 | +        [HttpPost]  | 
 | 125 | +        [SetViewBag]  | 
 | 126 | +        public async Task<IActionResult> Create(  | 
 | 127 | +            string instanceName,  | 
 | 128 | +            string signerEmail,  | 
 | 129 | +            string signerName,  | 
 | 130 | +            string ccEmail,  | 
 | 131 | +            string ccName)  | 
 | 132 | +        {  | 
 | 133 | +            // Check the token with minimal buffer time.  | 
 | 134 | +            bool tokenOk = this.CheckToken(3);  | 
 | 135 | +            if (!tokenOk)  | 
 | 136 | +            {  | 
 | 137 | +                // We could store the parameters of the requested operation  | 
 | 138 | +                // so it could be restarted automatically.  | 
 | 139 | +                // But since it should be rare to have a token issue here,  | 
 | 140 | +                // we'll make the user re-enter the form data after  | 
 | 141 | +                // authentication.  | 
 | 142 | +                this.RequestItemsService.EgName = this.EgName;  | 
 | 143 | +                return this.Redirect("/ds/mustAuthenticate");  | 
 | 144 | +            }  | 
 | 145 | + | 
 | 146 | +            string accessToken = this.RequestItemsService.User.AccessToken;  | 
 | 147 | +            string basePath = this.RequestItemsService.Session.IamBasePath;  | 
 | 148 | +            string accountId = this.RequestItemsService.Session.AccountId;  | 
 | 149 | +            string workflowId = this.RequestItemsService.WorkflowId;  | 
 | 150 | + | 
 | 151 | +            try  | 
 | 152 | +            {  | 
 | 153 | +                var instance = await TriggerMaestroWorkflow.TriggerWorkflowInstance(  | 
 | 154 | +                    basePath,  | 
 | 155 | +                    accessToken,  | 
 | 156 | +                    accountId,  | 
 | 157 | +                    workflowId,  | 
 | 158 | +                    signerEmail,  | 
 | 159 | +                    signerName,  | 
 | 160 | +                    ccEmail,  | 
 | 161 | +                    ccName,  | 
 | 162 | +                    instanceName);  | 
 | 163 | + | 
 | 164 | +                this.RequestItemsService.WorkflowId = workflowId;  | 
 | 165 | +                this.RequestItemsService.InstanceId = instance.InstanceId;  | 
 | 166 | + | 
 | 167 | +                this.ViewBag.h1 = this.CodeExampleText.ExampleName;  | 
 | 168 | +                this.ViewBag.Url = instance.InstanceUrl;  | 
 | 169 | +                this.ViewBag.message = string.Format(this.CodeExampleText.ResultsPageText);  | 
 | 170 | + | 
 | 171 | +                return this.View("embed");  | 
 | 172 | +            }  | 
 | 173 | +            catch (APIException exception)  | 
 | 174 | +            {  | 
 | 175 | +                this.ViewBag.errorCode = exception.StatusCode;  | 
 | 176 | +                this.ViewBag.SupportingTexts = this.LauncherTexts.ManifestStructure.SupportingTexts;  | 
 | 177 | +                this.ViewBag.errorMessage = exception.Message;  | 
 | 178 | + | 
 | 179 | +                return this.View("Error");  | 
 | 180 | +            }  | 
 | 181 | +        }  | 
 | 182 | +    }  | 
 | 183 | +}  | 
0 commit comments