using Serilog; using System; using System.Linq; using System.Threading.Tasks; using webapi.Domain.AggregatesModel.FlatApprovalAggregate; using webapi.Domain.AggregatesModel.UserFlatMappingAggregate; using webapi.Domain.Events; using webapi.Domain.SeedWork; using webapi.Exceptions; using webapi.Infractructure.Repositories; namespace webapi.Infractructure.EventHandlers { //public class AutoAddUserToFlatMappingDomainEventHandler : BaseEventHandler //{ // readonly IUserFlatMappingRepository _repository; // readonly IUserAuthRepository _userAuthRepository; // readonly IFlatRepository _flatRepository; // readonly ILogger _log; // public AutoAddUserToFlatMappingDomainEventHandler(ILogger log, IUserFlatMappingRepository repository, // IUserAuthRepository userAuthRepository, IFlatRepository flatRepository) { // _repository = repository; // _userAuthRepository = userAuthRepository; // _flatRepository = flatRepository; // _log = log; // } // public override async Task Handle(AutoAddUserToFlatMappingDomainEvent request) // { // _log.Information($"request.UserId.HasValue: {request.UserId.HasValue}"); // _log.Information($"request.FlatId.HasValue: {request.FlatId.HasValue}"); // if (request.UserId.HasValue) // { // var user = _userAuthRepository.GetByUserId(request.UserId.Value); // if (user == null) throw new WarningException($"User with userId: {request.UserId} is not found"); // //Get all flats with user info // _log.Information($"GetFlatWithUserInfo: {request.PMCId}"); // var flats2map = _flatRepository.GetFlatWithUserInfo(request.PMCId); // _log.Information($"flats2map: {flats2map.Count()}"); // _log.Information($"user.PINId: {user.PINId}"); // //Get all users from IDP with strong auth and matched PIN // var flats = flats2map.Where(x => x.Users.Any(u => u.PIN == user.PINId)); // //.Select(x => // //new // //{ // // Flat = x, // // PINs = x.Users // // .Where(u => !string.IsNullOrEmpty(u.PIN)) // // .Select(u => u.PIN) // // .Distinct().ToArray() // //}); // _log.Information($"flats: {flats.Count()}"); // foreach (var flat in flats) // { // //foreach (var pin in flatUsers.PINs) // //{ // //flatUsers.Flat.AutoAssignUser(pin); // //await _flatRepository.SaveChanges(flatUsers.Flat, skipEntitySaving: true); // //} // await SaveMapping(user.Id, flat.Id); // } // } // else if(request.FlatId.HasValue){ // var flat = _flatRepository.GetFlatById(request.FlatId.Value); // var users = flat.Users.Where(u => !string.IsNullOrEmpty(u.PIN)); // if (users.Any()) { // foreach (var user in users) // { // var userAuth = _userAuthRepository.GetUserByPIN(user.PIN); // if (userAuth != null) // { // await SaveMapping(userAuth.Id, flat.Id); // } // } // } // } // } // private async Task SaveMapping(Guid userAuthId, Guid flatId) { // _log.Information($"SaveMapping: {userAuthId} {flatId}"); // var u2f = _repository.GetUserFlatsMappings(userAuthId, flatId).FirstOrDefault(); // if (u2f == null) // { // u2f = new UserFlatMapping(flatId, userAuthId, Guid.NewGuid()); // } // u2f.SetAutomatic(); // await _repository.SaveChanges(u2f); // } //} }