Skip to content

Commit dc31214

Browse files
authored
Merge pull request #2063 from TechnologyEnhancedLearning/Develop/Feature/TD-985-AddCentreFunctionalityInSuperAdmin
TD-985 Added functionality of adding a new centre in Super Admin
2 parents 56898be + eb222cc commit dc31214

File tree

7 files changed

+428
-2
lines changed

7 files changed

+428
-2
lines changed

DigitalLearningSolutions.Data.Tests/DataServices/CentresDataServiceTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,49 @@ public void UpdateCentreManagerDetails_updates_centre()
184184
}
185185
}
186186

187+
[Test]
188+
public void AddCentre_inserts_new_centre()
189+
{
190+
using var transaction = new TransactionScope();
191+
try
192+
{
193+
// Given
194+
const string centreName = "TestCentre";
195+
const string contactFirstName = "TestFirstName";
196+
const string contactLastName = "TestLastName";
197+
const string contactEmail = "[email protected]";
198+
const string contactTelephone = "0123456789";
199+
const string registrationEmail = "[email protected]";
200+
201+
// When
202+
int insertedID = centresDataService.AddCentreForSuperAdmin(centreName, contactFirstName, contactLastName,
203+
contactEmail, contactTelephone, 1, 1, registrationEmail, "194.176.105", true,true);
204+
var insertedCentre = centresDataService.GetCentreDetailsById(insertedID)!;
205+
206+
// Then
207+
using (new AssertionScope())
208+
{
209+
insertedCentre.CentreName.Should().BeEquivalentTo(centreName);
210+
insertedCentre.ContactForename.Should().BeEquivalentTo(contactFirstName);
211+
insertedCentre.ContactSurname.Should().BeEquivalentTo(contactLastName);
212+
insertedCentre.ContactEmail.Should().BeEquivalentTo(contactEmail);
213+
insertedCentre.ContactTelephone.Should().BeEquivalentTo(contactTelephone);
214+
insertedCentre.CentreType.Should().BeEquivalentTo("NHS Organisation");
215+
insertedCentre.RegionName.Should().BeEquivalentTo("East Midlands");
216+
insertedCentre.RegistrationEmail.Should().BeEquivalentTo(registrationEmail);
217+
insertedCentre.ShowOnMap.Should().Be(true);
218+
insertedCentre.IpPrefix.Should().BeEquivalentTo("194.176.105");
219+
insertedCentre.CmsAdministratorSpots.Should().Be(5);
220+
insertedCentre.ContractType.Should().BeEquivalentTo("Basic");
221+
insertedCentre.TrainerSpots.Should().Be(0);
222+
}
223+
}
224+
finally
225+
{
226+
transaction.Dispose();
227+
}
228+
}
229+
187230
[Test]
188231
public void UpdateCentreWebsiteDetails_updates_centre()
189232
{

DigitalLearningSolutions.Data/DataServices/CentresDataService.cs

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
namespace DigitalLearningSolutions.Data.DataServices
22
{
33
using Dapper;
4+
using DigitalLearningSolutions.Data.Extensions;
45
using DigitalLearningSolutions.Data.Models.Centres;
56
using DigitalLearningSolutions.Data.Models.DbModels;
67
using Microsoft.Extensions.Logging;
78
using System;
89
using System.Collections.Generic;
910
using System.Data;
11+
using System.Transactions;
1012

1113
public interface ICentresDataService
1214
{
@@ -63,6 +65,20 @@ void UpdateCentreDetails(
6365
byte[]? centreLogo
6466
);
6567

68+
public int AddCentreForSuperAdmin(
69+
string centreName,
70+
string? contactFirstName,
71+
string? contactLastName,
72+
string? contactEmail,
73+
string? contactPhone,
74+
int? centreTypeId,
75+
int? regionId,
76+
string? registrationEmail,
77+
string? ipPrefix,
78+
bool showOnMap,
79+
bool AddITSPcourses
80+
);
81+
6682
public void UpdateCentreDetailsForSuperAdmin(
6783
int centreId,
6884
string centreName,
@@ -223,7 +239,7 @@ FROM Centres AS c
223239
c.ServerSpaceBytes,
224240
c.CentreTypeID,
225241
ctp.CentreType,
226-
c.ShowOnMap
242+
c.pwEmail as RegistrationEmail
227243
FROM Centres AS c
228244
INNER JOIN Regions AS r ON r.RegionID = c.RegionID
229245
INNER JOIN ContractTypes AS ct ON ct.ContractTypeID = c.ContractTypeId
@@ -448,6 +464,84 @@ bool showOnMap
448464
);
449465
}
450466

467+
public int AddCentreForSuperAdmin(
468+
string centreName,
469+
string? contactFirstName,
470+
string? contactLastName,
471+
string? contactEmail,
472+
string? contactPhone,
473+
int? centreTypeId,
474+
int? regionId,
475+
string? registrationEmail,
476+
string? ipPrefix,
477+
bool showOnMap,
478+
bool AddITSPcourses
479+
)
480+
{
481+
int newCentreId;
482+
connection.EnsureOpen();
483+
using var transaction = connection.BeginTransaction();
484+
{
485+
newCentreId = connection.QuerySingle<int>(
486+
@"Insert INTO Centres
487+
(CentreName,
488+
ContactForename,
489+
ContactSurname,
490+
ContactEmail,
491+
ContactTelephone,
492+
CentreTypeID,
493+
RegionID,
494+
pwEmail,
495+
IPPrefix,
496+
ShowOnMap
497+
)
498+
OUTPUT Inserted.CentreID
499+
Values
500+
(
501+
@centreName,
502+
@contactFirstName,
503+
@contactLastName,
504+
@contactEmail,
505+
@contactPhone,
506+
@centreTypeId,
507+
@regionId,
508+
@registrationEmail,
509+
@ipPrefix,
510+
@showOnMap
511+
)",
512+
new
513+
{
514+
centreName,
515+
contactFirstName,
516+
contactLastName,
517+
contactEmail,
518+
contactPhone,
519+
centreTypeId,
520+
regionId,
521+
registrationEmail,
522+
ipPrefix,
523+
showOnMap
524+
},
525+
transaction
526+
);
527+
if (AddITSPcourses)
528+
{
529+
connection.Execute(
530+
@"INSERT INTO [CentreApplications] ([CentreID], [ApplicationID])
531+
SELECT @newCentreId, ApplicationID
532+
FROM Applications
533+
WHERE (Debug = 0) AND (ArchivedDate IS NULL) AND (ASPMenu = 1) AND (CoreContent = 1) AND (BrandID = 1) AND (LaunchedAssess = 1)",
534+
new { newCentreId },
535+
transaction
536+
);
537+
}
538+
539+
transaction.Commit();
540+
return newCentreId;
541+
}
542+
543+
}
544+
451545
public (string firstName, string lastName, string email) GetCentreManagerDetails(int centreId)
452546
{
453547
var info = connection.QueryFirstOrDefault<(string, string, string)>(

DigitalLearningSolutions.Data/Models/Centres/Centre.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,7 @@ public class Centre
4141
public string CentreType { get; set; }
4242
public long CandidateByteLimit { get; set; }
4343
public DateTime? ContractReviewDate { get; set; }
44+
public string? RegistrationEmail { get; set; }
45+
public bool AddITSPcourses { get; set; }
4446
}
4547
}

DigitalLearningSolutions.Web/Controllers/SuperAdmin/Centres/CentresController.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,5 +401,50 @@ public IActionResult EditCentreRoleLimits(CentreRoleLimitsViewModel model)
401401

402402
return RedirectToAction("ManageCentre", "Centres", new { centreId = model.CentreId });
403403
}
404+
405+
[Route("SuperAdmin/Centres/AddCentre")]
406+
public IActionResult AddCentre()
407+
{
408+
var regions = regionDataService.GetRegionsAlphabetical().ToList();
409+
var centreTypes = this.centresDataService.GetCentreTypes().ToList();
410+
411+
var addCentreSuperAdminViewModel = new AddCentreSuperAdminViewModel();
412+
addCentreSuperAdminViewModel.IpPrefix = "194.176.105";
413+
414+
addCentreSuperAdminViewModel.RegionNameOptions = SelectListHelper.MapOptionsToSelectListItems(regions);
415+
addCentreSuperAdminViewModel.CentreTypeOptions = SelectListHelper.MapOptionsToSelectListItems(centreTypes);
416+
417+
return View(addCentreSuperAdminViewModel);
418+
}
419+
420+
[HttpPost]
421+
[Route("SuperAdmin/Centres/AddCentre")]
422+
public IActionResult AddCentre(AddCentreSuperAdminViewModel model)
423+
{
424+
if (!ModelState.IsValid)
425+
{
426+
var centreTypes = this.centresDataService.GetCentreTypes().ToList();
427+
var regions = regionDataService.GetRegionsAlphabetical().ToList();
428+
model.RegionNameOptions = SelectListHelper.MapOptionsToSelectListItems(regions);
429+
model.CentreTypeOptions = SelectListHelper.MapOptionsToSelectListItems(centreTypes);
430+
return View(model);
431+
}
432+
433+
int insertedID = centresDataService.AddCentreForSuperAdmin(
434+
model.CentreName,
435+
model.ContactFirstName,
436+
model.ContactLastName,
437+
model.ContactEmail,
438+
model.ContactPhone,
439+
model.CentreTypeId,
440+
model.RegionId,
441+
model.RegistrationEmail,
442+
model.IpPrefix,
443+
model.ShowOnMap,
444+
model.AddITSPcourses
445+
);
446+
447+
return RedirectToAction("ManageCentre", "Centres", new { centreId = insertedID });
448+
}
404449
}
405450
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
namespace DigitalLearningSolutions.Web.ViewModels.SuperAdmin.Centres
2+
{
3+
using DigitalLearningSolutions.Data.DataServices;
4+
using DigitalLearningSolutions.Data.Models.Centres;
5+
using DigitalLearningSolutions.Data.Models.DbModels;
6+
using DigitalLearningSolutions.Web.Attributes;
7+
using DocumentFormat.OpenXml.Wordprocessing;
8+
using Microsoft.AspNetCore.Mvc.Rendering;
9+
using System.Collections.Generic;
10+
using System.ComponentModel.DataAnnotations;
11+
12+
public class AddCentreSuperAdminViewModel
13+
{
14+
public AddCentreSuperAdminViewModel() { }
15+
16+
public AddCentreSuperAdminViewModel(Centre centre)
17+
{
18+
CentreEmail = centre.CentreEmail;
19+
CentreName = centre.CentreName;
20+
ContactFirstName = centre.ContactForename;
21+
ContactLastName = centre.ContactSurname;
22+
ContactEmail = centre.ContactEmail;
23+
ContactPhone = centre.ContactTelephone;
24+
CentreType = centre.CentreType;
25+
CentreTypeId = centre.CentreTypeId;
26+
RegionName = centre.RegionName;
27+
RegionId = centre.RegionId;
28+
RegistrationEmail = centre.RegistrationEmail;
29+
IpPrefix = centre.IpPrefix;
30+
AddITSPcourses = centre.AddITSPcourses;
31+
ShowOnMap = centre.ShowOnMap;
32+
}
33+
34+
[Required(ErrorMessage = "Enter a centre name")]
35+
public string CentreName { get; set; }
36+
37+
[Required(ErrorMessage = "Select a centre type")]
38+
public int? CentreTypeId { get; set; }
39+
40+
public string? CentreType { get; set; }
41+
42+
[Required(ErrorMessage = "Select a region")]
43+
public int? RegionId { get; set; }
44+
45+
public string? RegionName { get; set; }
46+
47+
[MaxLength(250, ErrorMessage = "Email must be 250 characters or fewer")]
48+
[EmailAddress(ErrorMessage = "Enter an email in the correct format, like [email protected]")]
49+
[NoWhitespace(ErrorMessage = "Email must not contain any whitespace characters")]
50+
public string? CentreEmail { get; set; }
51+
52+
public string? IpPrefix { get; set; }
53+
public bool ShowOnMap { get; set; }
54+
public string? ContactFirstName { get; set; }
55+
public string? ContactLastName { get; set; }
56+
public string? ContactEmail { get; set; }
57+
public string? ContactPhone { get; set; }
58+
59+
[MaxLength(250, ErrorMessage = "Email must be 250 characters or fewer")]
60+
[EmailAddress(ErrorMessage = "Enter an email in the correct format, like [email protected]")]
61+
[NoWhitespace(ErrorMessage = "Email must not contain any whitespace characters")]
62+
public string? RegistrationEmail { get; set; }
63+
64+
public bool AddITSPcourses { get; set; }
65+
66+
public IEnumerable<SelectListItem> CentreTypeOptions { get; set; } = new List<SelectListItem>();
67+
public IEnumerable<SelectListItem> RegionNameOptions { get; set; } = new List<SelectListItem>();
68+
}
69+
}

0 commit comments

Comments
 (0)