Skip to content

Commit 436e622

Browse files
committed
Improved ticket history and fixed default send email
1 parent 97ff42c commit 436e622

16 files changed

Lines changed: 215 additions & 89 deletions
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
using uSupport.Notifications;
22
using Umbraco.Cms.Core.Composing;
3-
using Umbraco.Cms.Core.DependencyInjection;
3+
using uSupport.Notifications.Tickets;
44
using uSupport.Notifications.Handlers;
5+
using Umbraco.Cms.Core.DependencyInjection;
56

67
namespace uSupport.Composers
78
{
89
public class uSupportNotificationComposer : IComposer
910
{
1011
public void Compose(IUmbracoBuilder builder)
1112
{
12-
builder.AddNotificationHandler<TicketHistoryNotification, TicketHistoryNotificationHandler>();
13+
builder.AddNotificationHandler<TicketHistoryNotification, TicketHistoryNotificationHandler>()
14+
.AddNotificationHandler<CreateTicketNotification, TicketHistoryNotificationHandler>()
15+
.AddNotificationHandler<EmailSendingNotification, TicketHistoryNotificationHandler>()
16+
.AddNotificationHandler<AddTicketCommentNotification, TicketHistoryNotificationHandler>();
1317
}
1418
}
1519
}

‎src/uSupport/Controllers/uSupportTicketCommentAuthorizedApiController.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ public ActionResult<IEnumerable<uSupportTicketComment>> Comment(uSupportTicketCo
7878

7979
_uSupportSettingsService.SendEmail(
8080
toAddress,
81-
_uSupportSettingsService.GetEmailSubjectUpdateTicket(),
81+
_uSupportSettingsService.GetEmailSubjectUpdateTicket(updatedTicket),
8282
_uSupportSettingsService.GetEmailTemplateUpdateTicketPath(),
83-
ticket);
83+
updatedTicket);
8484
}
8585

8686
_eventAggregator.Publish(new AddTicketCommentNotification(updatedTicket, comment));

‎src/uSupport/Dtos/Settings/uSupportSettingsTicket.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ public class uSupportSettingsTicket
66
{
77
public const string Tickets = uSupportConstants.SectionAlias + ":Settings:Tickets";
88
public bool SendEmailOnTicketCreated { get; set; } = true;
9+
public bool SendEmailOnTicketComment { get; set; } = true;
910
public string TicketUpdateEmail { get; set; } = "None";
1011
public string EmailSubjectNewTicket { get; set; } = "A new ticket '{ExternalTicketId}' has been created";
1112
public string EmailSubjectUpdateTicket { get; set; } = "Ticket '{ExternalTicketId}' has been updated";
12-
public string EmailTemplateNewTicketPath { get; set; } = "/App_Plugins/uSupport/templates/NewTicketEmail.cshtml";
13-
public string EmailTemplateUpdateTicketPath { get; set; } = "/App_Plugins/uSupport/templates/UpdateTicketEmail.cshtml";
13+
public string EmailTemplateNewTicketPath { get; set; } = "/Views/Partials/uSupport/Emails/NewTicketEmail.cshtml";
14+
public string EmailTemplateUpdateTicketPath { get; set; } = "/Views/Partials/uSupport/Emails/UpdateTicketEmail.cshtml";
1415
}
15-
}
16+
}

‎src/uSupport/Helpers/uSupportPageHelper.cs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
using System;
2-
using uSupport.Dtos;
3-
using uSupport.Dtos.Tables;
4-
using System.Collections.Generic;
1+
using uSupport.Dtos;
52

63
namespace uSupport.Helpers
74
{
85
public static class uSupportPageHelper
96
{
10-
public static uSupportPage<uSupportTicket> MapPageToUSupportPage(List<uSupportTicket> items, long totalItems, long currentPage, long itemsPerPage)
7+
public static uSupportPage<T> MapPageToUSupportPage<T>(List<T> items, long totalItems, long currentPage, long itemsPerPage)
118
{
12-
uSupportPage<uSupportTicket> page = new uSupportPage<uSupportTicket>()
9+
uSupportPage<T> page = new uSupportPage<T>()
1310
{
1411
TotalItems = totalItems,
1512
ItemsPerPage = itemsPerPage,

‎src/uSupport/Notifications/Handlers/TicketHistoryNotificationHandler.cs‎

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
using Umbraco.Cms.Core.Events;
44
using uSupport.Migrations.Schemas;
55
using uSupport.Services.Interfaces;
6+
using uSupport.Notifications.Tickets;
67

78
namespace uSupport.Notifications.Handlers
89
{
9-
public class TicketHistoryNotificationHandler : INotificationHandler<TicketHistoryNotification>
10+
public class TicketHistoryNotificationHandler : INotificationHandler<TicketHistoryNotification>,
11+
INotificationHandler<CreateTicketNotification>,
12+
INotificationHandler<EmailSendingNotification>,
13+
INotificationHandler<AddTicketCommentNotification>
1014
{
1115
private readonly IuSupportTicketHistoryService _uSupportTicketHistoryService;
1216
public TicketHistoryNotificationHandler(IuSupportTicketHistoryService uSupportTicketHistoryService)
@@ -20,14 +24,9 @@ public void Handle(TicketHistoryNotification notification)
2024
{
2125
TicketId = notification.NewTicket.Id,
2226
UserId = notification.UserId,
23-
ActionType = notification.OldTicket == null ? "Created" : "Updated",
27+
ActionType = "Updated",
2428
};
2529

26-
if (notification.OldTicket == null)
27-
{
28-
_uSupportTicketHistoryService.Create(history);
29-
return;
30-
}
3130

3231
var changes = new List<uSupportChange>();
3332

@@ -71,5 +70,50 @@ public void Handle(TicketHistoryNotification notification)
7170

7271
_uSupportTicketHistoryService.Create(history);
7372
}
73+
74+
public void Handle(CreateTicketNotification notification)
75+
{
76+
_uSupportTicketHistoryService.Create(new uSupportTicketHistorySchema()
77+
{
78+
TicketId = notification.Ticket.Id,
79+
UserId = notification.Ticket.AuthorId,
80+
ActionType = "Created",
81+
});
82+
}
83+
84+
public void Handle(EmailSendingNotification notification)
85+
{
86+
_uSupportTicketHistoryService.Create(new uSupportTicketHistorySchema()
87+
{
88+
TicketId = notification.TicketId,
89+
UserId = 0,
90+
ActionType = "SentEmail",
91+
ChangesJson = JsonSerializer.Serialize(new List<uSupportChange>()
92+
{
93+
new uSupportChange()
94+
{
95+
Field = "Email",
96+
New = notification.ToAddress
97+
}
98+
})
99+
});
100+
}
101+
102+
public void Handle(AddTicketCommentNotification notification)
103+
{
104+
_uSupportTicketHistoryService.Create(new uSupportTicketHistorySchema()
105+
{
106+
TicketId = notification.Ticket.Id,
107+
UserId = notification.Comment.UserId,
108+
ActionType = "Comment",
109+
ChangesJson = JsonSerializer.Serialize(new List<uSupportChange>()
110+
{
111+
new uSupportChange()
112+
{
113+
Field = "Comment"
114+
}
115+
})
116+
});
117+
}
74118
}
75119
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Umbraco.Cms.Core.Notifications;
2+
3+
namespace uSupport.Notifications.Tickets
4+
{
5+
public class EmailSendingNotification : INotification
6+
{
7+
public Guid TicketId { get; }
8+
public string ToAddress { get; }
9+
public string Subject { get; }
10+
public string TemplatePath { get; }
11+
12+
public EmailSendingNotification(Guid ticketId, string toAddress, string subject, string templatePath)
13+
{
14+
TicketId = ticketId;
15+
ToAddress = toAddress;
16+
Subject = subject;
17+
TemplatePath = templatePath;
18+
}
19+
}
20+
}

‎src/uSupport/Services/uSupportSettingsService.cs‎

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
using uSupport.Dtos.Tables;
33
using Umbraco.Cms.Core.Mail;
44
using uSupport.Dtos.Settings;
5+
using Umbraco.Cms.Core.Events;
56
using Microsoft.AspNetCore.Mvc;
67
using Microsoft.AspNetCore.Http;
78
using Microsoft.Extensions.Hosting;
89
using Microsoft.Extensions.Logging;
910
using Microsoft.Extensions.Options;
1011
using uSupport.Services.Interfaces;
12+
using Microsoft.AspNetCore.Hosting;
1113
using Umbraco.Cms.Core.Models.Email;
1214
using Microsoft.AspNetCore.Mvc.Razor;
15+
using uSupport.Notifications.Tickets;
1316
using Microsoft.AspNetCore.Mvc.Rendering;
1417
using Microsoft.AspNetCore.Mvc.ViewEngines;
1518
using 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+
}

‎src/uSupport/Services/uSupportTicketService.cs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public uSupportPage<uSupportTicket> GetPagedActiveTickets(long page, string? sea
8282
}
8383
else
8484
{
85-
sql.OrderBy("LastUpdated DESC");
85+
sql.OrderBy("LastUpdated DESC");
8686
}
8787

8888
var ticketCount = context.Scope.Database.Fetch<int>(sqlCount).FirstOrDefault();
@@ -188,7 +188,6 @@ public override uSupportTicket Create(uSupportTicketSchema dto)
188188
var ticket = base.Create(dto);
189189

190190
_eventAggregator.Publish(new CreateTicketNotification(ticket));
191-
_eventAggregator.Publish(new TicketHistoryNotification(dto.AuthorId, null, ticket));
192191

193192
return ticket;
194193
}

src/uSupport/wwwroot/templates/NewTicketEmail.cshtml renamed to src/uSupport/Views/Partials/uSupport/Emails/NewTicketEmail.cshtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@using uSupport.Extensions
1+
@using uSupport.Extensions
22
@inherits uSupportTemplate<uSupport.Dtos.Tables.uSupportTicket>
33
@{
44
var baseUrl = GetBaseUrl(Context.Request);
@@ -45,7 +45,7 @@
4545
<hr style="border: 0; height: 1px; background: #ebebe9; margin: 20px 0;" />
4646

4747
<h4 style="margin-bottom: 5px;">Summary</h4>
48-
<p style="margin-top: 0; font-size: 15px; line-height: 28px;">@Model.Summary</p>
48+
<p style="margin-top: 0; font-size: 15px; line-height: 28px;">@Html.Raw(Model.Summary)</p>
4949

5050
<hr style="border: 0; height: 1px; background: #ebebe9; margin: 20px 0;" />
5151

@@ -56,4 +56,4 @@
5656
</td>
5757
</tr>
5858
</table>
59-
</div>
59+
</div>

0 commit comments

Comments
 (0)