refactoring BodyCreation => MailBodyFactory

This commit is contained in:
ngfrolov 2022-05-04 15:02:12 +05:00
parent 6c845b7f9f
commit 5f21e9e8ce
7 changed files with 190 additions and 214 deletions

View File

@ -5,5 +5,6 @@ namespace AsbCloudApp.Services
public interface IEmailService public interface IEmailService
{ {
void EnqueueSend(IEnumerable<string> addresses, string subject, string htmlBody); void EnqueueSend(IEnumerable<string> addresses, string subject, string htmlBody);
void EnqueueSend(string address, string subject, string htmlBody);
} }
} }

View File

@ -53,4 +53,10 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="Res\logo_32.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>

View File

Before

Width:  |  Height:  |  Size: 4.7 KiB

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

@ -24,7 +24,6 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
private readonly IWellService wellService; private readonly IWellService wellService;
private readonly IConfiguration configuration; private readonly IConfiguration configuration;
private readonly IBackgroundWorkerService backgroundWorker; private readonly IBackgroundWorkerService backgroundWorker;
//email
private readonly IEmailService emailService; private readonly IEmailService emailService;
private readonly string connectionString; private readonly string connectionString;
@ -158,12 +157,12 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
await TryEnqueueMakeProgramAsync(idWell, state, token); await TryEnqueueMakeProgramAsync(idWell, state, token);
return state; return state;
} }
public async Task<int> AddFile(int idWell, int idFileCategory, int idUser, string fileFullName, System.IO.Stream fileStream, CancellationToken token = default) public async Task<int> AddFile(int idWell, int idFileCategory, int idUser, string fileFullName, System.IO.Stream fileStream, CancellationToken token = default)
{ {
var part = await context.DrillingProgramParts var part = await context.DrillingProgramParts
.Include(p => p.RelatedUsers) .Include(p => p.RelatedUsers)
.ThenInclude(r => r.User)
.FirstOrDefaultAsync(p => p.IdWell == idWell && p.IdFileCategory == idFileCategory, token); .FirstOrDefaultAsync(p => p.IdWell == idWell && p.IdFileCategory == idFileCategory, token);
if (part == null) if (part == null)
@ -181,35 +180,9 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
token); token);
await RemoveDrillingProgramAsync(part.IdWell, token); await RemoveDrillingProgramAsync(part.IdWell, token);
var well = await context.Wells
.FirstOrDefaultAsync(x => x.Id == idWell, token);
var cluster = await context.Clusters
.FirstOrDefaultAsync(x => x.Wells == well, token);
var deposit = await context.Deposits
.FirstOrDefaultAsync(x => x.Clusters == cluster, token);
var partApprovers = await context.RelationDrillingProgramPartUsers
.Where(y => y.IdDrillingProgramPart == part.Id & y.IdUserRole == idUserRoleApprover).ToListAsync(token);
foreach (var partApprover in partApprovers)
{
var approver = await context.Users
.FirstOrDefaultAsync(x => x.Id == partApprover.IdUser, token);
var bodyHtml = new MailCoordinating() await NotifyApproversAsync(part, result.Id, fileFullName, token);
{
idWell=idWell,
idDocument = result.Id,
documentName = fileFullName,
wellName = well.Caption,
clusterName = cluster.Caption,
fieldName = deposit.Caption,
userName = $"{approver.Name} {approver.Surname}"
};
emailService.EnqueueSend(new List<string> {approver.Email}, bodyHtml.mailSubject, bodyHtml.GetBodyHTML());
//emailService.EnqueueSend(new List<string> { "79827873134@yandex.ru" }, bodyHtml.mailSubject, bodyHtml.GetBodyHTML());
}
return result.Id; return result.Id;
} }
@ -256,6 +229,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
throw new ArgumentInvalidException($"User id == {idUser} does not exist", nameof(idUser)); throw new ArgumentInvalidException($"User id == {idUser} does not exist", nameof(idUser));
var part = await context.DrillingProgramParts var part = await context.DrillingProgramParts
.Include(p=>p.FileCategory)
.FirstOrDefaultAsync(p => p.IdWell == idWell && p.IdFileCategory == idFileCategory, token); .FirstOrDefaultAsync(p => p.IdWell == idWell && p.IdFileCategory == idFileCategory, token);
if (part is null) if (part is null)
@ -276,35 +250,12 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
IdDrillingProgramPart = part.Id, IdDrillingProgramPart = part.Id,
IdUserRole = idUserRole, IdUserRole = idUserRole,
}; };
var well = await context.Wells
.FirstOrDefaultAsync(x => x.Id == idWell, token);
var cluster = await context.Clusters
.FirstOrDefaultAsync(x => x.Wells == well, token);
context.RelationDrillingProgramPartUsers.Add(newRelation);
var deposit = await context.Deposits
.FirstOrDefaultAsync(x => x.Clusters == cluster, token);
var documentCategory = await context.FileCategories
.FirstOrDefaultAsync(x => x.Id == part.IdFileCategory, token);
context.RelationDrillingProgramPartUsers.Add(newRelation); context.RelationDrillingProgramPartUsers.Add(newRelation);
if (idUserRole == idUserRoleApprover) if (idUserRole == idUserRoleApprover)
await RemoveDrillingProgramAsync(part.IdWell, token); await RemoveDrillingProgramAsync(part.IdWell, token);
// проверяем роль пользователя - если публикатор формируем для него сообщение на отправку
if (idUserRole == idUserRolePublisher)
{
//создаем тело письма
var bodyHtml = new MailUserPublisher()
{
idWell=idWell,
userName = $"{user.Name} {user.Surname}",
wellName = well.Caption,
clusterName = cluster.Caption,
fieldName = deposit.Caption,
documentCategory = documentCategory.Name
};
emailService.EnqueueSend(new List<string> {user.Email}, bodyHtml.mailSubject, bodyHtml.GetBodyHTML()); await NotifyNewPublisherAsync(idWell, user, part.FileCategory.Name, token);
//emailService.EnqueueSend(new List<string> { "79827873134@yandex.ru" }, bodyHtml.mailSubject, bodyHtml.GetBodyHTML());
}
return await context.SaveChangesAsync(token); return await context.SaveChangesAsync(token);
} }
@ -322,9 +273,6 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
return await context.SaveChangesAsync(token); return await context.SaveChangesAsync(token);
} }
// 4 trigger??
public async Task<int> AddOrReplaceFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token) public async Task<int> AddOrReplaceFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token)
{ {
if (fileMarkDto.IdMarkType != idMarkTypeApprove && if (fileMarkDto.IdMarkType != idMarkTypeApprove &&
@ -361,73 +309,18 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
if (fileMarkDto.IdMarkType == idMarkTypeReject) if (fileMarkDto.IdMarkType == idMarkTypeReject)
{ {
await RemoveDrillingProgramAsync(fileInfo.IdWell, token); await RemoveDrillingProgramAsync(fileInfo.IdWell, token);
await NotifyPublisherOnRejectAsync(fileMarkDto, token);
var user = await context.Users
.FirstOrDefaultAsync(x => x.Id == idUser, token);
var well = await context.Wells
.FirstOrDefaultAsync(x => x.Id == part.IdWell, token);
var cluster = await context.Clusters
.FirstOrDefaultAsync(x => x.Wells == well, token);
var deposit = await context.Deposits
.FirstOrDefaultAsync(x => x.Clusters == cluster, token);
var document = await context.Files
.FirstOrDefaultAsync(x => x.Id == fileMarkDto.IdFile, token);
var author = await context.Users
.FirstOrDefaultAsync(x => x.Id == document.IdAuthor, token);
var bodyHtml = new PublisherRejected
{
idWell=well.Id,
idDocument=document.Id,
coordinatingName = $"{user.Name} {user.Surname}",
coordinatingComment = fileMarkDto.Comment,
wellName = well.Caption,
clusterName = cluster.Caption,
fieldName = deposit.Caption,
documentName = document.Name,
userName = $"{author.Name} {author.Surname}"
};
emailService.EnqueueSend(new List<string> { author.Email }, bodyHtml.mailSubject, bodyHtml.GetBodyHTML());
//emailService.EnqueueSend(new List<string> { "79827873134@yandex.ru" }, bodyHtml.mailSubject, bodyHtml.GetBodyHTML());
} }
// если все части согласованы уведомляем публикатора // если все согласованты согласовали - оповещаем публикатора
if (part.RelatedUsers if (part.RelatedUsers
.Where(u => u.IdUserRole == idUserRoleApprover) .Where(u => u.IdUserRole == idUserRoleApprover)
.All(user => fileInfo.FileMarks .All(user => fileInfo.FileMarks
.Any(mark => mark.IdMarkType == idMarkTypeApprove && mark.User.Id == user.IdUser))) .Any(mark => mark.IdMarkType == idMarkTypeApprove && mark.User.Id == user.IdUser)))
{ {
var well = await context.Wells await NotifyPublisherOnFullAccepAsync(fileMarkDto, token);
.FirstOrDefaultAsync(x => x.Id == part.IdWell, token);
var cluster = await context.Clusters
.FirstOrDefaultAsync(x => x.Wells == well, token);
var deposit = await context.Deposits
.FirstOrDefaultAsync(x => x.Clusters == cluster, token);
var document = await context.Files
.FirstOrDefaultAsync(x => x.Id == fileMarkDto.IdFile, token);
var author = await context.Users
.FirstOrDefaultAsync(x => x.Id == document.IdAuthor, token);
var documentCategory = await context.FileCategories
.FirstOrDefaultAsync(x => x.Id == part.IdFileCategory, token);
var bodyHtml = new AllApprovals
{
idWell=well.Id,
idDocument=document.Id,
documentCategory = documentCategory.Name,
wellName = well.Caption,
clusterName = cluster.Caption,
fieldName = deposit.Caption,
documentName = document.Name,
userName = $"{author.Name} {author.Surname}"
};
emailService.EnqueueSend(new List<string> { author.Email }, bodyHtml.mailSubject, bodyHtml.GetBodyHTML());
//emailService.EnqueueSend(new List<string> { "79827873134@yandex.ru" }, bodyHtml.mailSubject, bodyHtml.GetBodyHTML());
} }
return result; return result;
} }
@ -448,6 +341,55 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
return result; return result;
} }
private async Task NotifyPublisherOnFullAccepAsync(FileMarkDto fileMark, CancellationToken token)
{
var file = await fileService.GetInfoAsync(fileMark.IdFile, token);
var well = await wellService.GetAsync(file.IdWell, token);
var user = file.Author;
var factory = new MailBodyFactory(configuration);
var subject = MailBodyFactory.MakeSubject(well, "Загруженный вами документ полностью согласован");
var body = factory.MakeMailBodyForPublisherOnFullAccept(well, user.Name, file.Id, file.Name);
emailService.EnqueueSend(user.Email, subject, body);
}
private async Task NotifyPublisherOnRejectAsync(FileMarkDto fileMark, CancellationToken token)
{
var file = await fileService.GetInfoAsync(fileMark.IdFile, token);
var well = await wellService.GetAsync(file.IdWell, token);
var user = file.Author;
var factory = new MailBodyFactory(configuration);
var subject = MailBodyFactory.MakeSubject(well, "Загруженный вами документ отклонен");
var body = factory.MakeMailBodyForPublisherOnReject(well, user.Name, file.Id, file.Name, fileMark);
emailService.EnqueueSend(user.Email, subject, body);
}
private async Task NotifyApproversAsync(DrillingProgramPart part, int idFile, string fileName, CancellationToken token)
{
var well = await wellService.GetAsync(part.IdWell, token);
var factory = new MailBodyFactory(configuration);
var subject = MailBodyFactory.MakeSubject(well, "Загружен новый документ для согласования.");
var users = part.RelatedUsers
.Where(r => r.IdUserRole == idUserRoleApprover)
.Select(r => r.User);
foreach (var user in users)
{
var body = factory.MakeMailBodyForApproverNewFile(well, user.Name, idFile, fileName);
emailService.EnqueueSend(user.Email, subject, body);
}
}
private async Task NotifyNewPublisherAsync(int idWell, UserDto user, string documentCategory, CancellationToken token)
{
var well = await wellService.GetAsync(idWell, token);
var factory = new MailBodyFactory(configuration);
var subject = MailBodyFactory.MakeSubject(well, $"От вас ожидается загрузка на портал документа «{documentCategory}»");
var body = factory.MakeMailBodyForNewPublisher(well, user.Name, documentCategory);
emailService.EnqueueSend(user.Email, subject, body);
}
private DrillingProgramPartDto ConvertPart(int idUser, List<FileCategory> fileCategories, List<AsbCloudDb.Model.FileInfo> files, DrillingProgramPart partEntity, double timezoneOffset) private DrillingProgramPartDto ConvertPart(int idUser, List<FileCategory> fileCategories, List<AsbCloudDb.Model.FileInfo> files, DrillingProgramPart partEntity, double timezoneOffset)
{ {
var part = new DrillingProgramPartDto var part = new DrillingProgramPartDto

File diff suppressed because one or more lines are too long

View File

@ -34,6 +34,9 @@ namespace AsbCloudInfrastructure.Services
this.backgroundWorker = backgroundWorker; this.backgroundWorker = backgroundWorker;
} }
public void EnqueueSend(string address, string subject, string htmlBody)
=> new List<string> { address };
public void EnqueueSend(IEnumerable<string> addresses, string subject, string htmlBody) public void EnqueueSend(IEnumerable<string> addresses, string subject, string htmlBody)
{ {
if (!IsConfigured) if (!IsConfigured)
@ -86,9 +89,7 @@ namespace AsbCloudInfrastructure.Services
client.UseDefaultCredentials = false; client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(sender, smtpPassword); client.Credentials = new System.Net.NetworkCredential(sender, smtpPassword);
// TODO: uncomment next when tested
await client.SendMailAsync(message, token); await client.SendMailAsync(message, token);
await Task.Delay(0);
Trace.TraceInformation($"Send email to {string.Join(',', addresses)} subj:{subject} html body count {htmlBody.Count()}"); Trace.TraceInformation($"Send email to {string.Join(',', addresses)} subj:{subject} html body count {htmlBody.Count()}");
}; };
return func; return func;

View File

@ -0,0 +1,122 @@
using AsbCloudApp.Data;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
namespace AsbCloudInfrastructure
{
class MailBodyFactory
{
protected readonly string platformName;
private readonly string platformUrl;
protected readonly string companyName;
protected readonly string supportMail;
public MailBodyFactory(IConfiguration configuration)
{
platformName = configuration.GetValue("email:platformName", "Цифровое бурение");
platformUrl = configuration.GetValue("email:platformUrl", "");
companyName = configuration.GetValue("email:companyName", "ООО \"Цифровое бурение\"");
supportMail = configuration.GetValue("email:supportMail", "support@digitaldrilling.ru");
}
public static string MakeSubject(WellDto well, string action)
{
var subj = $"{well.Deposit}, {well.Cluster}, {well.Caption}. Программа бурения. {action}";
return subj;
}
public string MakeMailBodyForNewPublisher(WellDto well, string userName, string documentCategory)
{
var drillingProgramHref = MakeDrillingProgramHref(well);
var body = $"<html><body><h2>Здравствуйте, {userName}.<h2>" +
$"На портале {platformName} началось создание программы бурения скважины {drillingProgramHref}," +
$" куст {well.Cluster}, месторождение {well.Deposit}." +
$"<br><br>От вас ожидается загрузка на портал документа «{documentCategory}» в формате excel (*.xlsx)." +
MakeSignatue() +
$"</body></html>";
return body;
}
public string MakeMailBodyForApproverNewFile(WellDto well, string userName, int idFile, string fileName)
{
var fileDownloadHref = MakeFileDownloadHref(well.Id, idFile, fileName);
var drillingProgramHref = MakeDrillingProgramHref(well);
var body = $"<html><body><h2>Здравствуйте, {userName}.<h2>" +
$"На портал {platformName} загружен документ {fileDownloadHref}" +
$" для согласования при создании программы бурения скважины {drillingProgramHref}, куст ({well.Cluster})" +
$", месторождение ({well.Deposit}).<br>" +
MakeSignatue() +
$"</body></html>";
return body;
}
public string MakeMailBodyForPublisherOnReject(WellDto well, string userName, int idFile, string fileName, FileMarkDto fileMark)
{
var fileDownloadHref = MakeFileDownloadHref(well.Id, idFile, fileName);
var drillingProgramHref = MakeDrillingProgramHref(well);
var body = $"<html><body><h2>Здравствуйте, {userName}.<h2>" +
$"На портале {platformName} отклонен загруженный вами документ {fileDownloadHref} " +
$" по программе бурения скважины {drillingProgramHref}," +
$" куст {well.Cluster}, месторождение {well.Deposit}." +
$" Комментарий согласующего ({fileMark.User.Name} {fileMark.User.Surname}):<br>{fileMark.Comment}" +
MakeSignatue() +
$"</body></html>";
return body;
}
public string MakeMailBodyForPublisherOnFullAccept(WellDto well, string userName, int idFile, string fileName)
{
var fileDownloadHref = MakeFileDownloadHref(well.Id, idFile, fileName);
var drillingProgramHref = MakeDrillingProgramHref(well);
var body = $"<html><body><h2>Здравствуйте, {userName}.<h2>" +
$"На портале {platformName} полностью согласован документ {fileDownloadHref} " +
$" по программе бурения скважины {drillingProgramHref}," +
$" куст {well.Cluster}, месторождение {well.Deposit}." +
MakeSignatue() +
$"</body></html>";
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 drillingProgramHref = MakeHref(drillingProgramUrl, well.Caption);
return drillingProgramHref;
}
private static string MakeHref(string url, string text)
=> $"<a href=\"{url}\">{text}</a>";
protected string MakeSignatue()
{
var logo = GetImageBase64("logo_32.png");
var sign = $"<br>---<br><img src=\"{logo}\"/>" +
$"{companyName}<br>Это письмо сформировано автоматически.<br>Для получения помощи по работе на портале " +
$"{platformName} обращайтесь по адресу {supportMail}";
return sign;
}
public static string GetImageBase64(string resourceFileName)
{
if (string.IsNullOrEmpty(resourceFileName))
return null;
var format = Path.GetExtension(resourceFileName).Trim('.');
var logoFilePath = Path.Combine("Res" + resourceFileName);
var imageBytes = File.ReadAllBytes(logoFilePath);
return "data:image/" + format + ";base64," + Convert.ToBase64String(imageBytes);
}
}
}