Просмотр исходного кода

Fixed warnings and messages

Template changes
master
Kobus Botha 5 лет назад
Родитель
Сommit
6735318421

+ 1
- 0
UnivateProperties_API/Containers/Communication/PlaceHolderDto.cs Просмотреть файл

@@ -2,6 +2,7 @@
2 2
 {
3 3
     public class PlaceHolderDto
4 4
     {
5
+        public int Id { get; set; }
5 6
         public string Name { get; set; }
6 7
         public string BoundToClass { get; set; }
7 8
         public string BoundToClassDisplay { get; set; }

+ 4
- 1
UnivateProperties_API/Containers/Timeshare/WeekDto.cs Просмотреть файл

@@ -56,7 +56,10 @@ namespace UnivateProperties_API.Containers.Timeshare
56 56
             Owner = $"{week.Owner?.Name} {week.Owner?.Surname}";
57 57
             Resort = new ResortDto(week.ResortCode, week.ResortName);
58 58
             Region = new RegionDto(week.Region != null ? week.Region.Id : 0, week.Region?.Code, week.Region?.Description);
59
-            Status = new StatusDto(week.Status.Id, week.Status?.Code, week.Status?.Description);
59
+            if(week.Status != null)
60
+            {
61
+                Status = new StatusDto(week.Status.Id, week.Status.Code, week.Status.Description);
62
+            }            
60 63
             Bedrooms = week.Bedrooms;
61 64
             MaxSleep = week.MaxSleep;
62 65
             UnitNumber = week.UnitNumber;

+ 1
- 2
UnivateProperties_API/Context/DataContext.cs Просмотреть файл

@@ -181,8 +181,7 @@ namespace UnivateProperties_API.Context
181 181
             modelBuilder.Entity<BidItem>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
182 182
             modelBuilder.Entity<ProcessFlow>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
183 183
             modelBuilder.Entity<Template>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
184
-
185
-            
184
+            modelBuilder.Entity<PlaceHolder>().HasQueryFilter(m => EF.Property<bool>(m, "IsDeleted") == false);
186 185
         }
187 186
 
188 187
     

+ 11
- 0
UnivateProperties_API/Controllers/Communication/TemplateController.cs Просмотреть файл

@@ -70,5 +70,16 @@ namespace UnivateProperties_API.Controllers.Communication
70 70
             _Repo.RemoveAtId(id);
71 71
             return new OkResult();
72 72
         }
73
+
74
+        [HttpPost("sendTemplate/{id}")]
75
+        public IActionResult SendTemplate(int id)
76
+        {
77
+            using ( var scope = new TransactionScope())
78
+            {
79
+                (_Repo as TemplateRepository).SendEmailTemplate(id);
80
+                scope.Complete();
81
+                return new OkResult();
82
+            }
83
+        }
73 84
     }
74 85
 }

+ 11
- 0
UnivateProperties_API/Controllers/Timeshare/TimeshareWeekController.cs Просмотреть файл

@@ -104,5 +104,16 @@ namespace UnivateProperties_API.Controllers.Timeshare
104 104
             _Repo.RemoveAtId(id);
105 105
             return new OkResult();
106 106
         }
107
+
108
+        [HttpPost("verifyweek/{id}")]
109
+        public IActionResult Post(int id)
110
+        {
111
+            using (var scope = new TransactionScope())
112
+            {
113
+                (_Repo as WeekRepository).VerifyWeek(id);
114
+                scope.Complete();
115
+                return new OkResult();
116
+            }
117
+        }
107 118
     }
108 119
 }

+ 1294
- 0
UnivateProperties_API/Migrations/20191112124547_templateMod.Designer.cs
Разница между файлами не показана из-за своего большого размера
Просмотреть файл


+ 113
- 0
UnivateProperties_API/Migrations/20191112124547_templateMod.cs Просмотреть файл

@@ -0,0 +1,113 @@
1
+using Microsoft.EntityFrameworkCore.Migrations;
2
+
3
+namespace UnivateProperties_API.Migrations
4
+{
5
+    public partial class templateMod : Migration
6
+    {
7
+        protected override void Up(MigrationBuilder migrationBuilder)
8
+        {
9
+            migrationBuilder.AddColumn<int>(
10
+                name: "SenderId",
11
+                table: "Templates",
12
+                nullable: true);
13
+
14
+            migrationBuilder.AddColumn<int>(
15
+                name: "TemplateId",
16
+                table: "Individuals",
17
+                nullable: true);
18
+
19
+            migrationBuilder.AddColumn<int>(
20
+                name: "TemplateId",
21
+                table: "Agents",
22
+                nullable: true);
23
+
24
+            migrationBuilder.AddColumn<bool>(
25
+                name: "Default",
26
+                table: "Accounts",
27
+                nullable: false,
28
+                defaultValue: false);
29
+
30
+            migrationBuilder.CreateIndex(
31
+                name: "IX_Templates_SenderId",
32
+                table: "Templates",
33
+                column: "SenderId");
34
+
35
+            migrationBuilder.CreateIndex(
36
+                name: "IX_Individuals_TemplateId",
37
+                table: "Individuals",
38
+                column: "TemplateId");
39
+
40
+            migrationBuilder.CreateIndex(
41
+                name: "IX_Agents_TemplateId",
42
+                table: "Agents",
43
+                column: "TemplateId");
44
+
45
+            migrationBuilder.AddForeignKey(
46
+                name: "FK_Agents_Templates_TemplateId",
47
+                table: "Agents",
48
+                column: "TemplateId",
49
+                principalTable: "Templates",
50
+                principalColumn: "Id",
51
+                onDelete: ReferentialAction.Restrict);
52
+
53
+            migrationBuilder.AddForeignKey(
54
+                name: "FK_Individuals_Templates_TemplateId",
55
+                table: "Individuals",
56
+                column: "TemplateId",
57
+                principalTable: "Templates",
58
+                principalColumn: "Id",
59
+                onDelete: ReferentialAction.Restrict);
60
+
61
+            migrationBuilder.AddForeignKey(
62
+                name: "FK_Templates_Accounts_SenderId",
63
+                table: "Templates",
64
+                column: "SenderId",
65
+                principalTable: "Accounts",
66
+                principalColumn: "Id",
67
+                onDelete: ReferentialAction.Restrict);
68
+        }
69
+
70
+        protected override void Down(MigrationBuilder migrationBuilder)
71
+        {
72
+            migrationBuilder.DropForeignKey(
73
+                name: "FK_Agents_Templates_TemplateId",
74
+                table: "Agents");
75
+
76
+            migrationBuilder.DropForeignKey(
77
+                name: "FK_Individuals_Templates_TemplateId",
78
+                table: "Individuals");
79
+
80
+            migrationBuilder.DropForeignKey(
81
+                name: "FK_Templates_Accounts_SenderId",
82
+                table: "Templates");
83
+
84
+            migrationBuilder.DropIndex(
85
+                name: "IX_Templates_SenderId",
86
+                table: "Templates");
87
+
88
+            migrationBuilder.DropIndex(
89
+                name: "IX_Individuals_TemplateId",
90
+                table: "Individuals");
91
+
92
+            migrationBuilder.DropIndex(
93
+                name: "IX_Agents_TemplateId",
94
+                table: "Agents");
95
+
96
+            migrationBuilder.DropColumn(
97
+                name: "SenderId",
98
+                table: "Templates");
99
+
100
+            migrationBuilder.DropColumn(
101
+                name: "TemplateId",
102
+                table: "Individuals");
103
+
104
+            migrationBuilder.DropColumn(
105
+                name: "TemplateId",
106
+                table: "Agents");
107
+
108
+            migrationBuilder.DropColumn(
109
+                name: "Default",
110
+                table: "Accounts");
111
+        }
112
+    }
113
+}

+ 29
- 0
UnivateProperties_API/Migrations/DataContextModelSnapshot.cs Просмотреть файл

@@ -148,6 +148,8 @@ namespace UnivateProperties_API.Migrations
148 148
 
149 149
                     b.Property<DateTime>("Created");
150 150
 
151
+                    b.Property<bool>("Default");
152
+
151 153
                     b.Property<string>("DisplayName");
152 154
 
153 155
                     b.Property<bool>("IsDeleted");
@@ -210,10 +212,14 @@ namespace UnivateProperties_API.Migrations
210 212
 
211 213
                     b.Property<string>("Name");
212 214
 
215
+                    b.Property<int?>("SenderId");
216
+
213 217
                     b.Property<string>("Subject");
214 218
 
215 219
                     b.HasKey("Id");
216 220
 
221
+                    b.HasIndex("SenderId");
222
+
217 223
                     b.ToTable("Templates");
218 224
                 });
219 225
 
@@ -903,12 +909,16 @@ namespace UnivateProperties_API.Migrations
903 909
 
904 910
                     b.Property<string>("Telephone");
905 911
 
912
+                    b.Property<int?>("TemplateId");
913
+
906 914
                     b.Property<int?>("UserId");
907 915
 
908 916
                     b.HasKey("Id");
909 917
 
910 918
                     b.HasIndex("AgencyId");
911 919
 
920
+                    b.HasIndex("TemplateId");
921
+
912 922
                     b.HasIndex("UserId");
913 923
 
914 924
                     b.ToTable("Agents");
@@ -949,6 +959,8 @@ namespace UnivateProperties_API.Migrations
949 959
 
950 960
                     b.Property<string>("Telephone");
951 961
 
962
+                    b.Property<int?>("TemplateId");
963
+
952 964
                     b.Property<int?>("UserId");
953 965
 
954 966
                     b.HasKey("Id");
@@ -957,6 +969,8 @@ namespace UnivateProperties_API.Migrations
957 969
 
958 970
                     b.HasIndex("BankAccountId");
959 971
 
972
+                    b.HasIndex("TemplateId");
973
+
960 974
                     b.HasIndex("UserId");
961 975
 
962 976
                     b.HasIndex("Telephone", "CellNumber", "Email")
@@ -1065,6 +1079,13 @@ namespace UnivateProperties_API.Migrations
1065 1079
                         .OnDelete(DeleteBehavior.Cascade);
1066 1080
                 });
1067 1081
 
1082
+            modelBuilder.Entity("UnivateProperties_API.Model.Communication.Template", b =>
1083
+                {
1084
+                    b.HasOne("UnivateProperties_API.Model.Communication.SMTPAccount", "Sender")
1085
+                        .WithMany()
1086
+                        .HasForeignKey("SenderId");
1087
+                });
1088
+
1068 1089
             modelBuilder.Entity("UnivateProperties_API.Model.Misc.Address", b =>
1069 1090
                 {
1070 1091
                     b.HasOne("UnivateProperties_API.Model.Users.Individual", "Owner")
@@ -1231,6 +1252,10 @@ namespace UnivateProperties_API.Migrations
1231 1252
                         .WithMany("Agents")
1232 1253
                         .HasForeignKey("AgencyId");
1233 1254
 
1255
+                    b.HasOne("UnivateProperties_API.Model.Communication.Template")
1256
+                        .WithMany("AgentBCC")
1257
+                        .HasForeignKey("TemplateId");
1258
+
1234 1259
                     b.HasOne("UnivateProperties_API.Model.Users.User", "User")
1235 1260
                         .WithMany()
1236 1261
                         .HasForeignKey("UserId");
@@ -1246,6 +1271,10 @@ namespace UnivateProperties_API.Migrations
1246 1271
                         .WithMany()
1247 1272
                         .HasForeignKey("BankAccountId");
1248 1273
 
1274
+                    b.HasOne("UnivateProperties_API.Model.Communication.Template")
1275
+                        .WithMany("IndividualBCC")
1276
+                        .HasForeignKey("TemplateId");
1277
+
1249 1278
                     b.HasOne("UnivateProperties_API.Model.Users.User", "User")
1250 1279
                         .WithMany()
1251 1280
                         .HasForeignKey("UserId");

+ 66
- 0
UnivateProperties_API/Model/Communication/Email.cs Просмотреть файл

@@ -3,6 +3,9 @@ using System;
3 3
 using System.ComponentModel.DataAnnotations.Schema;
4 4
 using System.Net.Mail;
5 5
 using System.Text;
6
+using UnivateProperties_API.Model.Users;
7
+using System.Collections;
8
+using System.Collections.Generic;
6 9
 
7 10
 namespace UnivateProperties_API.Model.Communication
8 11
 {
@@ -30,6 +33,43 @@ namespace UnivateProperties_API.Model.Communication
30 33
             Body = body;
31 34
             Subject = subject;
32 35
         }
36
+
37
+        public Email(Template template, Person sendTo, List<BaseEntity> args)
38
+        {
39
+            if(sendTo != null && MyCommon.IsValidEmail(sendTo.Email))
40
+            {
41
+                if (template.SenderId != null)
42
+                {
43
+                    SenderId = template.SenderId.Value;
44
+                }
45
+                Sender = template.Sender;
46
+                To = sendTo.Email;
47
+                ToDisplay = sendTo.FullName;
48
+                BCC = ConcatEmails(template.AgentBCC, template.IndividualBCC);
49
+                IsBodyHtml = true;
50
+                Body = template.Body;
51
+                Subject = template.Subject;
52
+                foreach(var item in template.PlaceHolders)
53
+                {
54
+                    foreach(var obj in args)
55
+                    {
56
+                        if(obj.GetType() == Type.GetType(item.BoundToClass))
57
+                        {
58
+                            string replaceValue = (string)obj[item.BoundTo];
59
+                            if(Body.Contains(item.Name))
60
+                            {
61
+                                Body = Body.Replace(item.Name, replaceValue);
62
+                            }
63
+                            if(Subject.Contains(item.Name))
64
+                            {
65
+                                Subject = Subject.Replace(item.Name, replaceValue);
66
+                            }
67
+                        }
68
+                    }
69
+                }
70
+            }
71
+            
72
+        }
33 73
         #endregion Constructor
34 74
 
35 75
         #region Properties
@@ -137,6 +177,32 @@ namespace UnivateProperties_API.Model.Communication
137 177
             mail.From = mail.Sender;
138 178
             return mail;
139 179
         }
180
+
181
+        private string ConcatEmails(ICollection<Agent> agents, ICollection<Individual> individuals)
182
+        {
183
+            string value = string.Empty;
184
+            if (agents != null && agents.Count > 0)
185
+            {
186
+                foreach(var item in agents)
187
+                {
188
+                    if(MyCommon.IsValidEmail(item.Email))
189
+                    {
190
+                        value += $"{item.Email};";
191
+                    }
192
+                }
193
+            }
194
+            if (individuals != null && individuals.Count > 0)
195
+            {
196
+                foreach (var item in individuals)
197
+                {
198
+                    if (MyCommon.IsValidEmail(item.Email))
199
+                    {
200
+                        value += $"{item.Email};";
201
+                    }
202
+                }
203
+            }
204
+            return value;
205
+        }
140 206
         #endregion Methods
141 207
     }
142 208
 }

+ 2
- 0
UnivateProperties_API/Model/Communication/SMTPAccount.cs Просмотреть файл

@@ -33,11 +33,13 @@ namespace UnivateProperties_API.Model.Communication
33 33
         #region Properties
34 34
         public string Address { get; set; }
35 35
         public string DisplayName { get; set; }
36
+        public bool Default { get; set; }
36 37
         [ForeignKey("SMTPHost")]
37 38
         public int SMTPHostId { get; set; }
38 39
 
39 40
         public virtual SMTPHost SMTPHost { get; set; }
40 41
         public virtual ICollection<Email> Emails { get; set; }
42
+        public virtual ICollection<Template> Templates { get; set; }
41 43
         #endregion Properties
42 44
     }
43 45
 }

+ 7
- 0
UnivateProperties_API/Model/Communication/Template.cs Просмотреть файл

@@ -1,4 +1,6 @@
1 1
 using System.Collections.Generic;
2
+using System.ComponentModel.DataAnnotations.Schema;
3
+using UnivateProperties_API.Model.Users;
2 4
 
3 5
 namespace UnivateProperties_API.Model.Communication
4 6
 {
@@ -9,8 +11,13 @@ namespace UnivateProperties_API.Model.Communication
9 11
         public string Name { get; set; }
10 12
         public string Subject { get; set; }
11 13
         public string Body { get; set; }
14
+        [ForeignKey("Sender")]
15
+        public int? SenderId { get; set; }
12 16
 
13 17
         public virtual ICollection<PlaceHolder> PlaceHolders { get; set; }
18
+        public virtual SMTPAccount Sender { get; set; }
19
+        public virtual ICollection<Agent> AgentBCC { get; set; }
20
+        public virtual ICollection<Individual> IndividualBCC { get; set; }
14 21
         #endregion
15 22
     }
16 23
 }

+ 38
- 11
UnivateProperties_API/Model/Timeshare/TimeshareWeek.cs Просмотреть файл

@@ -10,21 +10,48 @@ namespace UnivateProperties_API.Model.Timeshare
10 10
 {
11 11
     public class TimeshareWeek : BaseEntity
12 12
     {
13
-        TimeshareWeek()
13
+        public TimeshareWeek()
14 14
         {
15
-
16 15
         }
17 16
 
18
-        TimeshareWeek(WeekDto week)
17
+        public TimeshareWeek(bool referedByAgent, int? agentId, int? agencyId, int ownerId, bool agentAsRep, bool otherResort, string otherResortName, string resortCode, string resortName, int regionId, string season, string module, int bedrooms, int maxSleep, string unitNumber, string weekNumber, double levyAmount, bool currentYearBanked, string bankedWith, bool leviesPaidInFull, bool weekPlacedForRental, double originalPurchasePrice, DateTime originalPurchaseDate, DateTime arrivalDate, DateTime departureDate, double sellPrice, double agentCommision, string mandate, int statusId, Status status, Province region, Individual owner, Agent agent, Agency agency, ICollection<BidItem> bidItems, ICollection<ProcessFlow.ProcessFlow> processFlows)
19 18
         {
20
-            ReferedByAgent = week.ReferedByAgent;
21
-            AgentAsRep = false;
22
-            ResortCode = week.Resort.ResortCode;
23
-            ResortName = week.Resort.ResortName;
24
-            RegionId = week.Region.Id;
25
-            Bedrooms = week.Bedrooms;
26
-            MaxSleep = week.MaxSleep;
27
-            UnitNumber = week.UnitNumber;
19
+            ReferedByAgent = referedByAgent;
20
+            AgentId = agentId;
21
+            AgencyId = agencyId;
22
+            OwnerId = ownerId;
23
+            AgentAsRep = agentAsRep;
24
+            OtherResort = otherResort;
25
+            OtherResortName = otherResortName;
26
+            ResortCode = resortCode;
27
+            ResortName = resortName;
28
+            RegionId = regionId;
29
+            Season = season;
30
+            Module = module;
31
+            Bedrooms = bedrooms;
32
+            MaxSleep = maxSleep;
33
+            UnitNumber = unitNumber;
34
+            WeekNumber = weekNumber;
35
+            LevyAmount = levyAmount;
36
+            CurrentYearBanked = currentYearBanked;
37
+            BankedWith = bankedWith;
38
+            LeviesPaidInFull = leviesPaidInFull;
39
+            WeekPlacedForRental = weekPlacedForRental;
40
+            OriginalPurchasePrice = originalPurchasePrice;
41
+            OriginalPurchaseDate = originalPurchaseDate;
42
+            ArrivalDate = arrivalDate;
43
+            DepartureDate = departureDate;
44
+            SellPrice = sellPrice;
45
+            AgentCommision = agentCommision;
46
+            Mandate = mandate;
47
+            StatusId = statusId;
48
+            Status = status;
49
+            Region = region;
50
+            Owner = owner;
51
+            Agent = agent;
52
+            Agency = agency;
53
+            BidItems = bidItems;
54
+            ProcessFlows = processFlows;
28 55
         }
29 56
 
30 57
         #region Properties

+ 67
- 0
UnivateProperties_API/Repository/Communication/TemplateRepository.cs Просмотреть файл

@@ -4,7 +4,9 @@ using System.Collections.Generic;
4 4
 using System.Linq;
5 5
 using UnivateProperties_API.Containers.Communication;
6 6
 using UnivateProperties_API.Context;
7
+using UnivateProperties_API.Model;
7 8
 using UnivateProperties_API.Model.Communication;
9
+using UnivateProperties_API.Model.Users;
8 10
 
9 11
 namespace UnivateProperties_API.Repository.Communication
10 12
 {
@@ -84,10 +86,31 @@ namespace UnivateProperties_API.Repository.Communication
84 86
 
85 87
         public void Update(Template item)
86 88
         {
89
+            var itemList = _dbContext.PlaceHolders.Where(x => x.TemplateId == item.Id).ToList();
90
+            CheckListChange(itemList, item.PlaceHolders.ToList());
87 91
             _dbContext.Entry(item).State = EntityState.Modified;
88 92
             Save();
89 93
         }
90 94
 
95
+        private void CheckListChange(List<PlaceHolder> holderOld, List<PlaceHolder> holderNew)
96
+        {
97
+            foreach(var item in holderOld)
98
+            {
99
+                if(!holderNew.Any(x => x.Id== item.Id))
100
+                {
101
+                    var i = _dbContext.PlaceHolders.FirstOrDefault(x => x.Id == item.Id);
102
+                    if(item != null)
103
+                    {
104
+                        _dbContext.PlaceHolders.Remove(i);
105
+                    }
106
+                }
107
+            }
108
+            foreach(var item in holderNew.Where(x => x.Id == 0))
109
+            {
110
+                _dbContext.Add(item);
111
+            }
112
+        }
113
+
91 114
         public void Save()
92 115
         {
93 116
             _dbContext.SaveChanges();
@@ -128,6 +151,7 @@ namespace UnivateProperties_API.Repository.Communication
128 151
             {
129 152
                 list.Add(new PlaceHolderDto()
130 153
                 {
154
+                    Id = item.Id,
131 155
                     Name = item.Name,
132 156
                     BoundTo = item.BoundTo,
133 157
                     BoundToClass = item.BoundToClass,
@@ -136,5 +160,48 @@ namespace UnivateProperties_API.Repository.Communication
136 160
             }
137 161
             return list;
138 162
         }
163
+
164
+        public void SendEmailTemplate(int id)
165
+        {
166
+            var item = _dbContext.Weeks.FirstOrDefault(x => x.Id == 4);
167
+            var indiv = _dbContext.Individuals.FirstOrDefault(x => x.Id == 73);
168
+            List<BaseEntity> list = new List<BaseEntity>
169
+            {
170
+                item,
171
+                indiv
172
+            };
173
+            var template = _dbContext.Templates.FirstOrDefault(x => x.Id == id);
174
+            if (template.SenderId == null)
175
+            {
176
+                var acc = _dbContext.Accounts.FirstOrDefault(x => x.Default);
177
+                if (acc != null)
178
+                {
179
+                    template.SenderId = acc.Id;
180
+                    template.Sender = acc;
181
+                }
182
+            }
183
+            else template.Sender = _dbContext.Accounts.FirstOrDefault(x => x.Id == template.SenderId);
184
+            template.PlaceHolders = _dbContext.PlaceHolders.Where(x => x.TemplateId == id).ToList();
185
+            if(template != null)
186
+            {
187
+                Person person = _dbContext.Individuals.FirstOrDefault(x => x.Email == "kobusb@provision-sa.com");
188
+                if(person == null)
189
+                {
190
+                    person = _dbContext.Agents.FirstOrDefault(x => x.Email == "kobusb@provision-sa.com");
191
+                }
192
+                if(person != null)
193
+                {
194
+                    try
195
+                    {
196
+                        Email email = new Email(template, person, list);
197
+                        EmailRepository emailRepo = new EmailRepository(_dbContext);
198
+                        emailRepo.Insert(email);
199
+                    }
200
+                    catch(Exception)
201
+                    {
202
+                    }                    
203
+                }                
204
+            }
205
+        }
139 206
     }
140 207
 }

+ 11
- 0
UnivateProperties_API/Repository/Timeshare/WeekRepository.cs Просмотреть файл

@@ -389,5 +389,16 @@ namespace UnivateProperties_API.Repository.Timeshare
389 389
             }
390 390
             return detList;
391 391
         }
392
+
393
+        public void VerifyWeek(int id)
394
+        {
395
+            var week = _dbContext.Weeks.FirstOrDefault(x => x.Id == id);
396
+            var newStatus = _dbContext.Status.FirstOrDefault(x => x.Code.ToLower() == "a2" && x.StatusType == StatusType.Timeshare);
397
+            if(week != null && newStatus != null)
398
+            {
399
+                week.Status = newStatus;
400
+                Update(week);
401
+            }
402
+        }
392 403
     }
393 404
 }

Загрузка…
Отмена
Сохранить