9
9
using StrDss . Model ;
10
10
using StrDss . Model . DelistingDtos ;
11
11
using StrDss . Model . OrganizationDtos ;
12
+ using StrDss . Model . RentalReportDtos ;
12
13
using StrDss . Service . CsvHelpers ;
13
14
using StrDss . Service . EmailTemplates ;
14
15
using System . Text . RegularExpressions ;
@@ -28,6 +29,8 @@ public interface IDelistingService
28
29
Task < ( Dictionary < string , List < string > > errors , EmailPreview preview ) > GetTakedownNoticesFromListingPreviewAsync ( TakedownNoticesFromListingDto [ ] listings ) ;
29
30
Task < Dictionary < string , List < string > > > CreateTakedownRequestsFromListingAsync ( TakedownRequestsFromListingDto [ ] listings ) ;
30
31
Task < ( Dictionary < string , List < string > > errors , EmailPreview preview ) > GetTakedownRequestsFromListingPreviewAsync ( TakedownRequestsFromListingDto [ ] listings ) ;
32
+ Task < ( Dictionary < string , List < string > > errors , EmailPreview preview ) > GetComplianceOrdersFromListingPreviewAsync ( ComplianceOrderDto [ ] listings ) ;
33
+ Task < Dictionary < string , List < string > > > CreateComplianceOrdersFromListingAsync ( ComplianceOrderDto [ ] listings ) ;
31
34
}
32
35
public class DelistingService : ServiceBase , IDelistingService
33
36
{
@@ -268,7 +271,7 @@ public async Task<Dictionary<string, List<string>>> CreateTakedownNoticesFromLis
268
271
var template = CreateTakedownNoticeTemplate ( listing , rentalListing ) ;
269
272
templates . Add ( template ) ;
270
273
271
- ValidateCcListEmails ( listing . CcList , emailRegex , errors ) ;
274
+ ValidateEmails ( listing . CcList , emailRegex , "ccList" , errors ) ;
272
275
ValidateLocalGovernmentContactEmail ( listing , emailRegex , errors ) ;
273
276
ValidateAndSetHostEmails ( listing , rentalListing , emailRegex , template , errors ) ;
274
277
@@ -410,7 +413,7 @@ private async Task SendTakedownNoticeEmailFromListingAsync(TakedownNoticesFromLi
410
413
var template = CreateTakedownNoticeTemplate ( listing , rentalListing ) ;
411
414
templates . Add ( template ) ;
412
415
413
- ValidateCcListEmails ( listing . CcList , emailRegex , errors ) ;
416
+ ValidateEmails ( listing . CcList , emailRegex , "ccList" , errors ) ;
414
417
ValidateLocalGovernmentContactEmail ( listing , emailRegex , errors ) ;
415
418
ValidateAndSetHostEmails ( listing , rentalListing , emailRegex , template , errors ) ;
416
419
@@ -478,7 +481,7 @@ private async Task ProcessListings(TakedownRequestsFromListingDto[] listings, Di
478
481
479
482
foreach ( var listing in listings )
480
483
{
481
- ValidateCcListEmails ( listing . CcList , emailRegex , errors ) ;
484
+ ValidateEmails ( listing . CcList , emailRegex , "ccList" , errors ) ;
482
485
483
486
var rentalListing = await _listingRepo . GetRentalListingForTakedownAction ( listing . RentalListingId , false ) ;
484
487
@@ -495,13 +498,13 @@ private async Task ProcessListings(TakedownRequestsFromListingDto[] listings, Di
495
498
}
496
499
}
497
500
498
- private void ValidateCcListEmails ( List < string > ccList , RegexInfo emailRegex , Dictionary < string , List < string > > errors )
501
+ private void ValidateEmails ( List < string > emails , RegexInfo emailRegex , string field , Dictionary < string , List < string > > errors )
499
502
{
500
- foreach ( var email in ccList )
503
+ foreach ( var email in emails )
501
504
{
502
505
if ( ! Regex . IsMatch ( email , emailRegex . Regex ) )
503
506
{
504
- errors . AddItem ( "ccList" , $ "Email ({ email } ) is invalid") ;
507
+ errors . AddItem ( field , $ "Email ({ email } ) is invalid") ;
505
508
}
506
509
}
507
510
}
@@ -630,7 +633,7 @@ public async Task<Dictionary<string, List<string>>> CreateTakedownRequestAsync(T
630
633
return errors ;
631
634
}
632
635
633
- await SendTakedownRequestAsync ( dto , platform , lg ) ;
636
+ await SendTakedownRequestAsync ( dto , platform ! , lg ! ) ;
634
637
635
638
return errors ;
636
639
}
@@ -780,11 +783,11 @@ public async Task ProcessTakedownRequestBatchEmailsAsync()
780
783
781
784
try
782
785
{
783
- await ProcessTakedownRequestBatchEmailAsync ( platform , allEmails ) ;
786
+ await ProcessTakedownRequestBatchEmailAsync ( platform ! , allEmails ) ;
784
787
}
785
788
catch ( Exception ex )
786
789
{
787
- _logger . LogError ( $ "Error while processing '{ EmailMessageTypes . BatchTakedownRequest } ' email for { platform . OrganizationNm } ") ;
790
+ _logger . LogError ( $ "Error while processing '{ EmailMessageTypes . BatchTakedownRequest } ' email for { platform ! . OrganizationNm } ") ;
788
791
_logger . LogError ( ex . ToString ( ) ) ;
789
792
//send email to admin?
790
793
}
@@ -883,7 +886,7 @@ public async Task<Dictionary<string, List<string>>> SendBatchTakedownRequestAsyn
883
886
}
884
887
885
888
//the existence of the contact email has been validated above
886
- var contacts = platform . ContactPeople
889
+ var contacts = platform ! . ContactPeople
887
890
. Where ( x => x . IsPrimary && x . EmailAddressDsc . IsNotEmpty ( ) && x . EmailMessageType == EmailMessageTypes . BatchTakedownRequest )
888
891
. Select ( x => x . EmailAddressDsc )
889
892
. ToArray ( ) ;
@@ -1067,5 +1070,137 @@ private async Task<Dictionary<string, List<string>>> ValidateBatchTakedownNotice
1067
1070
1068
1071
return errors ;
1069
1072
}
1073
+ public async Task < ( Dictionary < string , List < string > > errors , EmailPreview preview ) > GetComplianceOrdersFromListingPreviewAsync ( ComplianceOrderDto [ ] listings )
1074
+ {
1075
+ var errors = new Dictionary < string , List < string > > ( ) ;
1076
+ var templates = new List < ComplianceOrderFromListing > ( ) ;
1077
+
1078
+ await ProcessComplianceOrderListings ( listings , errors , templates ) ;
1079
+
1080
+ if ( errors . Count > 0 )
1081
+ {
1082
+ return ( errors , new EmailPreview ( ) ) ;
1083
+ }
1084
+
1085
+ var template = templates . FirstOrDefault ( ) ;
1086
+
1087
+ if ( template == null )
1088
+ {
1089
+ errors . AddItem ( "template" , "Wasn't able to create email templates from the selected listings" ) ;
1090
+ return ( errors , new EmailPreview ( ) ) ;
1091
+ }
1092
+
1093
+ template . Preview = true ;
1094
+
1095
+ return ( errors , new EmailPreview ( )
1096
+ {
1097
+ Content = template . GetHtmlPreview ( )
1098
+ } ) ;
1099
+ }
1100
+
1101
+ private async Task ProcessComplianceOrderListings ( ComplianceOrderDto [ ] listings , Dictionary < string , List < string > > errors ,
1102
+ List < ComplianceOrderFromListing > templates )
1103
+ {
1104
+ var emailRegex = RegexDefs . GetRegexInfo ( RegexDefs . Email ) ;
1105
+
1106
+ foreach ( var listing in listings )
1107
+ {
1108
+ var rentalListing = await _listingRepo . GetRentalListing ( listing . RentalListingId , false ) ;
1109
+
1110
+ if ( rentalListing == null ) continue ;
1111
+
1112
+ var template = CreateComplianceOrderTemplate ( listing , rentalListing ) ;
1113
+
1114
+ ValidateEmails ( listing . BccList , emailRegex , "bccList" , errors ) ;
1115
+
1116
+ listing . HostEmails = GetValidHostEmails ( rentalListing . Hosts . ToArray ( ) , emailRegex ) ;
1117
+
1118
+ template . OrgCd = rentalListing . OfferingOrganizationCd ! ;
1119
+ template . RentalListingId = rentalListing . RentalListingId ?? 0 ;
1120
+ template . To = listing . HostEmails ;
1121
+ template . Bcc = listing . BccList ;
1122
+ template . Comment = listing . Comment ;
1123
+ templates . Add ( template ) ;
1124
+ }
1125
+ }
1126
+ public async Task < Dictionary < string , List < string > > > CreateComplianceOrdersFromListingAsync ( ComplianceOrderDto [ ] listings )
1127
+ {
1128
+ var errors = new Dictionary < string , List < string > > ( ) ;
1129
+ var emailRegex = RegexDefs . GetRegexInfo ( RegexDefs . Email ) ;
1130
+ var templates = new List < ComplianceOrderFromListing > ( ) ;
1131
+
1132
+ await ProcessComplianceOrderListings ( listings , errors , templates ) ;
1133
+
1134
+ if ( errors . Count > 0 )
1135
+ {
1136
+ return errors ;
1137
+ }
1138
+
1139
+ await SendComplianceOrderEmailsFromListingAsync ( listings , templates , errors ) ;
1140
+
1141
+ return errors ;
1142
+ }
1143
+
1144
+ private ComplianceOrderFromListing CreateComplianceOrderTemplate ( ComplianceOrderDto listing , RentalListingViewDto rentalListing )
1145
+ {
1146
+ return new ComplianceOrderFromListing ( _emailService )
1147
+ {
1148
+ Url = rentalListing . PlatformListingUrl ?? "" ,
1149
+ ListingId = rentalListing . PlatformListingNo ,
1150
+ Comment = listing . Comment ,
1151
+ Info = $ "{ rentalListing . OfferingOrganizationCd } -{ rentalListing . PlatformListingNo } "
1152
+ } ;
1153
+ }
1154
+
1155
+ private List < string > GetValidHostEmails ( RentalListingContactDto [ ] contacts , RegexInfo emailRegex )
1156
+ {
1157
+ return contacts
1158
+ . Where ( contact => ! string . IsNullOrEmpty ( contact . EmailAddressDsc ) && Regex . IsMatch ( contact . EmailAddressDsc , emailRegex . Regex ) )
1159
+ . Select ( contact => contact . EmailAddressDsc ?? "" )
1160
+ . ToList ( ) ;
1161
+ }
1162
+
1163
+ private async Task SendComplianceOrderEmailsFromListingAsync ( ComplianceOrderDto [ ] listings , List < ComplianceOrderFromListing > templates , Dictionary < string , List < string > > errors )
1164
+ {
1165
+ foreach ( var template in templates )
1166
+ {
1167
+ try
1168
+ {
1169
+ await SendComplianceOrderEmailFromListingAsync ( listings , template ) ;
1170
+ }
1171
+ catch ( Exception ex )
1172
+ {
1173
+ _logger . LogError ( ex . ToString ( ) ) ;
1174
+ errors . AddItem ( $ "{ template . OrgCd } -{ template . ListingId } ", "Failed to send email for the listing." ) ;
1175
+ }
1176
+ }
1177
+ }
1178
+
1179
+ private async Task SendComplianceOrderEmailFromListingAsync ( ComplianceOrderDto [ ] listings , ComplianceOrderFromListing template )
1180
+ {
1181
+ var listing = listings . First ( x => x . RentalListingId == template . RentalListingId ) ;
1182
+
1183
+ var emailEntity = new DssEmailMessage
1184
+ {
1185
+ EmailMessageType = template . EmailMessageType ,
1186
+ MessageDeliveryDtm = DateTime . UtcNow ,
1187
+ MessageTemplateDsc = template . GetContent ( ) ,
1188
+ IsSubmitterCcRequired = true , //todo:
1189
+ UnreportedListingNo = template . ListingId ,
1190
+ HostEmailAddressDsc = listing . HostEmails . FirstOrDefault ( ) ,
1191
+ LgEmailAddressDsc = null ,
1192
+ CcEmailAddressDsc = string . Join ( "; " , template . Bcc ) ,
1193
+ UnreportedListingUrl = template . Url ,
1194
+ InitiatingUserIdentityId = _currentUser . Id ,
1195
+ AffectedByUserIdentityId = null ,
1196
+ ConcernedWithRentalListingId = listing . RentalListingId ,
1197
+ } ;
1198
+
1199
+ await _emailRepo . AddEmailMessage ( emailEntity ) ;
1200
+
1201
+ emailEntity . ExternalMessageNo = await template . SendEmail ( ) ;
1202
+
1203
+ _unitOfWork . Commit ( ) ;
1204
+ }
1070
1205
}
1071
1206
}
0 commit comments