using Autofac; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using Refit; using Serilog; using System; using System.Net.Http; using System.Threading.Tasks; using System.Web; using Taloyhtio.IDP.SPApi.Model; using webapi.Application.Services; using webapi.Domain.AggregatesModel.FlatAggregate; using webapi.Domain.AggregatesModel.FlatMappingAggregate; using webapi.Domain.Events; using webapi.Infractructure.Repositories; using webapi.Infractructure.Services; namespace webapi.core { public class FlatManager: IFlatManager { readonly IFlatRepository _repository; readonly ICondoMappingRepository _condoMappingRepository; readonly ISPService _spService; readonly ISPHandlerService _spHandlerService; readonly ILogger _log; //readonly IUserFlatMappingRepository _userFlatMappingRepository; //readonly IUserAuthRepository _userAuthRepository; public FlatManager(ILogger log, string connectionString, string spAPIUrl, ILifetimeScope container) : this(log, new SPService(log, contextFactory: null), RestService.For( new HttpClient(new HttpClientHandler { UseDefaultCredentials = true }) { BaseAddress = new Uri(spAPIUrl) }, new RefitSettings { ContentSerializer = new NewtonsoftJsonContentSerializer( new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }) }), new FlatRepository(log, connectionString, new EventsHandlerFactory(container)), //new UserFlatMappingRepository(log, connectionString, new Domain.Events.EventsHandlerFactory(container)), //new UserAuthRepository(log, connectionString, eventsFactory: null), new CondoMappingRepository(log, connectionString)) {} public FlatManager(ILogger log, ISPService spService, ISPHandlerService spHandlerService, IFlatRepository repository, //IUserFlatMappingRepository userFlatMappingRepository, //IUserAuthRepository userAuthRepository, ICondoMappingRepository condoMappingRepository) { _repository = repository; _spService = spService; _spHandlerService = spHandlerService; _condoMappingRepository = condoMappingRepository; //_userFlatMappingRepository = userFlatMappingRepository; //_userAuthRepository = userAuthRepository; _log = log; } public async Task ImportFlat(string flatTitle, string pmsCondoName, string taloyhtioCondoUrl) { _log.Information($"Get taloyhtioPMCId and taloyhtioPMCCondoId from condo url: \"{taloyhtioCondoUrl}\""); var response = await _spHandlerService.GetIdsByCondoUrl(new GetIdsByCondoUrlRequest() { TaloyhtioCondoUrl = taloyhtioCondoUrl }); response.TryGetValue("PMCId", out var taloyhtioPMCId); response.TryGetValue("CondoId", out var taloyhtioCondoId); _log.Information($"Add flat if needed"); var existedFlat = _repository.GetFlat(taloyhtioPMCId, pmsCondoName, flatTitle); if (existedFlat != null) { // _log.Information($"Revoke User Assignment for existed flat {flatTitle}"); // //existedFlat.FlatUsers = ""; // //var handler = new RevokeUserAssignmentDomainEventHandler(_log, _userFlatMappingRepository, _userAuthRepository); // //await handler.Handle(new Domain.Events.RevokeUserAssignmentDomainEvent(existedFlat)); // existedFlat.RevokeUserAssignment(); // await _repository.SaveChanges(existedFlat); } else { _log.Information($"Add condo mapping if needed"); await AddCondoMapping(pmsCondoName, taloyhtioPMCId, taloyhtioCondoId); _log.Information($"Add flat"); var flat = await AddFlat(flatTitle, taloyhtioPMCId, null, pmsCondoName); _log.Information($"Add flat folders in SP: \"{JsonConvert.SerializeObject(flat)}\""); await _spHandlerService.CreateFlatFolders(new CreateFlatFoldersRequest() { TaloyhtioCondoUrl = taloyhtioCondoUrl, FlatTitle = flat.FlatTitle, TaloyhtioPMCId = taloyhtioPMCId, TaloyhtioCondoId= taloyhtioCondoId }); } } public async Task AddFlat(string flatTitle, Guid taloyhtioPMCId, Guid? taloyhtioCondoId, string pmsCondoName, Guid? userId = null) { _log.Information($"Add flat"); var flat = _repository.GetFlat(taloyhtioPMCId, pmsCondoName, flatTitle); if (flat == null) { var flatId = Guid.NewGuid(); flat = new Flat(taloyhtioPMCId, pmsCondoName, flatTitle, flatId); _log.Information($"Create the condo mapping record: \"{JsonConvert.SerializeObject(flat)}\""); if (userId.HasValue && !Guid.Empty.Equals(userId)) { flat.AssignUser(userId.Value); //flat.SendApproval(userId.Value); } flat = await _repository.SaveChanges(flat); await _spHandlerService.CreateFlatFolders(new CreateFlatFoldersRequest() { FlatTitle = flat.FlatTitle, TaloyhtioPMCId=taloyhtioPMCId, TaloyhtioCondoId = taloyhtioCondoId ?? Guid.Empty }); } return flat; } public async Task AddCondoMapping(string pmsCondoName, Guid taloyhtioPMCId, Guid taloyhtioCondoId) { _log.Information($"Add condo mapping"); var mapping = _condoMappingRepository.GetMapping(taloyhtioPMCId, taloyhtioCondoId, pmsCondoName); if (mapping == null) { var mappingId = Guid.NewGuid(); mapping = new CondoMapping(taloyhtioPMCId, taloyhtioCondoId, pmsCondoName, mappingId); _log.Information($"Create the condo mapping record: \"{JsonConvert.SerializeObject(mapping)}\""); return await _condoMappingRepository.SaveChanges(mapping); } return mapping; } } }