Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/sonar-issues #235

Merged
merged 10 commits into from
Jul 18, 2024
104 changes: 60 additions & 44 deletions content/Dfe.EarlyYearsQualification.ContentUpload/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Contentful.Core.Models;
using Contentful.Core.Models.Management;
using Contentful.Core.Search;
using Dfe.EarlyYearsQualification.Content.Constants;
using Microsoft.VisualBasic.FileIO;

namespace Dfe.EarlyYearsQualification.ContentUpload;
Expand All @@ -15,6 +16,7 @@ public static class Program
private const string SpaceId = "";
private const string ManagementApiKey = "";
private const string CsvFile = "./csv/ey-quals-full-2024-with-new-reference-fields.csv";
private const string FieldTypeSymbol = "Symbol";

// ReSharper disable once UnusedParameter.Global
// ...args standard for Program.Main()
Expand All @@ -36,10 +38,12 @@ public static async Task Main(string[] args)
private static async Task PopulateContentfulWithQualifications(ContentfulManagementClient client)
{
// Check existing entries and identify those to update, and those to create.
var queryBuilder = new QueryBuilder<Entry<dynamic>>()
.ContentTypeIs(ContentTypes.Qualification)
.Limit(1000);

var currentEntries =
await
client.GetEntriesForLocale(new QueryBuilder<Entry<dynamic>>().ContentTypeIs("Qualification").Limit(1000),
Locale);
await client.GetEntriesForLocale(queryBuilder, Locale);

var qualificationsToAddOrUpdate = GetQualificationsToAddOrUpdate();

Expand All @@ -56,7 +60,8 @@ private static async Task PopulateContentfulWithQualifications(ContentfulManagem
}

// Create new entry
var entryToPublish = await client.CreateOrUpdateEntry(entryToAddOrUpdate, contentTypeId: "Qualification",
var entryToPublish = await client.CreateOrUpdateEntry(entryToAddOrUpdate,
contentTypeId: ContentTypes.Qualification,
version: entryToAddOrUpdate.SystemProperties.Version);

await client.PublishEntry(entryToPublish.SystemProperties.Id,
Expand All @@ -67,17 +72,17 @@ await client.PublishEntry(entryToPublish.SystemProperties.Id,
private static async Task SetUpContentModels(ContentfulManagementClient client)
{
// Check current version of model
var contentTypeModel = await client.GetContentType("Qualification");
var contentTypeModel = await client.GetContentType(ContentTypes.Qualification);

var version = contentTypeModel?.SystemProperties.Version ?? 1;

var contentType = new ContentType
{
SystemProperties = new SystemProperties
{
Id = "Qualification"
Id = ContentTypes.Qualification
},
Name = "Qualification",
Name = ContentTypes.Qualification,
Description = "Model for storing all the early years qualifications",
DisplayField = "qualificationName",
Fields =
Expand All @@ -86,15 +91,15 @@ private static async Task SetUpContentModels(ContentfulManagementClient client)
{
Name = "Qualification ID",
Id = "qualificationId",
Type = "Symbol",
Type = FieldTypeSymbol,
Required = true,
Validations = [new UniqueValidator()]
},
new Field
{
Name = "Qualification Name",
Id = "qualificationName",
Type = "Symbol",
Type = FieldTypeSymbol,
Required = true
},
new Field
Expand All @@ -108,26 +113,26 @@ private static async Task SetUpContentModels(ContentfulManagementClient client)
{
Name = "Awarding Organisation Title",
Id = "awardingOrganisationTitle",
Type = "Symbol",
Type = FieldTypeSymbol,
Required = true
},
new Field
{
Name = "From Which Year",
Id = "fromWhichYear",
Type = "Symbol"
Type = FieldTypeSymbol
},
new Field
{
Name = "To Which Year",
Id = "toWhichYear",
Type = "Symbol"
Type = FieldTypeSymbol
},
new Field
{
Name = "Qualification Number",
Id = "qualificationNumber",
Type = "Symbol"
Type = FieldTypeSymbol
},
new Field
{
Expand All @@ -144,10 +149,14 @@ private static async Task SetUpContentModels(ContentfulManagementClient client)
{
Type = "Link",
LinkType = "Entry",
Validations = [new LinkContentTypeValidator
{
ContentTypeIds = ["additionalRequirementQuestion"]
}]
Validations =
[
new LinkContentTypeValidator
{
ContentTypeIds =
["additionalRequirementQuestion"]
}
]
}
},
new Field
Expand All @@ -159,25 +168,28 @@ private static async Task SetUpContentModels(ContentfulManagementClient client)
{
Type = "Link",
LinkType = "Entry",
Validations = [new LinkContentTypeValidator
{
ContentTypeIds = ["ratioRequirement"]
}]
Validations =
[
new LinkContentTypeValidator
{
ContentTypeIds = ["ratioRequirement"]
}
]
}
}
]
};

var typeToActivate = await client.CreateOrUpdateContentType(contentType, version: version);
await client.ActivateContentType("Qualification", typeToActivate.SystemProperties.Version!.Value);
await client.ActivateContentType(ContentTypes.Qualification, typeToActivate.SystemProperties.Version!.Value);

Thread.Sleep(2000); // Allows the API time to activate the content type
await SetHelpText(client);
}

private static async Task SetHelpText(ContentfulManagementClient client)
{
var editorInterface = await client.GetEditorInterface("Qualification");
var editorInterface = await client.GetEditorInterface(ContentTypes.Qualification);
SetHelpTextForField(editorInterface, "qualificationId",
"The unique identifier used to reference the qualification");
SetHelpTextForField(editorInterface, "qualificationName", "The name of the qualification");
Expand All @@ -191,10 +203,13 @@ private static async Task SetHelpText(ContentfulManagementClient client)
SetHelpTextForField(editorInterface, "qualificationNumber", "The number of the qualification");
SetHelpTextForField(editorInterface, "additionalRequirements",
"The additional requirements for the qualification", SystemWidgetIds.MultipleLine);
SetHelpTextForField(editorInterface, "additionalRequirementQuestions", "The optional additional requirements questions", SystemWidgetIds.EntryMultipleLinksEditor);
SetHelpTextForField(editorInterface, "ratioRequirements", "The optional ratio requirements", SystemWidgetIds.EntryMultipleLinksEditor);

await client.UpdateEditorInterface(editorInterface, "Qualification", editorInterface.SystemProperties.Version!.Value);
SetHelpTextForField(editorInterface, "additionalRequirementQuestions",
"The optional additional requirements questions", SystemWidgetIds.EntryMultipleLinksEditor);
SetHelpTextForField(editorInterface, "ratioRequirements", "The optional ratio requirements",
SystemWidgetIds.EntryMultipleLinksEditor);

await client.UpdateEditorInterface(editorInterface, ContentTypes.Qualification,
editorInterface.SystemProperties.Version!.Value);
}

private static void SetHelpTextForField(EditorInterface editorInterface, string fieldId, string helpText,
Expand All @@ -207,25 +222,26 @@ private static void SetHelpTextForField(EditorInterface editorInterface, string

private static Entry<dynamic> BuildEntryFromQualification(QualificationUpload qualification)
{
var addtionalRequirementQuestions = new List<Reference>();
var additionalRequirementQuestions = new List<Reference>();
if (qualification.AdditionalRequirementQuestions is not null)
{
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var questionId in qualification.AdditionalRequirementQuestions)
{
addtionalRequirementQuestions.Add(new Reference(SystemLinkTypes.Entry, questionId));
additionalRequirementQuestions.Add(new Reference(SystemLinkTypes.Entry, questionId));
}
}

var ratioRequirements = new List<Reference>();
if (qualification.RatioRequirements is not null)
{
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var ratioId in qualification.RatioRequirements)
{
ratioRequirements.Add(new Reference(SystemLinkTypes.Entry, ratioId));
}
}



var entry = new Entry<dynamic>
{
SystemProperties = new SystemProperties
Expand Down Expand Up @@ -275,16 +291,16 @@ private static Entry<dynamic> BuildEntryFromQualification(QualificationUpload qu
{
{ Locale, qualification.AdditionalRequirements ?? "" }
},
additionalRequirementQuestions = new Dictionary<string, List<Reference>>()

additionalRequirementQuestions = new Dictionary<string, List<Reference>>
{
{ Locale, addtionalRequirementQuestions }
{ Locale, additionalRequirementQuestions }
},
ratioRequirements = new Dictionary<string, List<Reference>>()
{
{ Locale, ratioRequirements }
}

ratioRequirements = new Dictionary<string, List<Reference>>
{
{ Locale, ratioRequirements }
}
}
};

Expand All @@ -309,13 +325,13 @@ private static List<QualificationUpload> GetQualificationsToAddOrUpdate()
var additionalRequirements = t[7];
var additionalRequirementQuestionString = t[8];
var ratioRequirementsString = t[9];

string[] additionalRequirementQuestionsArray = [];
if (!string.IsNullOrEmpty(additionalRequirementQuestionString))
{
additionalRequirementQuestionsArray = additionalRequirementQuestionString.Split(':');
}

string[] ratioRequirementsArray = [];
if (!string.IsNullOrEmpty(ratioRequirementsString))
{
Expand Down Expand Up @@ -343,7 +359,7 @@ private static List<string[]> ReadCsvFile(string filePath)
{
var result = new List<string[]>();
using var csvParser = new TextFieldParser(filePath);
csvParser.SetDelimiters([","]);
csvParser.SetDelimiters(",");
csvParser.HasFieldsEnclosedInQuotes = true;

// Skip the row with the column names
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,35 @@ private static List<string> IncludeLinkedOrganisations(string awardingOrganisati
{
var result = new List<string>();

if (awardingOrganisation is AwardingOrganisations.Edexcel or AwardingOrganisations.Pearson)
switch (awardingOrganisation)
{
result.AddRange(new List<string> { AwardingOrganisations.Edexcel, AwardingOrganisations.Pearson });
}
else if (awardingOrganisation is AwardingOrganisations.Ncfe or AwardingOrganisations.Cache
&& startDateMonth.HasValue && startDateYear.HasValue)
{
var cutOffDate = new DateOnly(2014, 9, 1);
var date = new DateOnly(startDateYear.Value, startDateMonth.Value, 1);
if (date >= cutOffDate)
case AwardingOrganisations.Edexcel or AwardingOrganisations.Pearson:
{
result.AddRange(new List<string> { AwardingOrganisations.Ncfe, AwardingOrganisations.Cache });
result.AddRange(new List<string> { AwardingOrganisations.Edexcel, AwardingOrganisations.Pearson });
break;
}
else
case AwardingOrganisations.Ncfe or AwardingOrganisations.Cache
when startDateMonth.HasValue && startDateYear.HasValue:
{
var cutOffDate = new DateOnly(2014, 9, 1);
var date = new DateOnly(startDateYear.Value, startDateMonth.Value, 1);
if (date >= cutOffDate)
{
result.AddRange(new List<string> { AwardingOrganisations.Ncfe, AwardingOrganisations.Cache });
}
else
{
result.Add(awardingOrganisation);
}

break;
}
default:
{
result.Add(awardingOrganisation);
break;
}
}
else
{
result.Add(awardingOrganisation);
}

return result;
}
Expand Down Expand Up @@ -230,14 +237,14 @@ private List<Qualification> FilterQualificationsByDate(int? startDateMonth, int?
return (false, 0, 0);
}

if (!Months.TryGetValue(abbreviatedMonth, out var month))
if (Months.TryGetValue(abbreviatedMonth, out var month))
{
logger.LogError("Qualification date {QualificationDate} contains unexpected month value",
qualificationDate);

return (false, 0, 0);
return (true, month, yearPart);
}

return (true, month, yearPart);
logger.LogError("Qualification date {QualificationDate} contains unexpected month value",
qualificationDate);

return (false, 0, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ public async Task<IActionResult> Get()

return View(model);
}

[HttpPost]
public IActionResult Refine(string refineSearch)
public IActionResult Refine(string? refineSearch)
{
userJourneyCookieService.SetQualificationNameSearchCriteria(refineSearch);
if (!ModelState.IsValid)
{
logger.LogWarning($"Invalid model state in {nameof(QualificationDetailsController)} POST");
}

userJourneyCookieService.SetQualificationNameSearchCriteria(refineSearch ?? string.Empty);

return RedirectToAction("Get");
}
Expand Down Expand Up @@ -126,7 +131,7 @@ private FilterModel GetFilterModel(QualificationListPage content)
}

var level = userJourneyCookieService.GetLevelOfQualification();
if (level is not null && level > 0)
if (level > 0)
{
filterModel.Level = $"Level {level}";
}
Expand All @@ -143,6 +148,8 @@ private FilterModel GetFilterModel(QualificationListPage content)
private static List<BasicQualificationModel> GetBasicQualificationsModels(List<Qualification>? qualifications)
{
var basicQualificationsModels = new List<BasicQualificationModel>();

// ReSharper disable once InvertIf
if (qualifications is not null && qualifications.Count > 0)
{
foreach (var qualification in qualifications)
Expand Down
Loading
Loading