public async Task TryCheckout(CheckoutDto dto)
        {
            await Execute(async() => {
                using (UnitOfWork db = new UnitOfWork())
                {
                    CheckPointEntity checkPoint = await db.GetRepo <CheckPointEntity>().Get(dto.CheckPointId.Value);
                    AccountEntity account       = await db.GetRepo <AccountEntity>().Get(dto.AccountId.Value);

                    if (dto.CurrentManufactoryId.Value != checkPoint.Manufactory1Id && dto.CurrentManufactoryId.Value != checkPoint.Manufactory2Id)
                    {
                        DataValidationException exception = new DataValidationException();

                        exception.Add(
                            typeof(CheckoutDto), nameof(CheckoutDto.CurrentManufactoryId),
                            $"{nameof(CheckoutDto.CurrentManufactoryId)} must be one of manufactories associated with check point"
                            );

                        throw exception;
                    }

                    CheckPointEventEntity checkPointEvent = new CheckPointEventEntity()
                    {
                        account_id     = account.id,
                        check_point_id = checkPoint.id,
                        is_direct      = checkPoint.Manufactory1Id == dto.CurrentManufactoryId.Value,
                        timespan       = DateTime.Now
                    };

                    ManufactoryEntity targetManufactory = checkPointEvent.is_direct ? checkPoint.Manufactory2 : checkPoint.Manufactory1;
                    NotPermittedException ex            = null;

                    if (account.Roles.SelectMany(r => r.ManufactoryPermissions).Any(m => m.id == targetManufactory.id))
                    {
                        checkPointEvent.log = $"Checkout to Manufactory #{targetManufactory.id} via Check Point ${checkPoint.id} by Account #{account.id}: SUCCESS";
                    }
                    else
                    {
                        checkPointEvent.log = $"Checkout to Manufactory #{targetManufactory.id} via Check Point ${checkPoint.id} by Account #{account.id}: ACCESS DENIED";
                        ex = new NotPermittedException(checkPointEvent.log);
                    }

                    await db.GetRepo <CheckPointEventEntity>().Create(checkPointEvent);
                    await db.Save();

                    if (ex != null)
                    {
                        throw ex;
                    }
                }
            });
        }
        private void ValidateStorageCells(IEnumerable <StorageCellEntity> storageCells, int?pipelineId)
        {
            DataValidationException validationException = new DataValidationException();

            IEnumerable <StorageCellEntity> usedStorageCells = storageCells.Where(
                storageCell => storageCell.pipeline_id != null && storageCell.pipeline_id != pipelineId
                );

            foreach (StorageCellEntity usedStorageCell in usedStorageCells)
            {
                validationException.Add(
                    typeof(PipelineDto), nameof(PipelineDto.Connections),
                    $"Storage cell #{usedStorageCell.id} is already used in some pipeline"
                    );
            }

            if (validationException.InvalidFieldInfos.Count != 0)
            {
                throw validationException;
            }
        }
        private void ValidatePipelineItems(IEnumerable <PipelineItemEntity> pipelineItems, int?pipelineId)
        {
            DataValidationException validationException = new DataValidationException();

            IEnumerable <PipelineItemEntity> usedPipelineItems = pipelineItems.Where(
                pipelineItem => pipelineItem.pipeline_id != null && pipelineItem.pipeline_id != pipelineId
                );

            foreach (PipelineItemEntity usedPipelineItem in usedPipelineItems)
            {
                validationException.Add(
                    typeof(PipelineDto), nameof(PipelineDto.Connections),
                    $"Pipeline item #{usedPipelineItem.id} is already used in some pipeline"
                    );
            }

            if (validationException.InvalidFieldInfos.Count != 0)
            {
                throw validationException;
            }
        }
Example #4
0
        private void ValidatePlan(AuthorizedDto <SetupPlanDto> plan)
        {
            DataValidationException dataValidationException = new DataValidationException();

            if (plan.Data.CompanyPlanPoints.Any(point => (point.Id.HasValue && point.FakeId.HasValue) || (!point.Id.HasValue && !point.FakeId.HasValue)))
            {
                dataValidationException.Add(
                    typeof(CompanyPlanPointDto), nameof(CompanyPlanPointDto.FakeId),
                    $"One of fields '{nameof(CompanyPlanPointDto.FakeId)}' and '{nameof(CompanyPlanPointDto.Id)}' must be specified"
                    );
            }

            bool manufactoryPointsInvalid = plan.Data.Manufactories.Any(
                manufactory => manufactory.ManufactoryPlanPoints.Any(
                    point => (point.CompanyPlanPointId.HasValue && point.FakeCompanyPlanPointId.HasValue) ||
                    (!point.CompanyPlanPointId.HasValue && !point.FakeCompanyPlanPointId.HasValue)
                    )
                );

            if (manufactoryPointsInvalid)
            {
                dataValidationException.Add(
                    typeof(ManufactoryPlanPointDto), nameof(ManufactoryPlanPointDto.FakeCompanyPlanPointId),
                    $"One of fields '{nameof(ManufactoryPlanPointDto.FakeCompanyPlanPointId)}' and '{nameof(ManufactoryPlanPointDto.CompanyPlanPointId)}' must be specified"
                    );
            }

            bool checkPointsInvalid = plan.Data.CheckPoints.Any(
                checkPoint => (checkPoint.CompanyPlanUniquePoint1Id.HasValue && checkPoint.FakeCompanyPlanUniquePoint1Id.HasValue) ||
                (!checkPoint.CompanyPlanUniquePoint1Id.HasValue && !checkPoint.FakeCompanyPlanUniquePoint1Id.HasValue) ||
                (checkPoint.CompanyPlanUniquePoint2Id.HasValue && checkPoint.FakeCompanyPlanUniquePoint2Id.HasValue) ||
                (!checkPoint.CompanyPlanUniquePoint2Id.HasValue && !checkPoint.FakeCompanyPlanUniquePoint2Id.HasValue)
                );

            if (checkPointsInvalid)
            {
                dataValidationException.Add(
                    typeof(CheckPointDto), nameof(CheckPointDto.FakeCompanyPlanUniquePoint1Id),
                    $"One of fields '{nameof(CheckPointDto.FakeCompanyPlanUniquePoint1Id)}' and '{nameof(CheckPointDto.CompanyPlanUniquePoint1Id)}' must be specified"
                    );

                dataValidationException.Add(
                    typeof(CheckPointDto), nameof(CheckPointDto.FakeCompanyPlanUniquePoint2Id),
                    $"One of fields '{nameof(CheckPointDto.FakeCompanyPlanUniquePoint2Id)}' and '{nameof(CheckPointDto.CompanyPlanUniquePoint2Id)}' must be specified"
                    );
            }

            bool enterLeavePointsInvalid = plan.Data.EnterLeavePoints.Any(
                enterLeavePoint => (enterLeavePoint.CompanyPlanUniquePoint1Id.HasValue && enterLeavePoint.FakeCompanyPlanUniquePoint1Id.HasValue) ||
                (!enterLeavePoint.CompanyPlanUniquePoint1Id.HasValue && !enterLeavePoint.FakeCompanyPlanUniquePoint1Id.HasValue) ||
                (enterLeavePoint.CompanyPlanUniquePoint2Id.HasValue && enterLeavePoint.FakeCompanyPlanUniquePoint2Id.HasValue) ||
                (!enterLeavePoint.CompanyPlanUniquePoint2Id.HasValue && !enterLeavePoint.FakeCompanyPlanUniquePoint2Id.HasValue)
                );

            if (enterLeavePointsInvalid)
            {
                dataValidationException.Add(
                    typeof(EnterLeavePointDto), nameof(EnterLeavePointDto.FakeCompanyPlanUniquePoint1Id),
                    $"One of fields '{nameof(EnterLeavePointDto.FakeCompanyPlanUniquePoint1Id)}' and '{nameof(EnterLeavePointDto.CompanyPlanUniquePoint1Id)}' must be specified"
                    );

                dataValidationException.Add(
                    typeof(EnterLeavePointDto), nameof(EnterLeavePointDto.FakeCompanyPlanUniquePoint2Id),
                    $"One of fields '{nameof(EnterLeavePointDto.FakeCompanyPlanUniquePoint2Id)}' and '{nameof(EnterLeavePointDto.CompanyPlanUniquePoint2Id)}' must be specified"
                    );
            }

            if (dataValidationException.InvalidFieldInfos.Count != 0)
            {
                throw dataValidationException;
            }

            int[] realCompanyPlanPointIds = plan.Data.CompanyPlanPoints.Where(point => point.Id.HasValue).Select(point => point.Id.Value).ToArray();
            int[] fakeCompanyPlanPointIds = plan.Data.CompanyPlanPoints.Where(point => point.FakeId.HasValue).Select(point => point.FakeId.Value).ToArray();

            foreach (ManufactoryDto manufactory in plan.Data.Manufactories)
            {
                int[] realManufactoryCompanyPlanPointIds = manufactory.ManufactoryPlanPoints.Where(point => point.CompanyPlanPointId.HasValue)
                                                           .Select(point => point.CompanyPlanPointId.Value).ToArray();

                int[] fakeManufactoryCompanyPlanPointIds = manufactory.ManufactoryPlanPoints.Where(point => point.FakeCompanyPlanPointId.HasValue)
                                                           .Select(point => point.FakeCompanyPlanPointId.Value).ToArray();

                if (realManufactoryCompanyPlanPointIds.Intersect(realCompanyPlanPointIds).Count() != realManufactoryCompanyPlanPointIds.Length)
                {
                    dataValidationException.Add(
                        typeof(ManufactoryPlanPointDto), nameof(ManufactoryPlanPointDto.CompanyPlanPointId),
                        "company_plan_point_id provided for manufactory is not presented in company_plan_points collection"
                        );
                }

                if (fakeManufactoryCompanyPlanPointIds.Intersect(fakeCompanyPlanPointIds).Count() != fakeManufactoryCompanyPlanPointIds.Length)
                {
                    dataValidationException.Add(
                        typeof(ManufactoryPlanPointDto), nameof(ManufactoryPlanPointDto.CompanyPlanPointId),
                        "fake_company_plan_point_id provided for manufactory is not presented in company_plan_points collection"
                        );
                }

                if (realManufactoryCompanyPlanPointIds.Distinct().Count() != realManufactoryCompanyPlanPointIds.Length)
                {
                    dataValidationException.Add(
                        typeof(ManufactoryDto), nameof(ManufactoryDto.ManufactoryPlanPoints),
                        "company_plan_point_id's for one manufactory must be unique"
                        );
                }

                if (fakeManufactoryCompanyPlanPointIds.Distinct().Count() != fakeManufactoryCompanyPlanPointIds.Length)
                {
                    dataValidationException.Add(
                        typeof(ManufactoryDto), nameof(ManufactoryDto.ManufactoryPlanPoints),
                        "fake_company_plan_point_id's for one manufactory must be unique"
                        );
                }

                if (manufactory.ManufactoryPlanPoints.Length < 3)
                {
                    dataValidationException.Add(
                        typeof(ManufactoryDto), nameof(ManufactoryDto.ManufactoryPlanPoints),
                        "at least 3 company_plan_poin_id's must be specified for a manufactory"
                        );
                }
            }

            int[] realCheckPointCompanyPlanPointIds = plan.Data.CheckPoints
                                                      .Where(point => point.CompanyPlanUniquePoint1Id.HasValue).Select(point => point.CompanyPlanUniquePoint1Id.Value)
                                                      .Union(plan.Data.CheckPoints.Where(point => point.CompanyPlanUniquePoint2Id.HasValue).Select(point => point.CompanyPlanUniquePoint2Id.Value))
                                                      .ToArray();

            int[] fakeCheckPointCompanyPlanPointIds = plan.Data.CheckPoints
                                                      .Where(point => point.FakeCompanyPlanUniquePoint1Id.HasValue).Select(point => point.FakeCompanyPlanUniquePoint1Id.Value)
                                                      .Union(plan.Data.CheckPoints.Where(point => point.FakeCompanyPlanUniquePoint2Id.HasValue).Select(point => point.FakeCompanyPlanUniquePoint2Id.Value))
                                                      .ToArray();

            if (realCheckPointCompanyPlanPointIds.Intersect(realCompanyPlanPointIds).Count() != realCheckPointCompanyPlanPointIds.Length)
            {
                dataValidationException.Add(
                    typeof(CheckPointDto), $"{nameof(CheckPointDto.CompanyPlanUniquePoint1Id)} or {nameof(CheckPointDto.CompanyPlanUniquePoint2Id)}",
                    "company_plan_point_id provided for check point is not presented in company_plan_points collection"
                    );
            }

            if (fakeCheckPointCompanyPlanPointIds.Intersect(fakeCompanyPlanPointIds).Count() != fakeCheckPointCompanyPlanPointIds.Length)
            {
                dataValidationException.Add(
                    typeof(CheckPointDto), $"{nameof(CheckPointDto.FakeCompanyPlanUniquePoint1Id)} or {nameof(CheckPointDto.FakeCompanyPlanUniquePoint2Id)}",
                    "fake_company_plan_point_id provided for check point is not presented in company_plan_points collection"
                    );
            }

            int[] realEnterLeavePointCompanyPlanPointIds = plan.Data.EnterLeavePoints
                                                           .Where(point => point.CompanyPlanUniquePoint1Id.HasValue).Select(point => point.CompanyPlanUniquePoint1Id.Value)
                                                           .Union(plan.Data.EnterLeavePoints.Where(point => point.CompanyPlanUniquePoint2Id.HasValue).Select(point => point.CompanyPlanUniquePoint2Id.Value))
                                                           .ToArray();

            int[] fakeEnterLeavePointCompanyPlanPointIds = plan.Data.EnterLeavePoints
                                                           .Where(point => point.FakeCompanyPlanUniquePoint1Id.HasValue).Select(point => point.FakeCompanyPlanUniquePoint1Id.Value)
                                                           .Union(plan.Data.EnterLeavePoints.Where(point => point.FakeCompanyPlanUniquePoint2Id.HasValue).Select(point => point.FakeCompanyPlanUniquePoint2Id.Value))
                                                           .ToArray();

            if (realEnterLeavePointCompanyPlanPointIds.Intersect(realCompanyPlanPointIds).Count() != realEnterLeavePointCompanyPlanPointIds.Length)
            {
                dataValidationException.Add(
                    typeof(EnterLeavePointDto), $"{nameof(EnterLeavePointDto.CompanyPlanUniquePoint1Id)} or {nameof(EnterLeavePointDto.CompanyPlanUniquePoint2Id)}",
                    "company_plan_point_id provided for enter/leave point is not presented in company_plan_points collection"
                    );
            }

            if (fakeEnterLeavePointCompanyPlanPointIds.Intersect(fakeCompanyPlanPointIds).Count() != fakeEnterLeavePointCompanyPlanPointIds.Length)
            {
                dataValidationException.Add(
                    typeof(EnterLeavePointDto), $"{nameof(EnterLeavePointDto.FakeCompanyPlanUniquePoint1Id)} or {nameof(EnterLeavePointDto.FakeCompanyPlanUniquePoint2Id)}",
                    "fake_company_plan_point_id provided for enter/leave point is not presented in company_plan_points collection"
                    );
            }

            if (dataValidationException.InvalidFieldInfos.Count != 0)
            {
                throw dataValidationException;
            }

            Dictionary <int, bool> realCompanyPlanPointUsed = realCompanyPlanPointIds.ToDictionary(id => id, id => false);
            Dictionary <int, bool> fakeCompanyPlanPointUsed = fakeCompanyPlanPointIds.ToDictionary(id => id, id => false);

            (bool isCheckPoint, int?real1, int?real2, int?fake1, int?fake2)[] companyPlanPointIds = plan.Data.CheckPoints.Select(