Skip to content

Commit

Permalink
Merge pull request #414 from Strongminds/feature/KITOSUDV-1012-subtas…
Browse files Browse the repository at this point in the history
…ks-1412-1413

Feature/kitosudv 1012 subtasks 1412 1413
  • Loading branch information
JacobMalling authored Apr 19, 2021
2 parents dd5a3cb + 58ab649 commit 3f7fcdf
Show file tree
Hide file tree
Showing 18 changed files with 338 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public Task<Result<string, OperationError>> ExecuteAsync(CancellationToken token
updatesExecuted = HandleOrganizationUpdated(token, updatesExecuted, alreadyScheduledIds);
updatesExecuted = HandleBusinessTypeUpdates(token, updatesExecuted, alreadyScheduledIds);
updatesExecuted = HandleTaskRefUpdates(token, updatesExecuted, alreadyScheduledIds);
updatesExecuted = HandleContractUpdates(token, updatesExecuted, alreadyScheduledIds);

return Task.FromResult(Result<string, OperationError>.Success($"Completed {updatesExecuted} updates"));
}
Expand Down Expand Up @@ -110,13 +111,8 @@ private int HandleOrganizationUpdated(CancellationToken token, int updatesExecut
break;

using var transaction = _transactionManager.Begin(IsolationLevel.ReadCommitted);
var systemIds = _itSystemRepository.GetByRightsHolderId(update.SourceId).Select(x => x.Id).ToList();

var systemUsageIds = _itSystemUsageRepository.GetBySystemIds(systemIds).Select(x => x.Id);

var readModelIds = _readModelRepository.GetByRightsHolderId(update.SourceId).Select(x => x.SourceEntityId);

var ids = systemUsageIds.Concat(readModelIds).Distinct();
var ids = _readModelRepository.GetByDependentOrganizationId(update.SourceId).Select(x => x.SourceEntityId);

updatesExecuted = PerformUpdate(updatesExecuted, alreadyScheduledIds, ids, update, transaction);
}
Expand Down Expand Up @@ -158,6 +154,23 @@ private int HandleTaskRefUpdates(CancellationToken token, int updatesExecuted, H
return updatesExecuted;
}

private int HandleContractUpdates(CancellationToken token, int updatesExecuted, HashSet<int> alreadyScheduledIds)
{
foreach (var update in _updateRepository.GetMany(PendingReadModelUpdateSourceCategory.ItSystemUsage_Contract, BatchSize).ToList())
{
if (token.IsCancellationRequested)
break;

using var transaction = _transactionManager.Begin(IsolationLevel.ReadCommitted);

var ids = _readModelRepository.GetByContractId(update.SourceId).Select(x => x.SourceEntityId);

updatesExecuted = PerformUpdate(updatesExecuted, alreadyScheduledIds, ids, update, transaction);
}

return updatesExecuted;
}


private int PerformUpdate(
int updatesExecuted,
Expand Down
3 changes: 2 additions & 1 deletion Core.DomainModel/BackgroundJobs/PendingReadModelUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public enum PendingReadModelUpdateSourceCategory
ItSystemUsage_OrganizationUnit = 11,
ItSystemUsage_Organization = 12,
ItSystemUsage_BusinessType = 13,
ItSystemUsage_TaskRef = 14
ItSystemUsage_TaskRef = 14,
ItSystemUsage_Contract = 15
}

public class PendingReadModelUpdate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@ public ItSystemUsageOverviewReadModel()
public string LastChangedByName { get; set; }
public DateTime LastChanged { get; set; }
public DateTime? Concluded { get; set; }

public int? MainContractId { get; set; }
public string MainContractName { get; set; }
public int? MainContractSupplierId { get; set; }
public string MainContractSupplierName { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public interface IItSystemUsageOverviewReadModelRepository
void Update(ItSystemUsageOverviewReadModel readModel);
IQueryable<ItSystemUsageOverviewReadModel> GetByUserId(int userId);
IQueryable<ItSystemUsageOverviewReadModel> GetByOrganizationUnitId(int organizationUnitId);
IQueryable<ItSystemUsageOverviewReadModel> GetByRightsHolderId(int organizationId);
IQueryable<ItSystemUsageOverviewReadModel> GetByDependentOrganizationId(int organizationId);
IQueryable<ItSystemUsageOverviewReadModel> GetByBusinessTypeId(int businessTypeId);
IQueryable<ItSystemUsageOverviewReadModel> GetByContractId(int contractId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public IQueryable<ItSystemUsageOverviewReadModel> GetByOrganizationUnitId(int or
.Where(x => x.ResponsibleOrganizationUnitId == organizationUnitId);
}

public IQueryable<ItSystemUsageOverviewReadModel> GetByRightsHolderId(int organizationId)
public IQueryable<ItSystemUsageOverviewReadModel> GetByDependentOrganizationId(int organizationId)
{
//Gets all read models that have dependencies on the ItSystems RightsHolder organization
//Gets all read models that have dependencies on the ItSystems RightsHolder organization or on the MainContracts Supplier
return _repository
.AsQueryable()
.Where(x => x.ItSystemRightsHolderId == organizationId);
.Where(x => x.ItSystemRightsHolderId == organizationId || x.MainContractSupplierId == organizationId);
}

public IQueryable<ItSystemUsageOverviewReadModel> GetByBusinessTypeId(int businessTypeId)
Expand All @@ -90,5 +90,12 @@ public IQueryable<ItSystemUsageOverviewReadModel> GetByBusinessTypeId(int busine
.AsQueryable()
.Where(x => x.ItSystemBusinessTypeId == businessTypeId);
}

public IQueryable<ItSystemUsageOverviewReadModel> GetByContractId(int contractId)
{
return _repository
.AsQueryable()
.Where(x => x.MainContractId == contractId);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Core.DomainModel;
using Core.DomainModel.BackgroundJobs;
using Core.DomainModel.ItContract;
using Core.DomainModel.ItContract.DomainEvents;
using Core.DomainModel.ItSystem;
using Core.DomainModel.ItSystemUsage;
using Core.DomainModel.ItSystemUsage.Read;
Expand Down Expand Up @@ -28,7 +30,9 @@ public class BuildItSystemUsageOverviewReadModelOnChangesHandler :
IDomainEventHandler<EntityUpdatedEvent<TaskRef>>,
IDomainEventHandler<EntityDeletedEvent<ExternalReference>>,
IDomainEventHandler<EntityCreatedEvent<ExternalReference>>,
IDomainEventHandler<EntityUpdatedEvent<ExternalReference>>
IDomainEventHandler<EntityUpdatedEvent<ExternalReference>>,
IDomainEventHandler<EntityUpdatedEvent<ItContract>>,
IDomainEventHandler<ContractDeleted>
{
private readonly IPendingReadModelUpdateRepository _pendingReadModelUpdateRepository;
private readonly IItSystemUsageOverviewReadModelRepository _readModelRepository;
Expand Down Expand Up @@ -118,6 +122,16 @@ public void Handle(EntityUpdatedEvent<TaskRef> domainEvent)
_pendingReadModelUpdateRepository.Add(PendingReadModelUpdate.Create(domainEvent.Entity, PendingReadModelUpdateSourceCategory.ItSystemUsage_TaskRef));
}

public void Handle(EntityUpdatedEvent<ItContract> domainEvent)
{
_pendingReadModelUpdateRepository.Add(PendingReadModelUpdate.Create(domainEvent.Entity.Id, PendingReadModelUpdateSourceCategory.ItSystemUsage_Contract));
}

public void Handle(ContractDeleted domainEvent)
{
_pendingReadModelUpdateRepository.Add(PendingReadModelUpdate.Create(domainEvent.DeletedContract.Id, PendingReadModelUpdateSourceCategory.ItSystemUsage_Contract));
}

public void Handle(EntityDeletedEvent<ExternalReference> domainEvent) => HandleExternalReference(domainEvent);

public void Handle(EntityCreatedEvent<ExternalReference> domainEvent) => HandleExternalReference(domainEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public void Apply(ItSystemUsage source, ItSystemUsageOverviewReadModel destinati
PatchItSystemRightsHolder(source, destination);
PatchKLE(source, destination);
PatchReference(source, destination);
PatchMainContract(source, destination);
}

private static void PatchMainContract(ItSystemUsage source, ItSystemUsageOverviewReadModel destination)
{
destination.MainContractId = source.MainContract?.ItContractId;
destination.MainContractName = source.MainContract?.ItContract.Name;

destination.MainContractSupplierId = source.MainContract?.ItContract.Supplier?.Id;
destination.MainContractSupplierName = source.MainContract?.ItContract.Supplier?.Name;
}

private static void PatchReference(ItSystemUsage source, ItSystemUsageOverviewReadModel destination)
Expand Down
10 changes: 5 additions & 5 deletions Infrastructure.DataAccess/Infrastructure.DataAccess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,9 @@
<Compile Include="Migrations\202104071049413_Contract_Remove_DataHandler.designer.cs">
<DependentUpon>202104071049413_Contract_Remove_DataHandler.cs</DependentUpon>
</Compile>
<Compile Include="Migrations\202104190846204_Add_ItSystemUsageOverviewReadModel.cs" />
<Compile Include="Migrations\202104190846204_Add_ItSystemUsageOverviewReadModel.designer.cs">
<DependentUpon>202104190846204_Add_ItSystemUsageOverviewReadModel.cs</DependentUpon>
<Compile Include="Migrations\202104190852172_Add_ItSystemUsageOverviewReadModel.cs" />
<Compile Include="Migrations\202104190852172_Add_ItSystemUsageOverviewReadModel.designer.cs">
<DependentUpon>202104190852172_Add_ItSystemUsageOverviewReadModel.cs</DependentUpon>
</Compile>
<Compile Include="Migrations\Configuration.cs" />
<Compile Include="Mapping\TerminationDeadlineTypeMap.cs" />
Expand Down Expand Up @@ -1097,8 +1097,8 @@
<EmbeddedResource Include="Migrations\202104071049413_Contract_Remove_DataHandler.resx">
<DependentUpon>202104071049413_Contract_Remove_DataHandler.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Migrations\202104190846204_Add_ItSystemUsageOverviewReadModel.resx">
<DependentUpon>202104190846204_Add_ItSystemUsageOverviewReadModel.cs</DependentUpon>
<EmbeddedResource Include="Migrations\202104190852172_Add_ItSystemUsageOverviewReadModel.resx">
<DependentUpon>202104190852172_Add_ItSystemUsageOverviewReadModel.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Migrations\SQLScripts\Migrate_Contract_Relations.sql" />
<EmbeddedResource Include="Migrations\SQLScripts\Migrate_System_Url.sql" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Data.Entity.ModelConfiguration;
using Core.DomainModel;
using Core.DomainModel.ItContract;
using Core.DomainModel.ItSystem;
using Core.DomainModel.ItSystemUsage;
using Core.DomainModel.ItSystemUsage.Read;
Expand Down Expand Up @@ -80,6 +81,20 @@ public ItSystemUsageOverviewReadModelMap()
.HasMaxLength(UserConstraints.MaxNameLength)
.HasIndexAnnotation("ItSystemUsageOverviewReadModel_Index_LastChangedByName", 0);

Property(x => x.MainContractId)
.HasIndexAnnotation("ItSystemUsageOverviewReadModel_Index_MainContractId", 0);

Property(x => x.MainContractName)
.HasMaxLength(ItContractConstraints.MaxNameLength)
.HasIndexAnnotation("ItSystemUsageOverviewReadModel_Index_MainContractName", 0);

Property(x => x.MainContractSupplierId)
.HasIndexAnnotation("ItSystemUsageOverviewReadModel_Index_MainContractSupplierId", 0);

Property(x => x.MainContractSupplierName)
.HasMaxLength(Organization.MaxNameLength)
.HasIndexAnnotation("ItSystemUsageOverviewReadModel_Index_MainContractSupplierName", 0);


//No index bc we don't know how long it might be
Property(x => x.ItSystemKLEIdsAsCsv).IsOptional();
Expand Down

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public override void Up()
LastChangedByName = c.String(maxLength: 100),
LastChanged = c.DateTime(nullable: false, precision: 7, storeType: "datetime2"),
Concluded = c.DateTime(precision: 7, storeType: "datetime2"),
MainContractId = c.Int(),
MainContractName = c.String(maxLength: 200),
MainContractSupplierId = c.Int(),
MainContractSupplierName = c.String(maxLength: 100),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Organization", t => t.OrganizationId)
Expand All @@ -64,7 +68,11 @@ public override void Up()
.Index(t => t.ObjectOwnerId, name: "ItSystemUsageOverviewReadModel_Index_ObjectOwnerId")
.Index(t => t.ObjectOwnerName, name: "ItSystemUsageOverviewReadModel_Index_ObjectOwnerName")
.Index(t => t.LastChangedById, name: "ItSystemUsageOverviewReadModel_Index_LastChangedById")
.Index(t => t.LastChangedByName, name: "ItSystemUsageOverviewReadModel_Index_LastChangedByName");
.Index(t => t.LastChangedByName, name: "ItSystemUsageOverviewReadModel_Index_LastChangedByName")
.Index(t => t.MainContractId, name: "ItSystemUsageOverviewReadModel_Index_MainContractId")
.Index(t => t.MainContractName, name: "ItSystemUsageOverviewReadModel_Index_MainContractName")
.Index(t => t.MainContractSupplierId, name: "ItSystemUsageOverviewReadModel_Index_MainContractSupplierId")
.Index(t => t.MainContractSupplierName, name: "ItSystemUsageOverviewReadModel_Index_MainContractSupplierName");

CreateTable(
"dbo.ItSystemUsageOverviewTaskRefReadModels",
Expand Down Expand Up @@ -154,6 +162,10 @@ public override void Down()
DropIndex("dbo.ItSystemUsageOverviewTaskRefReadModels", new[] { "ParentId" });
DropIndex("dbo.ItSystemUsageOverviewTaskRefReadModels", "ItSystemUsageOverviewTaskRefReadModel_Index_KLEName");
DropIndex("dbo.ItSystemUsageOverviewTaskRefReadModels", "ItSystemUsageOverviewTaskRefReadModel_Index_KLEId");
DropIndex("dbo.ItSystemUsageOverviewReadModels", "ItSystemUsageOverviewReadModel_Index_MainContractSupplierName");
DropIndex("dbo.ItSystemUsageOverviewReadModels", "ItSystemUsageOverviewReadModel_Index_MainContractSupplierId");
DropIndex("dbo.ItSystemUsageOverviewReadModels", "ItSystemUsageOverviewReadModel_Index_MainContractName");
DropIndex("dbo.ItSystemUsageOverviewReadModels", "ItSystemUsageOverviewReadModel_Index_MainContractId");
DropIndex("dbo.ItSystemUsageOverviewReadModels", "ItSystemUsageOverviewReadModel_Index_LastChangedByName");
DropIndex("dbo.ItSystemUsageOverviewReadModels", "ItSystemUsageOverviewReadModel_Index_LastChangedById");
DropIndex("dbo.ItSystemUsageOverviewReadModels", "ItSystemUsageOverviewReadModel_Index_ObjectOwnerName");
Expand Down
Loading

0 comments on commit 3f7fcdf

Please sign in to comment.