| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512 | using Abp.Specifications;
using MimeKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Net.Mail;
using System.Text;
using UnivateProperties_API.Containers.Timeshare;
using UnivateProperties_API.Context;
using UnivateProperties_API.Helpers;
using UnivateProperties_API.Model.Communication;
using UnivateProperties_API.Model.ProcessFlow;
using UnivateProperties_API.Model.Timeshare;
using UnivateProperties_API.Model.Users;
namespace UnivateProperties_API.Repository.Communication
{
    public interface IMailRepository
    {
        void ContactUs(MailModel mm);
        void EnquireNow(MailModel mm);
        void AddRecipient(MailRecipient rec);
        List<MailRecipient> GetMailRecipients();
        MailRecipient GetMailRecipientById(int id);
        void UpdateMailRecipient(MailRecipient rec);
        void DeleteMailRecipient(int id);
        List<MailModel> GetEnquireNowLog();
        List<MailModel> GetContactUsLog();
    }
    public class MailRepository : IMailRepository
    {
        private readonly DataContext _dbContext;
        public MailRepository(DataContext db)
        {
            _dbContext = db;              
        }
        public MailRepository()
        {
        }        
        public void ContactUs(MailModel mm)
        {            
            var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "ContactUs").Where(y => y.IsDeleted == false).ToList();            
            string body = _dbContext.Templates.Where(x => x.Name == "ContactUs").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
            string error = "";
            body = body.Replace("[FULLNAME]", mm.Name);
            body = body.Replace("[USEREMAIL]", mm.Email);
            body = body.Replace("[USERCELLPHONE]", mm.Phone);
            body = body.Replace("[PROPERTYREF]", mm.Property);
            body = body.Replace("[USERMESSAGE]", mm.Message);
            string toList = "";
            int emailCount = 0;
            foreach (var recipient in recipients)
            {
                toList += recipient.RecipientMail + ", ";
                emailCount++;
            }
            if (toList.Length > 0)
            {
                if (emailCount == 1)
                    toList = toList.Substring(0, toList.Length - 2);
                else
                    toList = toList.Substring(0, toList.Length - 2);
            }
            try
            {
                var host = _dbContext.Hosts.FirstOrDefault();
                using (SmtpClient smtp = new SmtpClient(host.Host))
                {
                    MailMessage mail = new MailMessage();
                    mail.To.Add(toList);
                    mail.Subject = "Uni-Vate - New Contact Request";
                    mail.Body = body;
                    mail.IsBodyHtml = true;
                    mail.BodyEncoding = Encoding.ASCII;
                    mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                    mail.Sender = new MailAddress(host.User, "UniVate Properties");
                    mail.From = new MailAddress(mm.Email, mm.Name);
                    smtp.UseDefaultCredentials = host.NeedsAuthorize;
                    smtp.Credentials = host.GetNetworkCredential();
                    smtp.EnableSsl = host.UseSSL;
                    smtp.Send(mail);
                }
                error = "None";
            }
            catch (Exception ex)
            {
                error = ex.ToString();
            }
            mm.ComType = "Contact Us";
            mm.Error = error;
            _dbContext.CommunicationLog.Add(mm);
            _dbContext.SaveChanges();
        }
        public void EnquireNow(MailModel mm)
        {            
            var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "EnquireNow").Where(y => y.IsDeleted == false).ToList();
            string body = _dbContext.Templates.Where(x => x.Name == "EnquireNow").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
            var property = _dbContext.Properties.Where(x => x.Id == Convert.ToInt32(mm.Property)).FirstOrDefault();
            string error = "";
            body = body.Replace("[FULLNAME]", " " + mm.Name);
            body = body.Replace("[USEREMAIL]", " " + mm.Email);
            body = body.Replace("[USERCELLPHONE]", " " + mm.Phone);
            body = body.Replace("[PROPERTYID]", " " + mm.Property);
            body = body.Replace("[PROPERTYNAME]", property.PropertyName);
            body = body.Replace("[PROPERTYREF]", property.PropertyRef);
            body = body.Replace("[PROPERTYPRICE]", property.Price.ToString());
            body = body.Replace("[USERMESSAGE]", " " + mm.Message);
            body = body.Replace("[USERHEARDFROM]", " " + mm.HeardFrom);
            string toList = "";
            int emailCount = 0;
            foreach (var recipient in recipients)
            {
                toList += recipient.RecipientMail + ", ";
                emailCount++;
            }
            if (toList.Length > 0)
            {
                if (emailCount == 1)
                    toList = toList.Substring(0, toList.Length - 2);
                else
                    toList = toList.Substring(0, toList.Length - 2);
            }
            try
            {
                var host = _dbContext.Hosts.FirstOrDefault();
                using (SmtpClient smtp = new SmtpClient(host.Host))
                {
                    MailMessage mail = new MailMessage();
                    mail.To.Add(toList);
                    mail.Subject = "Uni-Vate - Enquiry to view property";
                    mail.Body = body;
                    mail.IsBodyHtml = true;
                    mail.BodyEncoding = Encoding.ASCII;
                    mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                    mail.Sender = new MailAddress(host.User, "UniVate Properties");
                    mail.From = new MailAddress(mm.Email, mm.Name);
                    smtp.UseDefaultCredentials = host.NeedsAuthorize;
                    smtp.Credentials = host.GetNetworkCredential();
                    smtp.EnableSsl = host.UseSSL;
                    smtp.Send(mail);
                }
                error = "None";
            }
            catch (Exception ex)
            {
                error = ex.ToString();
            }
            mm.ComType = "Enquire Now";
            mm.Error = error;
            _dbContext.CommunicationLog.Add(mm);
            _dbContext.SaveChanges();
        }
        public void ForgotPassword(Individual toPerson, string link)
        {
            string body = "<div style=\"margin: 5px\">" + 
                           "<h4>Dear " + toPerson.FullName + "</h4>" +
                           "<h4>There has been a request to reset your password. If this is incorrect please send an email to info@univateproperties.co.za</h4>" +
                           "<h4>Once your password has been reset you can use the username "+ toPerson.User.Username + " and the new password to log in.</h4>" +
                           "<div>" +
                           "<h4><a href='" + link + "'>Click here</a> to reset your password.</h4>" +
                           "<br />"+
                           "<h4>Thank You</h4>" +
                           "<h4>Team Uni-Vate</h4>" +
                           "</div>" +
                           "</div>";
            var host = _dbContext.Hosts.FirstOrDefault();
            using (SmtpClient smtp = new SmtpClient(host.Host))
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(new MailAddress(toPerson.Email, toPerson.FullName));
                mail.Subject = "Uni-Vate - Password Reset Request";
                mail.Body = body;
                mail.IsBodyHtml = true;
                mail.BodyEncoding = Encoding.ASCII;
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                mail.Sender = new MailAddress(host.User, "UniVate Properties");
                mail.From = mail.Sender;
                smtp.UseDefaultCredentials = host.NeedsAuthorize;
                smtp.Credentials = host.GetNetworkCredential();
                smtp.EnableSsl = host.UseSSL;
                smtp.Send(mail);
            }
        }
        public void WeekOfferMadeOwner(TimeshareWeek week, BidItem bid)
        {
            var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "WeekOfferMade-Owner").Where(y => y.IsDeleted == false).ToList();
            string body = _dbContext.Templates.Where(x => x.Name == "WeekOfferMade-Owner").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
            
            body = body.Replace("[OWNER]", week.DisplayOwner);
            body = body.Replace("[RESORTNAME]", week.ResortName);
            body = body.Replace("[UNITNUMBER]", week.UnitNumber);
            body = body.Replace("[MODULE]", week.Module);
            body = body.Replace("[OFFERMADE]", bid.Amount.ToString());
            body = body.Replace("[FULLNAME]", bid.BidMaker.Display);
            body = body.Replace("[USEREMAIL]", bid.BidMaker.Email);
            body = body.Replace("[USERCELLPHONE]", bid.BidMaker.CellNumber);
            body = body.Replace("[USERTELEPHONE]", bid.BidMaker.Telephone);
            body = body.Replace("[USERCOMMENT]", bid.Comment);
            string toList = "";
            int emailCount = 0;
            foreach (var recipient in recipients)
            {
                toList += recipient.RecipientMail + ", ";
                emailCount++;
            }
            if (toList.Length > 0)
            {
                if (emailCount == 1)
                    toList = toList.Substring(0, toList.Length - 2);
                else
                    toList = toList.Substring(0, toList.Length - 2);
            }
            var host = _dbContext.Hosts.FirstOrDefault();
            using (SmtpClient smtp = new SmtpClient(host.Host))
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(toList);
                mail.Subject = "Uni-Vate - New Contact Request";
                mail.Body = body;
                mail.IsBodyHtml = true;
                mail.BodyEncoding = Encoding.ASCII;
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                mail.Sender = new MailAddress(host.User, "UniVate Properties");
                mail.From = mail.Sender;
                smtp.UseDefaultCredentials = host.NeedsAuthorize;
                smtp.Credentials = host.GetNetworkCredential();
                smtp.EnableSsl = host.UseSSL;
                smtp.Send(mail);
            }
        }
        public void WeekOfferMadeUser(TimeshareWeek week, BidItem bid)
        {
            //var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "WeekOfferMade-User").Where(y => y.IsDeleted == false).ToList();
            string body = _dbContext.Templates.Where(x => x.Name == "WeekOfferMade-User").Where(y => y.IsDeleted == false).FirstOrDefault().Body; ;
            body = body.Replace("[OWNER]", week.DisplayOwner);
            body = body.Replace("[RESORTNAME]", week.ResortName);
            body = body.Replace("[UNITNUMBER]", week.UnitNumber);
            body = body.Replace("[MODULE]", week.Module);
            body = body.Replace("[OFFERMADE]", bid.Amount.ToString());
            body = body.Replace("[FULLNAME]", bid.BidMaker.Display);
            body = body.Replace("[USEREMAIL]", bid.BidMaker.Email);
            body = body.Replace("[USERCELLPHONE]", bid.BidMaker.CellNumber);
            body = body.Replace("[USERTELEPHONE]", bid.BidMaker.Telephone);
            body = body.Replace("[USERCOMMENT]", bid.Comment);
            var host = _dbContext.Hosts.FirstOrDefault();
            using (SmtpClient smtp = new SmtpClient(host.Host))
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(bid.BidMaker.Email);
                mail.Subject = "Uni-Vate - New Contact Request";
                mail.Body = body;
                mail.IsBodyHtml = true;
                mail.BodyEncoding = Encoding.ASCII;
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                mail.Sender = new MailAddress(host.User, "UniVate Properties");
                mail.From = mail.Sender;
                smtp.UseDefaultCredentials = host.NeedsAuthorize;
                smtp.Credentials = host.GetNetworkCredential();
                smtp.EnableSsl = host.UseSSL;
                smtp.Send(mail);
            }
        }
        public void WeekOfferMadeAdmin()
        {
            
        }
        public void WeekLoadedAgent(TimeshareWeekDto week)
        {
            string body = _dbContext.Templates.Where(x => x.Name == "WeekLoaded-Agent").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
            var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "WeekLoaded-Agent").Where(y => y.IsDeleted == false).ToList();
            #region ResortFields
            body = body.Replace("[RESORTNAME]", " " + week.ResortName);
            body = body.Replace("[UNITNUMBER]", " " + week.UnitNumber);
            body = body.Replace("[MODULE]", " " + week.Module);
            body = body.Replace("[RESORTPRICE]", " " + week.SellPrice.ToString());
            body = body.Replace("[RESORTSEASON]", " " + week.Season);
            body = body.Replace("[RESORTREGION]", " " + week.Region.Display);
            body = body.Replace("[RESORTLEVY]", " " + week.LevyAmount.ToString());
            body = body.Replace("[OWNER]", " " + week.DisplayOwner);
            body = body.Replace("[SLEEPMAX]", " " + week.MaxSleep.ToString());
            body = body.Replace("[WEEK]", " " + week.WeekNumber);
            body = body.Replace("[BEDROOMS]", " " + week.Bedrooms);
            body = body.Replace("[ASKINGPRICE]", " " + week.AskingPrice.ToString());
            body = body.Replace("[ARRIVALDATE]", " " + week.ArrivalDate.ToString());
            body = body.Replace("[DEPARTUREDATE]", " " + week.DepartureDate.ToString());
            body = body.Replace("[AGENTREFER]", week.ReferedByAgent ? " Yes" : " No");
            body = body.Replace("[ALLLEVY]", week.LeviesPaidInFull ? " Yes" : " No");
            body = body.Replace("[WEEKPLACEDFORRENTAL]", week.WeekPlacedForRental ? " Yes" : " No");
            body = body.Replace("[ORIGINALPURCHASEDATE]", " " + week.OriginalPurchaseDate.ToString());
            body = body.Replace("[CUROCCUPATIONDATES]", " " + week.ArrivalDate.ToString() + " - " + week.DepartureDate.ToString());
            body = body.Replace("[ORIGINALPURCHASEPRICE]", " " + week.OriginalPurchasePrice.ToString());
            body = body.Replace("[AGENTCOMM]", " " + week.AgentCommision.ToString());
            #endregion
            #region UserFields
            body = body.Replace("[FULLNAME]", " " + week.DisplayOwner);
            body = body.Replace("[USEREMAIL]", " " + week.OwnerObject.EmailAddress);
            body = body.Replace("[USERCELLPHONE]", " " + week.OwnerObject.CellNumber);
            body = body.Replace("[USERTELEPHONE]", " " + week.OwnerObject.LandlineNumber);
            #endregion
            string toList = "";
            int emailCount = 0;
            foreach (var recipient in recipients)
            {
                toList += recipient.RecipientMail + ", ";
                emailCount++;
            }
            if (toList.Length > 0)
            {
                if (emailCount == 1)
                    toList = toList.Substring(0, toList.Length - 2);
                else
                    toList = toList.Substring(0, toList.Length - 2);
            }
            var host = _dbContext.Hosts.FirstOrDefault();
            using (SmtpClient smtp = new SmtpClient(host.Host))
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(toList);
                mail.Subject = "Uni-Vate - Week Loaded by Agent";
                mail.Body = body;
                mail.IsBodyHtml = true;
                mail.BodyEncoding = Encoding.ASCII;
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                mail.Sender = new MailAddress(host.User, "UniVate Properties");
                mail.From = mail.Sender;
                smtp.UseDefaultCredentials = host.NeedsAuthorize;
                smtp.Credentials = host.GetNetworkCredential();
                smtp.EnableSsl = host.UseSSL;
                smtp.Send(mail);
            }
        }
        public void WeekLoadedOwner(TimeshareWeekDto week)
        {
            string body = _dbContext.Templates.Where(x => x.Name == "WeekLoaded-Owner").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
            var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "WeekLoaded-Owner").Where(y => y.IsDeleted == false).ToList();
            #region ResortFields
            body = body.Replace("[RESORTNAME]", " " + week.ResortName);
            body = body.Replace("[UNITNUMBER]", " " + week.UnitNumber);
            body = body.Replace("[MODULE]", " " + week.Module);
            body = body.Replace("[RESORTPRICE]", " " + week.SellPrice.ToString());
            body = body.Replace("[RESORTSEASON]", " " + week.Season);
            body = body.Replace("[RESORTREGION]", " " + week.Region.Display);
            body = body.Replace("[RESORTLEVY]", " " + week.LevyAmount.ToString());           
            body = body.Replace("[OWNER]", " " + week.DisplayOwner);
            body = body.Replace("[SLEEPMAX]", " " + week.MaxSleep.ToString());
            body = body.Replace("[WEEK]", " " + week.WeekNumber);
            body = body.Replace("[BEDROOMS]", " " + week.Bedrooms);
            body = body.Replace("[ASKINGPRICE]", " " + week.AskingPrice.ToString());
            body = body.Replace("[ARRIVALDATE]", " " + week.ArrivalDate.ToString());
            body = body.Replace("[DEPARTUREDATE]", " " + week.DepartureDate.ToString());
            body = body.Replace("[AGENTREFER]", week.ReferedByAgent ? " Yes" : " No");
            body = body.Replace("[ALLLEVY]", week.LeviesPaidInFull ? " Yes" : " No");
            body = body.Replace("[WEEKPLACEDFORRENTAL]", week.WeekPlacedForRental ? " Yes" : " No");
            body = body.Replace("[ORIGINALPURCHASEDATE]", " " + week.OriginalPurchaseDate.ToString());
            body = body.Replace("[CUROCCUPATIONDATES]", " " + week.ArrivalDate.ToString() + " - " + week.DepartureDate.ToString());
            body = body.Replace("[ORIGINALPURCHASEPRICE]", " " + week.OriginalPurchasePrice.ToString());
            body = body.Replace("[AGENTCOMM]", " " + week.AgentCommision.ToString());
            #endregion
            #region UserFields
            body = body.Replace("[FULLNAME]", " " + week.DisplayOwner );
            body = body.Replace("[USEREMAIL]", " " + week.OwnerObject.EmailAddress);
            body = body.Replace("[USERCELLPHONE]", " " + week.OwnerObject.CellNumber);
            body = body.Replace("[USERTELEPHONE]", " " + week.OwnerObject.LandlineNumber);
            #endregion
            string toList = "";
            int emailCount = 0;
            foreach (var recipient in recipients)
            {
                toList += recipient.RecipientMail + ", ";
                emailCount++;
            }
            if (toList.Length > 0)
            {
                if (emailCount == 1)
                    toList = toList.Substring(0, toList.Length - 2);
                else
                    toList = toList.Substring(0, toList.Length - 2);
            }
            var host = _dbContext.Hosts.FirstOrDefault();
            using (SmtpClient smtp = new SmtpClient(host.Host))
            {
                MailMessage mail = new MailMessage();
                mail.To.Add(toList);
                mail.Subject = "Uni-Vate - Week Loaded";
                mail.Body = body;
                mail.IsBodyHtml = true;
                mail.BodyEncoding = Encoding.ASCII;
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                mail.Sender = new MailAddress(host.User, "UniVate Properties");
                mail.From = mail.Sender;
                smtp.UseDefaultCredentials = host.NeedsAuthorize;
                smtp.Credentials = host.GetNetworkCredential();
                smtp.EnableSsl = host.UseSSL;
                smtp.Send(mail);
            }
        }
        public void AddRecipient(MailRecipient rec)
        {
            if (MyCommon.IsValidEmail(rec.RecipientMail))
            {
                _dbContext.MailRecipients.Add(rec);
                _dbContext.SaveChanges();
            }
            else
            {
                throw new Exception();
            }
            
        }
        public List<MailRecipient> GetMailRecipients()
        {
            return _dbContext.MailRecipients.Where(x => x.IsDeleted == false).ToList();
        }
        public MailRecipient GetMailRecipientById(int id)
        {
            return _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault(); 
        }
        public void UpdateMailRecipient(MailRecipient rec)
        {
            var recipient = _dbContext.MailRecipients.Where(x => x.Id == rec.Id).FirstOrDefault();
            if (recipient.RecipientMail != rec.RecipientMail)
            {
                recipient.RecipientMail = rec.RecipientMail;
            }
            if (recipient.RecipientName != rec.RecipientName)
            {
                recipient.RecipientName = rec.RecipientName;
            }
            if (recipient.RecipientUsage != rec.RecipientUsage)
            {
                recipient.RecipientUsage = rec.RecipientUsage;
            }
            _dbContext.MailRecipients.Update(recipient);
            _dbContext.SaveChanges();
        }
        public void DeleteMailRecipient(int id)
        {
            var rec = _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();        
            _dbContext.MailRecipients.Remove(rec);
            _dbContext.SaveChanges();
        }
        public List<MailModel> GetContactUsLog()
        {
            return _dbContext.CommunicationLog.Where(x => x.ComType == "Contact Us").ToList();            
        }
        public List<MailModel> GetEnquireNowLog()
        {
            return _dbContext.CommunicationLog.Where(x => x.ComType == "Enquire Now").ToList();
            
        }
    }
}
 |