добавлено логгирование

This commit is contained in:
gorbenko.pavel
2026-08-19 12:15:52 +05:00
parent 9d59074289
commit cf44ae3e2e
5 changed files with 30 additions and 3 deletions
@@ -1,12 +1,14 @@
using FluentResults;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Tnsu.ApprovalRoute.Application.Common.Errors;
using Tnsu.ApprovalRoute.Application.Common.Interfaces;
using Tnsu.ApprovalRoute.Domain.Entities;
using Tnsu.ApprovalRoute.Infrastructure.Persistence;
namespace Tnsu.ApprovalRoute.Application.RequestTypes;
public sealed class RequestTypeService(ApprovalRouteDbContext db)
public sealed class RequestTypeService(ApprovalRouteDbContext db, ICurrentUser currentUser, ILogger<RequestTypeService> logger)
{
public async Task<Result<IReadOnlyList<RequestTypeDto>>> ListAsync(CancellationToken cancellationToken)
{
@@ -45,6 +47,7 @@ public sealed class RequestTypeService(ApprovalRouteDbContext db)
};
db.RequestTypes.Add(entity);
await db.SaveChangesAsync(cancellationToken);
logger.LogInformation("Тип заявки {RequestTypeId} создан пользователем {Author}", entity.Id, currentUser.Email);
return Result.Ok(ToDto(entity));
}
@@ -67,6 +70,7 @@ public sealed class RequestTypeService(ApprovalRouteDbContext db)
entity.SlaHours = request.SlaHours;
entity.AskOverdueReason = request.AskOverdueReason;
await db.SaveChangesAsync(cancellationToken);
logger.LogInformation("Тип заявки {RequestTypeId} изменён пользователем {Author}", entity.Id, currentUser.Email);
return Result.Ok(ToDto(entity));
}
@@ -95,6 +99,7 @@ public sealed class RequestTypeService(ApprovalRouteDbContext db)
entity.SlaHours = request.SlaHours ?? entity.SlaHours;
entity.AskOverdueReason = request.AskOverdueReason ?? entity.AskOverdueReason;
await db.SaveChangesAsync(cancellationToken);
logger.LogInformation("Тип заявки {RequestTypeId} изменён пользователем {Author}", entity.Id, currentUser.Email);
return Result.Ok(ToDto(entity));
}
@@ -107,6 +112,7 @@ public sealed class RequestTypeService(ApprovalRouteDbContext db)
db.RequestTypes.Remove(entity);
await db.SaveChangesAsync(cancellationToken);
logger.LogInformation("Тип заявки {RequestTypeId} удалён пользователем {Author}", id, currentUser.Email);
return Result.Ok();
}
@@ -1,13 +1,16 @@
using FluentResults;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Tnsu.ApprovalRoute.Application.Common.Errors;
using Tnsu.ApprovalRoute.Application.Common.Interfaces;
using Tnsu.ApprovalRoute.Domain.Entities;
using Tnsu.ApprovalRoute.Domain.Enums;
using Tnsu.ApprovalRoute.Infrastructure.Persistence;
namespace Tnsu.ApprovalRoute.Application.TemplateRouteSteps;
public sealed class TemplateRouteStepService(ApprovalRouteDbContext db)
public sealed class TemplateRouteStepService(
ApprovalRouteDbContext db, ICurrentUser currentUser, ILogger<TemplateRouteStepService> logger)
{
public async Task<Result<IReadOnlyList<TemplateRouteStepDto>>> ListAsync(int routeId, CancellationToken cancellationToken)
{
@@ -79,6 +82,8 @@ public sealed class TemplateRouteStepService(ApprovalRouteDbContext db)
};
db.TemplateRouteSteps.Add(entity);
await db.SaveChangesAsync(cancellationToken);
logger.LogInformation(
"Шаг {StepId} маршрута {RouteId} создан пользователем {Author}", entity.Id, route.Id, currentUser.Email);
return Result.Ok(await ToDtoWithWarningAsync(entity, route, cancellationToken));
}
@@ -130,6 +135,8 @@ public sealed class TemplateRouteStepService(ApprovalRouteDbContext db)
entity.SlaHours = r.SlaHours;
entity.AskOverdueReason = r.AskOverdueReason;
await db.SaveChangesAsync(cancellationToken);
logger.LogInformation(
"Шаг {StepId} маршрута {RouteId} изменён пользователем {Author}", entity.Id, route.Id, currentUser.Email);
return Result.Ok(await ToDtoWithWarningAsync(entity, route, cancellationToken));
}
@@ -199,6 +206,8 @@ public sealed class TemplateRouteStepService(ApprovalRouteDbContext db)
entity.SlaHours = r.SlaHours ?? entity.SlaHours;
entity.AskOverdueReason = r.AskOverdueReason ?? entity.AskOverdueReason;
await db.SaveChangesAsync(cancellationToken);
logger.LogInformation(
"Шаг {StepId} маршрута {RouteId} изменён пользователем {Author}", entity.Id, route.Id, currentUser.Email);
return Result.Ok(await ToDtoWithWarningAsync(entity, route, cancellationToken));
}
@@ -211,6 +220,8 @@ public sealed class TemplateRouteStepService(ApprovalRouteDbContext db)
db.TemplateRouteSteps.Remove(entity);
await db.SaveChangesAsync(cancellationToken);
logger.LogInformation(
"Шаг {StepId} маршрута {RouteId} удалён пользователем {Author}", id, routeId, currentUser.Email);
return Result.Ok();
}
@@ -1,6 +1,7 @@
using System.Globalization;
using FluentResults;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Tnsu.ApprovalRoute.Application.Common.Errors;
using Tnsu.ApprovalRoute.Application.Common.Interfaces;
@@ -15,7 +16,8 @@ public sealed class TemplateRouteService(
ApprovalRouteDbContext db,
ITnsuPortalClient portalClient,
ICurrentUser currentUser,
IOptions<TnsuPortalOptions> portalOptions)
IOptions<TnsuPortalOptions> portalOptions,
ILogger<TemplateRouteService> logger)
{
public async Task<Result<IReadOnlyList<TemplateRouteDto>>> ListAsync(int? requestTypeId, CancellationToken cancellationToken)
{
@@ -61,6 +63,7 @@ public sealed class TemplateRouteService(
};
db.TemplateRoutes.Add(entity);
await db.SaveChangesAsync(cancellationToken);
logger.LogInformation("Маршрут {RouteId} создан пользователем {Author}", entity.Id, currentUser.Email);
return Result.Ok(ToDto(entity));
}
@@ -81,6 +84,7 @@ public sealed class TemplateRouteService(
entity.ByProject = r.ByProject;
entity.RequiresControl = r.RequiresControl;
await db.SaveChangesAsync(cancellationToken);
logger.LogInformation("Маршрут {RouteId} изменён пользователем {Author}", entity.Id, currentUser.Email);
return Result.Ok(ToDto(entity));
}
@@ -105,6 +109,7 @@ public sealed class TemplateRouteService(
entity.ByProject = byProject;
entity.RequiresControl = r.RequiresControl ?? entity.RequiresControl;
await db.SaveChangesAsync(cancellationToken);
logger.LogInformation("Маршрут {RouteId} изменён пользователем {Author}", entity.Id, currentUser.Email);
return Result.Ok(ToDto(entity));
}
@@ -117,6 +122,7 @@ public sealed class TemplateRouteService(
db.TemplateRoutes.Remove(entity);
await db.SaveChangesAsync(cancellationToken);
logger.LogInformation("Маршрут {RouteId} удалён пользователем {Author}", id, currentUser.Email);
return Result.Ok();
}
@@ -31,6 +31,9 @@ public sealed class TemplateRouteServiceTests : IClassFixture<PostgresDatabaseFi
portalClient
.Setup(x => x.GetProjectExecutorsAsync(It.IsAny<Guid>(), It.IsAny<CancellationToken>()))
.ReturnsAsync([new ProjectExecutorInfo(ProjectRole.ProjectManager, "pm@tnsu.kz")]);
portalClient
.Setup(x => x.GetEmployeeByPositionAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new EmployeeLookupResult(false, null, null));
_services = TestServiceProviderFactory.Build(_connectionString, portalClient: portalClient.Object);
_service = _services.GetRequiredService<TemplateRouteService>();
@@ -22,6 +22,7 @@ internal static class TestServiceProviderFactory
.Build();
var services = new ServiceCollection();
services.AddLogging();
services.AddApprovalRouteInfrastructure(configuration);
services.AddApprovalRouteApplication();
services.AddSingleton<ICurrentUser>(new TestCurrentUser(currentUserEmail));