22using uSupport . Dtos . Tables ;
33using Umbraco . Cms . Core . Mail ;
44using uSupport . Dtos . Settings ;
5+ using Umbraco . Cms . Core . Events ;
56using Microsoft . AspNetCore . Mvc ;
67using Microsoft . AspNetCore . Http ;
78using Microsoft . Extensions . Hosting ;
89using Microsoft . Extensions . Logging ;
910using Microsoft . Extensions . Options ;
1011using uSupport . Services . Interfaces ;
12+ using Microsoft . AspNetCore . Hosting ;
1113using Umbraco . Cms . Core . Models . Email ;
1214using Microsoft . AspNetCore . Mvc . Razor ;
15+ using uSupport . Notifications . Tickets ;
1316using Microsoft . AspNetCore . Mvc . Rendering ;
1417using Microsoft . AspNetCore . Mvc . ViewEngines ;
1518using Microsoft . AspNetCore . Mvc . Abstractions ;
@@ -21,35 +24,50 @@ namespace uSupport.Services
2124{
2225 public class uSupportSettingsService : IuSupportSettingsService
2326 {
27+ private const string LegacyNewTicketTemplatePath = "/App_Plugins/uSupport/templates/NewTicketEmail.cshtml" ;
28+ private const string LegacyUpdateTicketTemplatePath = "/App_Plugins/uSupport/templates/UpdateTicketEmail.cshtml" ;
29+ private const string DefaultNewTicketTemplatePath = "/Views/uSupport/Emails/NewTicketEmail.cshtml" ;
30+ private const string DefaultUpdateTicketTemplatePath = "/Views/uSupport/Emails/UpdateTicketEmail.cshtml" ;
31+
2432 private readonly uSupportSettingsTicket _defaultSettings ;
33+ private readonly ILogger < IuSupportTicketService > _logger ;
2534 private readonly IEmailSender _emailSender ;
35+ private readonly IEventAggregator _eventAggregator ;
2636 private readonly IRazorViewEngine _razorViewEngine ;
37+ private readonly ITempDataProvider _tempDataProvider ;
38+ private readonly IWebHostEnvironment _webHostEnvironment ;
2739 private readonly IOptions < GlobalSettings > _globalSettings ;
2840 private readonly IHttpContextAccessor _httpContextAccessor ;
41+
2942 private readonly IOptions < uSupportSettings > _uSupportSettings ;
30- private readonly ITempDataProvider _tempDataProvider ;
31- private readonly ILogger < IuSupportTicketService > _logger ;
32- public uSupportSettingsService ( IEmailSender emailSender ,
33- ITempDataProvider tempDataProvider ,
43+
44+
45+ public uSupportSettingsService ( ILogger < IuSupportTicketService > logger ,
46+ IEmailSender emailSender ,
47+ IEventAggregator eventAggregator ,
3448 IRazorViewEngine razorViewEngine ,
35- ILogger < IuSupportTicketService > logger ,
49+ ITempDataProvider tempDataProvider ,
3650 IHostEnvironment hostingEnvironment ,
37- IHttpContextAccessor httpContextAccessor ,
51+ IWebHostEnvironment webHostEnvironment ,
3852 IOptions < GlobalSettings > globalSettings ,
53+ IHttpContextAccessor httpContextAccessor ,
3954 IOptions < uSupportSettings > uSupportSettings )
4055 {
56+ _logger = logger ;
57+ _emailSender = emailSender ;
58+ _eventAggregator = eventAggregator ;
59+ _razorViewEngine = razorViewEngine ;
4160 _tempDataProvider = tempDataProvider ;
61+ _webHostEnvironment = webHostEnvironment ;
4262 _globalSettings = globalSettings ;
43- _uSupportSettings = uSupportSettings ;
4463 _httpContextAccessor = httpContextAccessor ;
45- _razorViewEngine = razorViewEngine ;
46- _emailSender = emailSender ;
47- _logger = logger ;
64+ _uSupportSettings = uSupportSettings ;
4865
4966 _defaultSettings = new uSupportSettingsTicket ( ) ;
5067 }
5168
5269 public bool GetSendEmailOnTicketCreatedSetting ( ) => _uSupportSettings . Value . Tickets . SendEmailOnTicketCreated ;
70+ public bool GetSendEmailOnTicketCommentSetting ( ) => _uSupportSettings . Value . Tickets . SendEmailOnTicketComment ;
5371 public string GetTicketUpdateEmailSetting ( ) => _uSupportSettings . Value . Tickets . TicketUpdateEmail ;
5472
5573 public string GetEmailSubjectNewTicket ( uSupportTicket ? ticket = null )
@@ -128,8 +146,11 @@ public async void SendEmail(string toAddress, string subject, string templateVie
128146 true ) ;
129147 }
130148
149+ if ( model is uSupportTicket ticket )
150+ _eventAggregator . Publish ( new EmailSendingNotification ( ticket . Id , toAddress , subject , templateViewPath ) ) ;
131151
132- await _emailSender . SendAsync ( message , emailType : "Contact" ) ;
152+
153+ await _emailSender . SendAsync ( message , emailType : "Contact" ) ;
133154 }
134155 catch ( Exception ex )
135156 {
@@ -142,15 +163,24 @@ public async Task<string> RenderEmailTemplateAsync(string templateViewPath, obje
142163 if ( string . IsNullOrEmpty ( templateViewPath ) )
143164 throw new Exception ( "Failed to find email template." ) ;
144165
145- if ( ! templateViewPath . EndsWith ( ".cshtml" ) )
166+ if ( ! templateViewPath . EndsWith ( ".cshtml" , StringComparison . OrdinalIgnoreCase ) )
146167 throw new Exception ( "Template file must end with '.cshtml'" ) ;
147168
148- ActionContext actionContext = new ( _httpContextAccessor . HttpContext , new Microsoft . AspNetCore . Routing . RouteData ( ) , new ActionDescriptor ( ) ) ;
169+ if ( _httpContextAccessor . HttpContext == null )
170+ throw new Exception ( "HttpContext is null" ) ;
171+
172+ ActionContext actionContext = new ( _httpContextAccessor . HttpContext , new Microsoft . AspNetCore . Routing . RouteData ( ) , new ActionDescriptor ( ) ) ;
149173 using ( StringWriter stringWriter = new ( ) )
150174 {
151- ViewEngineResult viewResult = _razorViewEngine . GetView ( templateViewPath , templateViewPath , false ) ;
175+ ViewEngineResult viewResult = _razorViewEngine . GetView ( null , templateViewPath , false ) ;
176+
177+ if ( ! viewResult . Success )
178+ {
179+ var searchedLocations = string . Join ( ", " , viewResult . SearchedLocations ?? [ ] ) ;
180+ throw new Exception ( $ "Failed to find template at '{ templateViewPath } '. Searched locations: { searchedLocations } ") ;
181+ }
152182
153- ViewDataDictionary viewData = new ( new EmptyModelMetadataProvider ( ) , new ModelStateDictionary ( ) )
183+ ViewDataDictionary viewData = new ( new EmptyModelMetadataProvider ( ) , new ModelStateDictionary ( ) )
154184 {
155185 Model = model
156186 } ;
@@ -166,5 +196,5 @@ public async Task<string> RenderEmailTemplateAsync(string templateViewPath, obje
166196 return stringWriter . ToString ( ) ;
167197 }
168198 }
169- }
170- }
199+ }
200+ }
0 commit comments