private static Category GetStandardSubCategory(VacancySummary vacancy, IList <Category> categories)
        {
            if (vacancy.VacancyType == VacancyType.Apprenticeship)
            {
                if (vacancy.StandardId.HasValue)
                {
                    var standardCode = CategoryPrefixes.GetStandardCode(vacancy.StandardId.Value);

                    var subCategories = categories
                                        .Where(c => c.SubCategories != null)
                                        .SelectMany(c => c.SubCategories)
                                        .ToList();

                    var standards = subCategories
                                    .Where(c => c.SubCategories != null && c.SubCategories.Any())
                                    .SelectMany(c => c.SubCategories);

                    var standard = standards
                                   .SingleOrDefault(c => c.CodeName.Split(CodeDivider).Contains(standardCode));

                    if (standard != null)
                    {
                        var standardSector = subCategories.Single(sc => sc.CodeName == standard.ParentCategoryCodeName);
                        return(new Category(standard.Id, standard.ParentCategoryCodeName, $"{standardSector.FullName} > {standard.FullName}", CategoryType.StandardSector, CategoryStatus.Active));
                    }
                }

                return(Category.UnknownStandardSector);
            }

            return(Category.InvalidStandardSector);
        }
        private static Category GetFrameworkSubCategory(VacancySummary vacancy, IList <Category> categories)
        {
            if (vacancy.VacancyType == VacancyType.Apprenticeship)
            {
                if (!string.IsNullOrWhiteSpace(vacancy.FrameworkCodeName))
                {
                    var frameworkCode = CategoryPrefixes.GetFrameworkCode(vacancy.FrameworkCodeName);

                    var subCategories = categories
                                        .Where(c => c.SubCategories != null)
                                        .SelectMany(c => c.SubCategories);

                    var framework = subCategories
                                    .SingleOrDefault(c => c.CodeName.Split(CodeDivider).Contains(frameworkCode));

                    if (framework != null)
                    {
                        return(new Category(framework.Id, framework.CodeName, framework.FullName, framework.CategoryType, framework.Status));
                    }
                }

                return(Category.UnknownFramework);
            }

            return(Category.InvalidFramework);
        }
 private static void LogSubCategory(VacancySummary vacancy, ILogService logService, Category subcategory)
 {
     if (!subcategory.IsValid())
     {
         logService.Warn("Cannot find a sub category for the apprenticship with Id {0}", vacancy.VacancyId);
     }
 }
        private static GeoPoint GetGeoPoint(VacancySummary vacancy)
        {
            if (vacancy.Address?.GeoPoint == null)
            {
                return(new GeoPoint());
            }

            return(new GeoPoint
            {
                Latitude = vacancy.Address.GeoPoint.Latitude,
                Longitude = vacancy.Address.GeoPoint.Longitude
            });
        }
        public static ApprenticeshipSummary GetApprenticeshipSummary(VacancySummary vacancy, Employer employer, Provider provider, IList <Category> categories, ILogService logService)
        {
            try
            {
                //Manually mapping rather than using automapper as the two enties are significantly different

                var location = GetGeoPoint(vacancy);

                var category    = vacancy.GetCategory(categories);
                var subcategory = vacancy.GetSubCategory(categories);
                LogCategoryAndSubCategory(vacancy, logService, category, subcategory);

                var summary = new ApprenticeshipSummary
                {
                    Id = vacancy.VacancyId,
                    //Goes into elastic unformatted for searching
                    VacancyReference = vacancy.VacancyReferenceNumber.ToString(),
                    Title            = vacancy.Title,
                    // ReSharper disable PossibleInvalidOperationException
                    PostedDate  = vacancy.DateQAApproved.Value,
                    StartDate   = vacancy.PossibleStartDate.Value,
                    ClosingDate = vacancy.ClosingDate.Value,
                    // ReSharper restore PossibleInvalidOperationException
                    Description               = vacancy.ShortDescription,
                    NumberOfPositions         = vacancy.NumberOfPositions,
                    EmployerName              = string.IsNullOrEmpty(vacancy.EmployerAnonymousName) ? employer.FullName : vacancy.EmployerAnonymousName,
                    ProviderName              = provider.TradingName,
                    IsPositiveAboutDisability = employer.IsPositiveAboutDisability,
                    IsEmployerAnonymous       = !string.IsNullOrEmpty(vacancy.EmployerAnonymousName),
                    Location            = location,
                    VacancyLocationType = vacancy.VacancyLocationType == VacancyLocationType.Nationwide ? ApprenticeshipLocationType.National : ApprenticeshipLocationType.NonNational,
                    ApprenticeshipLevel = vacancy.ApprenticeshipLevel.GetApprenticeshipLevel(),
                    Wage                  = vacancy.Wage,
                    WorkingWeek           = vacancy.WorkingWeek,
                    CategoryCode          = category.CodeName,
                    Category              = category.FullName,
                    SubCategoryCode       = subcategory.CodeName,
                    SubCategory           = subcategory.FullName,
                    AnonymousEmployerName = vacancy.EmployerAnonymousName
                };

                return(summary);
            }
            catch (Exception ex)
            {
                logService.Error($"Failed to map apprenticeship with Id: {vacancy?.VacancyId ?? 0}", ex);
                return(null);
            }
        }
        public static Category GetSubCategory(this VacancySummary vacancy, IList <Category> categories)
        {
            switch (vacancy.TrainingType)
            {
            case TrainingType.Unknown:     //Unknown assumes framework as this is a result of migrated data
            case TrainingType.Frameworks:
                return(GetFrameworkSubCategory(vacancy, categories));

            case TrainingType.Standards:
                return(GetStandardSubCategory(vacancy, categories));

            case TrainingType.Sectors:
                return(Category.InvalidSector);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        private static Category GetSectorCategory(this VacancySummary vacancy, IList <Category> categories)
        {
            if (vacancy.VacancyType == VacancyType.Traineeship)
            {
                if (!string.IsNullOrWhiteSpace(vacancy.SectorCodeName))
                {
                    var code = CategoryPrefixes.GetSectorSubjectAreaTier1Code(vacancy.SectorCodeName);

                    var category = categories
                                   .SingleOrDefault(c => c.CodeName.Split(CodeDivider).Contains(code));

                    if (category != null)
                    {
                        return(new Category(category.Id, category.CodeName, category.FullName, category.CategoryType, category.Status));
                    }
                }

                return(Category.UnknownSectorSubjectAreaTier1);
            }

            return(Category.InvalidSectorSubjectAreaTier1);
        }