This repository has been archived by the owner on Jun 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(back-end): Notify the customer when their service request is pro…
…cessed
- Loading branch information
1 parent
fb53441
commit 4fd776a
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Kaizen.Core.Domain; | ||
using Kaizen.Domain.Entities; | ||
|
||
namespace Kaizen.Domain.Events | ||
{ | ||
public class UpdatedServiceRequest : IDomainEvent | ||
{ | ||
public UpdatedServiceRequest(ServiceRequest serviceRequest) | ||
{ | ||
ServiceRequest = serviceRequest; | ||
} | ||
|
||
public ServiceRequest ServiceRequest { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AutoMapper; | ||
using Kaizen.Domain.Entities; | ||
using Kaizen.Domain.Events; | ||
using Kaizen.Domain.Repositories; | ||
using Kaizen.Hubs; | ||
using Kaizen.Models.Notification; | ||
using MediatR; | ||
using Microsoft.AspNetCore.SignalR; | ||
|
||
namespace Kaizen.DomainEvents.Handlers | ||
{ | ||
public class OnUpdatedServiceRequest | ||
{ | ||
public class Handler : INotificationHandler<DomainEventNotification<UpdatedServiceRequest>> | ||
{ | ||
private readonly IHubContext<NotificationHub> _notificationsHub; | ||
private readonly IMapper _mapper; | ||
private readonly IClientsRepository _clientsRepository; | ||
private readonly INotificationsRepository _notificationsRepository; | ||
private readonly IUnitWork _unitWork; | ||
|
||
public Handler(IHubContext<NotificationHub> notificationsHub, IClientsRepository clientsRepository, | ||
INotificationsRepository notificationsRepository, IUnitWork unitWork, IMapper mapper) | ||
{ | ||
_notificationsHub = notificationsHub; | ||
_clientsRepository = clientsRepository; | ||
_notificationsRepository = notificationsRepository; | ||
_unitWork = unitWork; | ||
_mapper = mapper; | ||
} | ||
|
||
public async Task Handle(DomainEventNotification<UpdatedServiceRequest> notification, | ||
CancellationToken cancellationToken) | ||
{ | ||
var serviceRequest = notification.DomainEvent.ServiceRequest; | ||
if (serviceRequest.State == ServiceRequestState.Pending) | ||
{ | ||
return; | ||
} | ||
|
||
var client = | ||
await _clientsRepository.GetClientWithUser(serviceRequest.ClientId); | ||
|
||
var clientNotification = await SaveServiceRequestNotification(serviceRequest.State, client.User); | ||
if (clientNotification is null) | ||
{ | ||
return; | ||
} | ||
|
||
await SendServiceRequestNotification(clientNotification, client.User.UserName, cancellationToken); | ||
} | ||
|
||
private async Task<Notification> SaveServiceRequestNotification(ServiceRequestState serviceRequestState, | ||
ApplicationUser user) | ||
{ | ||
var message = serviceRequestState switch | ||
{ | ||
ServiceRequestState.Accepted => | ||
"Hemos aceptado tu solicitud de servicio y agendamos las actividades a aplicar. " + | ||
"Consulta tu calendario.", | ||
ServiceRequestState.Rejected => "Desafortunadamente hemos rechazado tu solicitud de servicio.", | ||
ServiceRequestState.PendingSuggestedDate => | ||
"Te hemos sugerido una nueva fecha de aplicación de nuestros servicios." + | ||
"Puedes aceptarla o sugerirnos otra.", | ||
_ => string.Empty | ||
}; | ||
|
||
if (string.IsNullOrEmpty(message)) | ||
{ | ||
return null; | ||
} | ||
|
||
Notification notification = new Notification | ||
{ | ||
Title = "Respuesta de solicitud de servicio", | ||
Message = message, | ||
Icon = "question_answer", | ||
State = NotificationState.Pending, | ||
UserId = user.Id | ||
}; | ||
|
||
_notificationsRepository.Insert(notification); | ||
await _unitWork.SaveAsync(); | ||
|
||
return notification; | ||
} | ||
|
||
private async Task SendServiceRequestNotification(Notification clientNotification, string username, | ||
CancellationToken cancellationToken) | ||
{ | ||
var notificationViewModel = _mapper.Map<NotificationViewModel>(clientNotification); | ||
await _notificationsHub.Clients.User(username).SendAsync("OnNewNotification", | ||
notificationViewModel, cancellationToken); | ||
} | ||
} | ||
} | ||
} |