private static IEnumerable <KeyValueDTO> GetTabs(DocuSignApiConfiguration conf, object api, string id, Signer recipient)
        {
            var envelopesApi = api as EnvelopesApi;
            var templatesApi = api as TemplatesApi;
            var docutabs     = envelopesApi != null
                            ? envelopesApi.ListTabs(conf.AccountId, id, recipient.RecipientId)
                            : templatesApi.ListTabs(conf.AccountId, id, recipient.RecipientId, new Tabs());

            return(DocuSignTab.GetEnvelopeTabsPerSigner(JObject.Parse(docutabs.ToJson()), recipient.RoleName));
        }
        //this is purely for Send_DocuSign_Envelope activity
        public Tuple <IEnumerable <KeyValueDTO>, IEnumerable <DocuSignTabDTO> > GetTemplateRecipientsTabsAndDocuSignTabs(DocuSignApiConfiguration conf, string templateId)
        {
            var tmpApi            = new TemplatesApi(conf.Configuration);
            var recipientsAndTabs = new List <KeyValueDTO>();
            var docuTabs          = new List <DocuSignTabDTO>();

            var recipients = GetRecipients(conf, tmpApi, templateId);

            recipientsAndTabs.AddRange(MapRecipientsToFieldDTO(recipients));

            foreach (var signer in recipients.Signers)
            {
                var tabs            = tmpApi.ListTabs(conf.AccountId, templateId, signer.RecipientId, new Tabs());
                var signersdocutabs = DocuSignTab.ExtractTabs(JObject.Parse(tabs.ToJson()), signer.RoleName).ToList();
                docuTabs.AddRange(signersdocutabs);
                recipientsAndTabs.AddRange(DocuSignTab.MapTabsToFieldDTO(signersdocutabs));
            }

            return(new Tuple <IEnumerable <KeyValueDTO>, IEnumerable <DocuSignTabDTO> >(recipientsAndTabs, docuTabs));
        }
        public static DocuSignEnvelopeCM_v2 ParseAPIresponsesIntoCM(out DocuSignEnvelopeCM_v2 envelope, TemplateInformation templates, Recipients recipients)
        {
            envelope = new DocuSignEnvelopeCM_v2();
            envelope.CurrentRoutingOrderId = recipients.CurrentRoutingOrder;


            if (templates.Templates != null)
            {
                foreach (var ds_template in templates.Templates)
                {
                    DocuSignTemplate template = new DocuSignTemplate();
                    template.DocumentId = ds_template.DocumentId;
                    template.Name       = ds_template.Name;
                    template.TemplateId = ds_template.TemplateId;
                    envelope.Templates.Add(template);
                }
            }

            //Recipients

            if (recipients.Signers != null)
            {
                foreach (var dsrecipient in recipients.Signers)
                {
                    DocuSignRecipientStatus recipient = new DocuSignRecipientStatus();
                    recipient.Email          = dsrecipient.Email;
                    recipient.Name           = dsrecipient.Name;
                    recipient.RecipientId    = dsrecipient.RecipientId;
                    recipient.RoutingOrderId = dsrecipient.RoutingOrder;
                    recipient.Status         = dsrecipient.Status;
                    recipient.Type           = "Signer";
                    envelope.Recipients.Add(recipient);

                    //Tabs
                    if (dsrecipient.Tabs != null)
                    {
                        var tabsDTO = DocuSignTab.ExtractTabs(JObject.Parse(dsrecipient.Tabs.ToJson()), "");

                        foreach (var tabDTO in tabsDTO)
                        {
                            DocuSignTabStatus tab = new DocuSignTabStatus();
                            tab.DocumentId = tabDTO.DocumentId.ToString();
                            tab.Name       = tabDTO.Name;
                            tab.TabType    = tabDTO.Type;
                            tab.Value      = tabDTO.Value;
                            tab.TabLabel   = tabDTO.TabLabel;

                            if (tabDTO is DocuSignMultipleOptionsTabDTO)
                            {
                                var multiTabDTO = (DocuSignMultipleOptionsTabDTO)tabDTO;
                                foreach (var childDTO in multiTabDTO.Items)
                                {
                                    var childTab = new DocuSignTabStatus();
                                    childTab.Selected = childDTO.Selected.ToString();
                                    childTab.Value    = childDTO.Value;
                                    childTab.TabLabel = childDTO.Text;
                                    tab.Items.Add(childTab);
                                }
                            }
                            recipient.Tabs.Add(tab);
                        }
                    }
                }
            }
            return(envelope);
        }
        public void SendAnEnvelopeFromTemplate(DocuSignApiConfiguration loginInfo, List <KeyValueDTO> rolesList, List <KeyValueDTO> fieldList, string curTemplateId, StandardFileDescriptionCM fileHandler = null)
        {
            EnvelopesApi    envelopesApi      = new EnvelopesApi(loginInfo.Configuration);
            TemplatesApi    templatesApi      = new TemplatesApi(loginInfo.Configuration);
            bool            override_document = fileHandler != null;
            Recipients      recipients        = null;
            EnvelopeSummary envelopeSummary   = null;

            //creatig an envelope definiton
            EnvelopeDefinition envDef = new EnvelopeDefinition();

            envDef.EmailSubject = "Test message from Fr8";
            envDef.Status       = "created";

            Debug.WriteLine($"sending an envelope from template {curTemplateId} to {loginInfo}");
            var templateRecepients = templatesApi.ListRecipients(loginInfo.AccountId, curTemplateId);

            //adding file or applying template
            if (override_document)
            {
                //if we override document - we don't create an envelope yet
                //we create it with recipients once we've processed recipient values and tabs
                envDef.Documents = new List <Document>()
                {
                    new Document()
                    {
                        DocumentBase64 = fileHandler.TextRepresentation, FileExtension = fileHandler.Filetype,
                        DocumentId     = "1", Name = fileHandler.Filename ?? Path.GetFileName(fileHandler.DirectUrl) ?? "document"
                    }
                };
                recipients = templateRecepients;
            }
            else
            {
                //creating envelope
                envDef.TemplateId = curTemplateId;
                envelopeSummary   = envelopesApi.CreateEnvelope(loginInfo.AccountId, envDef);
                //requesting list of recipients since their Ids might be different from the one we got from tempates
                recipients = envelopesApi.ListRecipients(loginInfo.AccountId, envelopeSummary.EnvelopeId);
            }

            //updating recipients
            foreach (var recepient in recipients.Signers)
            {
                var correspondingTemplateRecipient = templateRecepients.Signers.FirstOrDefault(a => a.RoutingOrder == recepient.RoutingOrder);
                var relatedFields = rolesList.Where(a => a.Tags.Contains("recipientId:" + correspondingTemplateRecipient?.RecipientId)).ToArray();
                var newEmail      = relatedFields.FirstOrDefault(a => a.Key.Contains(DocuSignConstants.DocuSignRoleEmail))?.Value;
                var newName       = relatedFields.FirstOrDefault(a => a.Key.Contains(DocuSignConstants.DocuSignRoleName))?.Value;
                recepient.Name  = string.IsNullOrEmpty(newName) ? recepient.Name : newName;
                recepient.Email = string.IsNullOrEmpty(newEmail) ? recepient.Email : newEmail;

                if (!recepient.Email.IsValidEmailAddress())
                {
                    throw new ApplicationException($"'{recepient.Email}' is not a valid email address");
                }

                //updating tabs
                var tabs = override_document ? templatesApi.ListTabs(loginInfo.AccountId, curTemplateId, correspondingTemplateRecipient.RecipientId, new Tabs()) : envelopesApi.ListTabs(loginInfo.AccountId, envelopeSummary.EnvelopeId, recepient.RecipientId);

                JObject jobj = DocuSignTab.ApplyValuesToTabs(fieldList, correspondingTemplateRecipient, tabs);
                recepient.Tabs = jobj.ToObject <Tabs>();
            }

            if (override_document)
            {
                //deep copy to exclude tabs
                var recps_deep_copy = JsonConvert.DeserializeObject <Recipients>(JsonConvert.SerializeObject(recipients));
                recps_deep_copy.Signers.ForEach(a => a.Tabs = null);
                envDef.Recipients = recps_deep_copy;
                //creating envlope
                envelopeSummary = envelopesApi.CreateEnvelope(loginInfo.AccountId, envDef);
            }
            else
            {
                envelopesApi.UpdateRecipients(loginInfo.AccountId, envelopeSummary.EnvelopeId, recipients);
            }

            foreach (var recepient in recipients.Signers)
            {
                if (override_document)
                {
                    JObject jobj = JObject.Parse(recepient.Tabs.ToJson());
                    foreach (var item in jobj.Properties())
                    {
                        foreach (var tab in (JToken)item.Value)
                        {
                            tab["documentId"] = "1";
                        }
                    }
                    var tabs = jobj.ToObject <Tabs>();
                    envelopesApi.CreateTabs(loginInfo.AccountId, envelopeSummary.EnvelopeId, recepient.RecipientId, tabs);
                }
                else
                if (typeof(Tabs).GetProperties()
                    .Select(prop => prop.GetValue(recepient.Tabs, null))
                    .Any(val => val != null))
                {
                    envelopesApi.UpdateTabs(loginInfo.AccountId, envelopeSummary.EnvelopeId, recepient.RecipientId, recepient.Tabs);
                }
            }

            // sending an envelope
            envelopesApi.Update(loginInfo.AccountId, envelopeSummary.EnvelopeId, new Envelope()
            {
                Status = "sent"
            });
        }