Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PRIME-2798 Update deleting site and organization to soft-delete #2622

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public CommunitySiteFactory(Organization org = null)
Ignore(x => x.ApprovedDate);
Ignore(x => x.ArchivedDate);
Ignore(x => x.Mnemonic);
Ignore(x => x.DeletedDate);
Ignore(x => x.SiteSubmissions);

FinishWith((f, x) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public async Task<ActionResult> GetHealthAuthoritySite(int healthAuthorityId, in
return NotFound($"Health authority site not found with id {siteId}");
}

var site = await _healthAuthoritySiteService.GetSiteAsync(siteId);
var site = await _healthAuthoritySiteService.GetSiteViewModelAsync(siteId);

return Ok(site);
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Prime.Migrations
{
/// <inheritdoc />
public partial class AddDeletedDateToSiteOrganization : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTimeOffset>(
name: "DeletedDate",
table: "Site",
type: "timestamp with time zone",
nullable: true);

migrationBuilder.AddColumn<DateTimeOffset>(
name: "DeletedDate",
table: "Organization",
type: "timestamp with time zone",
nullable: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "DeletedDate",
table: "Site");

migrationBuilder.DropColumn(
name: "DeletedDate",
table: "Organization");
}
}
}
6 changes: 6 additions & 0 deletions prime-dotnet-webapi/Migrations/ApiDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13624,6 +13624,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<Guid>("CreatedUserId")
.HasColumnType("uuid");

b.Property<DateTimeOffset?>("DeletedDate")
.HasColumnType("timestamp with time zone");

b.Property<string>("DoingBusinessAs")
.HasColumnType("text");

Expand Down Expand Up @@ -17708,6 +17711,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<Guid>("CreatedUserId")
.HasColumnType("uuid");

b.Property<DateTimeOffset?>("DeletedDate")
.HasColumnType("timestamp with time zone");

b.Property<string>("DoingBusinessAs")
.HasColumnType("text");

Expand Down
2 changes: 2 additions & 0 deletions prime-dotnet-webapi/Models/SiteRegistration/Organization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public Organization()

public bool PendingTransfer { get; set; }

public DateTimeOffset? DeletedDate { get; set; }

[JsonIgnore]
public ICollection<CommunitySite> Sites { get; set; }

Expand Down
2 changes: 2 additions & 0 deletions prime-dotnet-webapi/Models/SiteRegistration/Site.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public Site()

public Admin Adjudicator { get; set; }

public DateTimeOffset? DeletedDate { get; set; }

[JsonIgnore]
public ICollection<SiteRegistrationReviewDocument> SiteRegistrationReviewDocuments { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion prime-dotnet-webapi/Services/AuthorizedUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task<IEnumerable<HealthAuthoritySiteListViewModel>> GetAuthorizedUs
var orgId = (int)authorizedUser.HealthAuthorityCode;

return await _context.HealthAuthoritySites
.Where(has => has.HealthAuthorityOrganizationId == orgId && has.ArchivedDate == null)
.Where(has => has.HealthAuthorityOrganizationId == orgId && has.DeletedDate == null && has.ArchivedDate == null)
.ProjectTo<HealthAuthoritySiteListViewModel>(_mapper.ConfigurationProvider)
.DecompileAsync()
.ToListAsync();
Expand Down
12 changes: 7 additions & 5 deletions prime-dotnet-webapi/Services/CommunitySiteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task<IEnumerable<CommunitySite>> GetSitesAsync(int? organizationId

if (organizationId != null)
{
query = query.Where(s => s.OrganizationId == organizationId);
query = query.Where(s => s.OrganizationId == organizationId && s.Organization.DeletedDate == null);
}

return await query.ToListAsync();
Expand All @@ -56,6 +56,7 @@ public async Task<PaginatedList<CommunitySiteAdminListViewModel>> GetSitesAsync(

var query = _context.CommunitySites
.AsNoTracking()
.Where(s => s.DeletedDate == null && s.Organization.DeletedDate == null)
.If(searchOptions.CareSettingCode.HasValue, q => q
.Where(s => s.CareSettingCode == searchOptions.CareSettingCode)
)
Expand Down Expand Up @@ -225,7 +226,7 @@ public async Task<PermissionsRecord> GetPermissionsRecordAsync(int siteId)
{
return await _context.CommunitySites
.AsNoTracking()
.Where(s => s.Id == siteId)
.Where(s => s.Id == siteId && s.DeletedDate == null)
.Select(s => new PermissionsRecord { Username = s.Organization.SigningAuthority.Username })
.SingleOrDefaultAsync();
}
Expand Down Expand Up @@ -635,7 +636,7 @@ public async Task<BusinessLicence> GetLatestBusinessLicenceAsync(int siteId)
return await _context.CommunitySites
.Include(s => s.BusinessLicences)
.ThenInclude(bl => bl.BusinessLicenceDocument)
.Where(s => s.Id == siteId)
.Where(s => s.Id == siteId && s.DeletedDate == null)
.Select(s => s.BusinessLicence)
.DecompileAsync()
.SingleOrDefaultAsync();
Expand All @@ -645,7 +646,7 @@ public async Task<bool> SiteExistsAsync(int siteId)
{
return await _context.CommunitySites
.AsNoTracking()
.AnyAsync(s => s.Id == siteId);
.AnyAsync(s => s.Id == siteId && s.DeletedDate == null);
}

public async Task<IEnumerable<IndividualDeviceProviderViewModel>> GetIndividualDeviceProvidersAsync(int siteId)
Expand All @@ -659,7 +660,7 @@ public async Task<IEnumerable<IndividualDeviceProviderViewModel>> GetIndividualD
public async Task UpdateSigningAuthorityForOrganization(int organizationId, int partyId)
{
var sites = await _context.CommunitySites
.Where(s => s.OrganizationId == organizationId)
.Where(s => s.OrganizationId == organizationId && s.DeletedDate == null)
.ToListAsync();

foreach (var site in sites)
Expand All @@ -673,6 +674,7 @@ public async Task UpdateSigningAuthorityForOrganization(int organizationId, int
private IQueryable<CommunitySite> GetBaseSiteQuery()
{
return _context.CommunitySites
.Where(s => s.DeletedDate == null)
.Include(s => s.Provisioner)
.Include(s => s.SiteVendors)
.ThenInclude(v => v.Vendor)
Expand Down
7 changes: 5 additions & 2 deletions prime-dotnet-webapi/Services/EnrolleeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,10 @@ public async Task<IEnumerable<EnrolleeDeviceProviderViewModel>> GetEnrolleeDevic
public async Task<IEnumerable<EnrolleeRemoteUserViewModel>> GetEnrolleeRemoteUsersAsync(int enrolleeId)
{
return await _context.EnrolleeRemoteUsers
.Where(eru => eru.EnrolleeId == enrolleeId)
.Where(eru => eru.EnrolleeId == enrolleeId &&
_context.RemoteUsers
.Where(ru => ru.Site.DeletedDate == null)
.Select(ru => ru.Id).Contains(eru.RemoteUserId))
.ProjectTo<EnrolleeRemoteUserViewModel>(_mapper.ConfigurationProvider)
.ToListAsync();
}
Expand Down Expand Up @@ -734,7 +737,7 @@ public async Task<IEnumerable<RemoteAccessSiteViewModel>> GetRemoteAccessSitesAs
// Currently, only maps from Community Sites as Remote Users are disabled on Health Authorities
return await _context.RemoteAccessSites
.AsNoTracking()
.Where(ras => ras.EnrolleeId == enrolleeId)
.Where(ras => ras.EnrolleeId == enrolleeId && ras.Site.DeletedDate == null)
.ProjectTo<RemoteAccessSiteViewModel>(_mapper.ConfigurationProvider)
.ToListAsync();
}
Expand Down
49 changes: 32 additions & 17 deletions prime-dotnet-webapi/Services/HealthAuthoritySiteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,29 @@ public async Task<PermissionsRecord> GetPermissionsRecordAsync(int siteId)
{
return await _context.HealthAuthoritySites
.AsNoTracking()
.Where(s => s.Id == siteId)
.Where(s => s.Id == siteId && s.DeletedDate == null)
.Select(s => new PermissionsRecord { Username = s.AuthorizedUser.Party.Username })
.SingleOrDefaultAsync();
}

public async Task<bool> AllowToSubmitSite(int siteId, string username)
{
var healthAuthorityCode = (int)await _context.AuthorizedUsers
.Where(au => au.Party.Username == username)
.Select(au => au.HealthAuthorityCode)
.SingleOrDefaultAsync();
.Where(au => au.Party.Username == username)
.Select(au => au.HealthAuthorityCode)
.SingleOrDefaultAsync();

return await _context.HealthAuthoritySites
.AsNoTracking()
.Where(s => s.Id == siteId)
.Where(s => s.HealthAuthorityOrganization.Id == healthAuthorityCode).AnyAsync();
.AsNoTracking()
.Where(s => s.Id == siteId && s.DeletedDate == null)
.Where(s => s.HealthAuthorityOrganization.Id == healthAuthorityCode).AnyAsync();
}

public async Task<bool> SiteExistsAsync(int healthAuthorityId, int siteId)
{
return await _context.HealthAuthoritySites
.AsNoTracking()
.AnyAsync(s => s.Id == siteId && s.HealthAuthorityOrganizationId == healthAuthorityId);
.AnyAsync(s => s.Id == siteId && s.HealthAuthorityOrganizationId == healthAuthorityId && s.DeletedDate == null);
}

public async Task<bool> SiteIsEditableAsync(int healthAuthorityId, int siteId)
Expand All @@ -65,7 +65,8 @@ public async Task<bool> SiteIsEditableAsync(int healthAuthorityId, int siteId)
.DecompileAsync()
.AnyAsync(s => s.Id == siteId
&& s.HealthAuthorityOrganizationId == healthAuthorityId
&& s.Status == SiteStatusType.Editable);
&& s.Status == SiteStatusType.Editable
&& s.DeletedDate == null);
}

public async Task<HealthAuthoritySiteViewModel> CreateSiteAsync(int healthAuthorityId, HealthAuthoritySiteCreateModel createModel)
Expand All @@ -92,6 +93,7 @@ public async Task<IEnumerable<HealthAuthoritySiteAdminListViewModel>> GetSitesAs
{
var siteList = await _context.HealthAuthoritySites
.AsNoTracking()
.Where(has => has.DeletedDate == null)
.If(healthAuthorityId.HasValue, q => q.Where(site => site.HealthAuthorityOrganizationId == healthAuthorityId))
.If(healthAuthoritySiteId.HasValue, q => q.Where(site => site.Id == healthAuthoritySiteId))
.Include(has => has.SiteSubmissions)
Expand Down Expand Up @@ -129,6 +131,7 @@ public async Task<IEnumerable<HealthAuthoritySiteAdminListViewModel>> GetSitesAs

var query = _context.HealthAuthoritySites
.AsNoTracking()
.Where(s => s.DeletedDate == null)
.If(!string.IsNullOrWhiteSpace(searchOptions.TextSearch),
q => q.Search(
s => s.SiteName,
Expand Down Expand Up @@ -171,14 +174,15 @@ public async Task<IEnumerable<HealthAuthoritySiteAdminListViewModel>> GetSitesAs
private async Task<int> GetDuplicatePecCount(string pec, int originalHASiteId, int originalSiteId)
{
return await _context.HealthAuthoritySites
.Where(s => s.PEC != null && s.PEC == pec && s.HealthAuthorityOrganizationId == originalHASiteId && originalSiteId != s.Id)
.Where(s => s.PEC != null && s.PEC == pec && s.HealthAuthorityOrganizationId == originalHASiteId && originalSiteId != s.Id && s.DeletedDate == null)
.CountAsync();
}

public async Task<HealthAuthoritySiteViewModel> GetSiteAsync(int siteId)
public async Task<HealthAuthoritySiteViewModel> GetSiteViewModelAsync(int siteId)
{
return await _context.HealthAuthoritySites
.AsNoTracking()
.Where(s => s.DeletedDate == null)
.ProjectTo<HealthAuthoritySiteViewModel>(_mapper.ConfigurationProvider)
.DecompileAsync()
.SingleOrDefaultAsync(has => has.Id == siteId);
Expand All @@ -191,14 +195,15 @@ public async Task<HealthAuthoritySite> GetHealthAuthoritySiteAsync(int siteId)
.Include(s => s.HealthAuthorityOrganization)
.Include(s => s.HealthAuthorityVendor)
.ThenInclude(v => v.Vendor)
.Where(s => s.Id == siteId)
.Where(s => s.Id == siteId && s.DeletedDate == null)
.FirstOrDefaultAsync();
}

public async Task<HealthAuthoritySiteAdminViewModel> GetAdminSiteAsync(int siteId)
{
return await _context.HealthAuthoritySites
.AsNoTracking()
.Where(s => s.DeletedDate == null)
.ProjectTo<HealthAuthoritySiteAdminViewModel>(_mapper.ConfigurationProvider)
.DecompileAsync()
.SingleOrDefaultAsync(has => has.Id == siteId);
Expand All @@ -209,7 +214,7 @@ public async Task UpdateSiteAsync(int siteId, HealthAuthoritySiteUpdateModel upd
var site = await _context.HealthAuthoritySites
.Include(site => site.PhysicalAddress)
.Include(site => site.BusinessHours)
.SingleOrDefaultAsync(has => has.Id == siteId);
.SingleOrDefaultAsync(has => has.Id == siteId && has.DeletedDate == null);

site.AuthorizedUserId = authorizedUserId;

Expand All @@ -235,8 +240,7 @@ public async Task UpdateSiteAsync(int siteId, HealthAuthoritySiteUpdateModel upd

public async Task SetSiteCompletedAsync(int siteId)
{
var site = await _context.HealthAuthoritySites
.SingleOrDefaultAsync(has => has.Id == siteId);
var site = await GetSiteAsync(siteId);

site.Completed = true;

Expand All @@ -245,8 +249,7 @@ public async Task SetSiteCompletedAsync(int siteId)

public async Task SiteSubmissionAsync(int siteId)
{
var site = await _context.HealthAuthoritySites
.SingleOrDefaultAsync(has => has.Id == siteId);
var site = await GetSiteAsync(siteId);

site.SubmittedDate = DateTimeOffset.Now;
site.AddStatus(SiteStatusType.InReview);
Expand All @@ -269,5 +272,17 @@ public async Task<List<int>> TransferAuthorizedUserAsync(int currentAuthorizedUs

return sites.Select(s => s.Id).ToList();
}

public async Task<Site> GetSiteAsync(int siteId)
{
return await GetBaseSiteQuery()
.SingleOrDefaultAsync(s => s.Id == siteId);
}

private IQueryable<Site> GetBaseSiteQuery()
{
return _context.HealthAuthoritySites
.Where(s => s.DeletedDate == null);
}
}
}
Loading
Loading