Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
feat(back-end): Notify the customer when their service request is pro…
Browse files Browse the repository at this point in the history
…cessed
  • Loading branch information
CarlosPavajeau committed Mar 11, 2021
1 parent fb53441 commit 4fd776a
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Domain/Events/UpdatedServiceRequest.cs
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; }
}
}
1 change: 1 addition & 0 deletions Kaizen/Controllers/ServiceRequestsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public async Task<ActionResult<ServiceRequestViewModel>> PutServiceRequest(int i
}

_mapper.Map(serviceRequestModel, serviceRequest);
serviceRequest.PublishEvent(new UpdatedServiceRequest(serviceRequest));
_serviceRequestsRepository.Update(serviceRequest);

try
Expand Down
99 changes: 99 additions & 0 deletions Kaizen/DomainEvents/Handlers/OnUpdatedServiceRequest.cs
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);
}
}
}
}

0 comments on commit 4fd776a

Please sign in to comment.