feat(api): Работы - управление автоконтролем и кол-вом связей
This commit is contained in:
@@ -1,31 +1,32 @@
|
||||
using FluentValidation;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using PARR.API.Contracts.V1.Requests;
|
||||
using PARR.Core.Repositories.Interfaces;
|
||||
using PARR.Core.Repositories.Interfaces.Job;
|
||||
using PARR.Core.Repositories.Interfaces.Unit;
|
||||
using PARR.Domain.Entities.JobGroupEntities;
|
||||
|
||||
namespace PARR.API.Validators
|
||||
{
|
||||
public class JobRequestValidator : AbstractValidator<JobRequest>
|
||||
{
|
||||
private readonly ITnkRepository tnkService;
|
||||
private readonly IJobGroupRepository jobGroupService;
|
||||
private readonly IJobRepository jobRepository;
|
||||
private readonly IUnitFieldRepository unitFieldService;
|
||||
private JobGroup? jobGroup;
|
||||
private readonly ITnkRepository _tnkRepository;
|
||||
private readonly IJobGroupRepository _jobGroupRepository;
|
||||
private readonly IJobRepository _jobRepository;
|
||||
private readonly IUnitFieldRepository _unitFieldRepository;
|
||||
//private JobGroup? jobGroup;
|
||||
|
||||
public JobRequestValidator(
|
||||
ITnkRepository tnkService,
|
||||
IJobGroupRepository jobGroupService,
|
||||
IJobRepository jobService,
|
||||
IUnitFieldRepository unitFieldService
|
||||
ITnkRepository tnkRepository,
|
||||
IJobGroupRepository jobGroupRepository,
|
||||
IJobRepository jobRepository,
|
||||
IUnitFieldRepository unitFieldRepository
|
||||
)
|
||||
{
|
||||
this.tnkService = tnkService;
|
||||
this.jobGroupService = jobGroupService;
|
||||
this.jobRepository = jobService;
|
||||
this.unitFieldService = unitFieldService;
|
||||
_tnkRepository = tnkRepository;
|
||||
_jobGroupRepository = jobGroupRepository;
|
||||
_jobRepository = jobRepository;
|
||||
_unitFieldRepository = unitFieldRepository;
|
||||
|
||||
RuleFor(t => t.Name)
|
||||
.NotNull().NotEmpty();
|
||||
@@ -48,14 +49,54 @@ namespace PARR.API.Validators
|
||||
.MustAsync(async (entity, value, c) => await IsUnitFiltersCorrect(entity))
|
||||
.WithMessage("Неверно заданы параметры фильтров. Внимательнее, пожалуйста!");
|
||||
|
||||
RuleFor(t => t.MinValueRelationships)
|
||||
.GreaterThanOrEqualTo(0);
|
||||
RuleFor(t => t.Relationships)
|
||||
.MustAsync(async (entity, value, c) =>
|
||||
{
|
||||
// Relationships должны быть обязательно заполнены если в типе работ IsRelationshipsAllowed==true
|
||||
var relationshipsIsRequired = await jobGroupRepository.Get()
|
||||
.AnyAsync(t => t.Id == entity.GroupId && t.GroupType!.IsRelationshipsAllowed, c);
|
||||
|
||||
if (relationshipsIsRequired && value != null)
|
||||
return true;
|
||||
|
||||
if (!relationshipsIsRequired && value == null)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
})
|
||||
.WithMessage("Некорректные настройки кол-ва связей");
|
||||
|
||||
When(t => t.Relationships != null, () =>
|
||||
{
|
||||
RuleFor(t => t.Relationships!.MinValueRelationships)
|
||||
.GreaterThanOrEqualTo(0);
|
||||
|
||||
RuleFor(t => t.Relationships!.MaxValueRelationships)
|
||||
.GreaterThanOrEqualTo(t => t.Relationships!.MinValueRelationships);
|
||||
});
|
||||
|
||||
RuleFor(t => t.MaxValueRelationships)
|
||||
.GreaterThanOrEqualTo(t => t.MinValueRelationships);
|
||||
|
||||
RuleFor(t => t.ResponseAreaMask)
|
||||
.NotNull().NotEmpty();
|
||||
|
||||
RuleFor(t => t.AutoControl)
|
||||
.MustAsync(async (entity, value, c) =>
|
||||
{
|
||||
// Автоконтроль разрешен только типам работ, у которых выключен IsJobGroupAutoControl
|
||||
var isAllowedJobAutocontrol = await jobGroupRepository.Get()
|
||||
.AnyAsync(t => t.Id == entity.GroupId && t.GroupType!.IsJobGroupAutoControl == false, c);
|
||||
|
||||
// Разрешен автоконтроль и есть значение
|
||||
if (isAllowedJobAutocontrol && value != null)
|
||||
return true;
|
||||
|
||||
// Запрещен автоконтроль и нет значения
|
||||
if (!isAllowedJobAutocontrol && value == null)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
})
|
||||
.WithMessage("Некорректные параметры автоконтроля");
|
||||
}
|
||||
|
||||
private async Task<bool> IsUnitFiltersCorrect(JobRequest entity)
|
||||
@@ -72,7 +113,7 @@ namespace PARR.API.Validators
|
||||
foreach (var fieldFilter in unitFilter.FieldFilters)
|
||||
{
|
||||
var fieldId = fieldFilter.FieldId;
|
||||
if (await unitFieldService.GetAsync(fieldId) == null)
|
||||
if (await _unitFieldRepository.GetAsync(fieldId) == null)
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -80,7 +121,7 @@ namespace PARR.API.Validators
|
||||
foreach (var relationshipFilter in unitFilter.RelationshipFilters!)
|
||||
{
|
||||
var fieldId = relationshipFilter.FieldId;
|
||||
if (await unitFieldService.GetAsync(fieldId) == null)
|
||||
if (await _unitFieldRepository.GetAsync(fieldId) == null)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -90,7 +131,7 @@ namespace PARR.API.Validators
|
||||
|
||||
private async Task<bool> IsGroupExist(JobRequest entity)
|
||||
{
|
||||
jobGroup = await jobGroupService.GetAsync(entity.GroupId);
|
||||
var jobGroup = await _jobGroupRepository.GetAsync(entity.GroupId);
|
||||
|
||||
return jobGroup != null;
|
||||
}
|
||||
@@ -98,7 +139,7 @@ namespace PARR.API.Validators
|
||||
|
||||
private async Task<bool> IsTnkExist(JobRequest entity)
|
||||
{
|
||||
return await tnkService.GetAsync(entity.TnkId) != null;
|
||||
return await _tnkRepository.GetAsync(entity.TnkId) != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user