diff --git a/AsbCloudDb/Model/IAsbCloudDbContext.cs b/AsbCloudDb/Model/IAsbCloudDbContext.cs index e31c6852..0870a8ab 100644 --- a/AsbCloudDb/Model/IAsbCloudDbContext.cs +++ b/AsbCloudDb/Model/IAsbCloudDbContext.cs @@ -53,7 +53,8 @@ namespace AsbCloudDb.Model DatabaseFacade Database { get; } - Task RefreshMaterializedViewAsync(string? mwName = null, CancellationToken token = default) where TEntity : class; + Task RefreshMaterializedViewAsync(string? mwName = null, CancellationToken token = default); + Task RefreshMaterializedViewAsync(CancellationToken token = default) where TEntity : class; int SaveChanges(); int SaveChanges(bool acceptAllChangesOnSuccess); Task SaveChangesAsync(CancellationToken cancellationToken); diff --git a/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionBackgroundService.cs b/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionBackgroundService.cs index 2834a1fd..98ff3fff 100644 --- a/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionBackgroundService.cs @@ -57,7 +57,6 @@ namespace AsbCloudInfrastructure.Services.DetectOperations catch (Exception ex) { Trace.TraceError(ex.Message); - Console.WriteLine(ex.Message); } GC.Collect(); } diff --git a/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramMaker.cs b/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramMaker.cs index 3a4962df..f4248a73 100644 --- a/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramMaker.cs +++ b/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramMaker.cs @@ -8,58 +8,8 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram { internal class DrillingProgramMaker { - private const int maxAllowedColumns = 256; - //public async Task GetOrCreateAsync(int idWell, int idUser, CancellationToken token = default) - //{ - // var programParts = (await fileService.GetInfosByCategoryAsync(idWell, idFileCategoryDrillingProgramItems, token) - // .ConfigureAwait(false)) - // .Where(f => f.FileMarks?.Any(m => m.IdMarkType == 1 && !m.IsDeleted) ?? false); - - // var well = await wellService.GetAsync(idWell, token) - // .ConfigureAwait(false); - - // var programs = await fileService.GetInfosByCategoryAsync(idWell, idFileCategoryDrillingProgram, token) - // .ConfigureAwait(false); - - // if (programs is not null && programs.Any() && programParts.Any()) - // { - // programs = programs.OrderByDescending(f => f.UploadDate); - // var matchFilesIterator = programs.GetEnumerator(); - // matchFilesIterator.MoveNext(); - // var matchFile = matchFilesIterator.Current; - - // if (programParts.All(pp => matchFile.UploadDate > pp.UploadDate) && - // File.Exists(fileService.GetUrl(matchFile))) - // return matchFile; - // else - // await fileService.DeleteAsync(matchFile.Id, token) - // .ConfigureAwait(false); - - // while (matchFilesIterator.MoveNext()) - // await fileService.DeleteAsync(matchFilesIterator.Current.Id, token) - // .ConfigureAwait(false); - // } - - // if (!programParts.Any()) - // throw new FileNotFoundException("Нет частей для формирования программы бурения"); - - // var resultFileName = $"Программа бурения {well.Cluster} {well.Caption}.xlsx"; - - // var filteredFilePaths = programParts - // .Select(file => fileService.GetUrl(file)); - - // var tempResultFilePath = Path.Combine(Path.GetTempPath(), "drillingProgram", resultFileName); - - // UniteExcelFiles(filteredFilePaths, tempResultFilePath); - - // var fileInfo = await fileService.MoveAsync(idWell, null, idFileCategoryDrillingProgram, - // resultFileName, tempResultFilePath, token).ConfigureAwait(false); - - // return fileInfo; - //} - public static void UniteExcelFiles(IEnumerable excelFilesNames, string resultExcelPath) { var resultExcelFile = new XLWorkbook(XLEventTracking.Disabled); diff --git a/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs b/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs index 44b88f06..aebb1657 100644 --- a/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs +++ b/AsbCloudInfrastructure/Services/DrillingProgram/DrillingProgramService.cs @@ -254,7 +254,8 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram if (idUserRole == idUserRoleApprover) await RemoveDrillingProgramAsync(part.IdWell, token); - await NotifyNewPublisherAsync(idWell, user, part.FileCategory.Name, token); + if (idUserRole == idUserRolePublisher) + await NotifyNewPublisherAsync(idWell, user, part.FileCategory.Name, token); return await context.SaveChangesAsync(token); } @@ -291,11 +292,16 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram var part = await context.DrillingProgramParts .Include(p => p.RelatedUsers) + .ThenInclude(r => r.User) + .AsNoTracking() .FirstOrDefaultAsync(p => p.IdWell == fileInfo.IdWell && p.IdFileCategory == fileInfo.IdCategory, token); - if (!part.RelatedUsers.Any(r => r.IdUser == idUser && r.IdUserRole == idUserRoleApprover)) + var user = part.RelatedUsers.FirstOrDefault(r => r.IdUser == idUser && r.IdUserRole == idUserRoleApprover)?.User; + if (user is null) throw new ForbidException($"User {idUser} is not in the approvers list."); + fileMarkDto.User = user.Adapt(); + var oldMarksIds = fileInfo.FileMarks ?.Where(m => m.User.Id == idUser) .Select(m => m.Id); @@ -311,16 +317,22 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram await RemoveDrillingProgramAsync(fileInfo.IdWell, token); await NotifyPublisherOnRejectAsync(fileMarkDto, token); } - - // если все согласованты согласовали - оповещаем публикатора - if (part.RelatedUsers - .Where(u => u.IdUserRole == idUserRoleApprover) - .All(user => fileInfo.FileMarks - .Any(mark => mark.IdMarkType == idMarkTypeApprove && mark.User.Id == user.IdUser))) + else { - await NotifyPublisherOnFullAccepAsync(fileMarkDto, token); + // если все согласованты согласовали - оповещаем публикатора + var approvers = part.RelatedUsers + .Where(u => u.IdUserRole == idUserRoleApprover); + if (approvers + .All(user => fileInfo.FileMarks + .Any(mark => (mark.IdMarkType == idMarkTypeApprove && mark.User.Id == user.IdUser && !mark.IsDeleted)) || + (fileMarkDto.IdMarkType == idMarkTypeApprove && user.IdUser == idUser))) + { + await NotifyPublisherOnFullAccepAsync(fileMarkDto, token); + } } + + return result; } diff --git a/AsbCloudInfrastructure/Services/Email/MailBodyFactory.cs b/AsbCloudInfrastructure/Services/Email/MailBodyFactory.cs index 06a76c50..d80b14d4 100644 --- a/AsbCloudInfrastructure/Services/Email/MailBodyFactory.cs +++ b/AsbCloudInfrastructure/Services/Email/MailBodyFactory.cs @@ -7,10 +7,10 @@ namespace AsbCloudInfrastructure { class MailBodyFactory { - protected readonly string platformName; + private readonly string platformName; private readonly string platformUrl; - protected readonly string companyName; - protected readonly string supportMail; + private readonly string companyName; + private readonly string supportMail; public MailBodyFactory(IConfiguration configuration) { @@ -26,11 +26,11 @@ namespace AsbCloudInfrastructure return subj; } - public string MakeMailBodyForNewPublisher(WellDto well, string userName, string documentCategory) + public string MakeMailBodyForNewPublisher(WellDto well, string publisherName, string documentCategory) { var drillingProgramHref = MakeDrillingProgramHref(well); - var body = $"

Здравствуйте, {userName}.

" + + var body = $"

Здравствуйте, {publisherName}.

" + $"На портале {platformName} началось создание программы бурения скважины {drillingProgramHref}," + $" куст {well.Cluster}, месторождение {well.Deposit}." + $"

От вас ожидается загрузка на портал документа «{documentCategory}» в формате excel (*.xlsx)." + @@ -39,13 +39,12 @@ namespace AsbCloudInfrastructure return body; } - public string MakeMailBodyForApproverNewFile(WellDto well, string userName, int idFile, string fileName) + public string MakeMailBodyForApproverNewFile(WellDto well, string approverName, int idFile, string fileName) { - var fileDownloadHref = MakeFileDownloadHref(well.Id, idFile, fileName); var drillingProgramHref = MakeDrillingProgramHref(well); - var body = $"

Здравствуйте, {userName}.

" + - $"На портал {platformName} загружен документ {fileDownloadHref}" + + var body = $"

Здравствуйте, {approverName}.

" + + $"На портал {platformName} загружен документ {fileName}" + $" для согласования при создании программы бурения скважины {drillingProgramHref}, куст ({well.Cluster})" + $", месторождение ({well.Deposit}).
" + MakeSignatue() + @@ -53,28 +52,26 @@ namespace AsbCloudInfrastructure return body; } - public string MakeMailBodyForPublisherOnReject(WellDto well, string userName, int idFile, string fileName, FileMarkDto fileMark) + public string MakeMailBodyForPublisherOnReject(WellDto well, string publisherName, int idFile, string fileName, FileMarkDto fileMark) { - var fileDownloadHref = MakeFileDownloadHref(well.Id, idFile, fileName); var drillingProgramHref = MakeDrillingProgramHref(well); - var body = $"

Здравствуйте, {userName}.

" + - $"На портале {platformName} отклонен загруженный вами документ {fileDownloadHref} " + + var body = $"

Здравствуйте, {publisherName}.

" + + $"На портале {platformName} отклонен загруженный вами документ {fileName} " + $" по программе бурения скважины {drillingProgramHref}," + $" куст {well.Cluster}, месторождение {well.Deposit}." + - $" Комментарий согласующего ({fileMark.User.Name} {fileMark.User.Surname}):
{fileMark.Comment}" + + $" Комментарий согласующего ({fileMark.User?.Name} {fileMark.User?.Surname}):
{fileMark.Comment}" + MakeSignatue() + $""; return body; } - public string MakeMailBodyForPublisherOnFullAccept(WellDto well, string userName, int idFile, string fileName) + public string MakeMailBodyForPublisherOnFullAccept(WellDto well, string publisherName, int idFile, string fileName) { - var fileDownloadHref = MakeFileDownloadHref(well.Id, idFile, fileName); var drillingProgramHref = MakeDrillingProgramHref(well); - var body = $"

Здравствуйте, {userName}.

" + - $"На портале {platformName} полностью согласован документ {fileDownloadHref} " + + var body = $"

Здравствуйте, {publisherName}.

" + + $"На портале {platformName} полностью согласован документ {fileName} " + $" по программе бурения скважины {drillingProgramHref}," + $" куст {well.Cluster}, месторождение {well.Deposit}." + MakeSignatue() + @@ -82,16 +79,9 @@ namespace AsbCloudInfrastructure return body; } - private string MakeFileDownloadHref(int idWell, int idFile, string fileName) - { - var fileDownloadUrl = $"{platformUrl}/api/well/{idWell}/files/{idFile}"; - var fileDownloadHref = MakeHref(fileDownloadUrl, fileName); - return fileDownloadHref; - } - private string MakeDrillingProgramHref(WellDto well) { - var drillingProgramUrl = $"{platformUrl}/api/well/{well.Id}/drillingProgram"; + var drillingProgramUrl = $"{platformUrl}/well/{well.Id}/drillingProgram"; var drillingProgramHref = MakeHref(drillingProgramUrl, well.Caption); return drillingProgramHref; } diff --git a/AsbCloudInfrastructure/Services/FileService.cs b/AsbCloudInfrastructure/Services/FileService.cs index c769788b..73f469aa 100644 --- a/AsbCloudInfrastructure/Services/FileService.cs +++ b/AsbCloudInfrastructure/Services/FileService.cs @@ -337,6 +337,7 @@ namespace AsbCloudInfrastructure.Services newFileMark.Id = default; newFileMark.DateCreated = DateTime.UtcNow; newFileMark.IdUser = idUser; + newFileMark.User = null; db.FileMarks.Add(newFileMark); return await db.SaveChangesAsync(token); diff --git a/AsbCloudWebApi/Controllers/DrillingProgramController.cs b/AsbCloudWebApi/Controllers/DrillingProgramController.cs index a083af86..05087540 100644 --- a/AsbCloudWebApi/Controllers/DrillingProgramController.cs +++ b/AsbCloudWebApi/Controllers/DrillingProgramController.cs @@ -125,7 +125,7 @@ namespace AsbCloudWebApi.Controllers if (!fileName.EndsWith(".xlsx")) return BadRequest(ArgumentInvalidException.MakeValidationError("file", "Файл должен быть xlsx")); - + var fileStream = files[0].OpenReadStream(); var result = await drillingProgramService.AddFile(idWell, idFileCategory, (int)idUser, fileName, fileStream, token); diff --git a/AsbCloudWebApi/appsettings.json b/AsbCloudWebApi/appsettings.json index b82232b2..1f76edf8 100644 --- a/AsbCloudWebApi/appsettings.json +++ b/AsbCloudWebApi/appsettings.json @@ -16,7 +16,12 @@ "email": { "smtpServer": "smtp.timeweb.ru", "sender": "bot@autodrilling.ru", - "password": "xHhgwZ4D" + "password": "xHhgwZ4D", + + "platformName": "Цифровое бурение", + "platformUrl": "cloud.digitaldrilling.ru", + "companyName": "ООО \"Цифровое бурение\"", + "supportMail": "support@digitaldrilling.ru" }, "Urls": "http://0.0.0.0:5000" //;https://0.0.0.0:5001" //, // See https man: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-6.0