53 lines
3.0 KiB
C#
53 lines
3.0 KiB
C#
using FluentValidation;
|
|
using Tnsu.ApprovalRoute.Domain.Enums;
|
|
|
|
namespace Tnsu.ApprovalRoute.Application.TemplateRouteSteps;
|
|
|
|
public sealed class CreateTemplateRouteStepRequestValidator : AbstractValidator<CreateTemplateRouteStepRequest>
|
|
{
|
|
public CreateTemplateRouteStepRequestValidator()
|
|
{
|
|
RuleFor(x => x.Status).NotEqual(RouteStepStatus.None).WithMessage("Статус обязателен: Approval или Execution.");
|
|
RuleFor(x => x)
|
|
.Must(x => TemplateRouteStepFields.CountFilled(x.ExecutorEmail, x.Position, x.ProjectRole, x.HeadType) == 1)
|
|
.WithMessage("Должно быть заполнено ровно одно из полей: исполнитель, должность, роль в проекте или тип руководителя.");
|
|
RuleFor(x => x)
|
|
.Must(x => TemplateRouteStepFields.IsConditionValid(x.FieldName, x.Operation, x.Value))
|
|
.WithMessage("Поля условия (field_name, operation, value) должны быть заполнены все вместе или не заполнены вовсе.");
|
|
}
|
|
}
|
|
|
|
public sealed class UpdateTemplateRouteStepRequestValidator : AbstractValidator<UpdateTemplateRouteStepRequest>
|
|
{
|
|
public UpdateTemplateRouteStepRequestValidator()
|
|
{
|
|
RuleFor(x => x.Status).NotEqual(RouteStepStatus.None).WithMessage("Статус обязателен: Approval или Execution.");
|
|
RuleFor(x => x)
|
|
.Must(x => TemplateRouteStepFields.CountFilled(x.ExecutorEmail, x.Position, x.ProjectRole, x.HeadType) == 1)
|
|
.WithMessage("Должно быть заполнено ровно одно из полей: исполнитель, должность, роль в проекте или тип руководителя.");
|
|
RuleFor(x => x)
|
|
.Must(x => TemplateRouteStepFields.IsConditionValid(x.FieldName, x.Operation, x.Value))
|
|
.WithMessage("Поля условия (field_name, operation, value) должны быть заполнены все вместе или не заполнены вовсе.");
|
|
}
|
|
}
|
|
|
|
internal static class TemplateRouteStepFields
|
|
{
|
|
public static int CountFilled(string? executorEmail, string? position, ProjectRole? projectRole, HeadType? headType) =>
|
|
(string.IsNullOrWhiteSpace(executorEmail) ? 0 : 1) +
|
|
(string.IsNullOrWhiteSpace(position) ? 0 : 1) +
|
|
(projectRole is null or ProjectRole.None ? 0 : 1) +
|
|
(headType is null or HeadType.None ? 0 : 1);
|
|
|
|
public static int CountConditionFilled(string? fieldName, ComparisonOperator? operation, string? value) =>
|
|
(string.IsNullOrWhiteSpace(fieldName) ? 0 : 1) +
|
|
(operation is null or ComparisonOperator.None ? 0 : 1) +
|
|
(string.IsNullOrWhiteSpace(value) ? 0 : 1);
|
|
|
|
public static bool IsConditionValid(string? fieldName, ComparisonOperator? operation, string? value)
|
|
{
|
|
var count = CountConditionFilled(fieldName, operation, value);
|
|
return count == 0 || count == 3;
|
|
}
|
|
}
|