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 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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(TimeshareWeekDto 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. #region ResortFields
  241. body = body.Replace("[RESORTNAME]", " " + week.ResortName);
  242. body = body.Replace("[UNITNUMBER]", " " + week.UnitNumber);
  243. body = body.Replace("[MODULE]", " " + week.Module);
  244. body = body.Replace("[RESORTPRICE]", " " + week.SellPrice.ToString());
  245. body = body.Replace("[RESORTSEASON]", " " + week.Season);
  246. body = body.Replace("[RESORTREGION]", " " + week.Region.Display);
  247. body = body.Replace("[RESORTLEVY]", " " + week.LevyAmount.ToString());
  248. body = body.Replace("[OWNER]", " " + week.DisplayOwner);
  249. body = body.Replace("[SLEEPMAX]", " " + week.MaxSleep.ToString());
  250. body = body.Replace("[WEEK]", " " + week.WeekNumber);
  251. body = body.Replace("[BEDROOMS]", " " + week.Bedrooms);
  252. body = body.Replace("[ASKINGPRICE]", " " + week.AskingPrice.ToString());
  253. body = body.Replace("[ARRIVALDATE]", " " + week.ArrivalDate.ToString());
  254. body = body.Replace("[DEPARTUREDATE]", " " + week.DepartureDate.ToString());
  255. body = body.Replace("[AGENTREFER]", week.ReferedByAgent ? " Yes" : " No");
  256. body = body.Replace("[ALLLEVY]", week.LeviesPaidInFull ? " Yes" : " No");
  257. body = body.Replace("[WEEKPLACEDFORRENTAL]", week.WeekPlacedForRental ? " Yes" : " No");
  258. body = body.Replace("[ORIGINALPURCHASEDATE]", " " + week.OriginalPurchaseDate.ToString());
  259. body = body.Replace("[CUROCCUPATIONDATES]", " " + week.ArrivalDate.ToString() + " - " + week.DepartureDate.ToString());
  260. body = body.Replace("[ORIGINALPURCHASEPRICE]", " " + week.OriginalPurchasePrice.ToString());
  261. body = body.Replace("[AGENTCOMM]", " " + week.AgentCommision.ToString());
  262. #endregion
  263. #region UserFields
  264. body = body.Replace("[FULLNAME]", " " + week.DisplayOwner);
  265. body = body.Replace("[USEREMAIL]", " " + week.OwnerObject.EmailAddress);
  266. body = body.Replace("[USERCELLPHONE]", " " + week.OwnerObject.CellNumber);
  267. body = body.Replace("[USERTELEPHONE]", " " + week.OwnerObject.LandlineNumber);
  268. #endregion
  269. string toList = "";
  270. int emailCount = 0;
  271. foreach (var recipient in recipients)
  272. {
  273. toList += recipient.RecipientMail + ", ";
  274. emailCount++;
  275. }
  276. if (toList.Length > 0)
  277. {
  278. if (emailCount == 1)
  279. toList = toList.Substring(0, toList.Length - 2);
  280. else
  281. toList = toList.Substring(0, toList.Length - 2);
  282. }
  283. var host = _dbContext.Hosts.FirstOrDefault();
  284. using (SmtpClient smtp = new SmtpClient(host.Host))
  285. {
  286. MailMessage mail = new MailMessage();
  287. mail.To.Add(toList);
  288. mail.Subject = "Uni-Vate - Week Loaded by Agent";
  289. mail.Body = body;
  290. mail.IsBodyHtml = true;
  291. mail.BodyEncoding = Encoding.ASCII;
  292. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  293. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  294. mail.From = mail.Sender;
  295. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  296. smtp.Credentials = host.GetNetworkCredential();
  297. smtp.EnableSsl = host.UseSSL;
  298. smtp.Send(mail);
  299. }
  300. }
  301. public void WeekLoadedOwner(TimeshareWeekDto week)
  302. {
  303. string body = _dbContext.Templates.Where(x => x.Name == "WeekLoaded-Owner").Where(y => y.IsDeleted == false).FirstOrDefault().Body;
  304. var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "WeekLoaded-Owner").Where(y => y.IsDeleted == false).ToList();
  305. #region ResortFields
  306. body = body.Replace("[RESORTNAME]", " " + week.ResortName);
  307. body = body.Replace("[UNITNUMBER]", " " + week.UnitNumber);
  308. body = body.Replace("[MODULE]", " " + week.Module);
  309. body = body.Replace("[RESORTPRICE]", " " + week.SellPrice.ToString());
  310. body = body.Replace("[RESORTSEASON]", " " + week.Season);
  311. body = body.Replace("[RESORTREGION]", " " + week.Region.Display);
  312. body = body.Replace("[RESORTLEVY]", " " + week.LevyAmount.ToString());
  313. body = body.Replace("[OWNER]", " " + week.DisplayOwner);
  314. body = body.Replace("[SLEEPMAX]", " " + week.MaxSleep.ToString());
  315. body = body.Replace("[WEEK]", " " + week.WeekNumber);
  316. body = body.Replace("[BEDROOMS]", " " + week.Bedrooms);
  317. body = body.Replace("[ASKINGPRICE]", " " + week.AskingPrice.ToString());
  318. body = body.Replace("[ARRIVALDATE]", " " + week.ArrivalDate.ToString());
  319. body = body.Replace("[DEPARTUREDATE]", " " + week.DepartureDate.ToString());
  320. body = body.Replace("[AGENTREFER]", week.ReferedByAgent ? " Yes" : " No");
  321. body = body.Replace("[ALLLEVY]", week.LeviesPaidInFull ? " Yes" : " No");
  322. body = body.Replace("[WEEKPLACEDFORRENTAL]", week.WeekPlacedForRental ? " Yes" : " No");
  323. body = body.Replace("[ORIGINALPURCHASEDATE]", " " + week.OriginalPurchaseDate.ToString());
  324. body = body.Replace("[CUROCCUPATIONDATES]", " " + week.ArrivalDate.ToString() + " - " + week.DepartureDate.ToString());
  325. body = body.Replace("[ORIGINALPURCHASEPRICE]", " " + week.OriginalPurchasePrice.ToString());
  326. body = body.Replace("[AGENTCOMM]", " " + week.AgentCommision.ToString());
  327. #endregion
  328. #region UserFields
  329. body = body.Replace("[FULLNAME]", " " + week.DisplayOwner );
  330. body = body.Replace("[USEREMAIL]", " " + week.OwnerObject.EmailAddress);
  331. body = body.Replace("[USERCELLPHONE]", " " + week.OwnerObject.CellNumber);
  332. body = body.Replace("[USERTELEPHONE]", " " + week.OwnerObject.LandlineNumber);
  333. #endregion
  334. string toList = "";
  335. int emailCount = 0;
  336. foreach (var recipient in recipients)
  337. {
  338. toList += recipient.RecipientMail + ", ";
  339. emailCount++;
  340. }
  341. if (toList.Length > 0)
  342. {
  343. if (emailCount == 1)
  344. toList = toList.Substring(0, toList.Length - 2);
  345. else
  346. toList = toList.Substring(0, toList.Length - 2);
  347. }
  348. var host = _dbContext.Hosts.FirstOrDefault();
  349. using (SmtpClient smtp = new SmtpClient(host.Host))
  350. {
  351. MailMessage mail = new MailMessage();
  352. mail.To.Add(toList);
  353. mail.Subject = "Uni-Vate - Week Loaded";
  354. mail.Body = body;
  355. mail.IsBodyHtml = true;
  356. mail.BodyEncoding = Encoding.ASCII;
  357. mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  358. mail.Sender = new MailAddress(host.User, "UniVate Properties");
  359. mail.From = mail.Sender;
  360. smtp.UseDefaultCredentials = host.NeedsAuthorize;
  361. smtp.Credentials = host.GetNetworkCredential();
  362. smtp.EnableSsl = host.UseSSL;
  363. smtp.Send(mail);
  364. }
  365. }
  366. public void AddRecipient(MailRecipient rec)
  367. {
  368. if (MyCommon.IsValidEmail(rec.RecipientMail))
  369. {
  370. _dbContext.MailRecipients.Add(rec);
  371. _dbContext.SaveChanges();
  372. }
  373. else
  374. {
  375. throw new Exception();
  376. }
  377. }
  378. public List<MailRecipient> GetMailRecipients()
  379. {
  380. return _dbContext.MailRecipients.Where(x => x.IsDeleted == false).ToList();
  381. }
  382. public MailRecipient GetMailRecipientById(int id)
  383. {
  384. return _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();
  385. }
  386. public void UpdateMailRecipient(MailRecipient rec)
  387. {
  388. var recipient = _dbContext.MailRecipients.Where(x => x.Id == rec.Id).FirstOrDefault();
  389. if (recipient.RecipientMail != rec.RecipientMail)
  390. {
  391. recipient.RecipientMail = rec.RecipientMail;
  392. }
  393. if (recipient.RecipientName != rec.RecipientName)
  394. {
  395. recipient.RecipientName = rec.RecipientName;
  396. }
  397. if (recipient.RecipientUsage != rec.RecipientUsage)
  398. {
  399. recipient.RecipientUsage = rec.RecipientUsage;
  400. }
  401. _dbContext.MailRecipients.Update(recipient);
  402. _dbContext.SaveChanges();
  403. }
  404. public void DeleteMailRecipient(int id)
  405. {
  406. var rec = _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();
  407. _dbContext.MailRecipients.Remove(rec);
  408. _dbContext.SaveChanges();
  409. }
  410. }
  411. }