forked from ddrilling/AsbCloudServer
Complete from Ivanov
This commit is contained in:
parent
6aae972aba
commit
6c845b7f9f
@ -24,6 +24,8 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
|
||||
private readonly IWellService wellService;
|
||||
private readonly IConfiguration configuration;
|
||||
private readonly IBackgroundWorkerService backgroundWorker;
|
||||
//email
|
||||
private readonly IEmailService emailService;
|
||||
private readonly string connectionString;
|
||||
|
||||
private const int idFileCategoryDrillingProgram = 1000;
|
||||
@ -52,8 +54,9 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
|
||||
IUserService userService,
|
||||
IWellService wellService,
|
||||
IConfiguration configuration,
|
||||
IBackgroundWorkerService backgroundWorker)
|
||||
{
|
||||
IBackgroundWorkerService backgroundWorker,
|
||||
IEmailService emailService)
|
||||
{
|
||||
this.context = context;
|
||||
this.fileService = fileService;
|
||||
this.userService = userService;
|
||||
@ -61,6 +64,7 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
|
||||
this.configuration = configuration;
|
||||
this.backgroundWorker = backgroundWorker;
|
||||
this.connectionString = configuration.GetConnectionString("DefaultConnection");
|
||||
this.emailService = emailService;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<UserDto>> GetAvailableUsers(int idWell, CancellationToken token = default)
|
||||
@ -154,7 +158,8 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
|
||||
await TryEnqueueMakeProgramAsync(idWell, state, token);
|
||||
return state;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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
|
||||
@ -176,6 +181,35 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
|
||||
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()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
@ -242,10 +276,35 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
|
||||
IdDrillingProgramPart = part.Id,
|
||||
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);
|
||||
if (idUserRole == idUserRoleApprover)
|
||||
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());
|
||||
//emailService.EnqueueSend(new List<string> { "79827873134@yandex.ru" }, bodyHtml.mailSubject, bodyHtml.GetBodyHTML());
|
||||
}
|
||||
return await context.SaveChangesAsync(token);
|
||||
}
|
||||
|
||||
@ -263,6 +322,9 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
|
||||
return await context.SaveChangesAsync(token);
|
||||
}
|
||||
|
||||
// 4 trigger??
|
||||
|
||||
|
||||
public async Task<int> AddOrReplaceFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token)
|
||||
{
|
||||
if (fileMarkDto.IdMarkType != idMarkTypeApprove &&
|
||||
@ -297,7 +359,74 @@ namespace AsbCloudInfrastructure.Services.DrillingProgram
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (fileMarkDto.IdMarkType == idMarkTypeReject)
|
||||
{
|
||||
await RemoveDrillingProgramAsync(fileInfo.IdWell, 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
|
||||
.Where(u => u.IdUserRole == idUserRoleApprover)
|
||||
.All(user => fileInfo.FileMarks
|
||||
.Any(mark => mark.IdMarkType == idMarkTypeApprove && mark.User.Id == user.IdUser)))
|
||||
{
|
||||
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 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;
|
||||
}
|
||||
|
96
AsbCloudInfrastructure/Services/Email/CreationBody.cs
Normal file
96
AsbCloudInfrastructure/Services/Email/CreationBody.cs
Normal file
File diff suppressed because one or more lines are too long
@ -76,8 +76,9 @@ namespace AsbCloudInfrastructure.Services
|
||||
foreach (var mailAddress in mailAddresses)
|
||||
message.To.Add(mailAddress);
|
||||
|
||||
message.Subject = subject;
|
||||
|
||||
message.Body = htmlBody;
|
||||
message.Subject = subject;
|
||||
message.IsBodyHtml = true;
|
||||
|
||||
var client = new SmtpClient(smtpServer);
|
||||
@ -86,9 +87,9 @@ namespace AsbCloudInfrastructure.Services
|
||||
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}");
|
||||
Trace.TraceInformation($"Send email to {string.Join(',', addresses)} subj:{subject} html body count {htmlBody.Count()}");
|
||||
};
|
||||
return func;
|
||||
}
|
BIN
AsbCloudInfrastructure/Services/Email/logo_32.png
Normal file
BIN
AsbCloudInfrastructure/Services/Email/logo_32.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.7 KiB |
@ -83,6 +83,7 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
private readonly Mock<IWellService> wellServiceMock;
|
||||
private readonly Mock<IConfiguration> configurationMock;
|
||||
private readonly Mock<IBackgroundWorkerService> backgroundWorkerMock;
|
||||
private readonly Mock<IEmailService> emailService;
|
||||
|
||||
public DrillingProgramServiceTest()
|
||||
{
|
||||
@ -112,7 +113,8 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
userServiceMock.Object,
|
||||
wellServiceMock.Object,
|
||||
configurationMock.Object,
|
||||
backgroundWorkerMock.Object);
|
||||
backgroundWorkerMock.Object,
|
||||
emailService.Object);
|
||||
|
||||
var users = await service.GetAvailableUsers(idWell, CancellationToken.None);
|
||||
|
||||
@ -128,7 +130,8 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
userServiceMock.Object,
|
||||
wellServiceMock.Object,
|
||||
configurationMock.Object,
|
||||
backgroundWorkerMock.Object);
|
||||
backgroundWorkerMock.Object,
|
||||
emailService.Object);
|
||||
|
||||
var result = await service.AddPartsAsync(idWell, new int[] { 1001, 1002 }, CancellationToken.None);
|
||||
|
||||
@ -146,7 +149,8 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
userServiceMock.Object,
|
||||
wellServiceMock.Object,
|
||||
configurationMock.Object,
|
||||
backgroundWorkerMock.Object);
|
||||
backgroundWorkerMock.Object,
|
||||
emailService.Object);
|
||||
|
||||
var result = await service.RemovePartsAsync(idWell, new int[] { 1005 }, CancellationToken.None);
|
||||
|
||||
@ -168,7 +172,8 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
userServiceMock.Object,
|
||||
wellServiceMock.Object,
|
||||
configurationMock.Object,
|
||||
backgroundWorkerMock.Object);
|
||||
backgroundWorkerMock.Object,
|
||||
emailService.Object);
|
||||
|
||||
var result = await service.AddUserAsync(idWell, 1001, publisher1.Id, 1, CancellationToken.None);
|
||||
|
||||
@ -201,7 +206,8 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
userServiceMock.Object,
|
||||
wellServiceMock.Object,
|
||||
configurationMock.Object,
|
||||
backgroundWorkerMock.Object);
|
||||
backgroundWorkerMock.Object,
|
||||
emailService.Object);
|
||||
|
||||
var result = await service.RemoveUserAsync(idWell, idFileCategory, publisher1.Id, idUserRole, CancellationToken.None);
|
||||
|
||||
@ -226,7 +232,8 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
userServiceMock.Object,
|
||||
wellServiceMock.Object,
|
||||
configurationMock.Object,
|
||||
backgroundWorkerMock.Object);
|
||||
backgroundWorkerMock.Object,
|
||||
emailService.Object);
|
||||
|
||||
var fileMark = new FileMarkDto
|
||||
{
|
||||
@ -256,7 +263,8 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
userServiceMock.Object,
|
||||
wellServiceMock.Object,
|
||||
configurationMock.Object,
|
||||
backgroundWorkerMock.Object);
|
||||
backgroundWorkerMock.Object,
|
||||
emailService.Object);
|
||||
var fileMark = new FileMarkDto
|
||||
{
|
||||
IdFile = file1001.Id,
|
||||
@ -293,7 +301,8 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
userServiceMock.Object,
|
||||
wellServiceMock.Object,
|
||||
configurationMock.Object,
|
||||
backgroundWorkerMock.Object);
|
||||
backgroundWorkerMock.Object,
|
||||
emailService.Object);
|
||||
|
||||
var fileMark = new FileMarkDto
|
||||
{
|
||||
@ -319,7 +328,8 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
userServiceMock.Object,
|
||||
wellServiceMock.Object,
|
||||
configurationMock.Object,
|
||||
backgroundWorkerMock.Object);
|
||||
backgroundWorkerMock.Object,
|
||||
emailService.Object);
|
||||
|
||||
var state = await service.GetStateAsync(idWell, publisher1.Id, CancellationToken.None);
|
||||
|
||||
@ -345,7 +355,8 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
userServiceMock.Object,
|
||||
wellServiceMock.Object,
|
||||
configurationMock.Object,
|
||||
backgroundWorkerMock.Object);
|
||||
backgroundWorkerMock.Object,
|
||||
emailService.Object);
|
||||
|
||||
var state = await service.GetStateAsync(idWell, publisher1.Id, CancellationToken.None);
|
||||
|
||||
@ -374,7 +385,8 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
userServiceMock.Object,
|
||||
wellServiceMock.Object,
|
||||
configurationMock.Object,
|
||||
backgroundWorkerMock.Object);
|
||||
backgroundWorkerMock.Object,
|
||||
emailService.Object);
|
||||
|
||||
var state = await service.GetStateAsync(idWell, publisher1.Id, CancellationToken.None);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user