} // ProcessPending private EchoSignSendResultCode Send(EchoSignEnvelope oLetter, EchoSignSendResult result) { if (!this.isReady) { const string msg = "EchoSign cannot send - not ready."; this.log.Warn("{0}", msg); result.AddErrorMessage(msg); return(EchoSignSendResultCode.Fail); } // if if (oLetter == null) { const string msg = "NULL EchoSign request discovered."; this.log.Warn("{0}", msg); result.AddErrorMessage(msg); return(EchoSignSendResultCode.Fail); } // if if (!oLetter.IsValid) { string msg = string.Format("Some data are missing in EchoSign request: {0}.", oLetter); this.log.Warn("{0}", msg); result.AddErrorMessage(msg); return(EchoSignSendResultCode.Fail); } // if return(Send( oLetter.CustomerID, oLetter.CashRequestID, oLetter.Directors ?? new int[0], oLetter.ExperianDirectors ?? new int[0], oLetter.TemplateID, oLetter.SendToCustomer, result )); } // Send
private EchoSignSendResultCode SendOne( Template oTemplate, byte[] oFileContent, List <Person> oAddressee, int nCustomerID, long cashRequestID, int nTemplateID, bool bSentToCustomer, EchoSignSendResult result ) { var oRecipients = oAddressee.Select(oPerson => new RecipientInfo { email = oPerson.Email, role = RecipientRole.SIGNER, }).ToArray(); var sAllRecipients = string.Join(", ", oRecipients.Select(r => r.email)); var fi = new FileInfo { fileName = oTemplate.FileName, mimeType = oTemplate.MimeType, file = oFileContent ?? oTemplate.FileContent, }; var dci = new DocumentCreationInfo { name = oTemplate.DocumentName, signatureType = SignatureType.ESIGN, reminderFrequency = this.reminderFrequency, signatureFlow = SignatureFlow.PARALLEL, daysUntilSigningDeadline = this.deadline, recipients = oRecipients, fileInfos = new[] { fi }, }; this.log.Debug("Sending a document '{0}' to {1}...", oTemplate.DocumentName, sAllRecipients); DocumentKey[] aryResult; try { if (this.isUseRestApi) { var response = this.restClient.SendAgreement(dci).Result; if (string.IsNullOrEmpty(response.agreementId)) { throw new Exception("error sending when sent agreement"); } DocumentKey documentKey = new DocumentKey { documentKey = response.agreementId }; aryResult = new[] { documentKey }; } else { aryResult = this.echoSign.sendDocument(this.apiKey, null, dci); } } catch (Exception e) { string msg = string.Format( "Something went exceptionally terrible while sending a document '{0}' to {1}.", oTemplate.DocumentName, sAllRecipients ); this.log.Warn(e, msg); result.AddErrorMessage(msg); return(EchoSignSendResultCode.Fail); } // try if (aryResult.Length != 1) { const string msg = "Failed to send documents for signing."; this.log.Alert(msg); result.AddErrorMessage(msg); } else { this.log.Debug("Sending result: document key is '{0}'.", aryResult[0].documentKey); var sp = new SpSaveEsignSent(this.db, this.log) { CustomerID = nCustomerID, CashRequestID = cashRequestID, Directors = oAddressee.Where(x => x.PersonType == PersonType.Director).Select(x => x.ID).ToList(), ExperianDirectors = oAddressee .Where(x => x.PersonType == PersonType.ExperianDirector) .Select(x => x.ID).ToList(), DocumentKey = aryResult[0].documentKey, SentToCustomer = bSentToCustomer, TemplateID = nTemplateID, }; sp.ExecuteNonQuery(); } // if return(EchoSignSendResultCode.Success); } // SendOne
} // Send private EchoSignSendResultCode Send( int nCustomerID, long cashRequestID, IEnumerable <int> aryDirectors, IEnumerable <int> aryExperianDirectors, int nTemplateID, bool bSendToCustomer, EchoSignSendResult result ) { SpLoadDataForEsign sp; try { sp = new SpLoadDataForEsign(this.db, this.log) { CustomerID = nCustomerID, TemplateID = nTemplateID, DirectorIDs = aryDirectors.ToList(), ExperianDirectorIDs = aryExperianDirectors.ToList(), }; sp.Load(); } catch (Exception e) { const string msg = "EchoSign cannot send: failed to load all the data from database."; this.log.Warn(e, msg); result.AddErrorMessage("{0} {1}", msg, e.Message); return(EchoSignSendResultCode.Fail); } // try if (!sp.IsReady) { string msg = string.Format( "EchoSign cannot send: failed to load all the data from database.\n{0}", string.Join("\n", sp.ErrorList) ); this.log.Warn(msg); result.AddErrorMessage(msg); return(EchoSignSendResultCode.Fail); } // if List <Person> oRecipients = new List <Person>(); oRecipients.AddRange(sp.Directors); oRecipients.AddRange(sp.ExperianDirectors); if (bSendToCustomer) { oRecipients.Add(sp.Customer); } if (oRecipients.Count < 1) { const string msg = "EchoSign cannot send: no recipients specified."; this.log.Warn(msg); result.AddErrorMessage(msg); return(EchoSignSendResultCode.Fail); } // if switch (sp.Template.TemplateType) { case TemplateType.BoardResolution: return(SendOne( sp.Template, null, oRecipients, sp.Customer.ID, cashRequestID, sp.Template.ID, bSendToCustomer, result )); case TemplateType.PersonalGuarantee: int nApprovedSum = this.db.ExecuteScalar <int>( "LoadCustomerLatestApprovedSum", CommandSpecies.StoredProcedure, new QueryParameter("CustomerID", sp.Customer.ID) ); if (nApprovedSum <= 0) { const string msg = "EchoSign cannot send: approved sum is not positive."; this.log.Warn(msg); result.AddErrorMessage(msg); return(EchoSignSendResultCode.Fail); } // if int nTotalCount = 0; int nSuccessCount = 0; foreach (Person oRecipient in oRecipients) { bool bIsCustomer = ReferenceEquals(oRecipient, sp.Customer); if (bIsCustomer && !bSendToCustomer) { continue; } nTotalCount++; EchoSignSendResultCode bSendOneResult = SendOne( sp.Template, sp.Template.PersonalGuarantee(oRecipient, nApprovedSum), new List <Person> { oRecipient }, sp.Customer.ID, cashRequestID, sp.Template.ID, bIsCustomer, result ); if (EchoSignSendResultCode.Success == bSendOneResult) { nSuccessCount++; } } // for each if (nSuccessCount == 0) { result.AddErrorMessage("No documents were sent."); return(EchoSignSendResultCode.Fail); } // if if (nTotalCount == nSuccessCount) { return(EchoSignSendResultCode.Success); } else { result.AddErrorMessage("Some documents were not sent."); return(EchoSignSendResultCode.Partial); } // if default: { string msg = string.Format( "EchoSign cannot send: don't know how to send template of type {0}.", sp.Template.TemplateType ); this.log.Warn("{0}", msg); result.AddErrorMessage(msg); return(EchoSignSendResultCode.Fail); } } // switch } // Send