API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MailRepository.cs 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. using Abp.Specifications;
  2. using MimeKit;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Linq.Dynamic.Core;
  7. using System.Net.Mail;
  8. using System.Text;
  9. using UnivateProperties_API.Containers.Timeshare;
  10. using UnivateProperties_API.Context;
  11. using UnivateProperties_API.Helpers;
  12. using UnivateProperties_API.Model.Communication;
  13. using UnivateProperties_API.Model.ProcessFlow;
  14. using UnivateProperties_API.Model.Timeshare;
  15. using UnivateProperties_API.Model.Users;
  16. namespace UnivateProperties_API.Repository.Communication
  17. {
  18. public interface IMailRepository
  19. {
  20. void ContactUs(MailModel mm);
  21. void EnquireNow(MailModel mm);
  22. void AddRecipient(MailRecipient rec);
  23. List<MailRecipient> GetMailRecipients();
  24. MailRecipient GetMailRecipientById(int id);
  25. void UpdateMailRecipient(MailRecipient rec);
  26. void DeleteMailRecipient(int id);
  27. }
  28. public class MailRepository : IMailRepository
  29. {
  30. private readonly DataContext _dbContext;
  31. public MailRepository(DataContext db)
  32. {
  33. _dbContext = db;
  34. }
  35. public MailRepository()
  36. {
  37. }
  38. public void ContactUs(MailModel mm)
  39. {
  40. var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "ContactUs").Where(y => y.IsDeleted == false).ToList();
  41. string body = _dbContext.Templates.Where(x => x.Name == "ContactUs").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
  42. body = body.Replace("[FULLNAME]", mm.Name);
  43. body = body.Replace("[USEREMAIL]", mm.Email);
  44. body = body.Replace("[USERCELLPHONE]", mm.Phone);
  45. body = body.Replace("[PROPERTYREF]", mm.Property);
  46. body = body.Replace("[USERMESSAGE]", mm.Message);
  47. string toList = "";
  48. int emailCount = 0;
  49. foreach (var recipient in recipients)
  50. {
  51. toList += recipient.RecipientMail + ", ";
  52. emailCount++;
  53. }
  54. if (toList.Length > 0)
  55. {
  56. if (emailCount == 1)
  57. toList = toList.Substring(0, toList.Length - 2);
  58. else
  59. toList = toList.Substring(0, toList.Length - 2);
  60. }
  61. var host = _dbContext.Hosts.FirstOrDefault();
  62. using (SmtpClient smtp = new SmtpClient(host.Host))
  63. {
  64. MailMessage mail = new MailMessage();
  65. mail.To.Add(toList);
  66. mail.Subject = "Uni-Vate - New Contact Request";
  67. mail.Body = body;
  68. mail.IsBodyHtml = true;
  69. mail.BodyEncoding = Encoding.ASCII;
  70. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  71. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  72. mail.From = new MailAddress(mm.Email, mm.Name);
  73. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  74. smtp.Credentials = host.GetNetworkCredential();
  75. smtp.EnableSsl = host.UseSSL;
  76. smtp.Send(mail);
  77. }
  78. }
  79. public void EnquireNow(MailModel mm)
  80. {
  81. var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "EnquireNow").Where(y => y.IsDeleted == false).ToList();
  82. string body = _dbContext.Templates.Where(x => x.Name == "EnquireNow").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
  83. var property = _dbContext.Properties.Where(x => x.Id == Convert.ToInt32(mm.Property)).FirstOrDefault();
  84. body = body.Replace("[FULLNAME]", mm.Name);
  85. body = body.Replace("[USEREMAIL]", mm.Email);
  86. body = body.Replace("[USERCELLPHONE]", mm.Phone);
  87. body = body.Replace("[PROPERTYID]", mm.Property);
  88. body = body.Replace("[PROPERTYNAME]", property.PropertyName);
  89. body = body.Replace("[PROPERTYREF]", property.PropertyRef);
  90. body = body.Replace("[PROPERTYPRICE]", property.Price.ToString());
  91. body = body.Replace("[USERMESSAGE]", mm.Message);
  92. string toList = "";
  93. int emailCount = 0;
  94. foreach (var recipient in recipients)
  95. {
  96. toList += recipient.RecipientMail + ", ";
  97. emailCount++;
  98. }
  99. if (toList.Length > 0)
  100. {
  101. if (emailCount == 1)
  102. toList = toList.Substring(0, toList.Length - 2);
  103. else
  104. toList = toList.Substring(0, toList.Length - 2);
  105. }
  106. var host = _dbContext.Hosts.FirstOrDefault();
  107. using (SmtpClient smtp = new SmtpClient(host.Host))
  108. {
  109. MailMessage mail = new MailMessage();
  110. mail.To.Add(toList);
  111. mail.Subject = "Uni-Vate - Enquiry to view property";
  112. mail.Body = body;
  113. mail.IsBodyHtml = true;
  114. mail.BodyEncoding = Encoding.ASCII;
  115. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  116. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  117. mail.From = new MailAddress(mm.Email, mm.Name);
  118. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  119. smtp.Credentials = host.GetNetworkCredential();
  120. smtp.EnableSsl = host.UseSSL;
  121. smtp.Send(mail);
  122. }
  123. }
  124. public void ForgotPassword(Individual toPerson, string link)
  125. {
  126. string body = "<div style=\"margin: 5px\">" +
  127. "<h4>Dear " + toPerson.FullName + "</h4>" +
  128. "<h4>There has been a request to reset your password. If this is incorrect please send an email to info@univateproperties.co.za</h4>" +
  129. "<h4>Once your password has been reset you can use the username "+ toPerson.User.Username + " and the new password to log in.</h4>" +
  130. "<div>" +
  131. "<h4><a href='" + link + "'>Click here</a> to reset your password.</h4>" +
  132. "<br />"+
  133. "<h4>Thank You</h4>" +
  134. "<h4>Team Uni-Vate</h4>" +
  135. "</div>" +
  136. "</div>";
  137. var host = _dbContext.Hosts.FirstOrDefault();
  138. using (SmtpClient smtp = new SmtpClient(host.Host))
  139. {
  140. MailMessage mail = new MailMessage();
  141. mail.To.Add(new MailAddress(toPerson.Email, toPerson.FullName));
  142. mail.Subject = "Uni-Vate - Password Reset Request";
  143. mail.Body = body;
  144. mail.IsBodyHtml = true;
  145. mail.BodyEncoding = Encoding.ASCII;
  146. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  147. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  148. mail.From = mail.Sender;
  149. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  150. smtp.Credentials = host.GetNetworkCredential();
  151. smtp.EnableSsl = host.UseSSL;
  152. smtp.Send(mail);
  153. }
  154. }
  155. public void WeekOfferMadeOwner(TimeshareWeek week, BidItem bid)
  156. {
  157. var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "WeekOfferMade-Owner").Where(y => y.IsDeleted == false).ToList();
  158. string body = _dbContext.Templates.Where(x => x.Name == "WeekOfferMade-Owner").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
  159. body = body.Replace("[OWNER]", week.DisplayOwner);
  160. body = body.Replace("[RESORTNAME]", week.ResortName);
  161. body = body.Replace("[UNITNUMBER]", week.UnitNumber);
  162. body = body.Replace("[MODULE]", week.Module);
  163. body = body.Replace("[OFFERMADE]", bid.Amount.ToString());
  164. body = body.Replace("[FULLNAME]", bid.BidMaker.Display);
  165. body = body.Replace("[USEREMAIL]", bid.BidMaker.Email);
  166. body = body.Replace("[USERCELLPHONE]", bid.BidMaker.CellNumber);
  167. body = body.Replace("[USERTELEPHONE]", bid.BidMaker.Telephone);
  168. body = body.Replace("[USERCOMMENT]", bid.Comment);
  169. string toList = "";
  170. int emailCount = 0;
  171. foreach (var recipient in recipients)
  172. {
  173. toList += recipient.RecipientMail + ", ";
  174. emailCount++;
  175. }
  176. if (toList.Length > 0)
  177. {
  178. if (emailCount == 1)
  179. toList = toList.Substring(0, toList.Length - 2);
  180. else
  181. toList = toList.Substring(0, toList.Length - 2);
  182. }
  183. var host = _dbContext.Hosts.FirstOrDefault();
  184. using (SmtpClient smtp = new SmtpClient(host.Host))
  185. {
  186. MailMessage mail = new MailMessage();
  187. mail.To.Add(toList);
  188. mail.Subject = "Uni-Vate - New Contact Request";
  189. mail.Body = body;
  190. mail.IsBodyHtml = true;
  191. mail.BodyEncoding = Encoding.ASCII;
  192. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  193. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  194. mail.From = mail.Sender;
  195. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  196. smtp.Credentials = host.GetNetworkCredential();
  197. smtp.EnableSsl = host.UseSSL;
  198. smtp.Send(mail);
  199. }
  200. }
  201. public void WeekOfferMadeUser(TimeshareWeek week, BidItem bid)
  202. {
  203. //var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "WeekOfferMade-User").Where(y => y.IsDeleted == false).ToList();
  204. string body = _dbContext.Templates.Where(x => x.Name == "WeekOfferMade-User").Where(y => y.IsDeleted == false).FirstOrDefault().Body; ;
  205. body = body.Replace("[OWNER]", week.DisplayOwner);
  206. body = body.Replace("[RESORTNAME]", week.ResortName);
  207. body = body.Replace("[UNITNUMBER]", week.UnitNumber);
  208. body = body.Replace("[MODULE]", week.Module);
  209. body = body.Replace("[OFFERMADE]", bid.Amount.ToString());
  210. body = body.Replace("[FULLNAME]", bid.BidMaker.Display);
  211. body = body.Replace("[USEREMAIL]", bid.BidMaker.Email);
  212. body = body.Replace("[USERCELLPHONE]", bid.BidMaker.CellNumber);
  213. body = body.Replace("[USERTELEPHONE]", bid.BidMaker.Telephone);
  214. body = body.Replace("[USERCOMMENT]", bid.Comment);
  215. var host = _dbContext.Hosts.FirstOrDefault();
  216. using (SmtpClient smtp = new SmtpClient(host.Host))
  217. {
  218. MailMessage mail = new MailMessage();
  219. mail.To.Add(bid.BidMaker.Email);
  220. mail.Subject = "Uni-Vate - New Contact Request";
  221. mail.Body = body;
  222. mail.IsBodyHtml = true;
  223. mail.BodyEncoding = Encoding.ASCII;
  224. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  225. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  226. mail.From = mail.Sender;
  227. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  228. smtp.Credentials = host.GetNetworkCredential();
  229. smtp.EnableSsl = host.UseSSL;
  230. smtp.Send(mail);
  231. }
  232. }
  233. public void WeekOfferMadeAdmin()
  234. {
  235. }
  236. public void WeekLoadedAgent(TimeshareWeek week)
  237. {
  238. string body = _dbContext.Templates.Where(x => x.Name == "WeekLoaded-Agent").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
  239. var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "WeekLoaded-Agent").Where(y => y.IsDeleted == false).ToList();
  240. body = body.Replace("[FULLNAME]", week.DisplayOwner);
  241. body = body.Replace("[RESORTNAME]", week.ResortName);
  242. body = body.Replace("[UNITNUMBER]", week.UnitNumber);
  243. body = body.Replace("[MODULE]", week.Module);
  244. string toList = "";
  245. int emailCount = 0;
  246. foreach (var recipient in recipients)
  247. {
  248. toList += recipient.RecipientMail + ", ";
  249. emailCount++;
  250. }
  251. if (toList.Length > 0)
  252. {
  253. if (emailCount == 1)
  254. toList = toList.Substring(0, toList.Length - 2);
  255. else
  256. toList = toList.Substring(0, toList.Length - 2);
  257. }
  258. var host = _dbContext.Hosts.FirstOrDefault();
  259. using (SmtpClient smtp = new SmtpClient(host.Host))
  260. {
  261. MailMessage mail = new MailMessage();
  262. mail.To.Add(toList);
  263. mail.Subject = "Uni-Vate - Week Loaded by Agent";
  264. mail.Body = body;
  265. mail.IsBodyHtml = true;
  266. mail.BodyEncoding = Encoding.ASCII;
  267. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  268. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  269. mail.From = mail.Sender;
  270. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  271. smtp.Credentials = host.GetNetworkCredential();
  272. smtp.EnableSsl = host.UseSSL;
  273. smtp.Send(mail);
  274. }
  275. }
  276. public void WeekLoadedOwner(TimeshareWeekDto week)
  277. {
  278. string body = _dbContext.Templates.Where(x => x.Name == "WeekLoaded-Owner").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
  279. var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "WeekLoaded-Owner").Where(y => y.IsDeleted == false).ToList();
  280. #region ResortFields
  281. body = body.Replace("[RESORTNAME]", " " + week.ResortName);
  282. body = body.Replace("[UNITNUMBER]", " " + week.UnitNumber);
  283. body = body.Replace("[MODULE]", " " + week.Module);
  284. body = body.Replace("[RESORTPRICE]", " " + week.SellPrice.ToString());
  285. body = body.Replace("[RESORTSEASON]", " " + week.Season);
  286. body = body.Replace("[RESORTREGION]", " " + week.Region.Display);
  287. body = body.Replace("[RESORTLEVY]", " " + week.LevyAmount.ToString());
  288. body = body.Replace("[OWNER]", " " + week.DisplayOwner);
  289. body = body.Replace("[SLEEPMAX]", " " + week.MaxSleep.ToString());
  290. body = body.Replace("[WEEK]", " " + week.WeekNumber);
  291. body = body.Replace("[BEDROOMS]", " " + week.Bedrooms);
  292. body = body.Replace("[ASKINGPRICE]", " " + week.AskingPrice.ToString());
  293. body = body.Replace("[ARRIVALDATE]", " " + week.ArrivalDate.ToString());
  294. body = body.Replace("[DEPARTUREDATE]", " " + week.DepartureDate.ToString());
  295. body = body.Replace("[AGENTREFER]", week.ReferedByAgent ? " Yes" : " No");
  296. body = body.Replace("[ALLLEVY]", week.LeviesPaidInFull ? " Yes" : " No");
  297. body = body.Replace("[WEEKPLACEDFORRENTAL]", week.WeekPlacedForRental ? " Yes" : " No");
  298. body = body.Replace("[ORIGINALPURCHASEDATE]", " " + week.OriginalPurchaseDate.ToString());
  299. body = body.Replace("[CUROCCUPATIONDATES]", " " + week.ArrivalDate.ToString() + " - " + week.DepartureDate.ToString());
  300. body = body.Replace("[ORIGINALPURCHASEPRICE]", " " + week.OriginalPurchasePrice.ToString());
  301. body = body.Replace("[AGENTCOMM]", " " + week.AgentCommision.ToString());
  302. #endregion
  303. #region UserFields
  304. body = body.Replace("[FULLNAME]", " " + week.DisplayOwner );
  305. body = body.Replace("[USEREMAIL]", " " + week.OwnerObject.EmailAddress);
  306. body = body.Replace("[USERCELLPHONE]", " " + week.OwnerObject.CellNumber);
  307. body = body.Replace("[USERTELEPHONE]", " " + week.OwnerObject.LandlineNumber);
  308. #endregion
  309. string toList = "";
  310. int emailCount = 0;
  311. foreach (var recipient in recipients)
  312. {
  313. toList += recipient.RecipientMail + ", ";
  314. emailCount++;
  315. }
  316. if (toList.Length > 0)
  317. {
  318. if (emailCount == 1)
  319. toList = toList.Substring(0, toList.Length - 2);
  320. else
  321. toList = toList.Substring(0, toList.Length - 2);
  322. }
  323. var host = _dbContext.Hosts.FirstOrDefault();
  324. using (SmtpClient smtp = new SmtpClient(host.Host))
  325. {
  326. MailMessage mail = new MailMessage();
  327. mail.To.Add(toList);
  328. mail.Subject = "Uni-Vate - Week Loaded";
  329. mail.Body = body;
  330. mail.IsBodyHtml = true;
  331. mail.BodyEncoding = Encoding.ASCII;
  332. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  333. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  334. mail.From = mail.Sender;
  335. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  336. smtp.Credentials = host.GetNetworkCredential();
  337. smtp.EnableSsl = host.UseSSL;
  338. smtp.Send(mail);
  339. }
  340. }
  341. public void AddRecipient(MailRecipient rec)
  342. {
  343. if (MyCommon.IsValidEmail(rec.RecipientMail))
  344. {
  345. _dbContext.MailRecipients.Add(rec);
  346. _dbContext.SaveChanges();
  347. }
  348. else
  349. {
  350. throw new Exception();
  351. }
  352. }
  353. public List<MailRecipient> GetMailRecipients()
  354. {
  355. return _dbContext.MailRecipients.Where(x => x.IsDeleted == false).ToList();
  356. }
  357. public MailRecipient GetMailRecipientById(int id)
  358. {
  359. return _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();
  360. }
  361. public void UpdateMailRecipient(MailRecipient rec)
  362. {
  363. var recipient = _dbContext.MailRecipients.Where(x => x.Id == rec.Id).FirstOrDefault();
  364. if (recipient.RecipientMail != rec.RecipientMail)
  365. {
  366. recipient.RecipientMail = rec.RecipientMail;
  367. }
  368. if (recipient.RecipientName != rec.RecipientName)
  369. {
  370. recipient.RecipientName = rec.RecipientName;
  371. }
  372. if (recipient.RecipientUsage != rec.RecipientUsage)
  373. {
  374. recipient.RecipientUsage = rec.RecipientUsage;
  375. }
  376. _dbContext.MailRecipients.Update(recipient);
  377. _dbContext.SaveChanges();
  378. }
  379. public void DeleteMailRecipient(int id)
  380. {
  381. var rec = _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();
  382. _dbContext.MailRecipients.Remove(rec);
  383. _dbContext.SaveChanges();
  384. }
  385. }
  386. }