From 42778501560aae1052f0aff7e236718ee0651674 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Wed, 23 Nov 2022 14:03:08 +0500 Subject: [PATCH] #7912198 implement ReNotifyPublishers --- .../Services/IWellFinalDocumentsService.cs | 20 ++++++--- .../Services/WellFinalDocumentsService.cs | 44 +++++++++++++++++-- .../WellFinalDocumentsController.cs | 32 +++++++++++--- 3 files changed, 81 insertions(+), 15 deletions(-) diff --git a/AsbCloudApp/Services/IWellFinalDocumentsService.cs b/AsbCloudApp/Services/IWellFinalDocumentsService.cs index 4da098ce..30459a43 100644 --- a/AsbCloudApp/Services/IWellFinalDocumentsService.cs +++ b/AsbCloudApp/Services/IWellFinalDocumentsService.cs @@ -25,10 +25,10 @@ namespace AsbCloudApp.Services /// Получение всех записей /// /// - /// + /// запрашивающий пользователь, для проверки его прав и текста сообщения /// /// - Task GetByWellId(int idWell, int idUser, CancellationToken token); + Task GetByWellIdAsync(int idWell, int idUser, CancellationToken token); /// /// Получение списка ответственных @@ -45,19 +45,29 @@ namespace AsbCloudApp.Services /// /// /// - Task GetFilesHistoryByIdCategory(int idWell, int idCategory, CancellationToken token); + Task GetFilesHistoryByIdCategoryAsync(int idWell, int idCategory, CancellationToken token); /// /// Сохранение файла /// /// /// - /// + /// пользователь, который сохраняет файл /// /// /// /// - Task SaveCategoryFile(int idWell, int idCategory, int idUser, Stream fileStream, string fileName, CancellationToken token); + Task SaveCategoryFileAsync(int idWell, int idCategory, int idUser, Stream fileStream, string fileName, CancellationToken token); + + /// + /// Повторно оповестить ответственных за загрузку + /// + /// + /// запрашивающий пользователь, для проверки его прав и текста сообщения + /// + /// + /// count of notified publishers + Task ReNotifyPublishersAsync(int idWell, int idUser, int idCategory, CancellationToken token); } #nullable disable } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/WellFinalDocumentsService.cs b/AsbCloudInfrastructure/Services/WellFinalDocumentsService.cs index ec417604..c0d352cc 100644 --- a/AsbCloudInfrastructure/Services/WellFinalDocumentsService.cs +++ b/AsbCloudInfrastructure/Services/WellFinalDocumentsService.cs @@ -4,7 +4,6 @@ using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb.Model; -using AsbCloudInfrastructure.Repository; using Mapster; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; @@ -50,6 +49,7 @@ namespace AsbCloudInfrastructure.Services this.fileCategoryService = fileCategoryService; } + /// public async Task UpdateRangeAsync(int idWell, IEnumerable? dtos, CancellationToken token) { if (dtos is not null) @@ -81,7 +81,8 @@ namespace AsbCloudInfrastructure.Services throw new ArgumentInvalidException("Данные по категориям отсутствуют."); } - public async Task GetByWellId(int idWell, int idUser, CancellationToken token) + /// + public async Task GetByWellIdAsync(int idWell, int idUser, CancellationToken token) { var entities = await context.WellFinalDocuments .Include(d => d.Category) @@ -127,6 +128,7 @@ namespace AsbCloudInfrastructure.Services return result; } + /// public async Task> GetAvailableUsersAsync(int idWell, CancellationToken token) { var companyIds = await context.RelationCompaniesWells @@ -145,7 +147,8 @@ namespace AsbCloudInfrastructure.Services .ToArray(); } - public async Task SaveCategoryFile(int idWell, int idCategory, int idUser, Stream fileStream, string fileName, CancellationToken token) + /// + public async Task SaveCategoryFileAsync(int idWell, int idCategory, int idUser, Stream fileStream, string fileName, CancellationToken token) { var entity = await context.WellFinalDocuments .AsNoTracking() @@ -162,7 +165,8 @@ namespace AsbCloudInfrastructure.Services return file?.Id ?? FileServiceThrewException; //TODO: изменить когда файловый сервис будет переведен на nullable } - public async Task GetFilesHistoryByIdCategory(int idWell, int idCategory, CancellationToken token) + /// + public async Task GetFilesHistoryByIdCategoryAsync(int idWell, int idCategory, CancellationToken token) { var request = new FileRequest { @@ -178,6 +182,37 @@ namespace AsbCloudInfrastructure.Services }; } + /// + public async Task ReNotifyPublishersAsync(int idWell, int idUser, int idCategory, CancellationToken token) + { + WellCaseDto wellCase = await GetByWellIdAsync(idWell, idUser, token); + + if (wellCase.PermissionToSetPubliher) + throw new ForbidException("Повторная отправка оповещений Вам не разрешена"); + + var requester = userRepository.GetOrDefault(idUser); + if (requester is null) + throw new ForbidException("Не удается вас опознать"); + + var docs = wellCase.WellFinalDocuments + .Where(doc => doc.IdCategory == idCategory) + .Where(doc => doc.File is null) + .SelectMany(doc => doc.Publishers + .Select(pub => new WellFinalDocumentDBDto { + IdCategory = idCategory, + IdUser = pub.Id, + IdWell = idWell + })); + + if(!docs.Any()) + throw new Exception("Нет такой категории, или в нее уже загружен документ"); + + var message = requester.MakeDisplayName() + " ожидает от Вас загрузку на портал документа «{{0}}»"; + await GenerateMessageAsync(docs, message, token); + + return docs.Count(); + } + private async Task GenerateMessageAsync(IEnumerable dtos, string message, CancellationToken token) { foreach (var item in dtos) @@ -203,6 +238,7 @@ namespace AsbCloudInfrastructure.Services private static WellFinalDocumentDBDto Convert(WellFinalDocument entity) => entity.Adapt(); + } #nullable disable } diff --git a/AsbCloudWebApi/Controllers/WellFinalDocumentsController.cs b/AsbCloudWebApi/Controllers/WellFinalDocumentsController.cs index 12be0baf..0e599da9 100644 --- a/AsbCloudWebApi/Controllers/WellFinalDocumentsController.cs +++ b/AsbCloudWebApi/Controllers/WellFinalDocumentsController.cs @@ -47,7 +47,7 @@ namespace AsbCloudWebApi.Controllers return Forbid(); var idUser = User?.GetUserId(); - var data = await this.wellFinalDocumentsService.GetByWellId(idWell, idUser ?? default, token); + var data = await this.wellFinalDocumentsService.GetByWellIdAsync(idWell, idUser ?? default, token); return Ok(data); } @@ -84,7 +84,27 @@ namespace AsbCloudWebApi.Controllers if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); - var data = await this.wellFinalDocumentsService.UpdateRangeAsync(idWell, dtos, token); + var data = await wellFinalDocumentsService.UpdateRangeAsync(idWell, dtos, token); + return Ok(data); + } + + /// + /// Повторно оповестить ответственных за загрузку + /// + /// + /// + /// + /// количество оповещенных публикаторов + [HttpPut("{idWell}")] + [Permission("WellFinalDocuments.editPublisher")] + [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] + public async Task ReNotifyPublishersAsync(int idWell, [Required] int idCategory, CancellationToken token = default) + { + if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) + return Forbid(); + + var idUser = User.GetUserId() ?? -1; + var data = await wellFinalDocumentsService.ReNotifyPublishersAsync(idWell, idUser, idCategory, token); return Ok(data); } @@ -98,14 +118,14 @@ namespace AsbCloudWebApi.Controllers [HttpGet("{idWell}/history")] [Permission] [ProducesResponseType(typeof(WellFinalDocumentsHistoryDto), (int)System.Net.HttpStatusCode.OK)] - public async Task GetFilesHistoryByIdCategory(int idWell, + public async Task GetFilesHistoryByIdCategoryAsync(int idWell, [Required] int idCategory, CancellationToken token = default) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); - var data = await this.wellFinalDocumentsService.GetFilesHistoryByIdCategory(idWell, idCategory, token); + var data = await this.wellFinalDocumentsService.GetFilesHistoryByIdCategoryAsync(idWell, idCategory, token); return Ok(data); } @@ -120,14 +140,14 @@ namespace AsbCloudWebApi.Controllers [HttpPost("{idWell}")] [Permission] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] - public async Task SaveCategoryFile(int idWell, [Required] int idCategory, [Required] IFormFile file, CancellationToken token = default) + public async Task SaveCategoryFileAsync(int idWell, [Required] int idCategory, [Required] IFormFile file, CancellationToken token = default) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var idUser = User.GetUserId() ?? -1; var fileStream = file.OpenReadStream(); - var data = await this.wellFinalDocumentsService.SaveCategoryFile(idWell, idCategory, idUser, fileStream, file.FileName, token); + var data = await this.wellFinalDocumentsService.SaveCategoryFileAsync(idWell, idCategory, idUser, fileStream, file.FileName, token); return Ok(data); }