Parcourir la source

Email Recipients

master
30117125 il y a 4 ans
Parent
révision
e0a9d848f3

+ 1
- 0
UnivateProperties_API/Context/DataContext.cs Voir le fichier

@@ -45,6 +45,7 @@ namespace UnivateProperties_API.Context
45 45
         public virtual DbSet<SMTPHost> Hosts { get; set; }
46 46
         public virtual DbSet<Template> Templates { get; set; }
47 47
         public virtual DbSet<PlaceHolder> PlaceHolders { get; set; }
48
+        public virtual DbSet<MailRecipient> MailRecipients { get; set; }
48 49
         #endregion Communication
49 50
 
50 51
         #region Property

+ 45
- 6
UnivateProperties_API/Controllers/Communication/MailController.cs Voir le fichier

@@ -1,9 +1,5 @@
1
-using System;
2
-using System.Collections.Generic;
3
-using System.Linq;
4
-using System.Threading.Tasks;
5
-using Microsoft.AspNetCore.Http;
6
-using Microsoft.AspNetCore.Mvc;
1
+using Microsoft.AspNetCore.Mvc;
2
+using RestSharp;
7 3
 using UnivateProperties_API.Model.Communication;
8 4
 using UnivateProperties_API.Repository.Communication;
9 5
 
@@ -40,5 +36,48 @@ namespace UnivateProperties_API.Controllers.Communication
40 36
                 return new BadRequestResult();
41 37
             }
42 38
         }
39
+
40
+        [HttpPost("mailrecipient")]
41
+        public IActionResult AddMailRecipient([FromBody] MailRecipient rec)
42
+        {
43
+            try
44
+            {
45
+                _repo.AddRecipient(rec);
46
+                return new OkResult();
47
+            }
48
+            catch
49
+            {
50
+                return new BadRequestResult();
51
+            }
52
+
53
+        }
54
+
55
+        [HttpGet("mailrecipients")]
56
+        public IActionResult GetMailRecipients()
57
+        {
58
+            var recipients = _repo.GetMailRecipients();
59
+            return new OkObjectResult(recipients);
60
+        }
61
+
62
+        [HttpGet("mailrecipient/{id}")]
63
+        public IActionResult GetRecipientById(int id)
64
+        {
65
+            var recipient = _repo.GetMailRecipientById(id);
66
+            return new OkObjectResult(recipient);
67
+        }
68
+
69
+        [HttpPut("mailrecipients")]
70
+        public IActionResult UpdateRecipient([FromBody] MailRecipient rec)
71
+        {
72
+            _repo.UpdateMailRecipient(rec);
73
+            return new OkResult();
74
+        }
75
+
76
+        [HttpDelete("mailrecipient/{id}")]
77
+        public IActionResult DeleteRecipient(int id)
78
+        {
79
+            _repo.DeleteMailRecipient(id);
80
+            return new OkResult();
81
+        }
43 82
     }
44 83
 }

+ 1687
- 0
UnivateProperties_API/Migrations/20201026073628_EmailRecipient.Designer.cs
Fichier diff supprimé car celui-ci est trop grand
Voir le fichier


+ 37
- 0
UnivateProperties_API/Migrations/20201026073628_EmailRecipient.cs Voir le fichier

@@ -0,0 +1,37 @@
1
+using System;
2
+using Microsoft.EntityFrameworkCore.Metadata;
3
+using Microsoft.EntityFrameworkCore.Migrations;
4
+
5
+namespace UnivateProperties_API.Migrations
6
+{
7
+    public partial class EmailRecipient : Migration
8
+    {
9
+        protected override void Up(MigrationBuilder migrationBuilder)
10
+        {
11
+            migrationBuilder.CreateTable(
12
+                name: "MailRecipients",
13
+                columns: table => new
14
+                {
15
+                    Id = table.Column<int>(nullable: false)
16
+                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
17
+                    Created = table.Column<DateTime>(nullable: false),
18
+                    Modified = table.Column<DateTime>(nullable: false),
19
+                    ModifiedBy = table.Column<string>(nullable: true),
20
+                    IsDeleted = table.Column<bool>(nullable: false),
21
+                    RecipientName = table.Column<string>(nullable: true),
22
+                    RecipientMail = table.Column<string>(nullable: true),
23
+                    RecipientUsage = table.Column<string>(nullable: true)
24
+                },
25
+                constraints: table =>
26
+                {
27
+                    table.PrimaryKey("PK_MailRecipients", x => x.Id);
28
+                });
29
+        }
30
+
31
+        protected override void Down(MigrationBuilder migrationBuilder)
32
+        {
33
+            migrationBuilder.DropTable(
34
+                name: "MailRecipients");
35
+        }
36
+    }
37
+}

+ 25
- 0
UnivateProperties_API/Migrations/DataContextModelSnapshot.cs Voir le fichier

@@ -234,6 +234,31 @@ namespace UnivateProperties_API.Migrations
234 234
                     b.ToTable("Emails");
235 235
                 });
236 236
 
237
+            modelBuilder.Entity("UnivateProperties_API.Model.Communication.MailRecipient", b =>
238
+                {
239
+                    b.Property<int>("Id")
240
+                        .ValueGeneratedOnAdd()
241
+                        .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
242
+
243
+                    b.Property<DateTime>("Created");
244
+
245
+                    b.Property<bool>("IsDeleted");
246
+
247
+                    b.Property<DateTime>("Modified");
248
+
249
+                    b.Property<string>("ModifiedBy");
250
+
251
+                    b.Property<string>("RecipientMail");
252
+
253
+                    b.Property<string>("RecipientName");
254
+
255
+                    b.Property<string>("RecipientUsage");
256
+
257
+                    b.HasKey("Id");
258
+
259
+                    b.ToTable("MailRecipients");
260
+                });
261
+
237 262
             modelBuilder.Entity("UnivateProperties_API.Model.Communication.PlaceHolder", b =>
238 263
                 {
239 264
                     b.Property<int>("Id")

+ 9
- 0
UnivateProperties_API/Model/Communication/MailRecipient.cs Voir le fichier

@@ -0,0 +1,9 @@
1
+namespace UnivateProperties_API.Model.Communication
2
+{
3
+    public class MailRecipient : BaseEntity
4
+    {
5
+        public string RecipientName { get; set; }
6
+        public string RecipientMail { get; set; }
7
+        public string RecipientUsage { get; set; }
8
+    }
9
+}

+ 85
- 6
UnivateProperties_API/Repository/Communication/MailRepository.cs Voir le fichier

@@ -1,9 +1,11 @@
1 1
 using MailKit.Net.Smtp;
2 2
 using MimeKit;
3 3
 using System;
4
+using System.Collections.Generic;
4 5
 using System.Linq;
5 6
 using System.Linq.Dynamic.Core;
6 7
 using UnivateProperties_API.Context;
8
+using UnivateProperties_API.Helpers;
7 9
 using UnivateProperties_API.Model.Communication;
8 10
 
9 11
 namespace UnivateProperties_API.Repository.Communication
@@ -12,6 +14,11 @@ namespace UnivateProperties_API.Repository.Communication
12 14
     {
13 15
         void ContactUs(MailModel mm);
14 16
         void EnquireNow(MailModel mm);
17
+        void AddRecipient(MailRecipient rec);
18
+        List<MailRecipient> GetMailRecipients();
19
+        MailRecipient GetMailRecipientById(int id);
20
+        void UpdateMailRecipient(MailRecipient rec);
21
+        void DeleteMailRecipient(int id);
15 22
     }
16 23
 
17 24
     public class MailRepository : IMailRepository
@@ -40,13 +47,19 @@ namespace UnivateProperties_API.Repository.Communication
40 47
             string name = mm.Name;
41 48
             string email = mm.Email;
42 49
             string message = mm.Message;
50
+            var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "ContactUs").ToList();
43 51
 
44 52
             from = new MailboxAddress("Admin", mm.FromAddress);
45 53
 
46
-            to = new MailboxAddress("User", mm.ToAddress);
54
+            InternetAddressList list = new InternetAddressList();
55
+            foreach (var recipient in recipients)
56
+            {
57
+                list.Add(new MailboxAddress(recipient.RecipientName, recipient.RecipientMail));
58
+            }
59
+            //to = new MailboxAddress("User", mm.ToAddress);
47 60
 
48 61
             messageObj.From.Add(from);
49
-            messageObj.To.Add(to);
62
+            messageObj.To.AddRange(list);
50 63
 
51 64
             messageObj.Subject = "Uni-Vate - New Contact Request";
52 65
 
@@ -80,13 +93,20 @@ namespace UnivateProperties_API.Repository.Communication
80 93
             string message = mm.Message;
81 94
             var props = _dbContext.Properties.ToList();
82 95
             var prop = props.Where(x => x.Id == Convert.ToInt32(mm.Property)).FirstOrDefault();
96
+            var recipients = _dbContext.MailRecipients.Where(x => x.RecipientUsage == "EnquireNow").ToList();
97
+
83 98
             
84 99
             from = new MailboxAddress("Admin", mm.FromAddress);
85 100
 
86
-            to = new MailboxAddress("User", mm.ToAddress);
101
+            InternetAddressList list = new InternetAddressList();
102
+            foreach (var recipient in recipients)
103
+            {
104
+                list.Add(new MailboxAddress(recipient.RecipientName, recipient.RecipientMail));
105
+            }
106
+            //to = new MailboxAddress("User", mm.ToAddress);
87 107
 
88 108
             messageObj.From.Add(from);
89
-            messageObj.To.Add(to);
109
+            messageObj.To.AddRange(list);
90 110
 
91 111
             messageObj.Subject = "Uni-Vate - Enquiry to view property";
92 112
 
@@ -130,10 +150,14 @@ namespace UnivateProperties_API.Repository.Communication
130 150
 
131 151
             bodyBuilder.HtmlBody = "<div style=\"margin: 5px\">" +
132 152
                 "<h4>Request to reset password</h4>" +
133
-                "<h4>Dear " + name + " there has been a request  to reset your password. If this is incorrect please send an email to info@univateproperties.co.za</h4>" +
153
+                "<h4>Dear " + name + ", </h4>" +
154
+                "<h4>There has been a request to reset your password. If this is incorrect please send an email to info@univateproperties.co.za</h4>" +
134 155
                 "<div>" +
156
+                "<h5>Please follow the link below to reset your password:</h5>" +
135 157
                 "<h3>"+ link + "</h3>" +
136
-
158
+                "<br />"+
159
+                "<h4>Thank You</h4>" +
160
+                "<h4>Team Uni-Vate</h4>" +
137 161
                 "</div>" +
138 162
                 "</div>" +
139 163
                 "</div>";
@@ -147,5 +171,60 @@ namespace UnivateProperties_API.Repository.Communication
147 171
             client.Disconnect(true);
148 172
             client.Dispose();
149 173
         }
174
+
175
+        public void AddRecipient(MailRecipient rec)
176
+        {
177
+            if (MyCommon.IsValidEmail(rec.RecipientMail))
178
+            {
179
+                _dbContext.MailRecipients.Add(rec);
180
+                _dbContext.SaveChanges();
181
+            }
182
+            else
183
+            {
184
+                throw new Exception();
185
+            }
186
+            
187
+        }
188
+
189
+        public List<MailRecipient> GetMailRecipients()
190
+        {
191
+            return _dbContext.MailRecipients.Where(x => x.IsDeleted == false).ToList();
192
+        }
193
+
194
+        public MailRecipient GetMailRecipientById(int id)
195
+        {
196
+            return _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault(); 
197
+        }
198
+
199
+        public void UpdateMailRecipient(MailRecipient rec)
200
+        {
201
+            var recipient = _dbContext.MailRecipients.Where(x => x.Id == rec.Id).FirstOrDefault();
202
+
203
+            if (recipient.RecipientMail != rec.RecipientMail)
204
+            {
205
+                recipient.RecipientMail = rec.RecipientMail;
206
+            }
207
+
208
+            if (recipient.RecipientName != rec.RecipientName)
209
+            {
210
+                recipient.RecipientName = rec.RecipientName;
211
+            }
212
+
213
+            if (recipient.RecipientUsage != rec.RecipientUsage)
214
+            {
215
+                recipient.RecipientUsage = rec.RecipientUsage;
216
+            }
217
+
218
+
219
+            _dbContext.MailRecipients.Update(recipient);
220
+            _dbContext.SaveChanges();
221
+        }
222
+
223
+        public void DeleteMailRecipient(int id)
224
+        {
225
+            var rec = _dbContext.MailRecipients.Where(x => x.Id == id).FirstOrDefault();        
226
+            _dbContext.MailRecipients.Remove(rec);
227
+            _dbContext.SaveChanges();
228
+        }
150 229
     }
151 230
 }

+ 1
- 0
UnivateProperties_API/Startup.cs Voir le fichier

@@ -156,6 +156,7 @@ namespace UnivateProperties_API
156 156
             services.AddTransient<IRepository<SMTPAccount>, SMTPAccountRepository>();
157 157
             services.AddTransient<IRepository<SMTPHost>, SMTPHostRepository>();
158 158
             services.AddTransient<IMailRepository, MailRepository>();
159
+
159 160
             #endregion Communication
160 161
             #region Logs 
161 162
             services.AddTransient<ISearchLogRepository, SearchLogRepository>();

+ 1
- 1
UnivateProperties_API/appsettings.json Voir le fichier

@@ -11,7 +11,7 @@
11 11
   "ConnectionStrings": {
12 12
     "DefaultConnection": "Data Source=localhost;Initial Catalog=UniVateDemo;Persist Security Info=True;User Id=Provision;Password=What123!;Pooling=false;",
13 13
     "TenderConnection": "http://www.unipoint-consoft.co.za/nph-srep.exe?cluvavail.sch&CLUB=LPA&RESORT=ALL&SUMMARY=N&HEAD=N",
14
-    "ReservationsURL": "http://training.provision-sa.com:84/ReservationsWebService.asmx", //Please note that ReservationsWebService must be in this case. 
14
+    "ReservationsURL": "https://www.pvsl.co.za:85/ReservationsWebService.asmx", //Please note that ReservationsWebService must be in this case. 
15 15
     "ReservationsUserCode": "UniInt",
16 16
     "ReservationsPassword": "Un11nt"
17 17
   }

Chargement…
Annuler
Enregistrer