feat(api): обновлен AppInWorkController под новый респонс, валидация для галки IsAutoDistributionEnabled

This commit is contained in:
Mikhail Trubnikov
2024-07-03 10:16:54 +10:00
parent cf5a465546
commit ff58a21cc5
11 changed files with 172 additions and 9 deletions

View File

@@ -1,5 +1,6 @@
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using PARR.API.Contracts.V1.Requests;
using PARR.DAL.Services.Interfaces;
@@ -11,18 +12,21 @@ namespace PARR.API.Validators
private readonly IWorkService workService;
private readonly IEsppSchTypeConfigService esppSchTypeConfigService;
private readonly IWorkGroupService workGroupService;
private readonly IEsppSchTypeValueService esppSchTypeValueService;
public ApplicationInWorkValidator(
IApplicationsInWorkService applicationsInWorkService,
IWorkService workService,
IEsppSchTypeConfigService esppSchTypeConfigService,
IWorkGroupService workGroupService
IWorkGroupService workGroupService,
IEsppSchTypeValueService esppSchTypeValueService
)
{
this.applicationsInWorkService = applicationsInWorkService;
this.workService = workService;
this.esppSchTypeConfigService = esppSchTypeConfigService;
this.workGroupService = workGroupService;
this.esppSchTypeValueService = esppSchTypeValueService;
RuleFor(t => t.TemplateDuration)
.NotNull().NotEmpty()//.WithMessage("Длительность данного задания на выполнение работ не может быть пустым")
@@ -96,8 +100,36 @@ namespace PARR.API.Validators
RuleFor(t => t.WorkGroups)
.MustAsync(async (entity, value, c) => await IsWorkGroupsExistAsync(entity))
.WithMessage("Указаные несуществующие Id рабочих групп");
//Автораспределение может быть включено, только если у EsppSchTypeValues не пустое поле DistributionPeriodId
RuleFor(t => t.IsAutoDistributionEnabled)
.MustAsync(async (entity, value, c) => await IsAllowAutoDistributionEnabledAsync(entity, value))
.WithMessage("Нельзя включить автораспределение для этого типа расписания");
}
private async Task<bool> IsAllowAutoDistributionEnabledAsync(ApplicationInWorkRequest entity, bool value)
{
//Автораспределение может быть включено, только если у EsppSchTypeValues не пустое поле DistributionPeriodId
if (value == false)
return true;
//Распределение включено, смотрим, разрешено ли оно в расписании
//по идее, это расписание только с одним значением в EsppSchValues, но мы проверим у всех, но такого быть не может по хорошему
foreach (var item in entity.Schedule)
{
var schVal = await esppSchTypeValueService.GetAsync(item.TypeValueId);
if (schVal != null)
if (schVal.DistributionPeriodId == null)
return false;
}
return true;
}
private async Task<bool> IsWorkGroupsExistAsync(ApplicationInWorkRequest entity)
{
var result = await workGroupService.Get().Where(t => entity.WorkGroups.Contains(t.Id)).ToListAsync();