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

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