namespace UGSK.K3.Product.Contract.ValidationRules.Services
public interface IPreviousContractControllerService
bool IsPreviousContractProlonged(int previousContractId);
namespace UGSK.K3.Product.ConstructorHome.ValidationRules.Services
public class PreviousContractControllerService : IPreviousContractControllerService
private IRepositoryWithLimitedPermissions<ConstructorHomeContract> _constructorHomeContracts;
public PreviousContractControllerService(IRepositoryWithLimitedPermissions<ConstructorHomeContract> constructorHomeContracts)
_constructorHomeContracts = constructorHomeContracts;
public bool IsPreviousContractProlonged(int previousContractId)
var contracts = _constructorHomeContracts.GetAll().Where(c => c.PreviousContract.Id == previousContractId).ToList();
foreach (var contract in contracts)
namespace UGSK.K3.Product.ConstructorHome.ValidationRules.PreCalculationValidation
public class PreCalculationContractValidator : AbstractValidator<ConstructorHomeContract>, IPreCalculationContractValidator<ConstructorHomeContract>
public PreCalculationContractValidator(IEmployeeGetter employeeGetter, IPreviousContractControllerService previousContractControllerService)
#region Проверки при пролонгации
var currentEmployee = employeeGetter.GetEmployee();
When(c => !currentEmployee.IsUnderwriter() && !currentEmployee.IsHeadquartersCurator(), () =>
When(c => c.PreviousContract != null, () =>
RuleFor(c => c.PreviousContract)
.Must((contract, previousContract) =>
!previousContractControllerService.IsPreviousContractProlonged(previousContract.Id))
.WithMessage("Предыдущий договор по указанному номеру уже пролонгирован")
.OverridePropertyName("PreviousContract");
namespace UGSK.K3.Product.ConstructorHome.Test
public class PreCalculationContractValidatorTest
private PreCalculationContractValidator _target;
private Mock<IEmployeeGetter> _mockEmployeeGetter;
private Employee _employee;
private Mock<IPreviousContractControllerService> _previousContractControllerService;
_mockEmployeeGetter = new Mock<IEmployeeGetter>();
_employee = new Employee { Position = new Position { CompatibilityCode = UserRole.Employee }, IsActive = true, SaleChannel = Employee.OfficeSaleChannel };
_mockEmployeeGetter.Setup(g => g.GetEmployee()).Returns(_employee);
_previousContractControllerService = new Mock<IPreviousContractControllerService>();
_previousContractControllerService.Setup(x => x.IsPreviousContractProlonged(It.IsAny<int>())).Returns(true);
_target = new PreCalculationContractValidator(_mockEmployeeGetter.Object, _previousContractControllerService.Object);
IFixture _fixture = new ConstructorHomeFixture();
var contractForProlongation = _fixture.Build<ConstructorHomeContract>()
.With(ctr => ctr.CalculationsForInsuranceObject, null)
.With(ctr => ctr.Employee, new Employee { IsActive = true, Position = new Position { CompatibilityCode = UserRole.Employee } })
.With(ctr => ctr.SigningDate, DateTimeOffset.Now)
.With(ctr => ctr.Insured, new Contractor { PersonLastName = "PersonLastName", PersonFirstName = "PersonFirstName", PersonMiddleName = "PersonMiddleName", PersonBirthday = DateTimeOffset.Now.Date })
.With(ctr => ctr.Property, new Property { CommonRealAddressFiasId = "g7dhf-34f34f-g54g4g-g45g45f", ConstructionYear = 2000 })
.With(ctr => ctr.AssumedRiskResposibilities, new List<AssumedRiskOnTheContract> { new AssumedRiskOnTheContract { ProductRiskId = 1 }, new AssumedRiskOnTheContract { ProductRiskId = 2 } })
.With(ctr => ctr.AssumedRiskOnTheAdditionalConstructions, new List<AssumedRiskOnAdditionalConstruction> { new AssumedRiskOnAdditionalConstruction { ProductRiskId = 6, AdditionalConstruction = new BathHouse { Guid = Guid.NewGuid() } } })
.With(ctr => ctr.Status, new ContractStatus() { Code = ContractStatusCode.Signed })
var contractProlonged = contractForProlongation;
contractProlonged.Id = 2;
contractProlonged.PreviousContract = contractForProlongation;
var contract = contractForProlongation;
var validateResult = _target.;
public void TestValidation()
IFixture _fixture = new ConstructorHomeFixture();
var contract = _fixture.Build<ConstructorHomeContract>()
.With(ctr => ctr.CalculationsForInsuranceObject, null)
.With(ctr => ctr.Employee, new Employee { IsActive = true, Position = new Position { CompatibilityCode = UserRole.Employee } })
.With(ctr => ctr.SigningDate, DateTimeOffset.Now)
.With(ctr => ctr.Insured, new Contractor { PersonLastName = "PersonLastName", PersonFirstName = "PersonFirstName", PersonMiddleName = "PersonMiddleName", PersonBirthday = DateTimeOffset.Now.Date })
.With(ctr => ctr.Property, new Property { CommonRealAddressFiasId = "g7dhf-34f34f-g54g4g-g45g45f", ConstructionYear = 2000 })
.With(ctr => ctr.AssumedRiskResposibilities, new List<AssumedRiskOnTheContract> { new AssumedRiskOnTheContract { ProductRiskId = 1 }, new AssumedRiskOnTheContract { ProductRiskId = 2 } })
.With(ctr => ctr.AssumedRiskOnTheAdditionalConstructions, new List<AssumedRiskOnAdditionalConstruction> { new AssumedRiskOnAdditionalConstruction { ProductRiskId = 6, AdditionalConstruction = new BathHouse { Guid = Guid.NewGuid() } } })
.With(ctr => ctr.PreviousContractId, 1)
.With(ctr => ctr.PreviousContract,
new ConstructorHomeContract
ContractTo = DateTime.Now.AddDays(10),
InsuranceTerm = new InsuranceTermConstructorHome { Value = Constants.MaxMonthsForInsuranceTerm },
Insured = new Contractor { PersonLastName = "PersonLastName", PersonFirstName = "PersonFirstName", PersonMiddleName = "PersonMiddleName", PersonBirthday = DateTimeOffset.Now.Date },
Property = new Property { CommonRealAddressFiasId = "g7dhf-34f34f-g54g4g-g45g45f", ConstructionYear = 2000 },
AssumedRiskResposibilities = new List<AssumedRiskOnTheContract> { new AssumedRiskOnTheContract { ProductRiskId = 1 }, new AssumedRiskOnTheContract { ProductRiskId = 2 }, new AssumedRiskOnTheContract { ProductRiskId = 6 } }
var validateResult = _target.Validate(contract);
Assert.That(validateResult.IsValid == false);
CollectionAssert.Contains(validateResult.Errors.Select(e => e.ErrorMessage),
"Предыдущий договор по указанному номеру уже пролонгирован");