diff --git a/Dfe.Academies.Academisation.Data/Repositories/ConversionProjectRepository.cs b/Dfe.Academies.Academisation.Data/Repositories/ConversionProjectRepository.cs index 807406d4a..ef02d6188 100644 --- a/Dfe.Academies.Academisation.Data/Repositories/ConversionProjectRepository.cs +++ b/Dfe.Academies.Academisation.Data/Repositories/ConversionProjectRepository.cs @@ -333,5 +333,12 @@ public async Task> GetProjectsByProjectGroupIdsAsync(IEnum .Where(p => projectGroupIds.Contains(p.ProjectGroupId.GetValueOrDefault())) .ToListAsync(cancellationToken); } + + public async Task> GetProjectsByIdsAsync(IEnumerable projectIds, CancellationToken cancellationToken) + { + return await dbSet + .Where(p => projectIds.Contains(p.Id)) + .ToListAsync(cancellationToken); + } } } diff --git a/Dfe.Academies.Academisation.Domain/ProjectAggregate/IConversionProjectRepository.cs b/Dfe.Academies.Academisation.Domain/ProjectAggregate/IConversionProjectRepository.cs index 0157fc1af..b7709b6fd 100644 --- a/Dfe.Academies.Academisation.Domain/ProjectAggregate/IConversionProjectRepository.cs +++ b/Dfe.Academies.Academisation.Domain/ProjectAggregate/IConversionProjectRepository.cs @@ -27,4 +27,5 @@ public interface IConversionProjectRepository : IRepository, IGenericRe Task> GetConversionProjectsForNewGroup(string trustReferenceNumber, CancellationToken cancellationToken); Task> GetProjectsByProjectGroupIdsAsync(IEnumerable projectGroupIds, CancellationToken cancellationToken); Task> GetConversionProjectsByProjectGroupIdAsync(int? projectGroupId, CancellationToken cancellationToken = default); + Task> GetProjectsByIdsAsync(IEnumerable projectIds, CancellationToken cancellationToken); } diff --git a/Dfe.Academies.Academisation.IService/Query/IProjectGroupQueryService.cs b/Dfe.Academies.Academisation.IService/Query/IProjectGroupQueryService.cs index 58c9ab4aa..f17c8d092 100644 --- a/Dfe.Academies.Academisation.IService/Query/IProjectGroupQueryService.cs +++ b/Dfe.Academies.Academisation.IService/Query/IProjectGroupQueryService.cs @@ -5,7 +5,7 @@ namespace Dfe.Academies.Academisation.IService.Query { public interface IProjectGroupQueryService { - Task GetProjectGroupById(int id, CancellationToken cancellationToken); + Task GetProjectGroupByIdAsync(int id, CancellationToken cancellationToken); Task?> GetProjectGroupsAsync(IEnumerable? states, string? title, IEnumerable? deliveryOfficers, IEnumerable? regions, IEnumerable? localAuthorities, IEnumerable? advisoryBoardDates, int page, int count, CancellationToken cancellationToken); diff --git a/Dfe.Academies.Academisation.IService/ServiceModels/ProjectGroup/ProjectGroupResponseModel.cs b/Dfe.Academies.Academisation.IService/ServiceModels/ProjectGroup/ProjectGroupResponseModel.cs index c7c99530b..0d01f33ed 100644 --- a/Dfe.Academies.Academisation.IService/ServiceModels/ProjectGroup/ProjectGroupResponseModel.cs +++ b/Dfe.Academies.Academisation.IService/ServiceModels/ProjectGroup/ProjectGroupResponseModel.cs @@ -2,7 +2,7 @@ namespace Dfe.Academies.Academisation.IService.ServiceModels.ProjectGroup { - public class ProjectGroupResponseModel(int id, string referenceNumber, string trustReferenceNumber, string trustName, string trustUkprn, User assignedUser) + public class ProjectGroupResponseModel(int id, string referenceNumber, string trustReferenceNumber, string trustName, string trustUkprn, User assignedUser, List projects) { public int Id { get; init; } = id; public string TrustReferenceNumber { get; init; } = trustReferenceNumber; @@ -13,6 +13,6 @@ public class ProjectGroupResponseModel(int id, string referenceNumber, string tr public string? ReferenceNumber { get; init; } = referenceNumber; public User AssignedUser { get; init; } = assignedUser; - public List projects { get; init; } + public List Projects { get; init; } = projects; } } diff --git a/Dfe.Academies.Academisation.Service.UnitTest/Commands/ProjectGroup/CreateProjectGroupCommandHandlerTests.cs b/Dfe.Academies.Academisation.Service.UnitTest/Commands/ProjectGroup/CreateProjectGroupCommandHandlerTests.cs index 6ea72aeb1..5fc3476b9 100644 --- a/Dfe.Academies.Academisation.Service.UnitTest/Commands/ProjectGroup/CreateProjectGroupCommandHandlerTests.cs +++ b/Dfe.Academies.Academisation.Service.UnitTest/Commands/ProjectGroup/CreateProjectGroupCommandHandlerTests.cs @@ -69,8 +69,8 @@ public async Task Handle_ValidCommandWithoutConversions_PersistsExpectedProjectG var responseModel = Assert.IsType>(result).Payload; Assert.Equal(responseModel.TrustReferenceNumber, request.TrustReferenceNumber); Assert.Equal(responseModel.ReferenceNumber, expectedProjectGroupReference); - Assert.Equal(responseModel.projects.Count(), expectedProjects.Count); - foreach (var conversion in responseModel.projects.Select((Value, Index) => (Value, Index))) + Assert.Equal(responseModel.Projects.Count(), expectedProjects.Count); + foreach (var conversion in responseModel.Projects.Select((Value, Index) => (Value, Index))) { Assert.Equal(conversion.Value.Urn, expectedProjects[conversion.Index].Details.Urn); Assert.Equal(conversion.Value.SchoolName, expectedProjects[conversion.Index].Details.SchoolName); @@ -104,10 +104,10 @@ public async Task Handle_ValidCommandWithConversions_PersistsExpectedProjectGrou // Assert var responseModel = Assert.IsType>(result).Payload; Assert.Equal(responseModel.TrustReferenceNumber, request.TrustReferenceNumber); - Assert.Equal(responseModel.projects.Count(), expectedProjects.Count); - Assert.NotEmpty(responseModel.ReferenceNumber); + Assert.Equal(responseModel.Projects.Count(), expectedProjects.Count); + Assert.NotEmpty(responseModel.ReferenceNumber!); Assert.StartsWith(responseModel.ReferenceNumber, "GRP_00000000"); - foreach (var conversion in responseModel.projects.Select((Value, Index) => (Value, Index))) + foreach (var conversion in responseModel.Projects.Select((Value, Index) => (Value, Index))) { Assert.Equal(conversion.Value.Urn, expectedProjects[conversion.Index].Details.Urn); Assert.Equal(conversion.Value.SchoolName, expectedProjects[conversion.Index].Details.SchoolName); diff --git a/Dfe.Academies.Academisation.Service.UnitTest/Commands/ProjectGroup/SetProjectGroupCommandHandlerTests.cs b/Dfe.Academies.Academisation.Service.UnitTest/Commands/ProjectGroup/SetProjectGroupCommandHandlerTests.cs index c7b57b9cd..acd0ddeaf 100644 --- a/Dfe.Academies.Academisation.Service.UnitTest/Commands/ProjectGroup/SetProjectGroupCommandHandlerTests.cs +++ b/Dfe.Academies.Academisation.Service.UnitTest/Commands/ProjectGroup/SetProjectGroupCommandHandlerTests.cs @@ -75,6 +75,7 @@ public async Task Handle_ValidRequestWithoutConversions_ReturnsSuccess() }; _mockProjectGroupRepository.Setup(x => x.Update(It.IsAny())); _mockProjectGroupRepository.Setup(x => x.GetByReferenceNumberAsync(request.GroupReferenceNumber, _cancellationToken)).ReturnsAsync(expectedProjectGroup); + _mockConversionProjectRepository.Setup(x => x.GetProjectsByIdsAsync(request.ConversionProjectIds, _cancellationToken)).ReturnsAsync([]); _mockConversionProjectRepository.Setup(x => x.GetConversionProjectsByProjectGroupIdAsync(expectedProjectGroup.Id, _cancellationToken)).ReturnsAsync([]); // Act @@ -84,6 +85,7 @@ public async Task Handle_ValidRequestWithoutConversions_ReturnsSuccess() // Assert var commandSuccessResult = Assert.IsType(result); + _mockConversionProjectRepository.Verify(x => x.GetProjectsByIdsAsync(request.ConversionProjectIds, _cancellationToken), Times.Once); _mockConversionProjectRepository.Verify(x => x.GetConversionProjectsByProjectGroupIdAsync(expectedProjectGroup.Id, _cancellationToken), Times.Once); _mockProjectGroupRepository.Verify(x => x.Update(It.IsAny()), Times.Never); _mockProjectGroupRepository.Verify(x => x.GetByReferenceNumberAsync(request.GroupReferenceNumber, _cancellationToken), Times.Once); @@ -105,7 +107,9 @@ public async Task Handle_ValidRequestWithNoRemovedConversions_ReturnsSuccess() }; _mockProjectGroupRepository.Setup(x => x.Update(It.IsAny())); _mockProjectGroupRepository.Setup(x => x.GetByReferenceNumberAsync(request.GroupReferenceNumber, _cancellationToken)).ReturnsAsync(expectedProjectGroup); - _mockConversionProjectRepository.Setup(x => x.GetConversionProjectsByProjectGroupIdAsync(expectedProjectGroup.Id, _cancellationToken)).ReturnsAsync(expectedProjects); + _mockConversionProjectRepository.Setup(x => x.GetProjectsByIdsAsync(request.ConversionProjectIds, _cancellationToken)).ReturnsAsync(expectedProjects); + _mockConversionProjectRepository.Setup(x => x.GetConversionProjectsByProjectGroupIdAsync(expectedProjectGroup.Id, _cancellationToken)).ReturnsAsync([]); + _mockConversionProjectRepository.Setup(x => x.Update(It.IsAny())); // Act @@ -115,6 +119,7 @@ public async Task Handle_ValidRequestWithNoRemovedConversions_ReturnsSuccess() // Assert var commandSuccessResult = Assert.IsType(result); + _mockConversionProjectRepository.Verify(x => x.GetProjectsByIdsAsync(request.ConversionProjectIds, _cancellationToken), Times.Once); _mockConversionProjectRepository.Verify(x => x.GetConversionProjectsByProjectGroupIdAsync(expectedProjectGroup.Id, _cancellationToken), Times.Once); _mockConversionProjectRepository.Verify(x => x.UnitOfWork.SaveChangesAsync(_cancellationToken), Times.Once); } @@ -134,6 +139,7 @@ public async Task Handle_ValidRequestWithOneRemovedConversions_ReturnsSuccess() }; _mockProjectGroupRepository.Setup(x => x.Update(It.IsAny())); _mockProjectGroupRepository.Setup(x => x.GetByReferenceNumberAsync(request.GroupReferenceNumber, _cancellationToken)).ReturnsAsync(expectedProjectGroup); + _mockConversionProjectRepository.Setup(x => x.GetProjectsByIdsAsync(request.ConversionProjectIds, _cancellationToken)).ReturnsAsync([]); _mockConversionProjectRepository.Setup(x => x.GetConversionProjectsByProjectGroupIdAsync(expectedProjectGroup.Id, _cancellationToken)).ReturnsAsync(expectedProjects); _mockConversionProjectRepository.Setup(x => x.Update(It.IsAny())); @@ -144,7 +150,8 @@ public async Task Handle_ValidRequestWithOneRemovedConversions_ReturnsSuccess() // Assert var commandSuccessResult = Assert.IsType(result); - _mockConversionProjectRepository.Verify(x => x.GetConversionProjectsByProjectGroupIdAsync(expectedProjectGroup.Id, _cancellationToken), Times.Once); + _mockConversionProjectRepository.Verify(x => x.GetProjectsByIdsAsync(request.ConversionProjectIds, _cancellationToken), Times.Once); + _mockConversionProjectRepository.Verify(x => x.GetConversionProjectsByProjectGroupIdAsync(expectedProjectGroup.Id, _cancellationToken), Times.Once); _mockConversionProjectRepository.Verify(x => x.Update(It.IsAny()), Times.Exactly(expectedProjects.Count)); _mockConversionProjectRepository.Verify(x => x.UnitOfWork.SaveChangesAsync(_cancellationToken), Times.Once); } diff --git a/Dfe.Academies.Academisation.Service.UnitTest/Queries/ProjectGroupQueryServiceTests.cs b/Dfe.Academies.Academisation.Service.UnitTest/Queries/ProjectGroupQueryServiceTests.cs index e704036f3..dec759869 100644 --- a/Dfe.Academies.Academisation.Service.UnitTest/Queries/ProjectGroupQueryServiceTests.cs +++ b/Dfe.Academies.Academisation.Service.UnitTest/Queries/ProjectGroupQueryServiceTests.cs @@ -3,7 +3,6 @@ using Dfe.Academies.Academisation.Domain.ProjectGroupsAggregate; using Dfe.Academies.Academisation.IService.ServiceModels.ProjectGroup; using Dfe.Academies.Academisation.Service.Commands.ProjectGroup.QueryService; -using Microsoft.Extensions.Logging; using Moq; using Xunit; using Dfe.Academies.Academisation.IService.ServiceModels.Legacy.ProjectAggregate; @@ -14,7 +13,6 @@ public class ProjectGroupQueryServiceTests { private Mock _mockProjectGroupRepository; private Mock _mockConversionProjectRepository; - private Mock> _mockLogger; private ProjectGroupQueryService _projectGroupQueryService; private CancellationToken _cancellationToken; private readonly Fixture _fixture; @@ -23,12 +21,10 @@ public ProjectGroupQueryServiceTests() { _mockProjectGroupRepository = new Mock(); _mockConversionProjectRepository = new Mock(); - _mockLogger = new Mock>(); _fixture = new(); _projectGroupQueryService = new ProjectGroupQueryService( _mockProjectGroupRepository.Object, - _mockConversionProjectRepository.Object, - _mockLogger.Object + _mockConversionProjectRepository.Object ); _cancellationToken = CancellationToken.None; } @@ -42,7 +38,7 @@ public async Task GetProjectGroupsAsync_WithReferenceNumber_ShouldReturnResultAs var expectedProject = _fixture.Create(); expectedProject.SetProjectGroupId(expectedProjectGroup.Id); var emptyList = new List(); - _mockProjectGroupRepository.Setup(x => x.SearchProjectGroups(searchModel.ReferenceNumber, _cancellationToken)) + _mockProjectGroupRepository.Setup(x => x.SearchProjectGroups(searchModel.ReferenceNumber!, _cancellationToken)) .ReturnsAsync([expectedProjectGroup]); _mockConversionProjectRepository.Setup(x => x.GetProjectsByProjectGroupIdsAsync(new List { expectedProjectGroup.Id }, _cancellationToken)).ReturnsAsync([expectedProject]); @@ -58,7 +54,7 @@ public async Task GetProjectGroupsAsync_WithReferenceNumber_ShouldReturnResultAs Assert.Equal(data.ReferenceNumber, expectedProjectGroup.ReferenceNumber); Assert.Equal(data.TrustReferenceNumber, expectedProjectGroup.TrustReference); } - _mockProjectGroupRepository.Verify(x => x.SearchProjectGroups(searchModel.ReferenceNumber, _cancellationToken), Times.Once()); + _mockProjectGroupRepository.Verify(x => x.SearchProjectGroups(searchModel.ReferenceNumber!, _cancellationToken), Times.Once()); _mockConversionProjectRepository.Verify(x => x.GetProjectsByProjectGroupIdsAsync(new List { expectedProjectGroup.Id }, _cancellationToken), Times.Once()); } @@ -71,7 +67,7 @@ public async Task GetProjectGroupsAsync_WithTrustReference_ShouldReturnResult() var expectedProjectGroup = _fixture.Create(); var expectedProject = _fixture.Create(); expectedProject.SetProjectGroupId(expectedProjectGroup.Id); - _mockProjectGroupRepository.Setup(x => x.SearchProjectGroups(searchModel.TrustReference, _cancellationToken)) + _mockProjectGroupRepository.Setup(x => x.SearchProjectGroups(searchModel.TrustReference!, _cancellationToken)) .ReturnsAsync([expectedProjectGroup]); _mockConversionProjectRepository.Setup(x => x.GetProjectsByProjectGroupIdsAsync(new List { expectedProjectGroup.Id }, _cancellationToken)).ReturnsAsync([expectedProject]); var emptyList = new List(); @@ -87,7 +83,7 @@ public async Task GetProjectGroupsAsync_WithTrustReference_ShouldReturnResult() Assert.Equal(data.ReferenceNumber, expectedProjectGroup.ReferenceNumber); Assert.Equal(data.TrustReferenceNumber, expectedProjectGroup.TrustReference); } - _mockProjectGroupRepository.Verify(x => x.SearchProjectGroups(searchModel.TrustReference, _cancellationToken), Times.Once()); + _mockProjectGroupRepository.Verify(x => x.SearchProjectGroups(searchModel.TrustReference!, _cancellationToken), Times.Once()); _mockConversionProjectRepository.Verify(x => x.GetProjectsByProjectGroupIdsAsync(new List { expectedProjectGroup.Id }, _cancellationToken), Times.Once()); } @@ -100,7 +96,7 @@ public async Task GetProjectGroupsAsync_WithTitle_ShouldReturnResult() var expectedProjectGroup = _fixture.Create(); var expectedProject = _fixture.Create(); expectedProject.SetProjectGroupId(expectedProjectGroup.Id); - _mockProjectGroupRepository.Setup(x => x.SearchProjectGroups(searchModel.Title, _cancellationToken)) + _mockProjectGroupRepository.Setup(x => x.SearchProjectGroups(searchModel.Title!, _cancellationToken)) .ReturnsAsync([expectedProjectGroup]); _mockConversionProjectRepository.Setup(x => x.GetProjectsByProjectGroupIdsAsync(new List { expectedProjectGroup.Id }, _cancellationToken)).ReturnsAsync([expectedProject]); var emptyList = new List(); @@ -116,9 +112,36 @@ public async Task GetProjectGroupsAsync_WithTitle_ShouldReturnResult() Assert.Equal(data.ReferenceNumber, expectedProjectGroup.ReferenceNumber); Assert.Equal(data.TrustReferenceNumber, expectedProjectGroup.TrustReference); } - _mockProjectGroupRepository.Verify(x => x.SearchProjectGroups(searchModel.Title, _cancellationToken), Times.Once()); + _mockProjectGroupRepository.Verify(x => x.SearchProjectGroups(searchModel.Title!, _cancellationToken), Times.Once()); _mockConversionProjectRepository.Verify(x => x.GetProjectsByProjectGroupIdsAsync(new List { expectedProjectGroup.Id }, _cancellationToken), Times.Once()); + } + + [Fact] + public async Task GetProjectGroupByIdAsync_WithId_ShouldReturnResult() + { + // Arrange + var expectedProjectGroup = _fixture.Create(); + var expectedProject = _fixture.Create(); + expectedProject.SetProjectGroupId(expectedProjectGroup.Id); + _mockProjectGroupRepository.Setup(x => x.GetById(expectedProjectGroup.Id)) + .ReturnsAsync(expectedProjectGroup); + _mockConversionProjectRepository.Setup(x => x.GetConversionProjectsByProjectGroupIdAsync(expectedProjectGroup.Id, _cancellationToken)).ReturnsAsync([expectedProject]); + + // Action + var result = await _projectGroupQueryService.GetProjectGroupByIdAsync(expectedProjectGroup.Id, _cancellationToken); + //Assert + var responseModel = Assert.IsType(result); + Assert.Equal(responseModel.TrustName, expectedProjectGroup.TrustName); + Assert.Equal(responseModel.ReferenceNumber, expectedProjectGroup.ReferenceNumber); + Assert.Equal(responseModel.Id, expectedProjectGroup.Id); + foreach (var project in responseModel.Projects) + { + Assert.Equal(project.Id, expectedProject.Id); + Assert.Equal(project.NameOfTrust, expectedProject.Details.NameOfTrust); + } + _mockProjectGroupRepository.Verify(x => x.GetById(expectedProjectGroup.Id), Times.Once()); + _mockConversionProjectRepository.Verify(x => x.GetConversionProjectsByProjectGroupIdAsync(expectedProjectGroup.Id, _cancellationToken), Times.Once()); } } } diff --git a/Dfe.Academies.Academisation.Service/CommandValidations/ProjectGroup/SetProjectGroupAssignUserCommandValidator.cs b/Dfe.Academies.Academisation.Service/CommandValidations/ProjectGroup/SetProjectGroupAssignUserCommandValidator.cs index 88d2ff998..4687ed54e 100644 --- a/Dfe.Academies.Academisation.Service/CommandValidations/ProjectGroup/SetProjectGroupAssignUserCommandValidator.cs +++ b/Dfe.Academies.Academisation.Service/CommandValidations/ProjectGroup/SetProjectGroupAssignUserCommandValidator.cs @@ -7,22 +7,25 @@ public class SetProjectGroupAssignUserCommandValidator : AbstractValidator x.GroupReferenceNumber) .NotEmpty().WithMessage("Must specify a group reference number") - .NotNull().WithMessage("Trust Reference must not be null"); - + .NotNull().WithMessage("Group Reference number must not be null"); + /* RuleFor(x => x.FullName) - .NotEmpty().WithMessage("Full name must not be empty"); + .NotEmpty() + .When(x => x.UserId != null && !string.IsNullOrEmpty(x.EmailAddress)) + .WithMessage("Full name must not be empty"); RuleFor(x => x.UserId) - .NotEmpty().WithMessage("Full name must not be empty"); - - RuleFor(x => x.FullName) - .NotEmpty().WithMessage("Full name must not be empty"); + .NotEmpty() + .When(x => !string.IsNullOrEmpty(x.FullName) && !string.IsNullOrEmpty(x.EmailAddress)) + .WithMessage("Full name must not be empty"); RuleFor(x => x.EmailAddress) - .NotEmpty().WithMessage("Email address must not be empty") - .EmailAddress().WithMessage("Must be a valid email address."); + .NotEmpty() + .When(x => x.UserId != null && !string.IsNullOrEmpty(x.FullName)) + .EmailAddress().WithMessage("Must be a valid email address.");*/ } } } diff --git a/Dfe.Academies.Academisation.Service/Commands/ProjectGroup/CreateProjectGroupCommandHandler.cs b/Dfe.Academies.Academisation.Service/Commands/ProjectGroup/CreateProjectGroupCommandHandler.cs index 0bdc7ba75..d58829ceb 100644 --- a/Dfe.Academies.Academisation.Service/Commands/ProjectGroup/CreateProjectGroupCommandHandler.cs +++ b/Dfe.Academies.Academisation.Service/Commands/ProjectGroup/CreateProjectGroupCommandHandler.cs @@ -50,9 +50,7 @@ public async Task Handle(CreateProjectGroupCommand message, Cancel conversionsProjectModels = conversionProjects.Select(p => p.MapToServiceModel()).ToList(); } - var responseModel = new ProjectGroupResponseModel(projectGroup.Id, projectGroup.ReferenceNumber!, projectGroup.TrustReference, null, null, null) { - projects = conversionsProjectModels - }; + var responseModel = new ProjectGroupResponseModel(projectGroup.Id, projectGroup.ReferenceNumber!, projectGroup.TrustReference, null, null, null, conversionsProjectModels); return new CreateSuccessResult(responseModel); diff --git a/Dfe.Academies.Academisation.Service/Commands/ProjectGroup/SetProjectGroupCommandHandler.cs b/Dfe.Academies.Academisation.Service/Commands/ProjectGroup/SetProjectGroupCommandHandler.cs index 6b34782df..26e5ba875 100644 --- a/Dfe.Academies.Academisation.Service/Commands/ProjectGroup/SetProjectGroupCommandHandler.cs +++ b/Dfe.Academies.Academisation.Service/Commands/ProjectGroup/SetProjectGroupCommandHandler.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.Logging; using Dfe.Academies.Academisation.Domain.ApplicationAggregate; using FluentValidation; +using Dfe.Academies.Academisation.IDomain.ProjectAggregate; namespace Dfe.Academies.Academisation.Service.Commands.ProjectGroup { @@ -20,7 +21,9 @@ public async Task Handle(SetProjectGroupCommand message, Cancella return new NotFoundCommandResult(); } - var conversionProjects = await conversionProjectRepository.GetConversionProjectsByProjectGroupIdAsync(projectGroup.Id, cancellationToken).ConfigureAwait(false); + var projects = await conversionProjectRepository.GetProjectsByIdsAsync(message.ConversionProjectIds, cancellationToken).ConfigureAwait(false); + var groupConversionProjects = await conversionProjectRepository.GetConversionProjectsByProjectGroupIdAsync(projectGroup.Id, cancellationToken).ConfigureAwait(false); + var conversionProjects = GetConversionProjects(projects, groupConversionProjects); if (conversionProjects != null && conversionProjects.Any()) { @@ -45,5 +48,26 @@ public async Task Handle(SetProjectGroupCommand message, Cancella } return new CommandSuccessResult(); } + + private IEnumerable GetConversionProjects(IEnumerable conversionProjects, IEnumerable groupConversionProjects) + { + var projects = new List(); + if (conversionProjects == null && !conversionProjects!.Any() && groupConversionProjects == null && !groupConversionProjects!.Any()) + { + return Enumerable.Empty(); + } + + if (conversionProjects != null && conversionProjects.Any()) + { + projects.AddRange(conversionProjects); + } + + if (groupConversionProjects != null && groupConversionProjects.Any()) + { + projects.AddRange(groupConversionProjects); + } + + return projects; + } } } diff --git a/Dfe.Academies.Academisation.Service/Mappers/Legacy/ProjectAggregate/LegacyProjectServiceModelMapper.cs b/Dfe.Academies.Academisation.Service/Mappers/Legacy/ProjectAggregate/LegacyProjectServiceModelMapper.cs index f2f782812..3d650d032 100644 --- a/Dfe.Academies.Academisation.Service/Mappers/Legacy/ProjectAggregate/LegacyProjectServiceModelMapper.cs +++ b/Dfe.Academies.Academisation.Service/Mappers/Legacy/ProjectAggregate/LegacyProjectServiceModelMapper.cs @@ -164,13 +164,10 @@ internal static FormAMatProjectServiceModel MapToFormAMatServiceModel(this IForm } internal static ProjectGroupResponseModel MapToProjectGroupServiceModel(this IProjectGroup projectGroup, IEnumerable projects) - { - ProjectGroupResponseModel serviceModel = new(projectGroup.Id, projectGroup.ReferenceNumber, projectGroup.TrustReference, projectGroup.TrustName, projectGroup.TrustUkprn, new User(projectGroup.AssignedUser?.Id ?? Guid.Empty, projectGroup.AssignedUser?.FullName ?? string.Empty, projectGroup.AssignedUser?.EmailAddress ?? string.Empty)) - { - projects = projects.Where(x => x.ProjectGroupId == projectGroup.Id).Select(p => p.MapToServiceModel()).ToList() - }; - return serviceModel; - } + => new (projectGroup.Id, projectGroup.ReferenceNumber!, projectGroup.TrustReference, + projectGroup.TrustName, projectGroup.TrustUkprn, new User(projectGroup.AssignedUser?.Id ?? Guid.Empty, + projectGroup.AssignedUser?.FullName ?? string.Empty, projectGroup.AssignedUser?.EmailAddress ?? string.Empty), + projects.Where(x => x.ProjectGroupId == projectGroup.Id).Select(p => p.MapToServiceModel()).ToList()); private static IEnumerable ToProjectNoteServiceModels(this IEnumerable? notes) { diff --git a/Dfe.Academies.Academisation.Service/Queries/ProjectGroupQueryService.cs b/Dfe.Academies.Academisation.Service/Queries/ProjectGroupQueryService.cs index 8f496aac4..65be6d183 100644 --- a/Dfe.Academies.Academisation.Service/Queries/ProjectGroupQueryService.cs +++ b/Dfe.Academies.Academisation.Service/Queries/ProjectGroupQueryService.cs @@ -13,9 +13,9 @@ namespace Dfe.Academies.Academisation.Service.Commands.ProjectGroup.QueryService { - public class ProjectGroupQueryService(IProjectGroupRepository projectGroupRepository, IConversionProjectRepository conversionProjectRepository, ILogger logger) : IProjectGroupQueryService + public class ProjectGroupQueryService(IProjectGroupRepository projectGroupRepository, IConversionProjectRepository conversionProjectRepository) : IProjectGroupQueryService { - public async Task GetProjectGroupById(int id, CancellationToken cancellationToken) + public async Task GetProjectGroupByIdAsync(int id, CancellationToken cancellationToken) { var projectGroup = await projectGroupRepository.GetById(id); var relatedProjects = await conversionProjectRepository.GetConversionProjectsByProjectGroupIdAsync(projectGroup.Id, cancellationToken).ConfigureAwait(false); diff --git a/Dfe.Academies.Academisation.SubcutaneousTest/ProjectAggregate/LegacyProjectUpdateTests.cs b/Dfe.Academies.Academisation.SubcutaneousTest/ProjectAggregate/LegacyProjectUpdateTests.cs index 1e608f884..2ea7bbfb2 100644 --- a/Dfe.Academies.Academisation.SubcutaneousTest/ProjectAggregate/LegacyProjectUpdateTests.cs +++ b/Dfe.Academies.Academisation.SubcutaneousTest/ProjectAggregate/LegacyProjectUpdateTests.cs @@ -45,11 +45,11 @@ public ProjectUpdateTests() _legacyProjectGetQuery = new ConversionProjectQueryService(conversionProjectRepository, formAMatProjectRepository); var services = new ServiceCollection(); - services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblies(Assembly.GetAssembly(typeof(ConversionProjectUpdateCommandHandler)))); + services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblies(Assembly.GetAssembly(typeof(ConversionProjectUpdateCommandHandler))!)); services.AddScoped(x => conversionProjectRepository); services.AddScoped(x => projectUpdateDataCommand); - _mediatr = services.BuildServiceProvider().GetService(); + _mediatr = services.BuildServiceProvider().GetService()!; } diff --git a/Dfe.Academies.Academisation.WebApi.UnitTest/Controller/ProjectGroupControllerTests.cs b/Dfe.Academies.Academisation.WebApi.UnitTest/Controller/ProjectGroupControllerTests.cs index b8ed6d65d..cbd08bc7c 100644 --- a/Dfe.Academies.Academisation.WebApi.UnitTest/Controller/ProjectGroupControllerTests.cs +++ b/Dfe.Academies.Academisation.WebApi.UnitTest/Controller/ProjectGroupControllerTests.cs @@ -43,7 +43,7 @@ public ProjectGroupControllerTests() public async Task CreateProjectGroup_ReturnsOk() { // Arrange - var response = new ProjectGroupResponseModel(1, "12312", _trustReferenceNumber, "trustName", null, null); + var response = new ProjectGroupResponseModel(1, "12312", _trustReferenceNumber, "trustName", null, null, null); var command = new CreateProjectGroupCommand("trustName", _trustReferenceNumber, _trustUkprn, []); _mediatrMock.Setup(x => x.Send(command, _cancellationToken)) .ReturnsAsync(new CreateSuccessResult(response)); diff --git a/Dfe.Academies.Academisation.WebApi/Controllers/ProjectGroupController.cs b/Dfe.Academies.Academisation.WebApi/Controllers/ProjectGroupController.cs index 806c54552..ff0acd2d6 100644 --- a/Dfe.Academies.Academisation.WebApi/Controllers/ProjectGroupController.cs +++ b/Dfe.Academies.Academisation.WebApi/Controllers/ProjectGroupController.cs @@ -91,7 +91,7 @@ await projectGroupQueryService.GetProjectGroupsAsync(searchModel!.StatusQueryStr [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task> GetProjectGrouptById(int id, CancellationToken cancellationToken) { - var project = await projectGroupQueryService.GetProjectGroupById(id, cancellationToken); + var project = await projectGroupQueryService.GetProjectGroupByIdAsync(id, cancellationToken); if (project == null) {