Taylohtio/IDP/webapi/webapi.Infrastructure/EventHandlers/UserMappingApproveDomainEve...

96 lines
4.0 KiB
C#

using Microsoft.Azure.Documents;
using Serilog;
using Serilog.Formatting.Json;
using System;
using System.Linq;
using System.Threading.Tasks;
using webapi.Application.Services;
using webapi.Domain.AggregatesModel.FlatApprovalAggregate;
using webapi.Domain.AggregatesModel.UsersFlatApprovalRequestAggregate;
using webapi.Domain.Events;
using webapi.Domain.SeedWork;
using webapi.Infractructure.Core;
using webapi.Infractructure.Repositories;
using webapi.Infrastructure.Services;
namespace webapi.Infractructure.EventHandlers
{
public class UserMappingApproveDomainEventHandler : BaseEventHandler<UserMappingApproveDomainEvent>
{
readonly IUsersFlatApprovalRepository _repository;
readonly IUserFlatMappingRepository _userFlatMappingRepository;
readonly IEmailService _emailService;
readonly IFlatRepository _flatRepository;
readonly IUserAuthRepository _userAuthRepository;
readonly ISPHandlerService _spHandlerService;
readonly ILogger _log;
public UserMappingApproveDomainEventHandler(
ILogger log,
IUsersFlatApprovalRepository repository,
IUserFlatMappingRepository userFlatMappingRepository,
IEmailService emailService,
IFlatRepository flatRepository,
IUserAuthRepository userAuthRepository,
ISPHandlerService spHandlerService)
{
_repository = repository;
_userFlatMappingRepository = userFlatMappingRepository;
_emailService = emailService;
_flatRepository = flatRepository;
_userAuthRepository = userAuthRepository;
_spHandlerService = spHandlerService;
_log = log;
}
public override async Task Handle(UserMappingApproveDomainEvent request)
{
_log.Information($"UserMappingApproveDomainEvent => PMCId: {request.PMCId}, UserId: {request.UserId}, FlatId: {request.FlatId}");
var mapping = _userFlatMappingRepository.GetUserFlatsMappings(request.UserId, request.FlatId)?.FirstOrDefault();
if (mapping == null || mapping.IsAutomatic) return;
var approval = _repository.GetApproval(request.UserId, request.FlatId);
if (approval == null)
{
approval = new UsersFlatApprovalRequest(request.UserId, request.FlatId, Guid.NewGuid());
approval.SetStatus(ApprovalStatus.Pending);
await _repository.SaveChanges(approval);
try
{
//Send notification
_log.Information($"GetApprovers => PMCId: {request.PMCId}");
var approvers = await _spHandlerService.GetApprovers(request.PMCId)
.ConfigureAwait(false);
_log.Information($"GetApprovers => Result approvers count: {approvers?.Count()}");
var user = _userAuthRepository.GetUserByTaloyhtioUserId(request.UserId);
var flat = _flatRepository.GetFlatById(request.FlatId);
_log.Information($"SendUserFlatApprovalEmail => pmcId: {request.PMCId}, DisplayName: {user?.DisplayName}, FlatTitle: {flat?.FlatTitle}");
await _emailService.SendUserFlatApprovalEmail(request.PMCId, approvers, user?.DisplayName, flat?.FlatTitle);
}
catch (Exception ex) {
_log.Error(ex, $"SendUserFlatApprovalEmail error: {ex.Message} {ex.StackTrace} {ex.InnerException}");
}
}
}
}
//public class UserMappingApproveDomainEventHandler : IEventHandler<CondoMapping, UserMappingApproveDomainEvent>
//{
// readonly ICondoMappingRepository _repository;
// public UserMappingApproveDomainEventHandler(ICondoMappingRepository repository)
// {
// _repository = repository;
// }
// public async Task<CondoMapping> Process()
// {
// return await _repository.SaveChanges(new CondoMapping());
// }
//}
}