|
| 1 | +// <copyright file="TriggerWorkflow.cs" company="DocuSign"> |
| 2 | +// Copyright (c) DocuSign. All rights reserved. |
| 3 | +// </copyright> |
| 4 | + |
| 5 | +namespace DocuSign.Maestro.Controllers |
| 6 | +{ |
| 7 | + using System; |
| 8 | + using DocuSign.CodeExamples; |
| 9 | + using DocuSign.CodeExamples.Common; |
| 10 | + using DocuSign.CodeExamples.Controllers; |
| 11 | + using DocuSign.CodeExamples.Maestro.Models; |
| 12 | + using DocuSign.CodeExamples.Models; |
| 13 | + using DocuSign.Maestro.Client; |
| 14 | + using DocuSign.WebForms.Examples; |
| 15 | + using Microsoft.AspNetCore.Mvc; |
| 16 | + using Microsoft.Extensions.Configuration; |
| 17 | + using Newtonsoft.Json; |
| 18 | + |
| 19 | + [Area("Maestro")] |
| 20 | + [Route("mae001")] |
| 21 | + public class TriggerWorkflow : EgController |
| 22 | + { |
| 23 | + private IConfiguration configuration; |
| 24 | + |
| 25 | + public TriggerWorkflow( |
| 26 | + DsConfiguration config, |
| 27 | + IConfiguration configuration, |
| 28 | + LauncherTexts launcherTexts, |
| 29 | + IRequestItemsService requestItemsService) |
| 30 | + : base(config, launcherTexts, requestItemsService) |
| 31 | + { |
| 32 | + this.configuration = configuration; |
| 33 | + CodeExampleText = GetExampleText(EgName, ExamplesApiType.Maestro); |
| 34 | + ViewBag.title = CodeExampleText.ExampleName; |
| 35 | + } |
| 36 | + |
| 37 | + public override string EgName => "mae001"; |
| 38 | + |
| 39 | + [MustAuthenticate] |
| 40 | + [HttpGet] |
| 41 | + public override IActionResult Get() |
| 42 | + { |
| 43 | + try |
| 44 | + { |
| 45 | + var actionResult = base.Get(); |
| 46 | + if (RequestItemsService.EgName == EgName) |
| 47 | + { |
| 48 | + return actionResult; |
| 49 | + } |
| 50 | + |
| 51 | + RequestItemsService.WorkflowId = configuration["DocuSign:WorkflowId"]; |
| 52 | + var accessToken = RequestItemsService.User.AccessToken; |
| 53 | + var accountId = RequestItemsService.Session.AccountId; |
| 54 | + |
| 55 | + if (!RequestItemsService.WorkflowPublished) |
| 56 | + { |
| 57 | + var docuSignClient = new DocuSignClient(RequestItemsService.Session.MaestroManageApiBasePath); |
| 58 | + docuSignClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken); |
| 59 | + TriggerWorkflowService.PublishWorkFlow(docuSignClient, accountId, RequestItemsService.WorkflowId); |
| 60 | + RequestItemsService.WorkflowPublished = true; |
| 61 | + } |
| 62 | + |
| 63 | + ViewBag.Config = Config; |
| 64 | + var workflowTriggerModel = new WorkflowTriggerModel(); |
| 65 | + |
| 66 | + return View("mae001", workflowTriggerModel); |
| 67 | + } |
| 68 | + catch (ApiException apiException) |
| 69 | + { |
| 70 | + ViewBag.errorCode = apiException.ErrorCode; |
| 71 | + ViewBag.errorMessage = apiException.Message; |
| 72 | + ViewBag.SupportingTexts = LauncherTexts.ManifestStructure.SupportingTexts; |
| 73 | + |
| 74 | + return View("Error"); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + [MustAuthenticate] |
| 79 | + [SetViewBag] |
| 80 | + [HttpPost] |
| 81 | + [ValidateAntiForgeryToken] |
| 82 | + public ActionResult SubmitForm(WorkflowTriggerModel model) |
| 83 | + { |
| 84 | + try |
| 85 | + { |
| 86 | + var accessToken = RequestItemsService.User.AccessToken; |
| 87 | + var accountId = RequestItemsService.Session.AccountId; |
| 88 | + var docuSignManageClient = new DocuSignClient(RequestItemsService.Session.MaestroManageApiBasePath); |
| 89 | + docuSignManageClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken); |
| 90 | + var workflow = TriggerWorkflowService.GetWorkFlowDefinition(docuSignManageClient, accountId, RequestItemsService.WorkflowId); |
| 91 | + |
| 92 | + var docuSignAuthClient = new DocuSignClient(RequestItemsService.Session.MaestroAuthApiBasePath); |
| 93 | + docuSignAuthClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken); |
| 94 | + var result = TriggerWorkflowService.TriggerWorkflow(docuSignAuthClient, accountId, new Uri(workflow.TriggerUrl), model); |
| 95 | + |
| 96 | + RequestItemsService.InstanceId = result.InstanceId; |
| 97 | + |
| 98 | + ViewBag.h1 = CodeExampleText.ExampleName; |
| 99 | + ViewBag.message = CodeExampleText.ResultsPageText; |
| 100 | + ViewBag.Locals.Json = JsonConvert.SerializeObject(result, Formatting.Indented); |
| 101 | + |
| 102 | + return View("example_done"); |
| 103 | + } |
| 104 | + catch (ApiException apiException) |
| 105 | + { |
| 106 | + ViewBag.errorCode = apiException.ErrorCode; |
| 107 | + ViewBag.errorMessage = apiException.Message; |
| 108 | + ViewBag.SupportingTexts = LauncherTexts.ManifestStructure.SupportingTexts; |
| 109 | + |
| 110 | + return View("Error"); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments