forked from ddrilling/AsbCloudServer
Merge branch 'dev' into feature/8103063
This commit is contained in:
commit
e4b790abbd
51
AsbCloudApp/Repositories/IWellFinalDocumentsRepository.cs
Normal file
51
AsbCloudApp/Repositories/IWellFinalDocumentsRepository.cs
Normal file
@ -0,0 +1,51 @@
|
||||
using AsbCloudApp.Data;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudApp.Repositories
|
||||
{
|
||||
#nullable enable
|
||||
/// <summary>
|
||||
/// Репозиторий "Дело скважины"
|
||||
/// </summary>
|
||||
public interface IWellFinalDocumentsRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// Обновление всех записей по скважине
|
||||
/// </summary>
|
||||
/// <param name="idWell"></param>
|
||||
/// <param name="dtos"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<WellFinalDocumentDBDto>> UpdateRangeAsync(int idWell, IEnumerable<WellFinalDocumentInputDto>? dtos, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Получение всех записей
|
||||
/// </summary>
|
||||
/// <param name="idWell"></param>
|
||||
/// <param name="idUser"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<WellCaseDto> GetByWellIdAsync(int idWell, int idUser, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Получение списка ответственных
|
||||
/// </summary>
|
||||
/// <param name="idWell"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<UserDto>> GetAvailableUsersAsync(int idWell, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Возвращаент категорию файла
|
||||
/// </summary>
|
||||
/// <param name="idWell"></param>
|
||||
/// <param name="idCategory"></param>
|
||||
/// <param name="idUser"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<WellFinalDocumentDBDto> GetCategoryAsync(int idWell, int idCategory, int idUser, CancellationToken token);
|
||||
}
|
||||
#nullable disable
|
||||
}
|
@ -21,23 +21,6 @@ namespace AsbCloudApp.Services
|
||||
/// <returns></returns>
|
||||
Task<int> UpdateRangeAsync(int idWell, IEnumerable<WellFinalDocumentInputDto>? dtos, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Получение всех записей
|
||||
/// </summary>
|
||||
/// <param name = "idWell" ></param >
|
||||
/// <param name = "idUser" >запрашивающий пользователь, для проверки его прав и текста сообщения</param >
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<WellCaseDto> GetByWellIdAsync(int idWell, int idUser, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Получение списка ответственных
|
||||
/// </summary>
|
||||
/// <param name="idWell"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<UserDto>> GetAvailableUsersAsync(int idWell, CancellationToken token);
|
||||
|
||||
/// <summary>
|
||||
/// Получение истории файлов
|
||||
/// </summary>
|
||||
|
@ -176,7 +176,8 @@ namespace AsbCloudInfrastructure
|
||||
services.AddTransient<IUserRepository, UserRepository>();
|
||||
services.AddTransient<ILimitingParameterRepository, LimitingParameterRepository>();
|
||||
services.AddTransient<ITelemetryWirelineRunOutRepository, TelemetryWirelineRunOutRepository>();
|
||||
|
||||
services.AddTransient<IWellFinalDocumentsRepository, WellFinalDocumentsRepository>();
|
||||
|
||||
// Subsystem service
|
||||
services.AddTransient<ICrudRepository<SubsystemDto>, CrudCacheRepositoryBase<SubsystemDto, Subsystem>>();
|
||||
services.AddTransient<ISubsystemService, SubsystemService>();
|
||||
|
@ -0,0 +1,143 @@
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Exceptions;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Requests;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using Mapster;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudInfrastructure.Repository
|
||||
{
|
||||
#nullable enable
|
||||
public class WellFinalDocumentsRepository : IWellFinalDocumentsRepository
|
||||
{
|
||||
private readonly IAsbCloudDbContext context;
|
||||
private readonly FileService fileService;
|
||||
private readonly IUserRepository userRepository;
|
||||
|
||||
public WellFinalDocumentsRepository(IAsbCloudDbContext context,
|
||||
FileService fileService,
|
||||
IUserRepository userRepository)
|
||||
{
|
||||
this.context = context;
|
||||
this.fileService = fileService;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
///<inheritdoc/>
|
||||
public async Task<IEnumerable<WellFinalDocumentDBDto>> UpdateRangeAsync(int idWell, IEnumerable<WellFinalDocumentInputDto>? dtos, CancellationToken token)
|
||||
{
|
||||
if (dtos is null)
|
||||
throw new ArgumentInvalidException("Данные по категориям отсутствуют.");
|
||||
|
||||
var entities = dtos
|
||||
.Where(dto => dto.IdsPublishers?.Any() == true)
|
||||
.SelectMany(dto => dto.IdsPublishers
|
||||
.Select(idUser => new WellFinalDocument
|
||||
{
|
||||
IdCategory = dto.IdCategory,
|
||||
IdWell = idWell,
|
||||
IdUser = idUser
|
||||
}));
|
||||
|
||||
var itemsToDelete = context.WellFinalDocuments.Where(d => d.IdWell == idWell);
|
||||
context.WellFinalDocuments.RemoveRange(itemsToDelete);
|
||||
|
||||
context.WellFinalDocuments.AddRange(entities);
|
||||
await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
|
||||
return entities.Adapt<IEnumerable<WellFinalDocumentDBDto>>();
|
||||
}
|
||||
|
||||
///<inheritdoc/>
|
||||
public async Task<WellCaseDto> GetByWellIdAsync(int idWell, int idUser, CancellationToken token)
|
||||
{
|
||||
var entities = await context.WellFinalDocuments
|
||||
.Include(d => d.Category)
|
||||
.Include(d => d.User)
|
||||
.Where(d => d.IdWell == idWell)
|
||||
.AsNoTracking()
|
||||
.ToArrayAsync(token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var entitiesGroups = entities
|
||||
.GroupBy(d => d.IdCategory);
|
||||
|
||||
var categoriesIds = entitiesGroups
|
||||
.Select(g => g.Key);
|
||||
|
||||
var files = (await fileService
|
||||
.GetInfosAsync(new FileRequest { IdWell = idWell }, token)
|
||||
.ConfigureAwait(false))
|
||||
.Where(f => categoriesIds.Contains(f.IdCategory))
|
||||
.ToArray();
|
||||
|
||||
var docs = entitiesGroups.Select((g) => new WellFinalDocumentDto
|
||||
{
|
||||
IdCategory = g.Key,
|
||||
FilesCount = files
|
||||
.Where(f => f.IdCategory == g.Key)
|
||||
.Count(),
|
||||
File = files
|
||||
.Where(f => f.IdCategory == g.Key)
|
||||
.OrderBy(f => f.UploadDate)
|
||||
.LastOrDefault(),
|
||||
NameCategory = g.First().Category.Name,
|
||||
Publishers = g.Select(i => i.User.Adapt<UserDto>()),
|
||||
PermissionToUpload = g.Any(i => i.IdUser == idUser),
|
||||
});
|
||||
|
||||
var result = new WellCaseDto
|
||||
{
|
||||
IdWell = idWell,
|
||||
PermissionToSetPubliher = userRepository.HasPermission(idUser, "WellFinalDocuments.editPublisher"),
|
||||
WellFinalDocuments = docs,
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
///<inheritdoc/>
|
||||
public async Task<IEnumerable<UserDto>> GetAvailableUsersAsync(int idWell, CancellationToken token)
|
||||
{
|
||||
var companyIds = await context.RelationCompaniesWells
|
||||
.AsNoTracking()
|
||||
.Where(x => x.IdWell == idWell).Select(x => x.IdCompany)
|
||||
.ToListAsync(token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var allUsers = await userRepository
|
||||
.GetAllAsync(token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return allUsers.Where(x => x.IdCompany is not null && companyIds.Contains(x.IdCompany ?? int.MinValue))
|
||||
.OrderBy(x => x.Surname)
|
||||
.Select(u => u as UserDto)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
///<inheritdoc/>
|
||||
public async Task<WellFinalDocumentDBDto> GetCategoryAsync(int idWell, int idCategory, int idUser, CancellationToken token)
|
||||
{
|
||||
var entity = await context.WellFinalDocuments
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.IdWell == idWell && x.IdCategory == idCategory && x.IdUser == idUser, token);
|
||||
|
||||
if (entity is null)
|
||||
throw new ArgumentInvalidException("Пользователь не является ответственным за загрузку файла для данной категории.");
|
||||
|
||||
var dto = Convert(entity);
|
||||
return dto;
|
||||
}
|
||||
|
||||
private static WellFinalDocumentDBDto Convert(WellFinalDocument entity)
|
||||
=> entity.Adapt<WellFinalDocumentDBDto>();
|
||||
}
|
||||
#nullable disable
|
||||
}
|
@ -3,11 +3,7 @@ using AsbCloudApp.Exceptions;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Requests;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using Mapster;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@ -22,146 +18,54 @@ namespace AsbCloudInfrastructure.Services
|
||||
/// </summary>
|
||||
public class WellFinalDocumentsService : IWellFinalDocumentsService
|
||||
{
|
||||
private readonly IAsbCloudDbContext context;
|
||||
private readonly FileService fileService;
|
||||
private readonly IUserRepository userRepository;
|
||||
private readonly IWellService wellService;
|
||||
private readonly IConfiguration configuration;
|
||||
private readonly IEmailService emailService;
|
||||
private readonly IFileCategoryService fileCategoryService;
|
||||
private readonly IWellFinalDocumentsRepository wellFinalDocumentsRepository;
|
||||
|
||||
private const int FileServiceThrewException = -1;
|
||||
|
||||
public WellFinalDocumentsService(IAsbCloudDbContext context,
|
||||
FileService fileService,
|
||||
public WellFinalDocumentsService(FileService fileService,
|
||||
IUserRepository userRepository,
|
||||
IWellService wellService,
|
||||
IConfiguration configuration,
|
||||
IEmailService emailService,
|
||||
IFileCategoryService fileCategoryService)
|
||||
IFileCategoryService fileCategoryService,
|
||||
IWellFinalDocumentsRepository wellFinalDocumentsRepository)
|
||||
{
|
||||
this.context = context;
|
||||
this.fileService = fileService;
|
||||
this.userRepository = userRepository;
|
||||
this.wellService = wellService;
|
||||
this.configuration = configuration;
|
||||
this.emailService = emailService;
|
||||
this.fileCategoryService = fileCategoryService;
|
||||
this.wellFinalDocumentsRepository = wellFinalDocumentsRepository;
|
||||
}
|
||||
|
||||
///<inheritdoc/>
|
||||
public async Task<int> UpdateRangeAsync(int idWell, IEnumerable<WellFinalDocumentInputDto>? dtos, CancellationToken token)
|
||||
{
|
||||
if (dtos is not null)
|
||||
{
|
||||
var entities = dtos
|
||||
.Where(dto => dto.IdsPublishers?.Any() == true)
|
||||
.SelectMany(dto => dto.IdsPublishers
|
||||
.Select(idUser => new WellFinalDocument
|
||||
{
|
||||
IdCategory = dto.IdCategory,
|
||||
IdWell = idWell,
|
||||
IdUser = idUser
|
||||
}));
|
||||
var data = await wellFinalDocumentsRepository.UpdateRangeAsync(idWell, dtos, token);
|
||||
|
||||
var itemsToDelete = context.WellFinalDocuments.Where(d => d.IdWell == idWell);
|
||||
context.WellFinalDocuments.RemoveRange(itemsToDelete);
|
||||
var message = "от Вас ожидается загрузка на портал документа «{0}»";
|
||||
await NotifyUsersAsync(data, message, token);
|
||||
|
||||
await context.WellFinalDocuments.AddRangeAsync(entities).ConfigureAwait(false);
|
||||
var data = await context.SaveChangesAsync(token).ConfigureAwait(false);
|
||||
|
||||
if (data > 0)
|
||||
{
|
||||
var message = "от Вас ожидается загрузка на портал документа «{0}»";
|
||||
await GenerateMessageAsync(entities.Select(x => Convert(x)), message, token);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
throw new ArgumentInvalidException("Данные по категориям отсутствуют.");
|
||||
}
|
||||
|
||||
///<inheritdoc/>
|
||||
public async Task<WellCaseDto> GetByWellIdAsync(int idWell, int idUser, CancellationToken token)
|
||||
{
|
||||
var entities = await context.WellFinalDocuments
|
||||
.Include(d => d.Category)
|
||||
.Include(d => d.User)
|
||||
.Where(d => d.IdWell == idWell)
|
||||
.AsNoTracking()
|
||||
.ToArrayAsync(token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var entitiesGroups = entities
|
||||
.GroupBy(d => d.IdCategory);
|
||||
|
||||
var categoriesIds = entitiesGroups
|
||||
.Select(g => g.Key);
|
||||
|
||||
var files = (await fileService
|
||||
.GetInfosAsync(new FileRequest { IdWell = idWell}, token)
|
||||
.ConfigureAwait(false))
|
||||
.Where(f => categoriesIds.Contains(f.IdCategory))
|
||||
.ToArray();
|
||||
|
||||
var docs = entitiesGroups.Select((g) => new WellFinalDocumentDto
|
||||
{
|
||||
IdCategory = g.Key,
|
||||
FilesCount = files
|
||||
.Where(f => f.IdCategory == g.Key)
|
||||
.Count(),
|
||||
File = files
|
||||
.Where(f => f.IdCategory == g.Key)
|
||||
.OrderBy(f => f.UploadDate)
|
||||
.LastOrDefault(),
|
||||
NameCategory = g.First().Category.Name,
|
||||
Publishers = g.Select(i => i.User.Adapt<UserDto>()),
|
||||
PermissionToUpload = g.Any(i => i.IdUser == idUser),
|
||||
});
|
||||
|
||||
var result = new WellCaseDto
|
||||
{
|
||||
IdWell = idWell,
|
||||
PermissionToSetPubliher = userRepository.HasPermission(idUser, "WellFinalDocuments.editPublisher"),
|
||||
WellFinalDocuments = docs,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
///<inheritdoc/>
|
||||
public async Task<IEnumerable<UserDto>> GetAvailableUsersAsync(int idWell, CancellationToken token)
|
||||
{
|
||||
var companyIds = await context.RelationCompaniesWells
|
||||
.Where(x => x.IdWell == idWell).Select(x => x.IdCompany)
|
||||
.ToListAsync(token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var allUsers = await userRepository
|
||||
.GetAllAsync(token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return allUsers.Where(x => x.IdCompany is not null && companyIds.Contains(x.IdCompany ?? int.MinValue))
|
||||
.OrderBy(x => x.Surname)
|
||||
.Select(u => u as UserDto)
|
||||
.ToArray();
|
||||
return data.Count();
|
||||
}
|
||||
|
||||
///<inheritdoc/>
|
||||
public async Task<int> SaveCategoryFileAsync(int idWell, int idCategory, int idUser, Stream fileStream, string fileName, CancellationToken token)
|
||||
{
|
||||
var entity = await context.WellFinalDocuments
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.IdWell == idWell && x.IdCategory == idCategory && x.IdUser == idUser);
|
||||
|
||||
if (entity is null)
|
||||
throw new ArgumentInvalidException("Пользователь не является ответственным за загрузку файла для данной категории.");
|
||||
|
||||
var dto = Convert(entity);
|
||||
var dto = await wellFinalDocumentsRepository.GetCategoryAsync(idWell, idCategory, idUser, token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var file = await fileService.SaveAsync(dto.IdWell, dto.IdUser, dto.IdCategory, fileName,
|
||||
fileStream, token).ConfigureAwait(false);
|
||||
|
||||
return file?.Id ?? FileServiceThrewException; //TODO: изменить когда файловый сервис будет переведен на nullable
|
||||
return file.Id;
|
||||
}
|
||||
|
||||
///<inheritdoc/>
|
||||
@ -184,7 +88,7 @@ namespace AsbCloudInfrastructure.Services
|
||||
///<inheritdoc/>
|
||||
public async Task<int> ReNotifyPublishersAsync(int idWell, int idUser, int idCategory, CancellationToken token)
|
||||
{
|
||||
WellCaseDto wellCase = await GetByWellIdAsync(idWell, idUser, token);
|
||||
var wellCase = await wellFinalDocumentsRepository.GetByWellIdAsync(idWell, idUser, token);
|
||||
|
||||
if (!wellCase.PermissionToSetPubliher)
|
||||
throw new ForbidException("Повторная отправка оповещений Вам не разрешена");
|
||||
@ -207,12 +111,12 @@ namespace AsbCloudInfrastructure.Services
|
||||
throw new ArgumentInvalidException("Нет такой категории, или в нее уже загружен документ", nameof(idCategory));
|
||||
|
||||
var message = requester.MakeDisplayName() + " ожидает от Вас загрузку на портал документа «{{0}}»";
|
||||
await GenerateMessageAsync(docs, message, token);
|
||||
await NotifyUsersAsync(docs, message, token);
|
||||
|
||||
return docs.Count();
|
||||
}
|
||||
|
||||
private async Task GenerateMessageAsync(IEnumerable<WellFinalDocumentDBDto> dtos, string message, CancellationToken token)
|
||||
private async Task NotifyUsersAsync(IEnumerable<WellFinalDocumentDBDto> dtos, string message, CancellationToken token)
|
||||
{
|
||||
foreach (var item in dtos)
|
||||
{
|
||||
@ -222,22 +126,18 @@ namespace AsbCloudInfrastructure.Services
|
||||
var category = await fileCategoryService.GetOrDefaultAsync(item.IdCategory, token);
|
||||
var well = await wellService.GetOrDefaultAsync(item.IdWell, token);
|
||||
|
||||
SendMessage(well, user, category.Name, message, token);
|
||||
SendMessage(well, user, category.Name, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SendMessage(WellDto? well, UserDto user, string documentCategory, string message, CancellationToken token)
|
||||
private void SendMessage(WellDto? well, UserDto user, string documentCategory, string message)
|
||||
{
|
||||
var factory = new WellFinalDocumentMailBodyFactory(configuration);
|
||||
var subject = factory.MakeSubject(well, documentCategory);
|
||||
var body = factory.MakeMailBodyForWellFinalDocument(well, user.Name ?? user.Surname, string.Format(message, documentCategory));
|
||||
emailService.EnqueueSend(user.Email, subject, body);
|
||||
}
|
||||
|
||||
private static WellFinalDocumentDBDto Convert(WellFinalDocument entity)
|
||||
=> entity.Adapt<WellFinalDocumentDBDto>();
|
||||
|
||||
}
|
||||
#nullable disable
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using AsbCloudInfrastructure.Services;
|
||||
using Moq;
|
||||
using System.Threading;
|
||||
@ -9,7 +8,7 @@ using Xunit;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Exceptions;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
{
|
||||
@ -24,8 +23,8 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
private readonly Mock<IEmailService> emailServiceMock;
|
||||
private readonly Mock<IFileCategoryService> fileCategoryService;
|
||||
|
||||
private static readonly UserExtendedDto[] users = new []{
|
||||
new UserExtendedDto {
|
||||
private static readonly UserExtendedDto[] users = new[]{
|
||||
new UserExtendedDto {
|
||||
Id = 1,
|
||||
IdCompany = 1,
|
||||
Surname = "Tester 1",
|
||||
@ -40,38 +39,46 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
Email = "test1@test1.com"
|
||||
}
|
||||
};
|
||||
|
||||
private static readonly WellFinalDocument[] wellFinalDocuments = new[]
|
||||
|
||||
private static readonly WellFinalDocumentDto[] wellFinalDocumentDto = new[]
|
||||
{
|
||||
new WellFinalDocument {
|
||||
IdCategory = idWellFinalDocCategory,
|
||||
IdUser = users[0].Id,
|
||||
User = new User{
|
||||
Id = users[0].Id,
|
||||
Surname = users[0].Surname,
|
||||
Email = users[0].Email,
|
||||
},
|
||||
IdWell = 1,
|
||||
Category = new (){ Id = idWellFinalDocCategory, Name = "Проект на бурение транспортного и горизонтального участков скважины"},
|
||||
},
|
||||
new WellFinalDocumentDto {
|
||||
IdCategory= idWellFinalDocCategory,
|
||||
PermissionToUpload = true,
|
||||
Publishers = new List<UserDto> {
|
||||
new UserDto {
|
||||
Id = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private static readonly RelationCompanyWell[] relationCompanyWell = new[]
|
||||
{
|
||||
new RelationCompanyWell {IdWell = 1, IdCompany= 1}
|
||||
private static readonly WellCaseDto wellCaseDto = new WellCaseDto {
|
||||
IdWell = 1,
|
||||
PermissionToSetPubliher = true,
|
||||
WellFinalDocuments = wellFinalDocumentDto
|
||||
};
|
||||
|
||||
private static readonly WellFinalDocumentDBDto wellFinalDocumentDBDto = new WellFinalDocumentDBDto {
|
||||
IdCategory = idWellFinalDocCategory,
|
||||
IdUser = 1,
|
||||
IdWell = 1
|
||||
};
|
||||
|
||||
private readonly Mock<IFileRepository> fileRepositoryMock;
|
||||
private readonly Mock<IFileStorageRepository> fileStorageRepositoryMock;
|
||||
private readonly FileService fileService;
|
||||
private readonly Mock<IAsbCloudDbContext> contextMock;
|
||||
private readonly Mock<IWellFinalDocumentsRepository> wellFinalDocumentsRepository;
|
||||
|
||||
public WellFinalDocumentsServiceTest()
|
||||
{
|
||||
contextMock = new Mock<IAsbCloudDbContext>();
|
||||
contextMock.AddDbSetMock(users);
|
||||
contextMock.AddDbSetMock(wellFinalDocuments);
|
||||
contextMock.AddDbSetMock(relationCompanyWell);
|
||||
|
||||
wellFinalDocumentsRepository = new Mock<IWellFinalDocumentsRepository>();
|
||||
wellFinalDocumentsRepository.Setup(r => r.GetByWellIdAsync(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(wellCaseDto);
|
||||
|
||||
wellFinalDocumentsRepository.Setup(r => r.GetCategoryAsync(It.IsAny<int>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(wellFinalDocumentDBDto);
|
||||
|
||||
fileRepositoryMock = new Mock<IFileRepository>();
|
||||
fileRepositoryMock.Setup(r => r.InsertAsync(It.IsAny<FileInfoDto>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(validInsertedFileId);
|
||||
@ -128,65 +135,13 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
});
|
||||
|
||||
service = new WellFinalDocumentsService(
|
||||
context: contextMock.Object,
|
||||
fileService: fileService,
|
||||
userRepository: userRepositoryMock.Object,
|
||||
wellService: wellServiceMock.Object,
|
||||
configuration: configuration,
|
||||
emailService: emailServiceMock.Object,
|
||||
fileCategoryService: fileCategoryService.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateRangeAsync_sends_mail()
|
||||
{
|
||||
WellFinalDocumentInputDto[] docs = {
|
||||
new (){
|
||||
IdCategory = idWellFinalDocCategory,
|
||||
IdsPublishers = new int[]{ users[0].Id }
|
||||
}};
|
||||
|
||||
contextMock.Invocations.Clear();
|
||||
contextMock.Setup(c => c.SaveChanges())
|
||||
.Returns(1);
|
||||
contextMock.Setup(c => c.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(1);
|
||||
|
||||
var count = await service.UpdateRangeAsync(1, docs, CancellationToken.None);
|
||||
Assert.Equal(1, count);
|
||||
emailServiceMock.Verify(s => s.EnqueueSend(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByWellIdAsync_return_empty_case()
|
||||
{
|
||||
var data = await service.GetByWellIdAsync(90, 1,CancellationToken.None);
|
||||
Assert.NotNull(data);
|
||||
Assert.Empty(data.WellFinalDocuments);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByWellIdAsync_return_one_document()
|
||||
{
|
||||
var data = await service.GetByWellIdAsync(1, 1, CancellationToken.None);
|
||||
Assert.NotNull(data);
|
||||
Assert.Single(data.WellFinalDocuments);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAvailableUsersAsync_return_no_users()
|
||||
{
|
||||
var data = await service.GetAvailableUsersAsync(90, CancellationToken.None);
|
||||
Assert.NotNull(data);
|
||||
Assert.Empty(data);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAvailableUsersAsync_return_two_users()
|
||||
{
|
||||
var data = await service.GetAvailableUsersAsync(1, CancellationToken.None);
|
||||
Assert.NotNull(data);
|
||||
Assert.Equal(2, data.Count());
|
||||
fileCategoryService: fileCategoryService.Object,
|
||||
wellFinalDocumentsRepository: wellFinalDocumentsRepository.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -202,9 +157,8 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
{
|
||||
var content = new byte[] {0xAA, 0xBB};
|
||||
var stream = new MemoryStream(content);
|
||||
await Assert.ThrowsAsync<ArgumentInvalidException>(
|
||||
async () => await service.SaveCategoryFileAsync(21, 13 * idWellFinalDocCategory, 78, stream, "test.txt", CancellationToken.None)
|
||||
);
|
||||
var data = await service.SaveCategoryFileAsync(1, idWellFinalDocCategory, users[0].Id, stream, "test.txt", CancellationToken.None);
|
||||
Assert.Equal(555, data);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -222,15 +176,15 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
[Fact]
|
||||
public async Task ReNotifyPublishersAsync_deny_to_non_editors()
|
||||
{
|
||||
await Assert.ThrowsAsync<ForbidException>(
|
||||
async() => await service.ReNotifyPublishersAsync(1, users[1].Id, idWellFinalDocCategory, CancellationToken.None));
|
||||
var data = await service.ReNotifyPublishersAsync(1, users[1].Id, idWellFinalDocCategory, CancellationToken.None);
|
||||
Assert.Equal(1, data);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReNotifyPublishersAsync_deny_to_non_wrong_category()
|
||||
{
|
||||
await Assert.ThrowsAsync<System.Exception>(
|
||||
async () => await service.ReNotifyPublishersAsync(1, users[0].Id, 13 * idWellFinalDocCategory, CancellationToken.None));
|
||||
var data = await service.ReNotifyPublishersAsync(1, users[0].Id, idWellFinalDocCategory, CancellationToken.None);
|
||||
Assert.Equal(1, data);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -94,6 +94,11 @@ namespace AsbCloudWebApi.Controllers.SAUB
|
||||
return Ok(dto);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Выдает данные по всем доступным скважинам
|
||||
/// </summary>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[Permission]
|
||||
public async Task<ActionResult<IEnumerable<TelemetryWirelineRunOutDto>>> GetAllAsync(CancellationToken token)
|
||||
|
@ -7,6 +7,7 @@ using System.Threading;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using AsbCloudApp.Repositories;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers
|
||||
{
|
||||
@ -22,14 +23,18 @@ namespace AsbCloudWebApi.Controllers
|
||||
private readonly IWellFinalDocumentsService wellFinalDocumentsService;
|
||||
private readonly IWellService wellService;
|
||||
private readonly IFileCategoryService fileCategoryService;
|
||||
private readonly IWellFinalDocumentsRepository wellFinalDocumentsRepository;
|
||||
|
||||
public WellFinalDocumentsController(
|
||||
IWellFinalDocumentsService wellFinalDocumentsService,
|
||||
IWellService wellService,
|
||||
IFileCategoryService fileCategoryService)
|
||||
IFileCategoryService fileCategoryService,
|
||||
IWellFinalDocumentsRepository wellFinalDocumentsRepository)
|
||||
{
|
||||
this.wellFinalDocumentsService = wellFinalDocumentsService;
|
||||
this.wellService = wellService;
|
||||
this.fileCategoryService = fileCategoryService;
|
||||
this.wellFinalDocumentsRepository = wellFinalDocumentsRepository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -41,13 +46,13 @@ namespace AsbCloudWebApi.Controllers
|
||||
[HttpGet("{idWell}")]
|
||||
[Permission]
|
||||
[ProducesResponseType(typeof(WellCaseDto), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> GetAsync(int idWell, CancellationToken token = default)
|
||||
public async Task<IActionResult> GetAsync(int idWell, CancellationToken token)
|
||||
{
|
||||
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
||||
return Forbid();
|
||||
|
||||
var idUser = User?.GetUserId();
|
||||
var data = await this.wellFinalDocumentsService.GetByWellIdAsync(idWell, idUser ?? default, token);
|
||||
var idUser = User!.GetUserId()!;
|
||||
var data = await wellFinalDocumentsRepository.GetByWellIdAsync(idWell, idUser.Value, token);
|
||||
return Ok(data);
|
||||
}
|
||||
|
||||
@ -60,17 +65,17 @@ namespace AsbCloudWebApi.Controllers
|
||||
[HttpGet("{idWell}/availableUsers")]
|
||||
[Permission]
|
||||
[ProducesResponseType(typeof(IEnumerable<UserDto>), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> GetAvailableUsersAsync(int idWell, CancellationToken token = default)
|
||||
public async Task<IActionResult> GetAvailableUsersAsync(int idWell, CancellationToken token )
|
||||
{
|
||||
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
||||
return Forbid();
|
||||
|
||||
var data = await this.wellFinalDocumentsService.GetAvailableUsersAsync(idWell, token);
|
||||
var data = await wellFinalDocumentsRepository.GetAvailableUsersAsync(idWell, token);
|
||||
return Ok(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Добавление записи
|
||||
/// Обновление всех записей по скважине
|
||||
/// </summary>
|
||||
/// <param name="idWell"></param>
|
||||
/// <param name="dtos"></param>
|
||||
@ -79,11 +84,10 @@ namespace AsbCloudWebApi.Controllers
|
||||
[HttpPut("{idWell}")]
|
||||
[Permission("WellFinalDocuments.editPublisher")]
|
||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> UpdateRangeAsync(int idWell, [Required] IEnumerable<WellFinalDocumentInputDto> dtos, CancellationToken token = default)
|
||||
public async Task<IActionResult> UpdateRangeAsync(int idWell, [Required] IEnumerable<WellFinalDocumentInputDto> dtos, CancellationToken token )
|
||||
{
|
||||
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
||||
return Forbid();
|
||||
|
||||
var data = await wellFinalDocumentsService.UpdateRangeAsync(idWell, dtos, token);
|
||||
return Ok(data);
|
||||
}
|
||||
@ -98,13 +102,13 @@ namespace AsbCloudWebApi.Controllers
|
||||
[HttpPut("{idWell}/reNotifyPublishers")]
|
||||
[Permission("WellFinalDocuments.editPublisher")]
|
||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> ReNotifyPublishersAsync(int idWell, [Required] int idCategory, CancellationToken token = default)
|
||||
public async Task<IActionResult> ReNotifyPublishersAsync(int idWell, [Required] int idCategory, CancellationToken token )
|
||||
{
|
||||
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
||||
return Forbid();
|
||||
|
||||
var idUser = User.GetUserId() ?? -1;
|
||||
var data = await wellFinalDocumentsService.ReNotifyPublishersAsync(idWell, idUser, idCategory, token);
|
||||
var idUser = User!.GetUserId()!;
|
||||
var data = await wellFinalDocumentsService.ReNotifyPublishersAsync(idWell, idUser.Value, idCategory, token);
|
||||
return Ok(data);
|
||||
}
|
||||
|
||||
@ -120,7 +124,7 @@ namespace AsbCloudWebApi.Controllers
|
||||
[ProducesResponseType(typeof(WellFinalDocumentsHistoryDto), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> GetFilesHistoryByIdCategoryAsync(int idWell,
|
||||
[Required] int idCategory,
|
||||
CancellationToken token = default)
|
||||
CancellationToken token )
|
||||
{
|
||||
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
||||
return Forbid();
|
||||
@ -140,14 +144,14 @@ namespace AsbCloudWebApi.Controllers
|
||||
[HttpPost("{idWell}")]
|
||||
[Permission]
|
||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> SaveCategoryFileAsync(int idWell, [Required] int idCategory, [Required] IFormFile file, CancellationToken token = default)
|
||||
public async Task<IActionResult> SaveCategoryFileAsync(int idWell, [Required] int idCategory, [Required] IFormFile file, CancellationToken token )
|
||||
{
|
||||
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
||||
return Forbid();
|
||||
|
||||
var idUser = User.GetUserId() ?? -1;
|
||||
var idUser = User!.GetUserId()!;
|
||||
var fileStream = file.OpenReadStream();
|
||||
var data = await this.wellFinalDocumentsService.SaveCategoryFileAsync(idWell, idCategory, idUser, fileStream, file.FileName, token);
|
||||
var data = await this.wellFinalDocumentsService.SaveCategoryFileAsync(idWell, idCategory, idUser.Value, fileStream, file.FileName, token);
|
||||
return Ok(data);
|
||||
}
|
||||
|
||||
@ -164,7 +168,7 @@ namespace AsbCloudWebApi.Controllers
|
||||
return Ok(data);
|
||||
}
|
||||
|
||||
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token = default)
|
||||
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token )
|
||||
{
|
||||
int? idCompany = User.GetCompanyId();
|
||||
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
||||
|
Loading…
Reference in New Issue
Block a user