/// <summary> /// Legal unit creation method /// </summary> /// <param name="data">Data</param> /// <param name="userId">User Id</param> /// <returns></returns> public async Task <Dictionary <string, string[]> > CreateLegalUnit(LegalUnitCreateM data, string userId) => await CreateUnitContext <LegalUnit, LegalUnitCreateM>(data, userId, unit => { var helper = new StatUnitCheckPermissionsHelper(_dbContext); helper.CheckRegionOrActivityContains(userId, data.Address?.RegionId, data.ActualAddress?.RegionId, data.PostalAddress?.RegionId, data.Activities.Select(x => x.ActivityCategoryId).ToList()); if (Common.HasAccess <LegalUnit>(data.DataAccess, v => v.LocalUnits)) { if (data.LocalUnits == null) { return(Task.CompletedTask); } var localUnits = _dbContext.LocalUnits.Where(x => data.LocalUnits.Contains(x.RegId)); foreach (var localUnit in localUnits) { unit.LocalUnits.Add(localUnit); } if (data.LocalUnits != null) { unit.HistoryLocalUnitIds = string.Join(",", data.LocalUnits); } } return(Task.CompletedTask); });
/// <summary> /// Delete / Restore stat. units /// </summary> /// <param name="unitType">Type of stat. units</param> /// <param name="id">Id stat. units</param> /// <param name="toDelete">Remoteness flag</param> /// <param name="userId">User ID</param> public void DeleteUndelete(StatUnitTypes unitType, int id, bool toDelete, string userId) { if (_dataAccessService.CheckWritePermissions(userId, unitType)) { throw new UnauthorizedAccessException(); } var item = _commonSvc.GetStatisticalUnitByIdAndType(id, unitType, true).Result; bool isEmployee = _userService.IsInRoleAsync(userId, DefaultRoleNames.Employee).Result; var mappedItem = Mapper.Map <IStatisticalUnit, ElasticStatUnit>(item); if (isEmployee) { var helper = new StatUnitCheckPermissionsHelper(_dbContext); helper.CheckRegionOrActivityContains(userId, mappedItem.RegionIds, mappedItem.ActivityCategoryIds); } if (item.IsDeleted == toDelete) { _elasticService.EditDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(item)).Wait(); } else { CheckBeforeDelete(item, toDelete); var deletedUnit = _deleteUndeleteActions[unitType](id, toDelete, userId); _postDeleteActions[unitType](deletedUnit, toDelete, userId); _elasticService.EditDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(deletedUnit)).Wait(); } }
/// <summary> /// Local unit creation method /// </summary> /// <param name="data">Data</param> /// <param name="userId">User Id</param> /// <returns></returns> public async Task <Dictionary <string, string[]> > CreateLocalUnit(LocalUnitCreateM data, string userId) { var helper = new StatUnitCheckPermissionsHelper(_dbContext); helper.CheckRegionOrActivityContains(userId, data.Address?.RegionId, data.ActualAddress?.RegionId, data.PostalAddress?.RegionId, data.Activities.Select(x => x.ActivityCategoryId).ToList()); return(await CreateUnitContext <LocalUnit, LocalUnitCreateM>(data, userId, null)); }
/// <summary> /// Stat search method. units /// </summary> /// <param name = "filter"> Request </param> /// <param name = "userId"> User Id </param> /// <param name = "isDeleted"> Distance flag </param> /// <returns> </returns> public async Task <SearchVm> Search(SearchQueryM filter, string userId, bool isDeleted = false) { await _elasticService.CheckElasticSearchConnection(); bool isAdmin = await _userService.IsInRoleAsync(userId, DefaultRoleNames.Administrator); long totalCount; List <ElasticStatUnit> units; if (filter.IsEmpty()) { var baseQuery = _dbContext.StatUnitSearchView .Where(s => s.IsDeleted == isDeleted && s.LiqDate == null); totalCount = baseQuery.Count(); units = (await baseQuery.Skip((filter.Page - 1) * filter.PageSize).Take(filter.PageSize).ToListAsync()) .Select(Mapper.Map <StatUnitSearchView, ElasticStatUnit>).ToList(); } else { var searchResponse = await _elasticService.Search(filter, userId, isDeleted); totalCount = searchResponse.TotalCount; units = searchResponse.Result.ToList(); } var finalIds = units.Where(x => x.UnitType != StatUnitTypes.EnterpriseGroup) .Select(x => x.RegId).ToList(); var finalRegionIds = units.Select(x => x.RegionId).ToList(); var unitsToPersonNames = await GetUnitsToPersonNamesByUnitIds(finalIds); var unitsToMainActivities = await GetUnitsToPrimaryActivities(finalIds); var regions = await GetRegionsFullPaths(finalRegionIds); var permissions = await _userService.GetDataAccessAttributes(userId, null); var helper = new StatUnitCheckPermissionsHelper(_dbContext); var result = units .Select(x => new SearchViewAdapterModel(x, unitsToPersonNames[x.RegId], unitsToMainActivities[x.RegId], regions.GetValueOrDefault(x.RegionId))) .Select(x => SearchItemVm.Create(x, x.UnitType, permissions.GetReadablePropNames(), !isAdmin && !helper.IsRegionOrActivityContains(userId, x.RegionId != null ? new List <int> { (int)x.RegionId } : new List <int>(), x.ActivityCategoryIds))); return(SearchVm.Create(result, totalCount)); }
/// <summary> /// Enterprise creation method /// </summary> /// <param name="data">Data</param> /// <param name="userId">User Id</param> /// <returns></returns> public async Task <Dictionary <string, string[]> > CreateEnterpriseUnit(EnterpriseUnitCreateM data, string userId) => await CreateUnitContext <EnterpriseUnit, EnterpriseUnitCreateM>(data, userId, unit => { var helper = new StatUnitCheckPermissionsHelper(_dbContext); helper.CheckRegionOrActivityContains(userId, data.Address?.RegionId, data.ActualAddress?.RegionId, data.PostalAddress?.RegionId, data.Activities.Select(x => x.ActivityCategoryId).ToList()); var legalUnits = _dbContext.LegalUnits.Where(x => data.LegalUnits.Contains(x.RegId)).ToList(); foreach (var legalUnit in legalUnits) { unit.LegalUnits.Add(legalUnit); } if (data.LegalUnits != null) { unit.HistoryLegalUnitIds = string.Join(",", data.LegalUnits); } return(Task.CompletedTask); });
/// <summary> /// Method for creating an enterprise group /// </summary> /// <param name="data">Data</param> /// <param name="userId">User Id</param> /// <returns></returns> public async Task <Dictionary <string, string[]> > CreateEnterpriseGroup(EnterpriseGroupCreateM data, string userId) => await CreateContext <EnterpriseGroup, EnterpriseGroupCreateM>(data, userId, unit => { var helper = new StatUnitCheckPermissionsHelper(_dbContext); helper.CheckRegionOrActivityContains(userId, data.Address?.RegionId, data.ActualAddress?.RegionId, data.PostalAddress?.RegionId, new List <int>()); if (Common.HasAccess <EnterpriseGroup>(data.DataAccess, v => v.EnterpriseUnits)) { var enterprises = _dbContext.EnterpriseUnits.Where(x => data.EnterpriseUnits.Contains(x.RegId)) .ToList(); foreach (var enterprise in enterprises) { unit.EnterpriseUnits.Add(enterprise); } if (data.EnterpriseUnits != null) { unit.HistoryEnterpriseUnitIds = string.Join(",", data.EnterpriseUnits); } } return(Task.CompletedTask); });
/// <summary> /// Method to get view model /// </summary> /// <param name = "id"> Id stat. units </param> /// <param name = "type"> Type of stat. units </param> /// <param name = "userId"> User Id </param> /// <param name = "ignoredActions"> </param> /// <returns> </returns> public async Task <StatUnitViewModel> GetViewModel(int?id, StatUnitTypes type, string userId, ActionsEnum ignoredActions) { bool isEmployee = await _userService.IsInRoleAsync(userId, DefaultRoleNames.Employee); var item = id.HasValue ? await _commonSvc.GetStatisticalUnitByIdAndType(id.Value, type, false) : GetDefaultDomainForType(type); if (item == null) { throw new BadRequestException(nameof(Resource.NotFoundMessage)); } if ((ignoredActions == ActionsEnum.Edit) && isEmployee) { var helper = new StatUnitCheckPermissionsHelper(_context); var mappedItem = new ElasticStatUnit(); Mapper.Map(item, mappedItem); helper.CheckRegionOrActivityContains(userId, mappedItem.RegionIds, mappedItem.ActivityCategoryIds); } var dataAccess = await _userService.GetDataAccessAttributes(userId, item.UnitType); var config = type == StatUnitTypes.EnterpriseGroup ? _mandatoryFields.EnterpriseGroup : (object)_mandatoryFields.StatUnit; var mandatoryDict = config.GetType().GetProperties().ToDictionary(x => x.Name, GetValueFrom(config)); if (type != StatUnitTypes.EnterpriseGroup) { object subConfig; switch (type) { case StatUnitTypes.LocalUnit: subConfig = _mandatoryFields.LocalUnit; break; case StatUnitTypes.LegalUnit: subConfig = _mandatoryFields.LegalUnit; break; case StatUnitTypes.EnterpriseUnit: subConfig = _mandatoryFields.Enterprise; break; default: throw new Exception("bad statunit type"); } var getValue = GetValueFrom(subConfig); subConfig.GetType().GetProperties().ForEach(x => mandatoryDict[x.Name] = getValue(x)); } //The LegalUnits field of the EnterpriseUnit type is required by business logic if (type == StatUnitTypes.EnterpriseUnit && mandatoryDict.ContainsKey("LegalUnits")) { mandatoryDict["LegalUnits"] = true; } return(StatUnitViewModelCreator.Create(item, dataAccess, mandatoryDict, ignoredActions)); Func <PropertyInfo, bool> GetValueFrom(object source) => prop => { var value = prop.GetValue(source); return(value is bool b && b); }; }