diff --git a/AsbCloudApp/Services/IWellOperationsStatService.cs b/AsbCloudApp/Services/IWellOperationsStatService.cs index a047f68d..dc71fb33 100644 --- a/AsbCloudApp/Services/IWellOperationsStatService.cs +++ b/AsbCloudApp/Services/IWellOperationsStatService.cs @@ -7,7 +7,7 @@ namespace AsbCloudApp.Services { public interface IWellOperationsStatService { - Task GetStatClusterAsync(int idCluster, CancellationToken token = default); + Task GetStatClusterAsync(int idCluster, int idCompany, CancellationToken token = default); Task GetStatWellAsync(int idWell, CancellationToken token = default); Task>> GetTvdAsync(int idWell, CancellationToken token); Task> GetWellsStatAsync(IEnumerable idWells, CancellationToken token); diff --git a/AsbCloudInfrastructure/Services/WellOperationService/WellOperationsStatService.cs b/AsbCloudInfrastructure/Services/WellOperationService/WellOperationsStatService.cs index 5f0e0725..f0082f32 100644 --- a/AsbCloudInfrastructure/Services/WellOperationService/WellOperationsStatService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationService/WellOperationsStatService.cs @@ -40,21 +40,20 @@ namespace AsbCloudInfrastructure.Services.WellOperationService cacheCluster = cache.GetCachedTable((DbContext)db); } - public async Task GetStatClusterAsync(int idCluster, CancellationToken token = default) + public async Task GetStatClusterAsync(int idCluster, int idCompany, CancellationToken token = default) { + var allWellsByCompany = await wellService.GetWellsByCompanyAsync(idCompany, token).ConfigureAwait(false); + + var idWellsByCompany = allWellsByCompany.Select(w=>w.Id).Distinct(); + var wells = await db.Wells .Include(w => w.WellOperations) .Where(o => o.IdCluster == idCluster) - .AsNoTracking() + .Where(w => idWellsByCompany.Contains(w.Id)) + .Select(w => w.Id) .ToListAsync(token); - var statsWells = new List(wells.Count()); - - foreach (var well in wells) - { - var statWellDto = await CalcStatWellAsync(well, token); - statsWells.Add(statWellDto); - } + var statsWells = await GetWellsStatAsync(wells, token).ConfigureAwait(false); var cluster = await cacheCluster.FirstOrDefaultAsync(c => c.Id == idCluster, token); var statClusterDto = new StatClusterDto diff --git a/AsbCloudWebApi/Controllers/WellOperationStatController.cs b/AsbCloudWebApi/Controllers/WellOperationStatController.cs index 28a97499..7d71cac3 100644 --- a/AsbCloudWebApi/Controllers/WellOperationStatController.cs +++ b/AsbCloudWebApi/Controllers/WellOperationStatController.cs @@ -38,11 +38,16 @@ namespace AsbCloudWebApi.Controllers public async Task GetStatClusterAsync(int idCluster, CancellationToken token = default) { + int? idCompanyOrNull = User.GetCompanyId(); + if(idCompanyOrNull is null) + return Forbid(); + + int idCompany = idCompanyOrNull ?? 0; // TODO: Fix next commented lines //if (!await CanUserAccessToWellAsync(idCluster, token).ConfigureAwait(false)) // return Forbid(); - var result = await operationsStatService.GetStatClusterAsync(idCluster, token) + var result = await operationsStatService.GetStatClusterAsync(idCluster, idCompany, token) .ConfigureAwait(false); return Ok(result); }