Skip to content

Commit

Permalink
Merge pull request #865 from DFE-Digital/chore/235157/remove-contentf…
Browse files Browse the repository at this point in the history
…ul-sql-dependencies

chore: Remove all Contentful schema dependencies
  • Loading branch information
katie-gardner authored Nov 6, 2024
2 parents b33d427 + 12874d3 commit b57ffa8
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface IPlanTechDbContext

Task<int> CallStoredProcedureWithReturnInt(string sprocName, IEnumerable<object> parameters, CancellationToken cancellationToken = default);

IQueryable<SectionStatusDto> GetSectionStatuses(string categoryId, int establishmentId);
IQueryable<SectionStatusDto> GetSectionStatuses(string sectionIds, int establishmentId);

Task<User?> GetUserBy(Expression<Func<User, bool>> predicate);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Dfe.PlanTech.Application.Persistence.Interfaces;
using Dfe.PlanTech.Domain.Questionnaire.Interfaces;
using Dfe.PlanTech.Domain.Questionnaire.Models;
using Dfe.PlanTech.Domain.Submissions.Enums;
using Dfe.PlanTech.Domain.Submissions.Interfaces;
using Dfe.PlanTech.Domain.Submissions.Models;
Expand All @@ -10,11 +11,13 @@ namespace Dfe.PlanTech.Application.Submissions.Queries;
public class GetSubmissionStatusesQuery(IPlanTechDbContext db, IUser userHelper) : IGetSubmissionStatusesQuery
{

public async Task<List<SectionStatusDto>> GetSectionSubmissionStatuses(string categoryId)
public async Task<List<SectionStatusDto>> GetSectionSubmissionStatuses(IEnumerable<Section> sections)
{
int establishmentId = await userHelper.GetEstablishmentId();

return await db.ToListAsync(db.GetSectionStatuses(categoryId, establishmentId));
var sectionIds = String.Join(',', sections.Select(section => section.Sys.Id));

return await db.ToListAsync(db.GetSectionStatuses(sectionIds, establishmentId));
}

public async Task<SectionStatus> GetSectionSubmissionStatusAsync(int establishmentId,
Expand All @@ -39,8 +42,8 @@ public async Task<SectionStatus> GetSectionSubmissionStatusAsync(int establishme
}

/// <summary>
/// For each submission, convert to SectionStatus,
/// group by Section,
/// For each submission, convert to SectionStatus,
/// group by Section,
/// then return latest for each grouping
/// optionally choosing from completed submissions first
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
ALTER PROCEDURE dbo.GetSectionStatuses @sectionIds NVARCHAR(MAX), @establishmentId INT
AS

SELECT
Value sectionId
INTO #SectionIds
FROM STRING_SPLIT(@sectionIds, ',')

SELECT
CurrentSubmission.sectionId,
CurrentSubmission.completed,
LastCompleteSubmission.maturity AS lastMaturity,
CurrentSubmission.dateCreated,
CurrentSubmission.dateLastUpdated AS dateUpdated
FROM #SectionIds SI
-- The current submission
CROSS APPLY (
SELECT TOP 1
sectionId,
completed,
S.id,
dateCreated,
dateLastUpdated
FROM [dbo].submission S
WHERE
SI.sectionId = S.sectionId
AND S.establishmentId = @establishmentId
AND S.deleted = 0
ORDER BY S.dateCreated DESC
) CurrentSubmission
-- Use maturity from most recent complete submission (if there is one) so that user always sees recommendation
OUTER APPLY (
SELECT TOP 1
maturity
FROM [dbo].submission S
WHERE
SI.sectionId = S.sectionId
AND S.establishmentId = @establishmentId
AND S.deleted = 0
AND s.completed = 1
ORDER BY S.dateCreated DESC
) LastCompleteSubmission

DROP TABLE #SectionIds

GO
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Dfe.PlanTech.Domain.Questionnaire.Interfaces;
using Dfe.PlanTech.Domain.Questionnaire.Models;
using Dfe.PlanTech.Domain.Submissions.Models;

namespace Dfe.PlanTech.Domain.Submissions.Interfaces;

public interface IGetSubmissionStatusesQuery
{
Task<List<SectionStatusDto>> GetSectionSubmissionStatuses(string categoryId);
Task<List<SectionStatusDto>> GetSectionSubmissionStatuses(IEnumerable<Section> sections);

Task<SectionStatus> GetSectionSubmissionStatusAsync(int establishmentId,
ISectionComponent section,
Expand Down
2 changes: 1 addition & 1 deletion src/Dfe.PlanTech.Infrastructure.Data/PlanTechDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
});
}

public IQueryable<SectionStatusDto> GetSectionStatuses(string categoryId, int establishmentId) => SectionStatusesSp.FromSqlInterpolated($"{DatabaseConstants.GetSectionStatuses} {categoryId} , {establishmentId}");
public IQueryable<SectionStatusDto> GetSectionStatuses(string sectionIds, int establishmentId) => SectionStatusesSp.FromSqlInterpolated($"{DatabaseConstants.GetSectionStatuses} {sectionIds} , {establishmentId}");

public void AddUser(User user) => Users.Add(user);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async Task<Category> RetrieveSectionStatuses(Category category)
{
try
{
category.SectionStatuses = await _query.GetSectionSubmissionStatuses(category.Sys.Id);
category.SectionStatuses = await _query.GetSectionSubmissionStatuses(category.Sections);
category.Completed = category.SectionStatuses.Count(x => x.Completed);
category.RetrievalError = false;
return category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class GetSubmissionStatusesQueryTests
private const int establishmentId = 1;
private const string maturity = "High";

private readonly static Section completeSection = new()
private static readonly Section completeSection = new()
{
Sys = new SystemDetails()
{
Expand All @@ -34,7 +34,7 @@ public class GetSubmissionStatusesQueryTests
Name = "section one"
};

private readonly static Section inprogressSection = new()
private static readonly Section inprogressSection = new()
{
Sys = new SystemDetails()
{
Expand All @@ -43,7 +43,7 @@ public class GetSubmissionStatusesQueryTests
Name = "section two"
};

private readonly static Section notstartedSection = new()
private static readonly Section notstartedSection = new()
{
Sys = new SystemDetails()
{
Expand Down Expand Up @@ -106,18 +106,15 @@ public class GetSubmissionStatusesQueryTests
}
};


private GetSubmissionStatusesQuery CreateStrut() => new GetSubmissionStatusesQuery(Db, user);

public GetSubmissionStatusesQueryTests()
{
Db.GetSectionStatuses(Arg.Any<string>(), Arg.Any<int>())
.Returns((callinfo) =>
{
var categoryId = callinfo.ArgAt<string>(0);
var category = categories.FirstOrDefault(category => category.Sys.Id == categoryId);
Assert.NotNull(category);
return SectionStatuses.Where(sectionStatus => category.Sections.Select(section => section.Sys.Id).Any(id => id == sectionStatus.SectionId)).AsQueryable();
var sectionIds = callinfo.ArgAt<string>(0).Split(",");
return SectionStatuses.Where(sectionStatus => sectionIds.Contains(sectionStatus.SectionId)).AsQueryable();
});


Expand Down Expand Up @@ -146,7 +143,7 @@ public async Task GetSectionSubmissionStatuses_ReturnsListOfStatuses()
{
var category = categories[0];
var sections = category.Sections;
var result = await CreateStrut().GetSectionSubmissionStatuses(category.Sys.Id);
var result = await CreateStrut().GetSectionSubmissionStatuses(category.Sections);

Assert.Equal(result.Count, sections.Count);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public async Task Returns_CategorySectionInfo_If_Slug_Exists_And_SectionIsComple
DateUpdated = DateTime.Parse(utcTime),
});

_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sys.Id).Returns([.. _category.SectionStatuses]);
_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sections).Returns([.. _category.SectionStatuses]);

var result = await _categorySectionViewComponent.InvokeAsync(_category) as ViewViewComponentResult;

Expand Down Expand Up @@ -271,7 +271,7 @@ public async Task Returns_CategorySelectionInfo_If_Slug_Exists_And_SectionIsNotC
DateUpdated = DateTime.Parse(utcTime)
});

_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sys.Id).Returns([.. _category.SectionStatuses]);
_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sections).Returns([.. _category.SectionStatuses]);

var result = await _categorySectionViewComponent.InvokeAsync(_category) as ViewViewComponentResult;

Expand Down Expand Up @@ -310,7 +310,7 @@ public async Task Returns_CategorySelectionInfo_If_Section_IsNotStarted()
{
_category.Completed = 0;

_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sys.Id).Returns([.. _category.SectionStatuses]);
_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sections).Returns([.. _category.SectionStatuses]);

var result = await _categorySectionViewComponent.InvokeAsync(_category) as ViewViewComponentResult;

Expand Down Expand Up @@ -363,7 +363,7 @@ public async Task Returns_NullSlug_And_ErrorMessage_In_CategorySectionInfo_If_Sl
Completed = true
});

_ = _getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sys.Id).Returns([.. _category.SectionStatuses]);
_ = _getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sections).Returns([.. _category.SectionStatuses]);

var result = await _categorySectionViewComponent.InvokeAsync(_category) as ViewViewComponentResult;

Expand Down Expand Up @@ -401,7 +401,7 @@ public async Task Returns_NullSlug_And_ErrorMessage_In_CategorySectionInfo_If_Sl
[Fact]
public async Task Returns_ProgressRetrievalError_When_ProgressCanNotBeRetrieved()
{
_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(Arg.Any<string>())
_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(Arg.Any<IEnumerable<Section>>())
.ThrowsAsync(new Exception("Error occurred fection sections"));

var result = await _categorySectionViewComponent.InvokeAsync(_category) as ViewViewComponentResult;
Expand Down Expand Up @@ -447,7 +447,7 @@ public async Task Returns_NoSectionsErrorRedirectUrl_If_SectionsAreEmpty()
}
};

_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sys.Id).Returns([.. _category.SectionStatuses]);
_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sections).Returns([.. _category.SectionStatuses]);

var result = await _categorySectionViewComponent.InvokeAsync(_category) as ViewViewComponentResult;

Expand Down Expand Up @@ -479,7 +479,7 @@ public async Task Returns_RecommendationInfo_If_It_Exists_ForMaturity()
});

_getSubTopicRecommendationQuery.GetSubTopicRecommendation(Arg.Any<string>()).Returns(_subtopic);
_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sys.Id).Returns(_category.SectionStatuses.ToList());
_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sections).Returns(_category.SectionStatuses.ToList());

var result = await _categorySectionViewComponent.InvokeAsync(_category) as ViewViewComponentResult;

Expand Down Expand Up @@ -509,7 +509,7 @@ public async Task Returns_RecommendationInfo_And_Logs_Error_If_Exception_Thrown_
});

_getSubTopicRecommendationQuery.GetSubTopicRecommendation(Arg.Any<string>()).Returns(_subtopic);
_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sys.Id).Throws(new Exception("test"));
_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sections).Throws(new Exception("test"));

var result = await _categorySectionViewComponent.InvokeAsync(_category) as ViewViewComponentResult;

Expand Down Expand Up @@ -541,7 +541,7 @@ public async Task Returns_NullRecommendationInfo_If_No_RecommendationPage_Exists
LastMaturity = "Low",
});

_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sys.Id).Returns(_category.SectionStatuses.ToList());
_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sections).Returns(_category.SectionStatuses.ToList());

var result = await _categorySectionViewComponent.InvokeAsync(_category) as ViewViewComponentResult;

Expand Down Expand Up @@ -572,7 +572,7 @@ public async Task DoesNotReturn_RecommendationInfo_If_Section_Has_Never_Been_Com
LastMaturity = null
});

_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sys.Id).Returns(_category.SectionStatuses.ToList());
_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sections).Returns(_category.SectionStatuses.ToList());

var result = await _categorySectionViewComponent.InvokeAsync(_category) as ViewViewComponentResult;

Expand Down Expand Up @@ -602,7 +602,7 @@ public async Task Returns_RecommendationInfo_If_Incomplete_Section_Is_Previously
});

_getSubTopicRecommendationQuery.GetSubTopicRecommendation(Arg.Any<string>()).Returns(_subtopic);
_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sys.Id).Returns(_category.SectionStatuses.ToList());
_getSubmissionStatusesQuery.GetSectionSubmissionStatuses(_category.Sections).Returns(_category.SectionStatuses.ToList());

var result = await _categorySectionViewComponent.InvokeAsync(_category) as ViewViewComponentResult;

Expand Down

0 comments on commit b57ffa8

Please sign in to comment.