internal List <LocationRef> CreateLocationRef(IEnumerable <ApprenticeshipLocation> locations) { List <LocationRef> locationRefs = new List <LocationRef>(); var subRegionItemModels = new SelectRegionModel().RegionItems.SelectMany(x => x.SubRegion); foreach (var location in locations) { if (location.Regions != null) { foreach (var region in location.Regions) { locationRefs.Add(new LocationRef { ID = subRegionItemModels.Where(x => x.Id == region).Select(y => y.ApiLocationId.Value).FirstOrDefault(), DeliveryModes = ConvertToApprenticeshipDeliveryModes(location.DeliveryModes), Radius = 10 }); } } else { locationRefs.Add(new LocationRef { ID = location.TribalId ?? location.LocationId, DeliveryModes = ConvertToApprenticeshipDeliveryModes(location.DeliveryModes), Radius = location.Radius.HasValue ? location.Radius.Value : 0 }); } } return(locationRefs); }
public IEnumerable <string> SanitiseRegionTextForCSVOutput(IEnumerable <string> regions) { SelectRegionModel selectRegionModel = new SelectRegionModel(); foreach (var selectRegionRegionItem in selectRegionModel.RegionItems.OrderBy(x => x.RegionName)) { //If Region is returned, check for existence of any subregions if (regions.Contains(selectRegionRegionItem.Id)) { var subregionsInList = from subRegion in selectRegionRegionItem.SubRegion where regions.Contains(subRegion.Id) select subRegion; //If true, then ignore subregions if (subregionsInList.Count() > 0) { foreach (var subRegion in subregionsInList) { regions = regions.Where(x => (x != subRegion.Id)).ToList(); } } } } return(regions); }
private List <DasLocationRef> CreateLocationRef(int exportKey, Dictionary <string, ApprenticeshipLocation> locations) { var locationRefs = new List <DasLocationRef>(); var subRegionItemModels = new SelectRegionModel().RegionItems.SelectMany(x => x.SubRegion); foreach (var(key, currentLocation) in locations) { // Regions if (currentLocation.SubRegionIds != null && currentLocation.SubRegionIds.Count > 0) { foreach (var region in currentLocation.SubRegionIds) { var locationId = subRegionItemModels .Where(x => x.Id == region) .Select(y => $"{y.ApiLocationId.Value}") .FirstOrDefault(); if (string.IsNullOrWhiteSpace(locationId)) { continue; } var regionId = locationId.Substring(locationId.Length - 3, 3); var regionIndex = $"{exportKey}2{regionId}"; locationRefs.Add(new DasLocationRef { Id = int.Parse(regionIndex), DeliveryModes = ConvertToApprenticeshipDeliveryModes(currentLocation), Radius = currentLocation.Radius ?? 50 // TODO: Add to config }); } } else { var isNational = currentLocation.National != null && currentLocation.National.Value; var radius = isNational ? 500 // National : currentLocation.Radius ?? 30; locationRefs.Add(new DasLocationRef { Id = int.Parse(key), DeliveryModes = ConvertToApprenticeshipDeliveryModes(currentLocation), Radius = radius }); } } return(locationRefs); }
private Result <IEnumerable <string> > ParseRegionData(string regions, string subRegions) { List <string> totalList = new List <string>(); var availableRegions = new SelectRegionModel(); var availableSubRegions = availableRegions.RegionItems.SelectMany(x => x.SubRegion); var listOfRegions = regions.Split(";").Select(p => p.Trim()).ToList(); var listOfSubregions = subRegions.Split(";").Select(p => p.Trim()).ToList(); //Get regions foreach (var region in listOfRegions) { if (!string.IsNullOrWhiteSpace(region)) { var id = availableRegions.RegionItems.Where(x => x.RegionName.ToUpper() == region.ToUpper()) .Select(y => y.Id); if (id.Count() > 0) { totalList.Add(id.FirstOrDefault()); } else { return(Result.Fail <IEnumerable <string> >("Problem with Bulk upload value")); } } } foreach (var subRegion in listOfSubregions) { if (!string.IsNullOrEmpty(subRegion)) { var id = availableSubRegions.Where(x => x.SubRegionName.ToUpper() == subRegion.ToUpper()) .Select(y => y.Id); if (id.Count() > 0) { totalList.Add(id.FirstOrDefault()); } else { return(Result.Fail <IEnumerable <string> >("Problem with Bulk upload value")); } } } return(Result.Ok <IEnumerable <string> >(totalList)); }
private SelectRegionModel GetRegions() { var selectRegion = new SelectRegionModel { LabelText = "Where in England can you deliver this course?", HintText = "Select all regions and areas that apply.", AriaDescribedBy = "Select all that apply." }; if (selectRegion.RegionItems != null && selectRegion.RegionItems.Any()) { selectRegion.RegionItems = selectRegion.RegionItems.OrderBy(x => x.RegionName); foreach (var selectRegionRegionItem in selectRegion.RegionItems) { selectRegionRegionItem.SubRegion = selectRegionRegionItem.SubRegion.OrderBy(x => x.SubRegionName).ToList(); } } return(selectRegion); }
private Dictionary <string, List <string> > SubRegionCodesToDictionary(IEnumerable <string> subRegions) { SelectRegionModel selectRegionModel = new SelectRegionModel(); Dictionary <string, List <string> > regionsAndSubregions = new Dictionary <string, List <string> >(); foreach (var subRegionCode in subRegions) { var isRegionCode = selectRegionModel.RegionItems.FirstOrDefault(x => String.Equals(x.Id, subRegionCode, StringComparison.CurrentCultureIgnoreCase)); if (isRegionCode != null) { if (!regionsAndSubregions.ContainsKey(isRegionCode.RegionName)) { var subRegionNamesList = isRegionCode.SubRegion.Select(x => x.SubRegionName).ToList(); regionsAndSubregions.Add(isRegionCode.RegionName, subRegionNamesList); } } else { var regionName = selectRegionModel.GetRegionNameForSubRegion(subRegionCode); if (string.IsNullOrWhiteSpace(regionName)) { continue; } if (!regionsAndSubregions.ContainsKey(regionName)) { regionsAndSubregions.Add(regionName, new List <string>()); } var subRegionItem = selectRegionModel.GetSubRegionItemByRegionCode(subRegionCode); var alreadyExists = CheckForExistingSubregions(subRegionItem.SubRegionName, regionsAndSubregions[regionName]); if (alreadyExists == false) { regionsAndSubregions[regionName].Add(subRegionItem.SubRegionName); } } } return(regionsAndSubregions); }
public void InsertIntoTable(SelectRegionModel model) { //Get regions using (SqlConnection sqlCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Connection"].ConnectionString)) { sqlCon.Open(); using (SqlCommand cmd = new SqlCommand { CommandText = "INSERT INTO [dbo].[Subregion](" + "[SubregionId]," + "[Lat]," + "[Long])" + "VALUES(" + "@subregionId, " + "@lat, " + "@long)", Connection = sqlCon }) { foreach (var region in model.RegionItems) { foreach (var subRegion in region.SubRegions) { cmd.Parameters.AddWithValue("@subregionId", subRegion.Id); cmd.Parameters.AddWithValue("@lat", subRegion.Latitude.ToString()); cmd.Parameters.AddWithValue("@long", subRegion.Longitude.ToString()); cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); } } sqlCon.Close(); } } }
private IEnumerable <DasLocation> RegionsToLocations(int exportKey, IEnumerable <string> regionCodes) { var apprenticeshipLocations = new List <DasLocation>(); var regions = new SelectRegionModel().RegionItems.SelectMany(x => x.SubRegion.Where(y => regionCodes.Contains(y.Id))); foreach (var region in regions) { if (!region.ApiLocationId.HasValue) { continue; } var locationId = $"{region.ApiLocationId.Value}"; if (!string.IsNullOrWhiteSpace(locationId)) { // get last three digits of region code var regionId = locationId.Substring(locationId.Length - 3, 3); var regionIndex = $"{exportKey}2{regionId}"; var dasLocation = new DasLocation { Id = int.Parse(regionIndex), Name = region.SubRegionName, Address = new DasAddress { Address1 = region.SubRegionName, Lat = region.Latitude, Long = region.Longitude } }; apprenticeshipLocations.Add(dasLocation); } } return(apprenticeshipLocations); }
internal CsvApprenticeship MapCsvApprenticeship(Apprenticeship apprenticeship, ApprenticeshipLocation location) { SelectRegionModel selectRegionModel = new SelectRegionModel(); return(new CsvApprenticeship { StandardCode = apprenticeship.StandardCode?.ToString(), Version = apprenticeship.Version?.ToString(), FrameworkCode = apprenticeship.FrameworkCode?.ToString(), ProgType = apprenticeship.ProgType?.ToString(), PathwayCode = apprenticeship.PathwayCode?.ToString(), ApprenticeshipInformation = _CSVHelper.SanitiseTextForCSVOutput(apprenticeship.MarketingInformation), ApprenticeshipWebpage = apprenticeship.Url, ContactEmail = apprenticeship.ContactEmail, ContactPhone = apprenticeship.ContactTelephone, ContactURL = apprenticeship.ContactWebsite, DeliveryMethod = DeliveryMethodConvert(location.ApprenticeshipLocationType), Venue = location.VenueId.HasValue ? _venueService.GetVenueByIdAsync(new GetVenueByIdCriteria (location.VenueId.Value.ToString())).Result.Value?.VenueName : String.Empty, Radius = location.Radius?.ToString(), DeliveryMode = DeliveryModeConvert(location.DeliveryModes), AcrossEngland = location.ApprenticeshipLocationType == ApprenticeshipLocationType.ClassroomBasedAndEmployerBased ? AcrossEnglandConvert(location.Radius, location.National) : String.Empty, NationalDelivery = location.ApprenticeshipLocationType == ApprenticeshipLocationType.EmployerBased ? BoolConvert(location.National) : string.Empty, Region = location.Regions != null?_CSVHelper.SemiColonSplit( selectRegionModel.RegionItems .Where(x => location.Regions.Contains(x.Id)) .Select(y => _CSVHelper.SanitiseTextForCSVOutput(y.RegionName).Replace(",", "")).ToList()) : string.Empty, Subregion = location.Regions != null?_CSVHelper.SemiColonSplit( selectRegionModel.RegionItems.SelectMany( x => x.SubRegion.Where( y => location.Regions.Contains(y.Id)).Select( z => _CSVHelper.SanitiseTextForCSVOutput(z.SubRegionName).Replace(",", "")).ToList())) : string.Empty, }); }
private async Task <CsvApprenticeship> MapCsvApprenticeship(Apprenticeship apprenticeship, ApprenticeshipLocation location) { var selectRegionModel = new SelectRegionModel(); return(new CsvApprenticeship { StandardCode = apprenticeship.StandardCode?.ToString(), Version = apprenticeship.Version?.ToString(), ApprenticeshipInformation = _CSVHelper.SanitiseTextForCSVOutput(apprenticeship.MarketingInformation), ApprenticeshipWebpage = apprenticeship.Url, ContactEmail = apprenticeship.ContactEmail, ContactPhone = apprenticeship.ContactTelephone, ContactURL = apprenticeship.ContactWebsite, DeliveryMethod = DeliveryMethodConvert(location.ApprenticeshipLocationType), Venue = location.VenueId.HasValue ? (await _cosmosDbQueryDispatcher.ExecuteQuery(new GetVenueById() { VenueId = location.VenueId.Value }))?.VenueName : string.Empty, Radius = location.Radius?.ToString(), DeliveryMode = DeliveryModeConvert(location.DeliveryModes), AcrossEngland = location.ApprenticeshipLocationType == ApprenticeshipLocationType.ClassroomBasedAndEmployerBased ? AcrossEnglandConvert(location.Radius, location.National) : String.Empty, NationalDelivery = location.ApprenticeshipLocationType == ApprenticeshipLocationType.EmployerBased ? BoolConvert(location.National) : string.Empty, Region = location.Regions != null?_CSVHelper.SemiColonSplit( selectRegionModel.RegionItems .Where(x => location.Regions.Contains(x.Id)) .Select(y => _CSVHelper.SanitiseTextForCSVOutput(y.RegionName).Replace(",", "")).ToList()) : string.Empty, Subregion = location.Regions != null?_CSVHelper.SemiColonSplit( selectRegionModel.RegionItems.SelectMany( x => x.SubRegion.Where( y => location.Regions.Contains(y.Id)).Select( z => _CSVHelper.SanitiseTextForCSVOutput(z.SubRegionName).Replace(",", "")).ToList())) : string.Empty, }); }
public List <Location> RegionsToLocations(string[] regionCodes) { List <Location> apprenticeshipLocations = new List <Location>(); var regions = new SelectRegionModel().RegionItems.SelectMany(x => x.SubRegion.Where(y => regionCodes.Contains(y.Id))); foreach (var region in regions) { Location location = new Location { ID = region.ApiLocationId, Name = region.SubRegionName, Address = new Address { Address1 = region.SubRegionName, Latitude = region.Latitude, Longitude = region.Longitude, Postcode = region.Postcode }, }; apprenticeshipLocations.Add(location); } return(apprenticeshipLocations); }
public static async Task Run( string input, // Work around https://github.com/Azure/azure-functions-vs-build-sdk/issues/168 ILogger log, [Inject] IConfigurationRoot configuration, [Inject] IProviderCollectionService providerCollectionService, [Inject] ICosmosDbHelper cosmosDbHelper, [Inject] IBlobStorageHelper blobhelper, [Inject] IApprenticeReferenceDataService apprenticeReferenceDataService, [Inject] IApprenticeshipServiceWrapper apprenticeshipService, [Inject] IVenueCollectionService venueCollectionService, [Inject] IOnspdService onspdService ) { var apprenticeshipCollectionId = configuration["CosmosDbCollectionSettings:ApprenticeshipCollectionId"]; var connectionString = configuration.GetConnectionString("TribalRestore"); var blobContainer = blobhelper.GetBlobContainer(configuration["BlobStorageSettings:Container"]); var whiteListProviders = await GetProviderWhiteList(); var result = new List <ApprenticeshipResultMessage>(); var venueExportFileName = $"ApprenticeshipExport-{DateTime.Now.ToString("dd-MM-yy HHmm")}"; const string WHITE_LIST_FILE = "ProviderWhiteList-Apprenticeships.txt"; var ukprnCache = new List <int>(); var databaseId = configuration["CosmosDbSettings:DatabaseId"]; var apprenticeshipList = new List <ApprenticeshipResult>(); var createdBy = "ApprenticeshipMigrator"; var createdDate = DateTime.Now; SemaphoreSlim semaphore = new SemaphoreSlim(5); var client = cosmosDbHelper.GetClient(); var apprenticeshipSQL = @"SELECT a.ApprenticeshipId, p.ProviderId, a.FrameworkCode, a.ProgType, a.PathwayCode, a.StandardCode, a.[Version], a.MarketingInformation, a.[Url], a.ContactEmail, a.ContactTelephone, a.ContactWebsite, a.RecordStatusId, a.CreatedByUserId, a.CreatedDateTimeUtc, p.Ukprn, coalesce(s.StandardName,f.NasTitle) as [ApprenticeshipTitle], s.NotionalEndLevel FROM Apprenticeship a INNER JOIN Provider p on p.ProviderId = a.ProviderId LEFT JOIN [Standard] s on (s.StandardCode = a.StandardCode and s.Version = a.Version) LEFT jOIN [Framework] f on (f.FrameworkCode = a.FrameworkCode AND f.PathwayCode = a.PathwayCode AND f.ProgType = a.ProgType) WHERE a.recordStatusId=2 ORDER BY ProviderId "; var apprenticeshipLocationsSQL = @"SELECT al.ApprenticeshipId, al.ApprenticeshipLocationId, l.LocationId, a.AddressId, a.AddressLine1, a.AddressLine2, a.County, a.Postcode, a.Town, a.Longitude, a.Latitude, l.Website, l.Email, als.CSV as DeliveryModeStr, l.Telephone, l.LocationName, p.ProviderId, p.Ukprn, al.Radius FROM ApprenticeshipLocation al INNER JOIN Location l on l.LocationId = al.LocationId INNER JOIN Provider p on p.ProviderId = l.ProviderId INNER JOIN Address a ON a.AddressId = l.AddressId CROSS APPLY (SELECT STRING_AGG(DeliveryModeId,',') as CSV, aldm.ApprenticeshipLocationId FROM ApprenticeshipLocationDeliveryMode aldm WHERE ApprenticeshipLocationId = al.ApprenticeshipLocationId GROUP BY aldm.ApprenticeshipLocationId ) als WHERE al.RecordStatusId = 2 and al.ApprenticeshipId = @ApprenticeshipId ORDER BY ApprenticeshipId,ApprenticeshipLocationId"; try { using (var conn1 = new SqlConnection(connectionString)) using (var apprenticeshipscmd = conn1.CreateCommand()) { await conn1.OpenAsync(); apprenticeshipscmd.CommandText = apprenticeshipSQL; using (var apprenticeshipReader = apprenticeshipscmd.ExecuteReader()) { while (await apprenticeshipReader.ReadAsync()) { apprenticeshipList.Add(ApprenticeshipResult.FromDataReader(apprenticeshipReader)); } } } } catch (Exception e) { AddResultMessage(0, 0, "Failed", null, e.Message); log.LogError("Error occured Migrating Apprenticeships", e.Message); } await Task.WhenAll(apprenticeshipList.Select(async apprenticeship => { var apprenticeshipErrors = new List <string>(); //Errors Here cause apprenticeships to go into pending var apprenticeshipWarning = new List <string>(); //informational messages await semaphore.WaitAsync(); try { if (IsOnWhiteList(apprenticeship.UKPRN)) { apprenticeshipErrors = new List <string>(); //get relevant info var exisitingApprenticeship = await GetExistingApprenticeship(apprenticeship); var referenceDataFramework = await GetReferenceDataFramework(apprenticeship); var referenceDataStandard = await GetReferenceDataStandard(apprenticeship); var locations = await GetLocations(apprenticeship); var cosmosVenues = await GetCosmosVenues(locations); var cosmosProvider = await GetProvider(apprenticeship); //map objects for creating cosmos record var locs = MapLocations(locations, cosmosVenues); var id = exisitingApprenticeship?.id.ToString() ?? Guid.NewGuid().ToString(); var apprenticeType = MapApprenticeshipType(apprenticeship); //check to see if framework code/standard code is valid VerifiyIfStandardOrFramework(apprenticeship, referenceDataFramework, referenceDataStandard); var mappedStatus = MapApprenticeshipRecordStatus(locs); var mappedApprenticeship = MapApprenticeship(locs, id, apprenticeship, apprenticeType, mappedStatus, referenceDataFramework?.Id.ToString(), referenceDataStandard?.id.ToString(), cosmosProvider?.id.ToString()); //insert record into cosmos await CreateOrUpdateApprenticeshipRecord(mappedApprenticeship); //log message to output AddResultMessage(apprenticeship.ApprenticeshipID, apprenticeship.UKPRN, Enum.GetName(typeof(RecordStatus), mappedApprenticeship.RecordStatus), mappedApprenticeship.ApprenticeshipTitle, string.Join("\n", apprenticeshipErrors), string.Join("\n", apprenticeshipWarning)); } else { AddResultMessage(apprenticeship.ApprenticeshipID, apprenticeship.UKPRN, "Skipped", null, $"PRN {apprenticeship.UKPRN} not whitelisted"); } } catch (Exception e) { AddResultMessage(apprenticeship.ApprenticeshipID, apprenticeship.UKPRN, "Failed", null, $"Exception occured creating record - {e.Message}"); log.LogError("Error occurred creating or updating apprenticeship record!", e); } finally { semaphore.Release(); } void VerifiyIfStandardOrFramework(ApprenticeshipResult tribalRecord, ReferenceDataFramework refDataFramework, ReferenceDateStandard refDataStandard) { if (refDataFramework == null && refDataStandard == null) { apprenticeshipWarning.Add($"Standard/Framework code does not exist - framework code {tribalRecord.FrameworkCode}, pathway code: {tribalRecord.PathWayCode}, standard code: {tribalRecord.StandardCode}, version: {tribalRecord.Version}"); } } ApprenticeshipDTO MapApprenticeship(IList <ApprenticeshipLocationDTO> locs, string id, ApprenticeshipResult tribalRecord, ApprenticeshipType apprenticeshipTye, RecordStatus recordStatus, string frameworkId, string standardId, string providerId) { var cosmosApprenticeship = new ApprenticeshipDTO() { id = id, ApprenticeshipId = tribalRecord.ApprenticeshipID, ApprenticeshipTitle = tribalRecord.ApprenticeshipTitle, ProviderId = providerId, PathWayCode = tribalRecord.PathWayCode, ProgType = tribalRecord.ProgType, ProviderUKPRN = tribalRecord.UKPRN, FrameworkId = frameworkId, StandardId = standardId, FrameworkCode = tribalRecord.FrameworkCode, StandardCode = tribalRecord.StandardCode, Version = tribalRecord.Version, MarketingInformation = tribalRecord.MarketingInformation, Url = tribalRecord.Url, ContactTelephone = tribalRecord.ContactTelephone, ContactEmail = tribalRecord.ContactEmail, ContactWebsite = tribalRecord.ContactWebsite, CreatedBy = createdBy, CreatedDate = createdDate, NotionalNVQLevelv2 = tribalRecord.NotionalEndLevel, ApprenticeshipLocations = locs, ApprenticeshipType = apprenticeshipTye, RecordStatus = recordStatus }; return(cosmosApprenticeship); } async Task <IList <Dfc.CourseDirectory.Models.Models.Venues.Venue> > GetCosmosVenues(IList <ApprenticeshipLocationResult> locations) { IList <Dfc.CourseDirectory.Models.Models.Venues.Venue> lst = new List <Dfc.CourseDirectory.Models.Models.Venues.Venue>(); foreach (var s in locations) { var venue = await venueCollectionService.GetDocumentByLocationId(s.LocationId, s.UKPRN); if (venue != null) { lst.Add(venue); } } return(lst); } async Task <Provider> GetProvider(ApprenticeshipResult item) { return(await providerCollectionService.GetDocumentByUkprn(item.UKPRN)); } async Task <List <ApprenticeshipLocationResult> > GetLocations(ApprenticeshipResult item) { using (var sqlConnection = new SqlConnection(connectionString)) { var lst = await sqlConnection.QueryAsync <ApprenticeshipLocationResult>(apprenticeshipLocationsSQL, new { apprenticeshipId = item.ApprenticeshipID }, commandType: CommandType.Text); return(lst.ToList()); } } async Task <ReferenceDateStandard> GetReferenceDataStandard(ApprenticeshipResult item) { var app = await apprenticeReferenceDataService.GetStandardById(item.StandardCode ?? 0, item.Version ?? 0); return(app?.Value?.Value); } async Task <ReferenceDataFramework> GetReferenceDataFramework(ApprenticeshipResult item) { //checks for framework apprenticeship var app = await apprenticeReferenceDataService.GetFrameworkByCode(item.FrameworkCode ?? 0, item.ProgType ?? 0, item.PathWayCode ?? 0); return(app?.Value?.Value); } async Task <Apprenticeship> GetExistingApprenticeship(ApprenticeshipResult item) { //fetch existing apprenticeship row. return(await apprenticeshipService.GetApprenticeshipByApprenticeshipID(item.ApprenticeshipID)); } async Task CreateOrUpdateApprenticeshipRecord(ApprenticeshipDTO app) { var s = UriFactory.CreateDocumentUri(databaseId, apprenticeshipCollectionId, app.id); Uri collectionUri = UriFactory.CreateDocumentCollectionUri(databaseId, apprenticeshipCollectionId); var res = await client.UpsertDocumentAsync(collectionUri, app); } RecordStatus MapApprenticeshipRecordStatus(IList <ApprenticeshipLocationDTO> mappedLocation) { //if there are any errors with apprenticeshipREcord, set record to migration pending. if (apprenticeshipErrors.Any()) { return(RecordStatus.MigrationPending); } else { return(RecordStatus.Live); } } //Taken entirely from previous migration logic. ApprenticeshipLocationType GetApprenticeshipLocationType(ApprenticeshipLocationResult lo) { var deliveryModes = lo.DeliveryModes; if ((deliveryModes.Contains(1) && !deliveryModes.Contains(2) && deliveryModes.Contains(3)) || (deliveryModes.Contains(1) && deliveryModes.Contains(2) && !deliveryModes.Contains(3)) || (deliveryModes.Contains(1) && deliveryModes.Contains(2) && deliveryModes.Contains(3))) { return(ApprenticeshipLocationType.ClassroomBasedAndEmployerBased); } else if ((!deliveryModes.Contains(1) && !deliveryModes.Contains(2) && deliveryModes.Contains(3)) || (!deliveryModes.Contains(1) && deliveryModes.Contains(2) && !deliveryModes.Contains(3)) || (!deliveryModes.Contains(1) && deliveryModes.Contains(2) && deliveryModes.Contains(3))) { return(ApprenticeshipLocationType.ClassroomBased); } else if (deliveryModes.Contains(1) && !deliveryModes.Contains(2) && !deliveryModes.Contains(3)) { return(ApprenticeshipLocationType.EmployerBased); } else { return(ApprenticeshipLocationType.Undefined); } } ApprenticeshipType MapApprenticeshipType(ApprenticeshipResult tribalRecord) { if (tribalRecord.StandardCode.HasValue) { return(ApprenticeshipType.StandardCode); } else if (tribalRecord.FrameworkCode.HasValue) { return(ApprenticeshipType.FrameworkCode); } else { apprenticeshipWarning.Add($"ApprenticeshipId: {tribalRecord.ApprenticeshipID} has undefined apprenticeshipType"); return(ApprenticeshipType.Undefined); } } IList <ApprenticeshipLocationDTO> MapLocations(IList <ApprenticeshipLocationResult> locations, IList <Dfc.CourseDirectory.Models.Models.Venues.Venue> venues) { var locationBasedApprenticeshipLocation = new List <ApprenticeshipLocationDTO>(); var regionBasedApprenticeshipLocation = new List <ApprenticeshipLocationDTO>(); //no need to proceed if (locations == null) { return(null); } //employer based apprenticeships - group all locations into regions/subregions foreach (var location in locations) { var type = GetApprenticeshipLocationType(location); if (type == ApprenticeshipLocationType.EmployerBased) { var allRegionsWithSubRegions = new SelectRegionModel(); var onspdRegionSubregion = onspdService.GetOnspdData(new OnspdSearchCriteria(location.Postcode)); if (onspdRegionSubregion.IsFailure) { apprenticeshipWarning.Add($"LocationId: {location.LocationId} - Querying onspd failed - {onspdRegionSubregion.Error}"); continue; } else if (!onspdRegionSubregion.HasValue) { apprenticeshipWarning.Add($"Location:{location.LocationId} - Did not find a record for postcode: {location.Postcode}"); continue; } var selectedSubRegion = allRegionsWithSubRegions.RegionItems.SelectMany(sr => sr.SubRegion.Where(sb => sb.SubRegionName == onspdRegionSubregion.Value.Value.LocalAuthority || sb.SubRegionName == onspdRegionSubregion.Value.Value.County || onspdRegionSubregion.Value.Value.LocalAuthority.Contains(sb.SubRegionName) )).FirstOrDefault(); if (selectedSubRegion == null) { apprenticeshipWarning.Add($"Location:{location.LocationId} Unable to match region with ons data api, location skipped"); continue; } else { var appLocation = new ApprenticeshipLocationDTO() { Id = Guid.NewGuid().ToString(), VenueId = Guid.Empty.ToString(), TribalId = location.ApprenticeshipLocationId, DeliveryModes = location.DeliveryModes, LocationId = selectedSubRegion.ApiLocationId, Name = location.LocationName, ProviderId = location.ProviderId, ProviderUKPRN = location.UKPRN, Radius = location.Radius, ApprenticeshipLocationType = type, LocationType = LocationType.SubRegion, LocationGuidId = null, Regions = new List <string> { selectedSubRegion.Id }, RecordStatus = VenueStatus.Live, CreatedBy = createdBy, CreatedDate = createdDate, UpdatedBy = createdBy, UpdatedDate = createdDate }; //region based apprenticeships regionBasedApprenticeshipLocation.Add(appLocation); } } else if (type == ApprenticeshipLocationType.ClassroomBased || type == ApprenticeshipLocationType.ClassroomBasedAndEmployerBased) { //venue based (location based apprenticeships) var cosmosVenueItem = venues.FirstOrDefault(x => x.LocationId == location.LocationId); var status = default(VenueStatus); //set status be that of what the venue status is, otherwise if venue is not found //set status to pending. if (cosmosVenueItem != null) { status = cosmosVenueItem.Status; } else { apprenticeshipWarning.Add($"LocationId: {location.LocationId} did not find a venue in cosmos, record marked as pending"); continue; } var appLocation = new ApprenticeshipLocationDTO() { Id = Guid.NewGuid().ToString(), VenueId = Guid.Empty.ToString(), TribalId = location.ApprenticeshipLocationId, Address = new Dfc.CourseDirectory.Models.Models.Apprenticeships.Address() { Address1 = cosmosVenueItem?.Address1, Address2 = cosmosVenueItem?.Address2, County = cosmosVenueItem?.County, Email = cosmosVenueItem?.Email, Website = cosmosVenueItem?.Website, Longitude = cosmosVenueItem?.Longitude, Latitude = cosmosVenueItem?.Latitude, Postcode = cosmosVenueItem?.PostCode, Town = cosmosVenueItem?.Town, Phone = cosmosVenueItem?.Telephone }, DeliveryModes = location.DeliveryModes, LocationId = location.LocationId, Name = location.LocationName, ProviderId = location.ProviderId, ProviderUKPRN = location.UKPRN, Radius = location.Radius, ApprenticeshipLocationType = type, LocationType = LocationType.Venue, LocationGuidId = cosmosVenueItem?.ID, Regions = null, RecordStatus = status, CreatedBy = createdBy, CreatedDate = createdDate, UpdatedBy = createdBy, UpdatedDate = createdDate }; locationBasedApprenticeshipLocation.Add(appLocation); } else { apprenticeshipWarning.Add($"LocationId: {location.LocationId} skipped as type was unknown {type}"); continue; } } //add a new location with all distinct regions. if (regionBasedApprenticeshipLocation.Any(x => x.RecordStatus == VenueStatus.Live)) { var regionLocation = regionBasedApprenticeshipLocation.FirstOrDefault(x => x.RecordStatus == VenueStatus.Live); regionLocation.Regions = regionBasedApprenticeshipLocation.Where(x => x.Regions != null).SelectMany(x => x.Regions).Distinct().ToList(); locationBasedApprenticeshipLocation.Add(regionLocation); } return(locationBasedApprenticeshipLocation); } })); //Log Results to blob storage var resultsObjBytes = GetResultAsByteArray(result); await WriteResultsToBlobStorage(resultsObjBytes); //log completion log.LogInformation("Migrating Apprenticeships Complete"); async Task <IList <int> > GetProviderWhiteList() { var list = new List <int>(); var whiteList = await blobhelper.ReadFileAsync(blobContainer, WHITE_LIST_FILE); if (!string.IsNullOrEmpty(whiteList)) { var lines = whiteList.Split(new[] { Environment.NewLine }, StringSplitOptions.None); foreach (string line in lines) { if (int.TryParse(line, out int id)) { list.Add(id); } } } return(list); } async Task WriteResultsToBlobStorage(byte[] data) { await blobhelper.UploadFile(blobContainer, venueExportFileName, data); } void AddResultMessage(int apprenticeshipId, int ukprn, string status, string apprenticeshipTitle, string message = "", string warnings = "") { lock (result) { var validateResult = new ApprenticeshipResultMessage() { ApprenticeshipID = apprenticeshipId, Status = status, Message = message, UKPRN = ukprn, ApprenticeshipTitle = apprenticeshipTitle, Warnings = warnings }; result.Add(validateResult); } } byte[] GetResultAsByteArray(IList <ApprenticeshipResultMessage> ob) { using (var memoryStream = new System.IO.MemoryStream()) { using (var streamWriter = new System.IO.StreamWriter(memoryStream)) using (var csvWriter = new CsvWriter(streamWriter, CultureInfo.InvariantCulture)) { csvWriter.WriteRecords <ApprenticeshipResultMessage>(ob); csvWriter.Flush(); } return(memoryStream.ToArray()); } } bool IsOnWhiteList(int ukprn) { if (!whiteListProviders.Any(x => x == ukprn)) { return(false); } else { return(true); } } }
public List <Course> MappingBulkUploadCourseToCourse(List <BulkUploadCourse> bulkUploadCourses, string userId, out List <string> errors) { errors = new List <string>(); var validationMessages = new List <string>(); var courses = new List <Course>(); var listsCourseRuns = new List <BulkUploadCourseRun>(); foreach (var bulkUploadCourse in bulkUploadCourses) { if (bulkUploadCourse.IsCourseHeader) { var course = new Course(); course.id = Guid.NewGuid(); course.QualificationCourseTitle = bulkUploadCourse.QualificationCourseTitle; course.LearnAimRef = bulkUploadCourse.LearnAimRef; course.NotionalNVQLevelv2 = bulkUploadCourse.NotionalNVQLevelv2; course.AwardOrgCode = bulkUploadCourse.AwardOrgCode; course.QualificationType = bulkUploadCourse.QualificationType; course.ProviderUKPRN = bulkUploadCourse.ProviderUKPRN; course.CourseDescription = bulkUploadCourse.CourseDescription; course.EntryRequirements = bulkUploadCourse.EntryRequirements; course.WhatYoullLearn = bulkUploadCourse.WhatYoullLearn; course.HowYoullLearn = bulkUploadCourse.HowYoullLearn; course.WhatYoullNeed = bulkUploadCourse.WhatYoullNeed; course.HowYoullBeAssessed = bulkUploadCourse.HowYoullBeAssessed; course.WhereNext = bulkUploadCourse.WhereNext; course.AdvancedLearnerLoan = bulkUploadCourse.AdvancedLearnerLoan.Equals("Yes", StringComparison.InvariantCultureIgnoreCase) ? true : false; course.AdultEducationBudget = bulkUploadCourse.AdultEducationBudget.Equals("Yes", StringComparison.InvariantCultureIgnoreCase) ? true : false; course.BulkUploadErrors = ParseBulkUploadErrors(bulkUploadCourse.BulkUploadLineNumber, _courseService.ValidateCourse(course)); course.IsValid = course.BulkUploadErrors.Any() ? false : true; course.CreatedBy = userId; course.CreatedDate = DateTime.Now; course.UpdatedBy = bulkUploadCourse.TempCourseId.ToString(); courses.Add(course); } var courseRun = new CourseRun(); courseRun.id = Guid.NewGuid(); courseRun.DeliveryMode = GetValueFromDescription <DeliveryMode>(bulkUploadCourse.DeliveryMode); if (courseRun.DeliveryMode.Equals(DeliveryMode.Undefined)) { validationMessages.Add($"DeliveryMode is Undefined, because you have entered ( { bulkUploadCourse.DeliveryMode } ), Line { bulkUploadCourse.BulkUploadLineNumber }, LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }"); } // Call VenueService and for VenueName get VenueId (GUID) (Applicable only for type ClassroomBased) if (string.IsNullOrEmpty(bulkUploadCourse.VenueName)) { validationMessages.Add($"NO Venue Name for Line { bulkUploadCourse.BulkUploadLineNumber }, LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }"); } else { //GetVenuesByPRNAndNameCriteria venueCriteria = new GetVenuesByPRNAndNameCriteria(bulkUploadCourse.ProviderUKPRN.ToString(), bulkUploadCourse.VenueName); var venueResultCache = cachedVenues.Where(o => o.VenueName.ToLower() == bulkUploadCourse.VenueName.ToLower() && o.Status == VenueStatus.Live).ToList(); if (null != venueResultCache && venueResultCache.Count > 0) { //var venues = (IEnumerable<Venue>)venueResultCeche.Value.Value; if (venueResultCache.Count().Equals(1)) { if (venueResultCache.FirstOrDefault().Status.Equals(VenueStatus.Live)) { courseRun.VenueId = new Guid(venueResultCache.FirstOrDefault().ID); } else { validationMessages.Add($"Venue is not LIVE (The status is { venueResultCache.FirstOrDefault().Status }) for VenueName { bulkUploadCourse.VenueName } - Line { bulkUploadCourse.BulkUploadLineNumber }, LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }"); } } else { validationMessages.Add($"We have obtained muliple Venues for { bulkUploadCourse.VenueName } - Line { bulkUploadCourse.BulkUploadLineNumber }, LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }"); if (venueResultCache.FirstOrDefault().Status.Equals(VenueStatus.Live)) { courseRun.VenueId = new Guid(venueResultCache.FirstOrDefault().ID); } else { validationMessages.Add($"The selected Venue is not LIVE (The status is { venueResultCache.FirstOrDefault().Status }) for VenueName { bulkUploadCourse.VenueName } - Line { bulkUploadCourse.BulkUploadLineNumber }, LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }"); } } } else { validationMessages.Add($"We could NOT obtain a Venue for { bulkUploadCourse.VenueName } - Line { bulkUploadCourse.BulkUploadLineNumber }, LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }"); } } courseRun.CourseName = bulkUploadCourse.CourseName; courseRun.ProviderCourseID = bulkUploadCourse.ProviderCourseID; courseRun.FlexibleStartDate = bulkUploadCourse.FlexibleStartDate.Equals("Yes", StringComparison.InvariantCultureIgnoreCase) ? true : false; DateTime specifiedStartDate; if (DateTime.TryParseExact(bulkUploadCourse.StartDate, "dd/MM/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out specifiedStartDate)) { courseRun.StartDate = specifiedStartDate; } else if (DateTime.TryParse(bulkUploadCourse.StartDate, out specifiedStartDate)) { //Remove time specifiedStartDate = specifiedStartDate.Date; courseRun.StartDate = specifiedStartDate; } else { courseRun.StartDate = null; validationMessages.Add($"StartDate is NULL, because you have entered ( { bulkUploadCourse.StartDate } ), Line { bulkUploadCourse.BulkUploadLineNumber }, LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }. We are expecting the date in 'dd/MM/yyyy' format."); } courseRun.CourseURL = bulkUploadCourse.CourseURL; decimal specifiedCost; if (decimal.TryParse(bulkUploadCourse.Cost, out specifiedCost)) { courseRun.Cost = specifiedCost; } else { courseRun.Cost = null; validationMessages.Add($"Cost is NULL, because you have entered ( { bulkUploadCourse.Cost } ), Line { bulkUploadCourse.BulkUploadLineNumber }, LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }"); } courseRun.CostDescription = ReplaceSpecialCharacters(bulkUploadCourse.CostDescription); courseRun.DurationUnit = GetValueFromDescription <DurationUnit>(bulkUploadCourse.DurationUnit); if (courseRun.DurationUnit.Equals(DurationUnit.Undefined)) { validationMessages.Add($"DurationUnit is Undefined, because you have entered ( { bulkUploadCourse.DurationUnit } ), Line { bulkUploadCourse.BulkUploadLineNumber }, LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }"); } int specifiedDurationValue; if (int.TryParse(bulkUploadCourse.DurationValue, out specifiedDurationValue)) { courseRun.DurationValue = specifiedDurationValue; } else { courseRun.DurationValue = null; validationMessages.Add($"DurationValue is NULL, because you have entered ( { bulkUploadCourse.DurationValue } ), Line { bulkUploadCourse.BulkUploadLineNumber }, LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }"); } courseRun.StudyMode = GetValueFromDescription <StudyMode>(bulkUploadCourse.StudyMode); if (courseRun.StudyMode.Equals(StudyMode.Undefined)) { validationMessages.Add($"StudyMode is Undefined, because you have entered ( { bulkUploadCourse.StudyMode } ), Line { bulkUploadCourse.BulkUploadLineNumber }, LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }"); } courseRun.AttendancePattern = GetValueFromDescription <AttendancePattern>(bulkUploadCourse.AttendancePattern); if (courseRun.AttendancePattern.Equals(AttendancePattern.Undefined)) { validationMessages.Add($"AttendancePattern is Undefined, because you have entered ( { bulkUploadCourse.AttendancePattern } ), Line { bulkUploadCourse.BulkUploadLineNumber }, LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }"); } switch (bulkUploadCourse.National.ToUpperInvariant()) { case "YES": { courseRun.National = true; var availableRegions = new SelectRegionModel(); courseRun.Regions = availableRegions.RegionItems.Select(x => x.Id).ToList(); break; } case "NO": { courseRun.National = false; var regionResult = ParseRegionData(bulkUploadCourse.Regions, bulkUploadCourse.SubRegions); if (regionResult.IsSuccess) { courseRun.Regions = regionResult.Value; } else if (!regionResult.IsSuccess) { validationMessages.Add($"Unable to get regions/subregions, Line { bulkUploadCourse.BulkUploadLineNumber }, LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }"); } break; } default: { courseRun.National = null; validationMessages.Add($"Choose if you can deliver this course anywhere in England, Line { bulkUploadCourse.BulkUploadLineNumber }, LARS_QAN = { bulkUploadCourse.LearnAimRef }, ID = { bulkUploadCourse.ProviderCourseID }"); break; } } courseRun.BulkUploadErrors = ParseBulkUploadErrors(bulkUploadCourse.BulkUploadLineNumber, _courseService.ValidateCourseRun(courseRun, ValidationMode.BulkUploadCourse)); courseRun.RecordStatus = courseRun.BulkUploadErrors.Any() ? RecordStatus.BulkUploadPending : RecordStatus.BulkUploadReadyToGoLive; courseRun.CreatedBy = userId; courseRun.CreatedDate = DateTime.Now; listsCourseRuns.Add(new BulkUploadCourseRun { LearnAimRef = bulkUploadCourse.LearnAimRef, TempCourseId = bulkUploadCourse.TempCourseId, CourseRun = courseRun }); } foreach (var course in courses) { int currentTempCourseId; if (int.TryParse(course.UpdatedBy, out currentTempCourseId)) { course.CourseRuns = listsCourseRuns.Where(cr => cr.LearnAimRef == course.LearnAimRef && cr.TempCourseId == currentTempCourseId).Select(cr => cr.CourseRun).ToList(); } else { validationMessages.Add($"Problem with parsing TempCourseId - ( { course.UpdatedBy } ), LARS_QAN = { course.LearnAimRef }, CourseFor = { course.CourseDescription }"); } course.UpdatedBy = null; } //// Uncomment only for DEV and TESTING //int courseNumber = 1; //foreach (var course in courses) //{ // string jsonBulkUploadCoursesFilesPath = "D:\\FindACourse-BulkUploadJSONfiles"; // var courseJson = JsonConvert.SerializeObject(course); // string jsonFileName = string.Format("{0}-{1}-{2}-{3}-{4}.json", DateTime.Now.ToString("yyMMdd-HHmmss"), course.ProviderUKPRN, course.LearnAimRef, courseNumber, course.CourseRuns.Count().ToString()); // File.WriteAllText(string.Format(@"{0}\{1}", jsonBulkUploadCoursesFilesPath, jsonFileName), courseJson); // courseNumber++; //} // Push the courses to the CourseService foreach (var course in courses) { var courseResult = Task.Run(async() => await _courseService.AddCourseAsync(course)).Result; if (courseResult.IsSuccess) { // Do nothing. Eventually we could have a count on successfully uploaded courses } else { errors.Add($"The course is NOT BulkUploaded, LARS_QAN = { course.LearnAimRef }. Error - { courseResult.Error }"); } } return(courses); }
public async Task <IActionResult> Publish() { if (!_session.GetInt32("UKPRN").HasValue) { return(RedirectToAction("Index", "Home", new { errmsg = "Please select a Provider." })); } var model = _session.GetObject <CopyCourseRunSaveViewModel>(CopyCourseRunSaveViewModelSessionKey); if (model == null) { return(NotFound()); } if (!model.CourseId.HasValue) { return(NotFound()); } var allRegions = await _regionCache.GetAllRegions(); var allSubRegionIds = allRegions.SelectMany(r => r.SubRegions).Select(sr => sr.Id).ToHashSet(); bool flexibleStartDate = model.StartDateType != StartDateType.SpecifiedStartDate; DateTime?specifiedStartDate = null; if (!flexibleStartDate) { specifiedStartDate = DateTime.ParseExact( $"{int.Parse(model.Day):00}-{int.Parse(model.Month):00}-{model.Year}", "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture); } if (model.National == true) { model.SelectedRegions = null; } var validationResult = new CopyCourseRunSaveViewModelValidator(allRegions, _clock).Validate(model); if (!validationResult.IsValid) { return(BadRequest()); } var createCommand = new CreateCourseRun() { CourseId = model.CourseId.Value, CourseRunId = Guid.NewGuid(), DurationUnit = model.DurationUnit, DeliveryMode = model.DeliveryMode, Cost = !string.IsNullOrEmpty(model.Cost) ? Convert.ToDecimal(model.Cost) : (decimal?)null, CostDescription = model.CostDescription ?? "", CourseName = model.CourseName, CourseUrl = model.Url, DurationValue = Convert.ToInt32(model.DurationLength), ProviderCourseId = model.CourseProviderReference ?? "", FlexibleStartDate = flexibleStartDate, StartDate = specifiedStartDate, CreatedBy = _currentUserProvider.GetCurrentUser(), CreatedOn = _clock.UtcNow }; switch (model.DeliveryMode) { case CourseDeliveryMode.ClassroomBased: createCommand.AttendancePattern = model.AttendanceMode; createCommand.StudyMode = model.StudyMode; createCommand.SubRegionIds = null; createCommand.VenueId = model.VenueId; break; case CourseDeliveryMode.WorkBased: createCommand.VenueId = null; var availableRegions = new SelectRegionModel(); if (model.National == true) { createCommand.National = true; createCommand.SubRegionIds = availableRegions.RegionItems.Select(x => x.Id).ToList(); } else { createCommand.National = false; createCommand.SubRegionIds = model.SelectedRegions.Where(id => allSubRegionIds.Contains(id)); createCommand.AttendancePattern = null; createCommand.StudyMode = null; } break; case CourseDeliveryMode.Online: createCommand.SubRegionIds = null; createCommand.VenueId = null; createCommand.AttendancePattern = null; createCommand.StudyMode = null; break; } var createResult = await _sqlQueryDispatcher.ExecuteQuery(createCommand); if (!(createResult.Value is Success)) { return(BadRequest()); } _session.SetObject(CopyCourseRunPublishedCourseSessionKey, new PublishedCourseViewModel { CourseId = createCommand.CourseId, CourseRunId = createCommand.CourseRunId, CourseName = createCommand.CourseName }); _session.Remove("NewAddedVenue"); _session.Remove("Option"); _session.Remove(CopyCourseRunSaveViewModelSessionKey); return(RedirectToAction("Published")); }
public async Task <IActionResult> Publish() { if (!_session.GetInt32("UKPRN").HasValue) { return(RedirectToAction("Index", "Home", new { errmsg = "Please select a Provider." })); } var model = _session.GetObject <CopyCourseRunSaveViewModel>(CopyCourseRunSaveViewModelSessionKey); if (model == null) { return(NotFound()); } if (!model.CourseId.HasValue) { return(NotFound()); } var course = await _courseService.GetCourseByIdAsync(new GetCourseByIdCriteria(model.CourseId.Value)); if (!course.IsSuccess) { return(NotFound()); } var courseRun = new CourseRun { id = Guid.NewGuid(), DurationUnit = model.DurationUnit, AttendancePattern = model.AttendanceMode, DeliveryMode = model.DeliveryMode, StudyMode = model.StudyMode, CostDescription = _htmlEncoder.Encode(model.CostDescription ?? ""), CourseName = _htmlEncoder.Encode(model.CourseName ?? ""), CourseURL = model.Url, DurationValue = Convert.ToInt32(model.DurationLength), ProviderCourseID = _htmlEncoder.Encode(model.CourseProviderReference ?? ""), RecordStatus = RecordStatus.Live, Cost = !string.IsNullOrEmpty(model.Cost) ? Convert.ToDecimal(model.Cost) : (decimal?)null, FlexibleStartDate = model.StartDateType != StartDateType.SpecifiedStartDate, CreatedDate = DateTime.Now, CreatedBy = User.Claims.Where(c => c.Type == "email").Select(c => c.Value).SingleOrDefault(), }; if (!courseRun.FlexibleStartDate) { courseRun.StartDate = DateTime.ParseExact( $"{int.Parse(model.Day):00}-{int.Parse(model.Month):00}-{model.Year}", "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture); } switch (model.DeliveryMode) { case DeliveryMode.ClassroomBased: courseRun.AttendancePattern = model.AttendanceMode; courseRun.StudyMode = model.StudyMode; courseRun.Regions = null; courseRun.VenueId = model.VenueId; break; case DeliveryMode.WorkBased: courseRun.VenueId = null; var availableRegions = new SelectRegionModel(); if (model.National) { courseRun.National = true; courseRun.Regions = availableRegions.RegionItems.Select(x => x.Id).ToList(); } else { courseRun.National = false; courseRun.Regions = model.SelectedRegions; string[] selectedRegions = availableRegions.SubRegionsDataCleanse(courseRun.Regions.ToList()); var subRegions = selectedRegions.Select(selectedRegion => availableRegions.GetSubRegionItemByRegionCode(selectedRegion)).ToList(); courseRun.SubRegions = subRegions; courseRun.AttendancePattern = AttendancePattern.Undefined; courseRun.StudyMode = StudyMode.Undefined; } break; case DeliveryMode.Online: courseRun.Regions = null; courseRun.VenueId = null; courseRun.AttendancePattern = AttendancePattern.Undefined; courseRun.StudyMode = StudyMode.Undefined; break; } course.Value.CourseRuns = course.Value.CourseRuns.Append(courseRun); try { var result = await _courseService.UpdateCourseAsync(course.Value); if (!result.IsSuccess) { throw new Exception($"{nameof(_courseService.UpdateCourseAsync)} failed during CopyCourseRun: {result.Error}"); } _session.SetObject(CopyCourseRunPublishedCourseSessionKey, new PublishedCourseViewModel { CourseId = course.Value.id, CourseRunId = courseRun.id, CourseName = courseRun.CourseName }); return(RedirectToAction("Published")); } finally { _session.Remove("NewAddedVenue"); _session.Remove("Option"); _session.Remove(CopyCourseRunSaveViewModelSessionKey); } }
public async Task <IActionResult> Index(EditCourseRunSaveViewModel model) { int?UKPRN; if (Session.GetInt32("UKPRN") != null) { UKPRN = Session.GetInt32("UKPRN").Value; } else { return(RedirectToAction("Index", "Home", new { errmsg = "Please select a Provider." })); } if (model.CourseId.HasValue) { var courseForEdit = await _courseService.GetCourseByIdAsync(new GetCourseByIdCriteria(model.CourseId.Value)); if (courseForEdit.IsSuccess) { var regions = new List <string>(); var courseRunForEdit = courseForEdit.Value.CourseRuns.SingleOrDefault(cr => cr.id == model.CourseRunId); courseRunForEdit.DurationUnit = model.DurationUnit; courseRunForEdit.DeliveryMode = model.DeliveryMode; courseRunForEdit.FlexibleStartDate = model.FlexibleStartDate; courseRunForEdit.Cost = Convert.ToDecimal(model.Cost); if (string.IsNullOrEmpty(model.Cost)) { courseRunForEdit.Cost = null; } courseRunForEdit.CostDescription = _htmlEncoder.Encode(model.CostDescription ?? ""); courseRunForEdit.CourseName = _htmlEncoder.Encode(model.CourseName); courseRunForEdit.CourseURL = model.Url; courseRunForEdit.DurationValue = Convert.ToInt32(model.DurationLength); courseRunForEdit.ProviderCourseID = _htmlEncoder.Encode(model.CourseProviderReference ?? ""); bool flexibleStartDate = true; DateTime?specifiedStartDate = null; if (model.StartDateType.Equals("SpecifiedStartDate", StringComparison.InvariantCultureIgnoreCase)) { string day = model.Day.Length == 1 ? string.Concat("0", model.Day) : model.Day; string month = model.Month.Length == 1 ? string.Concat("0", model.Month) : model.Month; string startDate = string.Format("{0}-{1}-{2}", day, month, model.Year); specifiedStartDate = DateTime.ParseExact(startDate, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture); flexibleStartDate = false; } courseRunForEdit.FlexibleStartDate = flexibleStartDate; courseRunForEdit.StartDate = specifiedStartDate; courseRunForEdit.UpdatedDate = DateTime.Now; courseRunForEdit.UpdatedBy = User.Claims.Where(c => c.Type == "email").Select(c => c.Value).SingleOrDefault(); //Set to null by default courseRunForEdit.National = null; switch (model.DeliveryMode) { case DeliveryMode.ClassroomBased: courseRunForEdit.National = null; courseRunForEdit.Regions = null; courseRunForEdit.VenueId = model.VenueId; courseRunForEdit.AttendancePattern = model.AttendanceMode; courseRunForEdit.StudyMode = model.StudyMode; break; case DeliveryMode.WorkBased: courseRunForEdit.VenueId = null; var availableRegions = new SelectRegionModel(); if (model.National) { courseRunForEdit.National = true; courseRunForEdit.Regions = availableRegions.RegionItems.Select(x => x.Id).ToList(); } else { courseRunForEdit.National = false; courseRunForEdit.Regions = model.SelectedRegions; string[] selectedRegions = availableRegions.SubRegionsDataCleanse(courseRunForEdit.Regions.ToList()); var subRegions = selectedRegions.Select(selectedRegion => availableRegions.GetSubRegionItemByRegionCode(selectedRegion)).ToList(); courseRunForEdit.SubRegions = subRegions; courseRunForEdit.AttendancePattern = AttendancePattern.Undefined; courseRunForEdit.StudyMode = StudyMode.Undefined; } break; case DeliveryMode.Online: courseRunForEdit.Regions = null; courseRunForEdit.VenueId = null; courseRunForEdit.National = null; courseRunForEdit.AttendancePattern = AttendancePattern.Undefined; courseRunForEdit.StudyMode = StudyMode.Undefined; break; } // Check if courserun has any errors courseRunForEdit.ValidationErrors = _courseService.ValidateCourseRun(courseRunForEdit, ValidationMode.MigrateCourse).Select(x => x.Value); courseForEdit.Value.ValidationErrors = _courseService.ValidateCourse(courseForEdit.Value).Select(x => x.Value); // Check if course run has issues bool isCourseValid = !(courseForEdit.Value.ValidationErrors != null && courseForEdit.Value.ValidationErrors.Any()); bool isValidCourseRun = !(courseRunForEdit.ValidationErrors != null && courseRunForEdit.ValidationErrors.Any()); //todo when real data switch (model.Mode) { case PublishMode.BulkUpload: courseRunForEdit.RecordStatus = RecordStatus.BulkUploadReadyToGoLive; break; case PublishMode.Migration: // Set courserun status to Migration Ready to go live if no errors found courseRunForEdit.RecordStatus = isValidCourseRun ? RecordStatus.MigrationReadyToGoLive : RecordStatus.MigrationPending; break; case PublishMode.DataQualityIndicator: default: courseRunForEdit.RecordStatus = RecordStatus.Live; break; } Session.Remove("NewAddedVenue"); Session.Remove("Option"); // Check if course has no errors if (model.Mode == PublishMode.Migration) { if (isCourseValid) { // Change courseruns status of MigrationReadyToGoLive to Live so the entire course can go live foreach (var courseRun in courseForEdit.Value.CourseRuns.Where(x => x.RecordStatus == RecordStatus.MigrationReadyToGoLive)) { courseRun.RecordStatus = RecordStatus.Live; } } } var status = courseForEdit.Value.CourseStatus; var message = string.Empty; RecordStatus[] validStatuses = new[] { RecordStatus.MigrationReadyToGoLive, RecordStatus.Live }; // Coures run is valid course is invalid, course run is fixed if (courseRunForEdit.RecordStatus == RecordStatus.MigrationReadyToGoLive && !isCourseValid) { message = $"'{courseRunForEdit.CourseName}' was successfully fixed"; } // Course is valid and ALL course runs are fixed (course runs = Live), course can be publisehd if (isCourseValid && !(courseForEdit.Value.CourseRuns.Where(x => !validStatuses.Contains(x.RecordStatus)).Any())) { message = $"'{courseForEdit.Value.QualificationCourseTitle}' was successfully fixed and published."; } var updatedCourse = await _courseService.UpdateCourseAsync(courseForEdit.Value); if (updatedCourse.IsSuccess) { switch (model.Mode) { case PublishMode.BulkUpload: return(RedirectToAction("Index", "PublishCourses", new { publishMode = model.Mode, courseId = model.CourseId, courseRunId = model.CourseRunId, notificationTitle = "" })); case PublishMode.Migration: return(RedirectToAction("Index", "PublishCourses", new { publishMode = model.Mode, courseId = model.CourseId, courseRunId = model.CourseRunId, notificationTitle = message })); case PublishMode.DataQualityIndicator: return(RedirectToAction("Index", "PublishCourses", new { publishMode = model.Mode, courseId = model.CourseId, courseRunId = model.CourseRunId, NotificationTitle = model.CourseName + " has been updated", NotificationMessage = "Start date edited" })); default: TempData[TempDataKeys.ShowCourseUpdatedNotification] = true; return(RedirectToAction("Index", "CourseSummary", new { courseId = model.CourseId, courseRunId = model.CourseRunId })); } } } } return(View("Error", new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier })); }
public async Task <IActionResult> Index(EditCourseRunSaveViewModel model) { if (!model.CourseId.HasValue) { return(BadRequest()); } var courseId = model.CourseId.Value; var allRegions = await _regionCache.GetAllRegions(); var allSubRegionIds = allRegions.SelectMany(r => r.SubRegions).Select(sr => sr.Id).ToHashSet(); bool flexibleStartDate = true; DateTime?specifiedStartDate = null; if (model.StartDateType.Equals("SpecifiedStartDate", StringComparison.InvariantCultureIgnoreCase)) { string day = model.Day.Length == 1 ? string.Concat("0", model.Day) : model.Day; string month = model.Month.Length == 1 ? string.Concat("0", model.Month) : model.Month; string startDate = string.Format("{0}-{1}-{2}", day, month, model.Year); specifiedStartDate = DateTime.ParseExact(startDate, "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture); model.StartDate = specifiedStartDate.Value; flexibleStartDate = false; } if (model.National == true) { model.SelectedRegions = null; } model.FlexibleStartDate = flexibleStartDate; var validationResult = new EditCourseRunSaveViewModelValidator(allRegions, _clock).Validate(model); if (!validationResult.IsValid) { return(BadRequest()); } var updateCommand = new UpdateCourseRun() { CourseRunId = model.CourseRunId, DurationUnit = model.DurationUnit, DeliveryMode = model.DeliveryMode, Cost = !string.IsNullOrEmpty(model.Cost) ? Convert.ToDecimal(model.Cost) : (decimal?)null, CostDescription = model.CostDescription ?? "", CourseName = model.CourseName, CourseUrl = model.Url, DurationValue = Convert.ToInt32(model.DurationLength), ProviderCourseId = model.CourseProviderReference ?? "", FlexibleStartDate = flexibleStartDate, StartDate = specifiedStartDate, UpdatedBy = _currentUserProvider.GetCurrentUser(), UpdatedOn = _clock.UtcNow }; updateCommand.National = null; switch (model.DeliveryMode) { case CourseDeliveryMode.ClassroomBased: updateCommand.National = null; updateCommand.SubRegionIds = null; updateCommand.VenueId = model.VenueId; updateCommand.AttendancePattern = model.AttendanceMode; updateCommand.StudyMode = model.StudyMode; break; case CourseDeliveryMode.WorkBased: updateCommand.VenueId = null; var availableRegions = new SelectRegionModel(); if (model.National.Value) { updateCommand.National = true; updateCommand.SubRegionIds = null; } else { updateCommand.National = false; updateCommand.SubRegionIds = model.SelectedRegions.Where(id => allSubRegionIds.Contains(id)); updateCommand.AttendancePattern = null; updateCommand.StudyMode = null; } break; case CourseDeliveryMode.Online: updateCommand.SubRegionIds = null; updateCommand.VenueId = null; updateCommand.National = null; updateCommand.AttendancePattern = null; updateCommand.StudyMode = null; break; } var updateResult = await _sqlQueryDispatcher.ExecuteQuery(updateCommand); if (!(updateResult.Value is Success)) { return(BadRequest()); } Session.Remove("NewAddedVenue"); Session.Remove("Option"); switch (model.Mode) { case PublishMode.DataQualityIndicator: TempData[TempDataKeys.ExpiredCoursesNotification] = model.CourseName + " has been updated"; return(RedirectToAction("Index", "ExpiredCourseRuns") .WithProviderContext(_providerContextProvider.GetProviderContext(withLegacyFallback: true))); default: TempData[TempDataKeys.ShowCourseUpdatedNotification] = true; return(RedirectToAction("Index", "CourseSummary", new { courseId = model.CourseId, courseRunId = model.CourseRunId })); } }
private IEnumerable <CsvCourse> CoursesToCsvCourses(IEnumerable <Course> courses) { List <CsvCourse> csvCourses = new List <CsvCourse>(); foreach (var course in courses) { //First course run is on same line as course line in CSV var courseRuns = course.CourseRuns.Where(r => r.RecordStatus == RecordStatus.Live).ToArray(); if (!courseRuns.Any()) { continue; } var firstCourseRun = courseRuns.First(); if (firstCourseRun.Regions != null) { firstCourseRun.Regions = _CSVHelper.SanitiseRegionTextForCSVOutput(firstCourseRun.Regions); } SelectRegionModel selectRegionModel = new SelectRegionModel(); CsvCourse csvCourse = new CsvCourse { LearnAimRef = course.LearnAimRef != null?_CSVHelper.SanitiseTextForCSVOutput(course.LearnAimRef) : string.Empty, CourseDescription = !string.IsNullOrWhiteSpace(course.CourseDescription) ? _CSVHelper.SanitiseTextForCSVOutput(course.CourseDescription) : string.Empty, EntryRequirements = !string.IsNullOrWhiteSpace(course.EntryRequirements) ? _CSVHelper.SanitiseTextForCSVOutput(course.EntryRequirements) : string.Empty, WhatYoullLearn = !string.IsNullOrWhiteSpace(course.WhatYoullLearn) ? _CSVHelper.SanitiseTextForCSVOutput(course.WhatYoullLearn) : string.Empty, HowYoullLearn = !string.IsNullOrWhiteSpace(course.HowYoullLearn) ? _CSVHelper.SanitiseTextForCSVOutput(course.HowYoullLearn) : string.Empty, WhatYoullNeed = !string.IsNullOrWhiteSpace(course.WhatYoullNeed) ? _CSVHelper.SanitiseTextForCSVOutput(course.WhatYoullNeed) : string.Empty, HowYoullBeAssessed = !string.IsNullOrWhiteSpace(course.HowYoullBeAssessed) ? _CSVHelper.SanitiseTextForCSVOutput(course.HowYoullBeAssessed) : string.Empty, WhereNext = !string.IsNullOrWhiteSpace(course.WhereNext) ? _CSVHelper.SanitiseTextForCSVOutput(course.WhereNext) : string.Empty, AdvancedLearnerLoan = course.AdvancedLearnerLoan ? "Yes" : "No", AdultEducationBudget = course.AdultEducationBudget ? "Yes" : "No", CourseName = firstCourseRun.CourseName != null?_CSVHelper.SanitiseTextForCSVOutput(firstCourseRun.CourseName) : string.Empty, ProviderCourseID = firstCourseRun.ProviderCourseID != null?_CSVHelper.SanitiseTextForCSVOutput(firstCourseRun.ProviderCourseID) : string.Empty, DeliveryMode = firstCourseRun.DeliveryMode.ToDescription(), StartDate = firstCourseRun.StartDate.HasValue ? firstCourseRun.StartDate.Value.ToString("dd/MM/yyyy") : string.Empty, FlexibleStartDate = firstCourseRun.FlexibleStartDate ? "Yes" : string.Empty, National = firstCourseRun.National.HasValue ? (firstCourseRun.National.Value ? "Yes" : "No") : string.Empty, Regions = firstCourseRun.Regions != null?_CSVHelper.SemiColonSplit( selectRegionModel.RegionItems .Where(x => firstCourseRun.Regions.Contains(x.Id)) .Select(y => _CSVHelper.SanitiseTextForCSVOutput(y.RegionName).Replace(",", "")).ToList()) : string.Empty, SubRegions = firstCourseRun.Regions != null?_CSVHelper.SemiColonSplit( selectRegionModel.RegionItems.SelectMany( x => x.SubRegion.Where( y => firstCourseRun.Regions.Contains(y.Id)).Select( z => _CSVHelper.SanitiseTextForCSVOutput(z.SubRegionName).Replace(",", "")).ToList())) : string.Empty, CourseURL = firstCourseRun.CourseURL != null?_CSVHelper.SanitiseTextForCSVOutput(firstCourseRun.CourseURL) : string.Empty, Cost = firstCourseRun.Cost.HasValue ? firstCourseRun.Cost.Value.ToString() : string.Empty, CostDescription = firstCourseRun.CostDescription != null?_CSVHelper.SanitiseTextForCSVOutput(firstCourseRun.CostDescription) : string.Empty, DurationValue = firstCourseRun.DurationValue.HasValue ? firstCourseRun.DurationValue.Value.ToString() : string.Empty, DurationUnit = firstCourseRun.DurationUnit.ToDescription(), StudyMode = firstCourseRun.StudyMode.ToDescription(), AttendancePattern = firstCourseRun.AttendancePattern.ToDescription() }; if (firstCourseRun.VenueId.HasValue) { var result = _cosmosDbQueryDispatcher.ExecuteQuery( new GetVenueById() { VenueId = firstCourseRun.VenueId.Value }).Result; if (!string.IsNullOrWhiteSpace(result?.VenueName)) { csvCourse.VenueName = result.VenueName; } } csvCourses.Add(csvCourse); foreach (var courseRun in courseRuns) { //Ignore the first course run as we've already captured it if (courseRun.id == firstCourseRun.id) { continue; } //Sanitise regions if (courseRun.Regions != null) { courseRun.Regions = _CSVHelper.SanitiseRegionTextForCSVOutput(courseRun.Regions); } CsvCourse csvCourseRun = new CsvCourse { LearnAimRef = course.LearnAimRef != null?_CSVHelper.SanitiseTextForCSVOutput(course.LearnAimRef) : string.Empty, CourseName = courseRun.CourseName != null?_CSVHelper.SanitiseTextForCSVOutput(courseRun.CourseName) : string.Empty, ProviderCourseID = courseRun.ProviderCourseID != null?_CSVHelper.SanitiseTextForCSVOutput(courseRun.ProviderCourseID) : string.Empty, DeliveryMode = courseRun.DeliveryMode.ToDescription(), StartDate = courseRun.StartDate.HasValue ? courseRun.StartDate.Value.ToString("dd/MM/yyyy") : string.Empty, FlexibleStartDate = courseRun.FlexibleStartDate ? "Yes" : string.Empty, National = courseRun.National.HasValue ? (courseRun.National.Value ? "Yes" : "No") : string.Empty, Regions = courseRun.Regions != null?_CSVHelper.SemiColonSplit( selectRegionModel.RegionItems .Where(x => courseRun.Regions.Contains(x.Id)) .Select(y => _CSVHelper.SanitiseTextForCSVOutput(y.RegionName).Replace(",", "")).ToList()) : string.Empty, SubRegions = courseRun.Regions != null?_CSVHelper.SemiColonSplit( selectRegionModel.RegionItems.SelectMany( x => x.SubRegion.Where( y => courseRun.Regions.Contains(y.Id)).Select( z => _CSVHelper.SanitiseTextForCSVOutput(z.SubRegionName).Replace(",", "")).ToList())) : string.Empty, CourseURL = courseRun.CourseURL != null?_CSVHelper.SanitiseTextForCSVOutput(courseRun.CourseURL) : string.Empty, Cost = courseRun.Cost.HasValue ? courseRun.Cost.Value.ToString() : string.Empty, CostDescription = courseRun.CostDescription != null?_CSVHelper.SanitiseTextForCSVOutput(courseRun.CostDescription) : string.Empty, DurationValue = courseRun.DurationValue.HasValue ? courseRun.DurationValue.Value.ToString() : string.Empty, DurationUnit = courseRun.DurationUnit.ToDescription(), StudyMode = courseRun.StudyMode.ToDescription(), AttendancePattern = courseRun.AttendancePattern.ToDescription() }; if (courseRun.VenueId.HasValue) { var result = _cosmosDbQueryDispatcher.ExecuteQuery(new GetVenueById() { VenueId = courseRun.VenueId.Value }).Result; if (!string.IsNullOrWhiteSpace(result?.VenueName)) { csvCourseRun.VenueName = result.VenueName; } } csvCourses.Add(csvCourseRun); } } return(csvCourses); }
public static (IReadOnlyCollection <string> regionIds, IReadOnlyCollection <SubRegionItemModel> subRegions)? FindRegions( int venueLocationId) { var lookup = LazyInitializer.EnsureInitialized(ref s_lookup, LoadLookupFile); lookup.TryGetValue(venueLocationId, out var regionId); if (regionId == default) { return(null); } else { var availableRegions = new SelectRegionModel(); // If region is not a subregion, expand its subregions var region = availableRegions.RegionItems.SingleOrDefault(r => r.Id == regionId); if (region != null) { var regionIds = region.SubRegion.Select(sr => sr.Id).ToList(); var subRegions = new[] { availableRegions.GetSubRegionItemByRegionCode(regionId) }; return(regionIds, subRegions); } else { var subRegion = availableRegions.RegionItems.SelectMany(ri => ri.SubRegion).Single(r => r.Id == regionId); var regionIds = new[] { regionId }; var subRegions = new[] { subRegion }; return(regionIds, subRegions); } } Dictionary <int, string> LoadLookupFile() { var results = new Dictionary <int, string>(); // Keep a record of all the VenueLocationIds so we can detect duplicates. // Don't add duplicates to the output - it should be a lookup error. var venueLocationIds = new HashSet <int>(); var lookupFileResourceName = "Dfc.ProviderPortal.TribalExporter.VenueLookup.txt"; using (var lookupFile = typeof(RegionLookup).Assembly.GetManifestResourceStream(lookupFileResourceName)) using (var reader = new StreamReader(lookupFile)) { string line; while ((line = reader.ReadLine()) != null) { if (string.IsNullOrWhiteSpace(line)) { continue; } var parts = line.Split("\t,".ToCharArray()); var vlId = int.Parse(parts[0]); var region = parts[1]; if (venueLocationIds.Add(vlId)) { results.Add(vlId, region); } else { // Duplicate - remove all for VenueLocationId results.Remove(vlId); } } } return(results); } }