Пример #1
0
        private void ProcessCampaignPassPrioritiesAndApplyToScenario(Scenario existingScenario, List <CreateCampaignPassPriorityModel> campaignPassPriorities,
                                                                     List <CampaignWithProductFlatModel> allCampaigns)
        {
            // this check is already done in the validation but if incase this method is called from elsewhere
            // if contains any CampaignPassPriorities then reset the pass ids and names with the information from the existingScenario passes
            if (campaignPassPriorities != null && campaignPassPriorities.Any())
            {
                // get the pass deatils for the existing Scenario passes
                var existingScenarioPassIds  = existingScenario.Passes.Select(p => p.Id).ToList();
                var existingScenarioPasses   = _passRepository.FindByIds(existingScenarioPassIds).ToList();
                var numberofPassesInScenario = existingScenarioPassIds.Count;

                campaignPassPriorities.ForEach(c =>
                {
                    // this check is already done in the validation but if incase this method is called from elsewhere
                    // hence, an extra check to ensure that the scenario and campaignPassPriorities contains the same number of passes
                    if (c.PassPriorities.Count == numberofPassesInScenario)
                    {
                        // priorities are assigned passes in the order it exists in the scenario
                        for (int i = 0; i < numberofPassesInScenario; i++)
                        {
                            c.PassPriorities[i].PassId   = existingScenario.Passes[i].Id;
                            c.PassPriorities[i].PassName = existingScenarioPasses.Where(p => p.Id == c.PassPriorities[i].PassId)
                                                           .Select(p => p.Name).FirstOrDefault();
                        }
                    }
                });

                // create the CampaignPassPriorities for the campaigns which are not in the campaignPassPriorities
                var campaignPassPrioritiesForMissingCampaigns = CreateCampaignPassPriorities(allCampaigns, campaignPassPriorities, existingScenarioPasses);
                if (campaignPassPrioritiesForMissingCampaigns != null && campaignPassPrioritiesForMissingCampaigns.Any())
                {
                    campaignPassPriorities.AddRange(campaignPassPrioritiesForMissingCampaigns);
                }

                //apply Campaign Pass Priorities to existing Scenario
                existingScenario.CampaignPassPriorities =
                    CampaignPassPrioritiesServiceMapper.MapToCampaignPassPriorities(
                        campaignPassPriorities,
                        allCampaigns,
                        _mapper);
            }
        }