|
| 1 | +// <copyright file="DeleteRestoreEnvelope.cs" company="Docusign"> |
| 2 | +// Copyright (c) Docusign. All rights reserved. |
| 3 | +// </copyright> |
| 4 | + |
| 5 | +namespace DocuSign.CodeExamples.ESignature.Controllers |
| 6 | +{ |
| 7 | + using System.Linq; |
| 8 | + using DocuSign.CodeExamples.Common; |
| 9 | + using DocuSign.CodeExamples.Controllers; |
| 10 | + using DocuSign.CodeExamples.Models; |
| 11 | + using DocuSign.eSign.Model; |
| 12 | + using Microsoft.AspNetCore.Mvc; |
| 13 | + using Newtonsoft.Json; |
| 14 | + |
| 15 | + [Area("eSignature")] |
| 16 | + [Route("Eg045")] |
| 17 | + public class DeleteRestoreEnvelope : EgController |
| 18 | + { |
| 19 | + private const string DeleteFolderId = "recyclebin"; |
| 20 | + |
| 21 | + private const string SentItemsFolderName = "Sent Items"; |
| 22 | + |
| 23 | + public DeleteRestoreEnvelope(DsConfiguration config, LauncherTexts launcherTexts, IRequestItemsService requestItemsService) |
| 24 | + : base(config, launcherTexts, requestItemsService) |
| 25 | + { |
| 26 | + this.CodeExampleText = this.GetExampleText(this.EgName, ExamplesApiType.ESignature); |
| 27 | + this.ViewBag.title = this.CodeExampleText.ExampleName; |
| 28 | + } |
| 29 | + |
| 30 | + public override string EgName => "Eg045"; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Deletes an envelope by moving it to the recycle bin. |
| 34 | + /// </summary> |
| 35 | + /// <returns>IActionResult</returns> |
| 36 | + [HttpPost] |
| 37 | + [Route("DeleteEnvelopeAction")] |
| 38 | + [SetViewBag] |
| 39 | + public IActionResult DeleteEnvelopeAction(string envelopeId) |
| 40 | + { |
| 41 | + bool tokenOk = this.CheckToken(3); |
| 42 | + |
| 43 | + if (!tokenOk) |
| 44 | + { |
| 45 | + this.RequestItemsService.EgName = this.EgName; |
| 46 | + return this.Redirect("/ds/mustAuthenticate"); |
| 47 | + } |
| 48 | + |
| 49 | + string basePath = this.RequestItemsService.Session.BasePath + "/restapi"; |
| 50 | + string accessToken = this.RequestItemsService.User.AccessToken; |
| 51 | + string accountId = this.RequestItemsService.Session.AccountId; |
| 52 | + |
| 53 | + this.RequestItemsService.EnvelopeId = envelopeId; |
| 54 | + |
| 55 | + global::ESignature.Examples.DeleteRestoreEnvelope.DeleteEnvelope( |
| 56 | + accessToken, |
| 57 | + basePath, |
| 58 | + accountId, |
| 59 | + envelopeId); |
| 60 | + |
| 61 | + this.ViewBag.h1 = this.CodeExampleText.ExampleName; |
| 62 | + this.ViewBag.ConfirmAdditionalLink = nameof(this.GetRestoreEnvelope); |
| 63 | + this.ViewBag.OnlyConfirmAdditionalLink = true; |
| 64 | + this.ViewBag.message = string.Format( |
| 65 | + this.CodeExampleText.AdditionalPages |
| 66 | + .FirstOrDefault(x => x.Name.Equals("envelope_is_deleted")) |
| 67 | + ?.ResultsPageText, |
| 68 | + envelopeId); |
| 69 | + |
| 70 | + return this.View("example_done"); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Restores an envelope from the recycle bin back to the Sent Items folder. |
| 75 | + /// </summary> |
| 76 | + /// <returns>IActionResult</returns> |
| 77 | + [HttpPost] |
| 78 | + [Route("RestoreEnvelopeAction")] |
| 79 | + [SetViewBag] |
| 80 | + public IActionResult RestoreEnvelopeAction(string folderName) |
| 81 | + { |
| 82 | + bool tokenOk = this.CheckToken(3); |
| 83 | + folderName = folderName ?? SentItemsFolderName; |
| 84 | + |
| 85 | + if (!tokenOk) |
| 86 | + { |
| 87 | + this.RequestItemsService.EgName = this.EgName; |
| 88 | + return this.Redirect("/ds/mustAuthenticate"); |
| 89 | + } |
| 90 | + |
| 91 | + string basePath = this.RequestItemsService.Session.BasePath + "/restapi"; |
| 92 | + string accessToken = this.RequestItemsService.User.AccessToken; |
| 93 | + string accountId = this.RequestItemsService.Session.AccountId; |
| 94 | + |
| 95 | + FoldersResponse availableFolders = global::ESignature.Examples.DeleteRestoreEnvelope.GetFolders( |
| 96 | + accessToken, |
| 97 | + basePath, |
| 98 | + accountId); |
| 99 | + Folder folder = global::ESignature.Examples.DeleteRestoreEnvelope.GetFolderByName( |
| 100 | + availableFolders.Folders, |
| 101 | + folderName); |
| 102 | + |
| 103 | + if (folder == null) |
| 104 | + { |
| 105 | + this.ViewBag.h1 = this.CodeExampleText.ExampleName; |
| 106 | + this.ViewBag.message = string.Format(this.CodeExampleText.AdditionalPages[1].ResultsPageText, folderName); |
| 107 | + this.ViewBag.ConfirmAdditionalLink = nameof(this.GetRestoreEnvelope); |
| 108 | + this.ViewBag.OnlyConfirmAdditionalLink = true; |
| 109 | + |
| 110 | + return this.View("example_done"); |
| 111 | + } |
| 112 | + |
| 113 | + global::ESignature.Examples.DeleteRestoreEnvelope.MoveEnvelopeToFolder( |
| 114 | + accessToken, |
| 115 | + basePath, |
| 116 | + accountId, |
| 117 | + this.RequestItemsService.EnvelopeId, |
| 118 | + folder.FolderId, |
| 119 | + DeleteFolderId); |
| 120 | + |
| 121 | + this.ViewBag.h1 = this.CodeExampleText.ExampleName; |
| 122 | + this.ViewBag.message = string.Format( |
| 123 | + this.CodeExampleText.ResultsPageText, |
| 124 | + this.RequestItemsService.EnvelopeId, |
| 125 | + folder.Type, |
| 126 | + folderName); |
| 127 | + |
| 128 | + return this.View("example_done"); |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Displays a page allowing the user to restore a previously deleted envelope. |
| 133 | + /// </summary> |
| 134 | + /// <returns>ActionResult</returns> |
| 135 | + [SetViewBag] |
| 136 | + [HttpGet] |
| 137 | + [Route("GetRestoreEnvelope")] |
| 138 | + public ActionResult GetRestoreEnvelope() |
| 139 | + { |
| 140 | + this.ViewBag.CodeExampleText = this.CodeExampleText; |
| 141 | + this.ViewBag.SupportingTexts = this.LauncherTexts.ManifestStructure.SupportingTexts; |
| 142 | + this.ViewBag.EnvelopeId = this.RequestItemsService.EnvelopeId; |
| 143 | + |
| 144 | + return this.View("restoreEnvelope"); |
| 145 | + } |
| 146 | + |
| 147 | + protected override void InitializeInternal() |
| 148 | + { |
| 149 | + base.InitializeInternal(); |
| 150 | + this.ViewBag.EnvelopeId = this.RequestItemsService.EnvelopeId; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments