diff --git a/DigitalLearningSolutions.Data/DataServices/SupervisorDataService.cs b/DigitalLearningSolutions.Data/DataServices/SupervisorDataService.cs index 6be9d58c08..1ed74ef831 100644 --- a/DigitalLearningSolutions.Data/DataServices/SupervisorDataService.cs +++ b/DigitalLearningSolutions.Data/DataServices/SupervisorDataService.cs @@ -393,13 +393,21 @@ LEFT JOIN UserCentreDetails ucd public IEnumerable GetSupervisorForEnrolDelegate(int CustomisationID, int CentreID) { return connection.Query( - $@"SELECT AdminID, Forename + ' ' + Surname + ' (' + Email +')' AS Name, Email FROM AdminUsers AS au - WHERE (Supervisor = 1) AND (CentreID = @CentreID) AND (CategoryID = 0 OR - CategoryID = (SELECT au.CategoryID FROM Applications AS a INNER JOIN - Customisations AS c ON a.ApplicationID = c.ApplicationID - WHERE (c.CustomisationID = @CustomisationID))) AND (Active = 1) AND (Approved = 1) - GROUP BY AdminID, Surname, Forename, Email, CentreName - ORDER BY Surname, Forename", + $@"SELECT aa.ID AS AdminID, + u.FirstName + ' ' + u.LastName + ' (' + COALESCE(ucd.Email, u.PrimaryEmail) +')' AS Name, + COALESCE(ucd.Email, u.PrimaryEmail) AS Email + FROM AdminAccounts AS aa INNER JOIN + Users AS u ON aa.UserID = u.ID INNER JOIN + Centres AS c ON aa.CentreID = c.CentreID LEFT OUTER JOIN + UserCentreDetails AS ucd ON u.ID = ucd.UserID AND c.CentreID = ucd.CentreID + WHERE (aa.IsSupervisor = 1) AND (c.CentreID = @CentreID) AND + (ISNULL(aa.CategoryID, 0) = 0 OR CategoryID = + (SELECT aa.CategoryID FROM Applications AS a INNER JOIN + Customisations AS c ON a.ApplicationID = c.ApplicationID + WHERE (c.CustomisationID = @CustomisationID))) AND + (aa.Active = 1) + GROUP BY aa.ID, u.LastName, u.FirstName, COALESCE(ucd.Email, u.PrimaryEmail), CentreName + ORDER BY u.FirstName, u.LastName", new { CentreID, CustomisationID }); } diff --git a/DigitalLearningSolutions.Data/DataServices/UserDataService/AdminUserDataService.cs b/DigitalLearningSolutions.Data/DataServices/UserDataService/AdminUserDataService.cs index 0a9ff0b872..687aeec180 100644 --- a/DigitalLearningSolutions.Data/DataServices/UserDataService/AdminUserDataService.cs +++ b/DigitalLearningSolutions.Data/DataServices/UserDataService/AdminUserDataService.cs @@ -225,6 +225,27 @@ public List GetAdminUsersByCentreId(int centreId) return users; } + public List GetAdminUsersAtCentreForCategory(int centreId, int categoryId) + { + var users = connection.Query( + @$"SELECT + aa.ID AS Id, + COALESCE(ucd.Email, u.PrimaryEmail) AS EmailAddress, + u.FirstName, + u.LastName + FROM AdminAccounts AS aa INNER JOIN + Users AS u ON aa.UserID = u.ID INNER JOIN + Centres AS c ON c.CentreID = aa.CentreID LEFT OUTER JOIN + UserCentreDetails AS ucd ON u.ID = ucd.UserID AND c.CentreID = ucd.CentreID LEFT OUTER JOIN + CourseCategories AS cc ON cc.CourseCategoryID = aa.CategoryID + WHERE aa.Active = 1 AND aa.CentreId = @centreId AND aa.IsSupervisor = 1 AND + (aa.CategoryId = @categoryId OR aa.CategoryId IS NULL) + ORDER BY u.FirstName, u.LastName", + new { centreId, categoryId } + ).ToList(); + + return users; + } public int GetNumberOfAdminsAtCentre(int centreId) { diff --git a/DigitalLearningSolutions.Data/DataServices/UserDataService/UserDataService.cs b/DigitalLearningSolutions.Data/DataServices/UserDataService/UserDataService.cs index b3cf74f145..978364ce77 100644 --- a/DigitalLearningSolutions.Data/DataServices/UserDataService/UserDataService.cs +++ b/DigitalLearningSolutions.Data/DataServices/UserDataService/UserDataService.cs @@ -25,6 +25,7 @@ public interface IUserDataService AdminUser? GetAdminUserById(int id); List GetAdminUsersByCentreId(int centreId); + List GetAdminUsersAtCentreForCategory(int centreId, int categoryId); AdminUser? GetAdminUserByEmailAddress(string emailAddress); diff --git a/DigitalLearningSolutions.Web.Tests/Services/UserServiceTests.cs b/DigitalLearningSolutions.Web.Tests/Services/UserServiceTests.cs index 0d822251ce..a3270f664e 100644 --- a/DigitalLearningSolutions.Web.Tests/Services/UserServiceTests.cs +++ b/DigitalLearningSolutions.Web.Tests/Services/UserServiceTests.cs @@ -8,7 +8,7 @@ using DigitalLearningSolutions.Data.DataServices.UserDataService; using DigitalLearningSolutions.Data.Exceptions; using DigitalLearningSolutions.Data.Models; - using DigitalLearningSolutions.Data.Models.User; + using DigitalLearningSolutions.Data.Models.User; using DigitalLearningSolutions.Data.Utilities; using DigitalLearningSolutions.Web.Services; using DigitalLearningSolutions.Web.Tests.TestHelpers; @@ -735,15 +735,13 @@ public void GetSupervisorsAtCentreForCategory_returns_expected_admins() .With(au => au.IsSupervisor = true) .With(au => au.CategoryId = 2) .TheRest().With(au => au.IsSupervisor = false).Build().ToList(); - A.CallTo(() => userDataService.GetAdminUsersByCentreId(A._)).Returns(adminUsers); + A.CallTo(() => userDataService.GetAdminUsersAtCentreForCategory(A._, A._)).Returns(adminUsers); // When var result = userService.GetSupervisorsAtCentreForCategory(1, 1).ToList(); // Then - result.Should().HaveCount(5); - result.Should().OnlyContain(au => au.IsSupervisor); - result.Should().OnlyContain(au => au.CategoryId == null || au.CategoryId == 1); + result.Should().BeEquivalentTo(adminUsers); } [Test] diff --git a/DigitalLearningSolutions.Web/Services/UserService.cs b/DigitalLearningSolutions.Web/Services/UserService.cs index 87db1ad6c6..540b4b164a 100644 --- a/DigitalLearningSolutions.Web/Services/UserService.cs +++ b/DigitalLearningSolutions.Web/Services/UserService.cs @@ -317,8 +317,8 @@ public IEnumerable GetSupervisorsAtCentre(int centreId) public IEnumerable GetSupervisorsAtCentreForCategory(int centreId, int categoryId) { - return userDataService.GetAdminUsersByCentreId(centreId).Where(au => au.IsSupervisor) - .Where(au => au.CategoryId == categoryId || au.CategoryId == null); + return userDataService.GetAdminUsersAtCentreForCategory(centreId, categoryId); + } public bool DelegateUserLearningHubAccountIsLinked(int delegateId) @@ -894,7 +894,7 @@ public void ApproveDelegateUsers(params int[] ids) { userDataService.ApproveDelegateUsers(ids); } - + public (IEnumerable, int) GetAllAdmins(string search, int offset, int rows, int? adminId, string userStatus, string role, int? centreId, int failedLoginThreshold) { return userDataService.GetAllAdmins(search, offset, rows, adminId, userStatus, role, centreId, failedLoginThreshold); @@ -902,7 +902,7 @@ public void ApproveDelegateUsers(params int[] ids) public void UpdateAdminUserAndSpecialPermissions(int adminId, bool isCentreAdmin, bool isSupervisor, bool isNominatedSupervisor, bool isTrainer, bool isContentCreator, bool isContentManager, bool importOnly, int? categoryId, bool isCentreManager, bool isSuperAdmin, bool isReportsViewer, bool isLocalWorkforceManager, bool isFrameworkDeveloper, bool isWorkforceManager) { - userDataService.UpdateAdminUserAndSpecialPermissions(adminId, isCentreAdmin, isSupervisor, isNominatedSupervisor, isTrainer, isContentCreator, isContentManager,importOnly, categoryId, isCentreManager, isSuperAdmin, isReportsViewer, isLocalWorkforceManager, isFrameworkDeveloper, isWorkforceManager); + userDataService.UpdateAdminUserAndSpecialPermissions(adminId, isCentreAdmin, isSupervisor, isNominatedSupervisor, isTrainer, isContentCreator, isContentManager, importOnly, categoryId, isCentreManager, isSuperAdmin, isReportsViewer, isLocalWorkforceManager, isFrameworkDeveloper, isWorkforceManager); } public int GetUserIdFromAdminId(int adminId) @@ -929,7 +929,7 @@ public bool IsUserAlreadyAdminAtCentre(int? userId, int centreId) { return userDataService.IsUserAlreadyAdminAtCentre(userId, centreId); } - + public IEnumerable GetAdminsByCentreId(int centreId) { return userDataService.GetAdminsByCentreId(centreId);