Skip to content

Commit

Permalink
Merge pull request #637 from DFE-Digital/168019-CompletedTask
Browse files Browse the repository at this point in the history
functionality and api tests
  • Loading branch information
jack-nimbleapproach authored Jun 28, 2024
2 parents 6483225 + 5c04f90 commit 3a88019
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public class ProjectByTaskSummaryResponse
public TaskSummaryResponse CommissionedExternalExpert { get; set; }

public TaskSummaryResponse MovingToOpen { get; set; }

public int TaskCount { get; set; }

public int CompletedTasks { get; set; }
}

public class TaskSummaryResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using System.Net;
using System.Net.Http.Json;
using System.Threading.Tasks;
using Dfe.ManageFreeSchoolProjects.API.Contracts.Task;
using DocumentFormat.OpenXml.Spreadsheet;
using Kpi = Dfe.ManageFreeSchoolProjects.Data.Entities.Existing.Kpi;

namespace Dfe.ManageFreeSchoolProjects.API.Tests.Integration
{
Expand All @@ -31,7 +34,7 @@ public async Task GetProjectTaskList_Returns_200()

var taskListResponse = await _client.GetAsync($"/api/v1/client/projects/{project.ProjectStatusProjectId}/tasks/summary");
taskListResponse.StatusCode.Should().Be(HttpStatusCode.OK);

var content = await taskListResponse.Content.ReadFromJsonAsync<ApiSingleResponseV2<ProjectByTaskSummaryResponse>>();

var result = content.Data;
Expand Down Expand Up @@ -98,12 +101,84 @@ public async Task GetProjectTaskList_ApplicationsEvidence_HiddenWithSchoolType_R
result.ApplicationsEvidence.Status.Should().Be(ProjectTaskStatus.NotStarted);
result.ApplicationsEvidence.IsHidden.Should().BeTrue();
}

[Fact]
public async Task GetProjectTaskList_HiddenApplicationsEvidence_ReturnsLowerTaskCount()
{
var project = await setUpProject(false);

var taskListResponse = await _client.GetAsync($"/api/v1/client/projects/{project.ProjectStatusProjectId}/tasks/summary");
taskListResponse.StatusCode.Should().Be(HttpStatusCode.OK);

var content = await taskListResponse.Content.ReadFromJsonAsync<ApiSingleResponseV2<ProjectByTaskSummaryResponse>>();

var result = content.Data;

var taskCount = result.TaskCount;

var projectWithAp = await setUpProject(true);

var taskListResponseWithAp = await _client.GetAsync($"/api/v1/client/projects/{projectWithAp.ProjectStatusProjectId}/tasks/summary");
taskListResponseWithAp.StatusCode.Should().Be(HttpStatusCode.OK);

var contentWithAp = await taskListResponseWithAp.Content.ReadFromJsonAsync<ApiSingleResponseV2<ProjectByTaskSummaryResponse>>();

var resultWithAp = contentWithAp.Data;

resultWithAp.TaskCount.Should().Be(taskCount - 1);
}

[InlineData("School", ProjectTaskStatus.Completed)]
[Theory]
public async Task GetProjectTaskList_CompletedTaskCountIsCorrect(string expectedTaskName, ProjectTaskStatus expectedProjectTaskStatus)
{
var project = await setUpProject(false);

var updateTaskStatusRequest = new UpdateTaskStatusRequest
{
ProjectTaskStatus = expectedProjectTaskStatus, TaskName = expectedTaskName
};

var taskUpdateResponse =
await _client.PatchAsync($"/api/v1/{project.ProjectStatusProjectId}/task/status", updateTaskStatusRequest.ConvertToJson());
taskUpdateResponse.StatusCode.Should().Be(HttpStatusCode.OK);

var taskStatusResponse =
await _client.GetAsync($"/api/v1/{project.ProjectStatusProjectId}/task/status?taskName={expectedTaskName}");
taskStatusResponse.StatusCode.Should().Be(HttpStatusCode.OK);

var taskListResponse = await _client.GetAsync($"/api/v1/client/projects/{project.ProjectStatusProjectId}/tasks/summary");

var content = await taskListResponse.Content.ReadFromJsonAsync<ApiSingleResponseV2<ProjectByTaskSummaryResponse>>();

var result = content.Data;
result.CompletedTasks.Should().Be(1);
}


[Fact]
public async Task Get_ProjectTaskList_ProjectDoesNotExist_Returns_404()
{
var taskListResponse = await _client.GetAsync($"/api/v1/client/projects/NotExist/tasks/summary");
taskListResponse.StatusCode.Should().Be(HttpStatusCode.NotFound);
}

public async Task<Kpi> setUpProject(bool schoolIsAp)
{
using var context = _testFixture.GetContext();

var project = DatabaseModelBuilder.BuildProject();

if (schoolIsAp)
{
project.SchoolDetailsSchoolTypeMainstreamApEtc = "FS - AP";
}

context.Kpi.Add(project);

await context.SaveChangesAsync();

return project;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class GetAllTasksStatusService : IGetTasksService
{
private readonly MfspContext _context;

private static int _hiddenCompletedTasks;
private static int _tasksCount;

public GetAllTasksStatusService(MfspContext context)
{
_context = context;
Expand Down Expand Up @@ -49,6 +52,8 @@ public async Task<ProjectByTaskSummaryResponse> Execute(string projectId)

private static ProjectByTaskSummaryResponse BuildProjectByTaskSummaryResponse(Kpi dbKpi, IEnumerable<TaskSummaryResponse> projectTasks)
{
_tasksCount = 0;
_hiddenCompletedTasks = 0;
var result = new ProjectByTaskSummaryResponse
{
SchoolName = dbKpi.ProjectStatusCurrentFreeSchoolName,
Expand Down Expand Up @@ -80,13 +85,21 @@ private static ProjectByTaskSummaryResponse BuildProjectByTaskSummaryResponse(Kp
};

var applicationsEvidenceTask = SafeRetrieveTaskSummary(projectTasks, TaskName.ApplicationsEvidence.ToString());

result.ApplicationsEvidence = BuildApplicationsEvidenceTask(applicationsEvidenceTask, dbKpi);


result.TaskCount = _tasksCount;

RemoveHiddenCompletedTaskStatus(result.ApplicationsEvidence);

result.CompletedTasks = projectTasks.Count(x => x.Status == ProjectTaskStatus.Completed) - _hiddenCompletedTasks;

return result;
}

private static TaskSummaryResponse SafeRetrieveTaskSummary(IEnumerable<TaskSummaryResponse> projectTasks, string taskName)
{
_tasksCount++;
return projectTasks.FirstOrDefault(x => x.Name == taskName, new TaskSummaryResponse { Name = taskName, Status = ProjectTaskStatus.NotStarted });
}

Expand All @@ -97,10 +110,20 @@ private static TaskSummaryResponse BuildApplicationsEvidenceTask(TaskSummaryResp
SchoolType = ProjectMapper.ToSchoolType(kpi.SchoolDetailsSchoolTypeMainstreamApEtc),
TaskSummary = taskSummaryResponse
};

var result = new ApplicationsEvidenceTaskSummaryBuilder().Build(parameters);


_tasksCount -= taskSummaryResponse.IsHidden ? 1 : 0;

return result;
}

private static void RemoveHiddenCompletedTaskStatus(TaskSummaryResponse taskSummaryResponse)
{
if (taskSummaryResponse.IsHidden && taskSummaryResponse.Status == ProjectTaskStatus.Completed)
{
_hiddenCompletedTasks++;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@

<h2 class="govuk-heading-m govuk-!-margin-bottom-2">Project progress</h2>

<p class="govuk-body govuk-!-margin-bottom-7">You have completed 0 of 22 sections.</p>
<p class="govuk-body govuk-!-margin-bottom-7">You have completed @Model.ProjectTaskListSummary.CompletedTasks of @Model.ProjectTaskListSummary.TaskCount sections.</p>
<ol class="app-task-list">
<li>
<h2 class="app-task-list__section">
Expand Down

0 comments on commit 3a88019

Please sign in to comment.