Skip to content

Commit

Permalink
API tweaks for Check (#930)
Browse files Browse the repository at this point in the history
* Only return active ITT records

* Fix ITT query to return records without a provider

* Add a StatusDescription property to v3 Induction responses
  • Loading branch information
gunndabad authored Nov 24, 2023
1 parent 0b8f4d8 commit ffc789d
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 55 deletions.
42 changes: 36 additions & 6 deletions TeachingRecordSystem/src/TeachingRecordSystem.Api/EnumHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,57 @@ namespace TeachingRecordSystem.Api;

public static class EnumHelper
{
public static TResult ConvertToEnum<TSource, TResult>(this TSource input)
public static TResult ConvertToEnumByValue<TSource, TResult>(this TSource input)
where TSource : struct, Enum
where TResult : struct, Enum
{
if (!TryConvertToEnum<TSource, TResult>(input, out var result))
if (!TryConvertToEnumByValue<TSource, TResult>(input, out var result))
{
throw new FormatException($"Unknown {typeof(TSource).Name}: '{Convert.ToInt32(input)}'.");
}

return result;
}

public static TResult ConvertToEnumByName<TSource, TResult>(this TSource input)
where TSource : struct, Enum
where TResult : struct, Enum
{
if (!TryConvertToEnumByName<TSource, TResult>(input, out var result))
{
throw new FormatException($"Unknown {typeof(TSource).Name}: '{input}'.");
}

return result;
}

public static bool TryConvertToEnum<TSource, TResult>(this TSource input, out TResult result)
public static bool TryConvertToEnumByName<TSource, TResult>(this TSource input, out TResult result)
where TSource : struct, Enum
where TResult : struct, Enum
{
var inputAsName = input.ToString();

if (Enum.TryParse(typeof(TResult), inputAsName, out var resultObj))
{
result = (TResult)resultObj;
return true;
}
else
{
result = default;
return false;
}
}

public static bool TryConvertToEnumByValue<TSource, TResult>(this TSource input, out TResult result)
where TSource : struct, Enum
where TResult : struct, Enum
{
var inputAsInt = Convert.ToInt32(input);
var inputAsValue = Convert.ToInt32(input);

if (Enum.IsDefined(typeof(TResult), inputAsInt))
if (Enum.IsDefined(typeof(TResult), inputAsValue))
{
result = (TResult)Enum.ToObject(typeof(TResult), inputAsInt);
result = (TResult)Enum.ToObject(typeof(TResult), inputAsValue);
return true;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Qualification MapQualification(dfeta_qualification qualification)
Subject2Code = subject2?.dfeta_Value,
Subject3Code = subject3?.dfeta_Value,
HeQualificationName = heQualification?.dfeta_name,
ClassDivision = qualification.dfeta_HE_ClassDivision?.ConvertToEnum<dfeta_classdivision, ClassDivision>()
ClassDivision = qualification.dfeta_HE_ClassDivision?.ConvertToEnumByValue<dfeta_classdivision, ClassDivision>()
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public enum ClassDivision
public static class ClassDivisionExtensions
{
public static dfeta_classdivision ConvertToClassDivision(this ClassDivision input) =>
input.ConvertToEnum<ClassDivision, dfeta_classdivision>();
input.ConvertToEnumByValue<ClassDivision, dfeta_classdivision>();

public static bool TryConvertToClassDivision(this ClassDivision input, out dfeta_classdivision result) =>
input.TryConvertToEnum(out result);
input.TryConvertToEnumByValue(out result);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public enum Gender
public static class GenderExtensions
{
public static Contact_GenderCode ConvertToContact_GenderCode(this Gender input) =>
input.ConvertToEnum<Gender, Contact_GenderCode>();
input.ConvertToEnumByValue<Gender, Contact_GenderCode>();

public static bool TryConvertToContact_GenderCode(this Gender input, out Contact_GenderCode result) =>
input.TryConvertToEnum(out result);
input.TryConvertToEnumByValue(out result);
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public enum IttProgrammeType
public static class IttProgrammeTypeExtensions
{
public static dfeta_ITTProgrammeType ConvertToIttProgrammeType(this IttProgrammeType input) =>
input.ConvertToEnum<IttProgrammeType, dfeta_ITTProgrammeType>();
input.ConvertToEnumByValue<IttProgrammeType, dfeta_ITTProgrammeType>();

public static bool TryConvertToIttProgrammeType(this IttProgrammeType input, out dfeta_ITTProgrammeType result) =>
input.TryConvertToEnum(out result);
input.TryConvertToEnumByValue(out result);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public enum IttQualificationAim
public static class IttQualificationAimExtensions
{
public static dfeta_ITTQualificationAim ConvertToIttQualficationAim(this IttQualificationAim input) =>
input.ConvertToEnum<IttQualificationAim, dfeta_ITTQualificationAim>();
input.ConvertToEnumByValue<IttQualificationAim, dfeta_ITTQualificationAim>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ public async Task<TrnRequestInfo> Handle(GetOrCreateTrnRequest request, Cancella
} :
null,
HusId = request.HusId,
TeacherType = EnumHelper.ConvertToEnum<Requests.CreateTeacherType, Core.Dqt.Models.CreateTeacherType>(request.TeacherType),
TeacherType = EnumHelper.ConvertToEnumByValue<Requests.CreateTeacherType, Core.Dqt.Models.CreateTeacherType>(request.TeacherType),
RecognitionRoute = request.RecognitionRoute.HasValue ?
EnumHelper.ConvertToEnum<Requests.CreateTeacherRecognitionRoute, TeachingRecordSystem.Core.Dqt.Models.CreateTeacherRecognitionRoute>(request.RecognitionRoute.Value) :
EnumHelper.ConvertToEnumByValue<Requests.CreateTeacherRecognitionRoute, TeachingRecordSystem.Core.Dqt.Models.CreateTeacherRecognitionRoute>(request.RecognitionRoute.Value) :
null,
QtsDate = request.QtsDate,
InductionRequired = request.InductionRequired,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,25 @@ public async Task<GetTeacherResponse> Handle(GetTeacherRequest request, Cancella
Value = earlyYearsStatus.dfeta_Value
} :
null,
InitialTeacherTraining = itt.Select(i => new GetTeacherResponseInitialTeacherTraining()
InitialTeacherTraining = itt.Select(i =>
{
ProgrammeEndDate = i.dfeta_ProgrammeEndDate.ToDateOnlyWithDqtBstFix(isLocalTime: true),
ProgrammeStartDate = i.dfeta_ProgrammeStartDate.ToDateOnlyWithDqtBstFix(isLocalTime: true),
ProgrammeType = i.dfeta_ProgrammeType?.ConvertToEnum<dfeta_ITTProgrammeType, IttProgrammeType>(),
Result = i.dfeta_Result.HasValue ? i.dfeta_Result.Value.ConvertFromITTResult() : null,
Provider = new()
var provider = i.Extract<Account>("establishment", Account.PrimaryIdAttribute);

return new GetTeacherResponseInitialTeacherTraining()
{
Ukprn = i.Extract<Account>("establishment", Account.PrimaryIdAttribute).dfeta_UKPRN
},
HusId = i.dfeta_TraineeID,
Active = i.StateCode == dfeta_initialteachertrainingState.Active
ProgrammeEndDate = i.dfeta_ProgrammeEndDate.ToDateOnlyWithDqtBstFix(isLocalTime: true),
ProgrammeStartDate = i.dfeta_ProgrammeStartDate.ToDateOnlyWithDqtBstFix(isLocalTime: true),
ProgrammeType = i.dfeta_ProgrammeType?.ConvertToEnumByValue<dfeta_ITTProgrammeType, IttProgrammeType>(),
Result = i.dfeta_Result.HasValue ? i.dfeta_Result.Value.ConvertFromITTResult() : null,
Provider = provider is not null ?
new()
{
Ukprn = provider.dfeta_UKPRN
} :
null,
HusId = i.dfeta_TraineeID,
Active = i.StateCode == dfeta_initialteachertrainingState.Active
};
})
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using TeachingRecordSystem.Core.Dqt.Models;

namespace TeachingRecordSystem.Api.V3.ApiModels;

public enum InductionStatus
Expand All @@ -12,3 +14,23 @@ public enum InductionStatus
PassedinWales = 8,
RequiredtoComplete = 9,
}

public static class InductionStatusExtensions
{
public static InductionStatus ConvertToInductionStatus(this dfeta_InductionStatus input) =>
input.ConvertToEnumByName<dfeta_InductionStatus, InductionStatus>();

public static string GetDescription(this dfeta_InductionStatus input) => input switch
{
dfeta_InductionStatus.Exempt => "Exempt",
dfeta_InductionStatus.Fail => "Fail",
dfeta_InductionStatus.FailedinWales => "Failed in Wales",
dfeta_InductionStatus.InductionExtended => "Extended",
dfeta_InductionStatus.InProgress => "In progress",
dfeta_InductionStatus.NotYetCompleted => "Not yet completed",
dfeta_InductionStatus.Pass => "Pass",
dfeta_InductionStatus.PassedinWales => "Passed in Wales",
dfeta_InductionStatus.RequiredtoComplete => "Required to complete",
_ => throw new ArgumentException($"Unknown {nameof(InductionStatus)}: '{input}'.")
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public enum IttProgrammeType
public static class IttProgrammeTypeExtensions
{
public static dfeta_ITTProgrammeType ConvertToIttProgrammeType(this IttProgrammeType input) =>
input.ConvertToEnum<IttProgrammeType, dfeta_ITTProgrammeType>();
input.ConvertToEnumByValue<IttProgrammeType, dfeta_ITTProgrammeType>();

public static bool TryConvertToIttProgrammeType(this IttProgrammeType input, out dfeta_ITTProgrammeType result) =>
input.TryConvertToEnum(out result);
input.TryConvertToEnumByValue(out result);

public static string GetDescription(this IttProgrammeType input) => input switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public GetTeacherHandler(
dfeta_ittqualification.PrimaryIdAttribute,
dfeta_ittqualification.Fields.dfeta_name
},
false);
activeOnly: true);
}

dfeta_qualification[]? qualifications = default;
Expand Down Expand Up @@ -303,8 +303,8 @@ public GetTeacherHandler(
Option.Some(itt!.Select(i => new GetTeacherResponseInitialTeacherTraining()
{
Qualification = MapIttQualification(i),
ProgrammeType = i.dfeta_ProgrammeType?.ConvertToEnum<dfeta_ITTProgrammeType, IttProgrammeType>(),
ProgrammeTypeDescription = i.dfeta_ProgrammeType?.ConvertToEnum<dfeta_ITTProgrammeType, IttProgrammeType>().GetDescription(),
ProgrammeType = i.dfeta_ProgrammeType?.ConvertToEnumByValue<dfeta_ITTProgrammeType, IttProgrammeType>(),
ProgrammeTypeDescription = i.dfeta_ProgrammeType?.ConvertToEnumByValue<dfeta_ITTProgrammeType, IttProgrammeType>().GetDescription(),
StartDate = i.dfeta_ProgrammeStartDate.ToDateOnlyWithDqtBstFix(isLocalTime: true),
EndDate = i.dfeta_ProgrammeEndDate.ToDateOnlyWithDqtBstFix(isLocalTime: true),
Result = i.dfeta_Result.HasValue ? i.dfeta_Result.Value.ConvertFromITTResult() : null,
Expand Down Expand Up @@ -374,14 +374,15 @@ public GetTeacherHandler(
{
StartDate = induction.dfeta_StartDate.ToDateOnlyWithDqtBstFix(isLocalTime: true),
EndDate = induction.dfeta_CompletionDate.ToDateOnlyWithDqtBstFix(isLocalTime: true),
Status = MapInductionStatus(induction.dfeta_InductionStatus),
Status = induction.dfeta_InductionStatus?.ConvertToInductionStatus(),
StatusDescription = induction.dfeta_InductionStatus?.GetDescription(),
CertificateUrl =
(induction.dfeta_InductionStatus == dfeta_InductionStatus.Pass || induction.dfeta_InductionStatus == dfeta_InductionStatus.PassedinWales) &&
induction.dfeta_CompletionDate is not null &&
accessMode == AccessMode.IdentityAccessToken ?
"/v3/certificates/induction" :
null,
Periods = inductionperiods.Select(p => MapInductionPeriod(p)).ToArray()
Periods = inductionperiods.Select(MapInductionPeriod).ToArray()
} :
null;

Expand All @@ -403,22 +404,6 @@ private static GetTeacherResponseInductionPeriod MapInductionPeriod(dfeta_induct
};
}

private static InductionStatus? MapInductionStatus(dfeta_InductionStatus? inductionStatus) =>
inductionStatus switch
{
dfeta_InductionStatus.Exempt => InductionStatus.Exempt,
dfeta_InductionStatus.Fail => InductionStatus.Fail,
dfeta_InductionStatus.FailedinWales => InductionStatus.FailedinWales,
dfeta_InductionStatus.InductionExtended => InductionStatus.InductionExtended,
dfeta_InductionStatus.InProgress => InductionStatus.InProgress,
dfeta_InductionStatus.NotYetCompleted => InductionStatus.NotYetCompleted,
dfeta_InductionStatus.Pass => InductionStatus.Pass,
dfeta_InductionStatus.PassedinWales => InductionStatus.PassedinWales,
dfeta_InductionStatus.RequiredtoComplete => InductionStatus.RequiredtoComplete,
null => (InductionStatus?)null,
_ => throw new NotImplementedException($"{nameof(InductionStatus)}: {inductionStatus} is not currently supported.")
};

private static GetTeacherResponseInitialTeacherTrainingQualification? MapIttQualification(dfeta_initialteachertraining initialTeacherTraining)
{
var qualification = initialTeacherTraining.Extract<dfeta_ittqualification>("qualification", dfeta_ittqualification.PrimaryIdAttribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public record GetTeacherResponseInduction
public required DateOnly? StartDate { get; init; }
public required DateOnly? EndDate { get; init; }
public required InductionStatus? Status { get; init; }
public required string? StatusDescription { get; init; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public required string? CertificateUrl { get; init; }
public required IEnumerable<GetTeacherResponseInductionPeriod> Periods { get; init; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ public Task<dfeta_initialteachertraining[]> GetInitialTeacherTrainingByTeacher(
bool activeOnly = true) =>
GetInitialTeacherTrainingByTeacher(teacherId, columnNames, establishmentColumnNames, subjectColumnNames, qualificationColumnNames, requestBuilder: null, activeOnly);


public async Task<dfeta_initialteachertraining[]> GetInitialTeacherTrainingBySlugId(string slugId, string[] columnNames, RequestBuilder requestBuilder, bool activeOnly = true)
{
requestBuilder ??= RequestBuilder.CreateSingle(_service);
Expand Down Expand Up @@ -260,7 +259,7 @@ public async Task<dfeta_initialteachertraining[]> GetInitialTeacherTrainingByTea
Account.EntityLogicalName,
dfeta_initialteachertraining.Fields.dfeta_EstablishmentId,
Account.PrimaryIdAttribute,
JoinOperator.Inner);
JoinOperator.LeftOuter);

establishmentLink.Columns = new ColumnSet(establishmentColumnNames);

Expand Down Expand Up @@ -298,7 +297,6 @@ public async Task<dfeta_initialteachertraining[]> GetInitialTeacherTrainingByTea

var result = await requestBuilder.AddRequest<RetrieveMultipleResponse>(request).GetResponseAsync();


return result.EntityCollection.Entities.Select(entity => entity.ToEntity<dfeta_initialteachertraining>()).ToArray();

static void AddSubjectLink(QueryExpression query, string subjectIdField, string alias, string[] columnNames)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ protected async Task ValidRequestWithInduction_ReturnsExpectedInductionContent(
{
startDate = induction.dfeta_StartDate?.ToString("yyyy-MM-dd"),
endDate = induction.dfeta_CompletionDate?.ToString("yyyy-MM-dd"),
status = induction.dfeta_InductionStatus.ToString(),
status = induction.dfeta_InductionStatus?.ToString(),
statusDescription = induction.dfeta_InductionStatus?.GetDescription(),
certificateUrl = "/v3/certificates/induction",
periods = new[]
{
Expand Down Expand Up @@ -199,7 +200,7 @@ protected async Task ValidRequestWithInitialTeacherTraining_ReturnsExpectedIniti
name = itt.GetAttributeValue<AliasedValue>($"qualification.{dfeta_ittqualification.Fields.dfeta_name}").Value
},
programmeType = itt.dfeta_ProgrammeType.ToString(),
programmeTypeDescription = itt.dfeta_ProgrammeType?.ConvertToEnum<dfeta_ITTProgrammeType, IttProgrammeType>().GetDescription(),
programmeTypeDescription = itt.dfeta_ProgrammeType?.ConvertToEnumByValue<dfeta_ITTProgrammeType, IttProgrammeType>().GetDescription(),
startDate = itt.dfeta_ProgrammeStartDate?.ToString("yyyy-MM-dd"),
endDate = itt.dfeta_ProgrammeEndDate?.ToString("yyyy-MM-dd"),
result = itt.dfeta_Result?.ToString(),
Expand Down Expand Up @@ -682,7 +683,7 @@ private async Task ConfigureMocks(
It.IsAny<string[]>(),
It.IsAny<string[]>(),
It.IsAny<string[]>(),
false))
/*activeOnly: */true))
.ReturnsAsync(itt != null ? new[] { itt } : Array.Empty<dfeta_initialteachertraining>());

DataverseAdapterMock
Expand Down

0 comments on commit ffc789d

Please sign in to comment.