Пример #1
0
 /// <summary>
 /// This function returns the list for top search
 /// </summary>
 /// <param name="keyword"></param>
 /// <returns></returns>
 public List<AutoComplete> GetAutoCompleteTopSearch(string keyword)
 {
     var reslut = new ClientsAndContacts().GetTopSearchResults(new ClientContactFilter
     {
         Keyword = keyword,
         RetrieveType = "ALL",
         RecordsPerPage = 25,
         CurrentPage = 1
     }).ToList();
     return reslut.Select(t => new AutoComplete
     {
         id = t.IsContact ? t.ContactId : t.ClientId,
         name = t.IsContact ? (t.Forename + " " + t.Surname) : t.ClientName,
         dataObj = t
     }).ToList();
 }
 private void LoadContacts(int clientId)
 {
     var contacts = new ClientsAndContacts().GetClientsAndContacts(new ClientContactFilter
     {
         ClientId = clientId
     });
 }
Пример #3
0
        /// <summary>
        /// This function get the email preview
        /// </summary>
        /// <param name="emailRequest"></param>
        /// <returns></returns>
        public static string GetEmailPreview(PostEmailRequest emailRequest)
        {
            User fromUser = null;
            User consultant = null;
            Job job = null;
            User sendUser = null;

            // Retrieve objects if an id passed
            if (emailRequest.JobId > 0)
                job = new Jobs().GetJob(emailRequest.JobId);
            // get the user if user id passed
            if (emailRequest.SentBy > 0)
                sendUser = new Users().GetUser(emailRequest.SentBy);
            //get the from address
            if (emailRequest.FromUserId > 0)
                fromUser = new Users().GetUser(emailRequest.FromUserId);

            //Get the consultant/user object
            if (emailRequest.ConsultantId > 0 || (fromUser != null && fromUser.UserType.UserTypeId == 1))
            {
                consultant = emailRequest.ConsultantId > 0 ? new Users().GetUser(emailRequest.ConsultantId) : fromUser;
            }
            // set the footer  
            emailRequest.RawBody = Utils.GetFullEmailReplacingMainTemplateTags(emailRequest.FromUserId, emailRequest.RawBody, true, true, false);
            // consultant id (if consultant id passed the use that else check whether the from user is a consultant and use that for tags )
            var consultantId = emailRequest.ConsultantId > 0
                ? emailRequest.ConsultantId
                : ((fromUser != null && fromUser.UserType.UserTypeId == 1) ? fromUser.UserId : 0);

            if (emailRequest.SelectedCount == 1)
            {
                // Get contacts for the filters or sent ids (calling a stored procedure, might take some time as no paging added )
                var contactsAndClients =
                    new ClientsAndContacts().GetClientContactsForEmailAndSms(emailRequest.SelectedContactIds,
                        emailRequest.SelectedClientIds).ToList();

                if (contactsAndClients.Count > 0)
                {
                    var obj = contactsAndClients[0];
                    var contact = obj.ClientOrContact as Candidate;
                    var client = obj.ClientOrContact as Client;

                    if ((contact != null && !string.IsNullOrEmpty(contact.Email)) || (client != null && !string.IsNullOrEmpty(client.Email)))
                    {
                        // get the original message 
                        emailRequest.Body = new Utils().GetOriginalBodyForPreview(emailRequest.RawBody,
                            (contact != null ? contact.CandidateId : -1),
                            emailRequest.JobId, emailRequest.SentBy, consultantId
                            , (client != null ? client.ClientId : -1), contact, job, sendUser, consultant, client, 0, emailRequest.TimeSlotId);
                        return emailRequest.Body;
                    }
                }
            }

            // get the original message 
            emailRequest.Body = new Utils().GetOriginalBodyForPreview(emailRequest.RawBody, 0, emailRequest.JobId, emailRequest.SentBy, consultantId, 0, null, job,
                sendUser, consultant, null, 0, emailRequest.TimeSlotId);


            return emailRequest.Body;
        }
Пример #4
0
    /// <summary>
    /// This function send the sms for the contacts
    /// </summary>
    /// <param name="smsRequest"></param>
    /// <returns></returns>
    // ReSharper disable once FunctionComplexityOverflow
    public PostSmsResponse SendContactSms(PostSmsRequest smsRequest)
    {
        // init the response object
        var response = new PostSmsResponse { IsSuccess = true, ErrorContacts = new List<EmailSmsError>() };

        try
        {
            if (smsRequest.RawMessage != null)
            {
                // removes the htnk from the message and replace the brs with new lines.
                smsRequest.RawMessage = RemoveHtml(smsRequest.RawMessage);
                // Get the sms body by replacing the tags
                Job job = null;
                User cUser = null;
                // Retrieve objects if an id passed
                if (smsRequest.JobId > 0)
                    job = new Jobs().GetJob(smsRequest.JobId);
                //Get the user object
                if (smsRequest.UserId > 0)
                    cUser = new Users().GetUser(smsRequest.UserId);


                // Get contacts for the sent ids 
                var clientContacts = new ClientsAndContacts().GetClientContactsForEmailAndSms(smsRequest.SelectedIds, "");

                //iterate through the ontacts and send the sms
                foreach (var obj in clientContacts)
                {
                    var contact = obj.ClientOrContact as Candidate;
                    if (contact != null)
                    {
                        EmailSmsError errorContact = null;
                        var message = smsRequest.RawMessage;
                        if (!string.IsNullOrEmpty(contact.Mobile))
                        {
                            // get the original message
                            message = new Utils().GetOriginalBody(smsRequest.RawMessage, contact.CandidateId, smsRequest.JobId,
                                smsRequest.UserId, 0, 0, contact, job, cUser);
                            // send sms
                            try
                            {
                                var responseFromServer = SendSms(new Sms { Message = message, Recipient = contact.Mobile, Sender = smsRequest.Sender });
                                if (!responseFromServer.Contains("From"))
                                {
                                    //error from SMS Api server
                                    errorContact = new EmailSmsError { ContactId = contact.CandidateId, Error = responseFromServer };
                                    response.ErrorContacts.Add(errorContact);
                                }
                            }
                            catch (Exception ex)
                            {
                                //error sending the sms 
                                errorContact = new EmailSmsError { ContactId = contact.CandidateId, Error = ex.ToString() };
                                response.ErrorContacts.Add(errorContact);
                            }
                        }
                        else
                        {
                            errorContact = new EmailSmsError { ContactId = contact.CandidateId, Error = "Mobile Number not found for the contact!" };
                            response.ErrorContacts.Add(errorContact);
                        }
                        // On success save sms into the database
                        var context = new dbDataContext();
                        var objSms = new tbl_CandidateSm()
                        {
                            Message = message,
                            Sender = smsRequest.Sender,
                            CandidateId = contact.CandidateId,
                            ClientUserId = smsRequest.UserId,
                            SentDate = DateTime.Now,
                            RawMessage = smsRequest.RawMessage,
                            Error = errorContact != null ? errorContact.Error : "",
                            IsError = errorContact != null,
                            MobileNubmer = contact.Mobile
                        };
                        context.tbl_CandidateSms.InsertOnSubmit(objSms);
                        context.SubmitChanges();
                        // Add History 
                        if (errorContact == null)
                        {
                            new Histories().AddHistory(new History
                            {
                                RefId = contact.CandidateId,
                                RefType = "Contact",
                                ClientUserId = smsRequest.UserId,
                                TypeId = 10,
                                SubRefType = "Sms",
                                SubRefId = objSms.SmsId
                            });
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            response.IsSuccess = false;
            response.Error = e.ToString();
        }
        return response;
    }
Пример #5
0
        /// <summary>
        /// This function sends the email and save a record to the database
        /// </summary>
        /// <param name="emailRequest"></param>
        /// <returns></returns>
        // ReSharper disable once FunctionComplexityOverflow
        public PostEmailResponse SendContactEmails(PostEmailRequest emailRequest)
        {
            // init the response object
            var response = new PostEmailResponse { IsSuccess = true, ErrorContacts = new List<EmailSmsError>() };
            try
            {
                User fromUser = null;
                User consultant = null;
                Job job = null;
                User sendUser = null;

                // Retrieve objects if an id passed
                if (emailRequest.JobId > 0)
                    job = new Jobs().GetJob(emailRequest.JobId);
                // get the user if user id passed- who is sending
                if (emailRequest.SentBy > 0)
                    sendUser = new Users().GetUser(emailRequest.SentBy);
                //get the from address
                if (emailRequest.FromUserId > 0)
                    fromUser = new Users().GetUser(emailRequest.FromUserId);

                //Get the consultant/user object
                if (emailRequest.ConsultantId > 0 || (fromUser != null && fromUser.UserType.UserTypeId == 1))
                {
                    consultant = emailRequest.ConsultantId > 0 ? new Users().GetUser(emailRequest.ConsultantId) : fromUser;
                }

                emailRequest.RawBody = Utils.GetFullEmailReplacingMainTemplateTags(emailRequest.FromUserId, emailRequest.RawBody, true, true, false);
                var toAddressesToAppend = "This email has been sent to following contacts, <br />";

                // Get contacts for the filters or sent ids)
                var contactsAndClients = new ClientsAndContacts().GetClientContactsForEmailAndSms(emailRequest.SelectedContactIds, emailRequest.SelectedClientIds);


                //iterate through the contacts and send the sms
                foreach (var obj in contactsAndClients)
                {
                    var contact = obj.ClientOrContact as Candidate;
                    var client = obj.ClientOrContact as Client;
                    EmailSmsError errorContact = null;
                    var email = new Email();

                    // consultant id (if consultant id passed the use that else check whether the from user is a consultant and use that for tags )
                    var consultantId = emailRequest.ConsultantId > 0 ? emailRequest.ConsultantId : ((fromUser != null && fromUser.UserType.UserTypeId == 1) ? fromUser.UserId : 0);

                    // check for the contact email
                    if (contact != null && contact.DoNotEmail)
                    {
                        errorContact = new EmailSmsError { ContactId = contact != null ? contact.CandidateId : (client != null ? client.ClientId : -1), Error = "Contact prefence is not to send emails." };
                        response.ErrorContacts.Add(errorContact);
                    }
                    else if ((contact != null && !string.IsNullOrEmpty(contact.Email)) || (client != null && !string.IsNullOrEmpty(client.Email)))
                    {
                        var intRequestId = 0;
                        // set the interview request
                        if (emailRequest.InterviewRequestId > 0)
                        {
                            intRequestId = emailRequest.InterviewRequestId;
                        }
                        else if (emailRequest.InterviewId > 0)
                        {
                            // interview request email
                            var request = new InterviewRequest
                            {
                                InterviewId = emailRequest.InterviewId,
                                ContactId = contact.CandidateId
                            };
                            intRequestId = new Interviews().AddInterviewRequest(request);
                        }

                        // get the original message 
                        emailRequest.Body = new Utils().GetOriginalBody(emailRequest.RawBody, (contact != null ? contact.CandidateId : -1),
                            emailRequest.JobId, emailRequest.SentBy, consultantId, (client != null ? client.ClientId : -1), contact, job, sendUser, consultant, client, intRequestId, emailRequest.TimeSlotId);

                        //set the to address
                        email.ToAddress = new Recipient
                        {
                            MailAddress = contact != null ? contact.Email : (client != null ? client.Email : ""),
                            DisplayName = contact != null ? (contact.Forename + " " + contact.Surname) : (client != null ? client.ClientName : "")
                        };


                        if (contact != null)
                            toAddressesToAppend += ("&nbsp;&nbsp; - ") + ("\t" + contact.Forename + " " + contact.Surname);
                        else if (client != null)
                            toAddressesToAppend += ("&nbsp;&nbsp; - ") + ("\t" + client.ClientName);
                        toAddressesToAppend += "<br />";

                        // set the from email address with display name
                        if (fromUser != null)
                        {
                            email.FromAddress = new Recipient
                            {
                                MailAddress = fromUser.Email,
                                DisplayName = fromUser.Forename + " " + fromUser.Surname
                            };
                        }
                        else
                        {
                            email.FromAddress = new Recipient
                            {
                                MailAddress = "*****@*****.**",
                                DisplayName = "Resonate Search and Selection"
                            };
                        }

                        email.Subject = emailRequest.Subject;
                        email.Body = emailRequest.Body;
                        email.Attachments = emailRequest.Attachments;
                        // set the guid of not passed
                        email.Guid = string.IsNullOrEmpty(emailRequest.Guid) ? Guid.NewGuid().ToString() : emailRequest.Guid;
                        // send Email
                        try
                        {
                            SendEmail(email);
                        }
                        catch (Exception ex)
                        {
                            //error sending the sms 
                            errorContact = new EmailSmsError { ContactId = contact != null ? contact.CandidateId : (client != null ? client.ClientId : -1), Error = ex.ToString() };
                            response.ErrorContacts.Add(errorContact);
                        }
                    }
                    else
                    {
                        errorContact = new EmailSmsError { ContactId = contact != null ? contact.CandidateId : (client != null ? client.ClientId : -1), Error = "Email Address not found for the " + contact != null ? "contact" : "client" + "!" };
                        response.ErrorContacts.Add(errorContact);
                    }

                    // On success save Email into the database
                    var context = new dbDataContext();
                    var objEmail = new tbl_Email
                    {
                        Body = email.Body,
                        FromAddress = (email != null && email.FromAddress != null) ? email.FromAddress.MailAddress : "",
                        RefId = contact != null ? contact.CandidateId : (client != null ? client.ClientId : -1),
                        RefType = emailRequest.Type,
                        Subject = emailRequest.Subject,
                        SentBy = emailRequest.SentBy,
                        SentDate = DateTime.Now,
                        UserId = emailRequest.FromUserId,
                        Consultant = consultantId,
                        RawBody = emailRequest.RawBody,
                        HasAttachments = (emailRequest.Attachments != null && emailRequest.Attachments.Count > 0),
                        Error = errorContact != null ? errorContact.Error : "",
                        IsError = errorContact != null,
                        Guid = string.IsNullOrEmpty(email.Guid) ? Guid.NewGuid().ToString() : email.Guid,
                        ToAddress = (email != null && email.ToAddress != null) ? email.ToAddress.MailAddress : "",
                        EmailType = "OUT",
                        CcAddresses =
                            emailRequest.CcAddresses != null
                                ? (emailRequest.CcAddresses.Aggregate("", (current, to) => current + (to + ";")))
                                : ""
                    };
                    context.tbl_Emails.InsertOnSubmit(objEmail);
                    context.SubmitChanges();

                    if (errorContact == null)
                    {
                        // Add History 
                        new Histories().AddHistory(new History
                        {
                            RefId = contact != null ? contact.CandidateId : (client != null ? client.ClientId : -1),
                            RefType = contact != null ? "Contact" : "Client",
                            ClientUserId = emailRequest.FromUserId,
                            TypeId = 8,
                            SubRefType = "Email",
                            SubRefId = objEmail.EmailId
                        });
                    }

                    // save attachments into the email location
                    try
                    {
                        var sourcePath = HttpContext.Current.Server.MapPath("/system/temp/" + email.Guid + "/");
                        var destLocation =
                            HttpContext.Current.Server.MapPath("/system/email/" + objEmail.EmailId + "/");
                        if (!Directory.Exists(destLocation))
                            Directory.CreateDirectory(destLocation);
                        if (Directory.Exists(destLocation) && Directory.Exists(sourcePath))
                            Directory.GetFiles(sourcePath)
                                .ToList()
                                .ForEach(f => File.Copy(f, destLocation + "/" + Path.GetFileName(f)));
                    }
                    catch (Exception)
                    {
                        // ignored
                    }
                }
                //Send Copies
                if (emailRequest.CcAddresses != null)
                    SendEmailCopy(emailRequest, toAddressesToAppend, fromUser);
            }
            catch (Exception e)
            {
                response.IsSuccess = false;
                response.Error = e.ToString();
            }
            return response;

        }