-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refresh workforce data with "most open" version of establishment (#1248)
- Loading branch information
Showing
7 changed files
with
270 additions
and
79 deletions.
There are no files selected for viewing
64 changes: 5 additions & 59 deletions
64
TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/RefreshEstablishmentsJob.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,14 @@ | ||
using TeachingRecordSystem.Core.DataStore.Postgres; | ||
using TeachingRecordSystem.Core.Jobs.Scheduling; | ||
using TeachingRecordSystem.Core.Services.Establishments; | ||
using TeachingRecordSystem.Core.Services.WorkforceData; | ||
|
||
namespace TeachingRecordSystem.Core.Jobs; | ||
|
||
public class RefreshEstablishmentsJob(TrsDbContext dbContext, IEstablishmentMasterDataService establishmentMasterDataService) | ||
public class RefreshEstablishmentsJob(IBackgroundJobScheduler backgroundJobScheduler) | ||
{ | ||
public async Task ExecuteAsync(CancellationToken cancellationToken) | ||
{ | ||
int i = 0; | ||
await foreach (var establishment in establishmentMasterDataService.GetEstablishments()) | ||
{ | ||
var existingEstablishment = await dbContext.Establishments.SingleOrDefaultAsync(e => e.Urn == establishment.Urn); | ||
if (existingEstablishment == null) | ||
{ | ||
dbContext.Establishments.Add(new() | ||
{ | ||
EstablishmentId = Guid.NewGuid(), | ||
Urn = establishment.Urn, | ||
LaCode = establishment.LaCode, | ||
LaName = establishment.LaName, | ||
EstablishmentNumber = establishment.EstablishmentNumber, | ||
EstablishmentName = establishment.EstablishmentName, | ||
EstablishmentTypeCode = establishment.EstablishmentTypeCode, | ||
EstablishmentTypeName = establishment.EstablishmentTypeName, | ||
EstablishmentTypeGroupCode = establishment.EstablishmentTypeGroupCode, | ||
EstablishmentTypeGroupName = establishment.EstablishmentTypeGroupName, | ||
EstablishmentStatusCode = establishment.EstablishmentStatusCode, | ||
EstablishmentStatusName = establishment.EstablishmentStatusName, | ||
Street = establishment.Street, | ||
Locality = establishment.Locality, | ||
Address3 = establishment.Address3, | ||
Town = establishment.Town, | ||
County = establishment.County, | ||
Postcode = establishment.Postcode | ||
}); | ||
} | ||
else | ||
{ | ||
existingEstablishment.LaCode = establishment.LaCode; | ||
existingEstablishment.LaName = establishment.LaName; | ||
existingEstablishment.EstablishmentName = establishment.EstablishmentName; | ||
existingEstablishment.EstablishmentTypeCode = establishment.EstablishmentTypeCode; | ||
existingEstablishment.EstablishmentTypeName = establishment.EstablishmentTypeName; | ||
existingEstablishment.EstablishmentTypeGroupCode = establishment.EstablishmentTypeGroupCode; | ||
existingEstablishment.EstablishmentTypeGroupName = establishment.EstablishmentTypeGroupName; | ||
existingEstablishment.EstablishmentStatusCode = establishment.EstablishmentStatusCode; | ||
existingEstablishment.EstablishmentStatusName = establishment.EstablishmentStatusName; | ||
existingEstablishment.Street = establishment.Street; | ||
existingEstablishment.Locality = establishment.Locality; | ||
existingEstablishment.Address3 = establishment.Address3; | ||
existingEstablishment.Town = establishment.Town; | ||
existingEstablishment.County = establishment.County; | ||
existingEstablishment.Postcode = establishment.Postcode; | ||
} | ||
|
||
if (++i % 2000 == 0) | ||
{ | ||
await dbContext.SaveChangesAsync(cancellationToken); | ||
} | ||
} | ||
|
||
if (dbContext.ChangeTracker.HasChanges()) | ||
{ | ||
await dbContext.SaveChangesAsync(cancellationToken); | ||
} | ||
var refreshJobId = await backgroundJobScheduler.Enqueue<EstablishmentRefresher>(j => j.RefreshEstablishments(cancellationToken)); | ||
await backgroundJobScheduler.ContinueJobWith<TpsCsvExtractProcessor>(refreshJobId, j => j.UpdateLatestEstablishmentVersions(cancellationToken)); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...ordSystem/src/TeachingRecordSystem.Core/Services/Establishments/EstablishmentRefresher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using TeachingRecordSystem.Core.DataStore.Postgres; | ||
|
||
namespace TeachingRecordSystem.Core.Services.Establishments; | ||
|
||
public class EstablishmentRefresher( | ||
TrsDbContext dbContext, | ||
IEstablishmentMasterDataService establishmentMasterDataService) | ||
{ | ||
public async Task RefreshEstablishments(CancellationToken cancellationToken) | ||
{ | ||
int i = 0; | ||
await foreach (var establishment in establishmentMasterDataService.GetEstablishments()) | ||
{ | ||
var existingEstablishment = await dbContext.Establishments.SingleOrDefaultAsync(e => e.Urn == establishment.Urn); | ||
if (existingEstablishment == null) | ||
{ | ||
dbContext.Establishments.Add(new() | ||
{ | ||
EstablishmentId = Guid.NewGuid(), | ||
Urn = establishment.Urn, | ||
LaCode = establishment.LaCode, | ||
LaName = establishment.LaName, | ||
EstablishmentNumber = establishment.EstablishmentNumber, | ||
EstablishmentName = establishment.EstablishmentName, | ||
EstablishmentTypeCode = establishment.EstablishmentTypeCode, | ||
EstablishmentTypeName = establishment.EstablishmentTypeName, | ||
EstablishmentTypeGroupCode = establishment.EstablishmentTypeGroupCode, | ||
EstablishmentTypeGroupName = establishment.EstablishmentTypeGroupName, | ||
EstablishmentStatusCode = establishment.EstablishmentStatusCode, | ||
EstablishmentStatusName = establishment.EstablishmentStatusName, | ||
Street = establishment.Street, | ||
Locality = establishment.Locality, | ||
Address3 = establishment.Address3, | ||
Town = establishment.Town, | ||
County = establishment.County, | ||
Postcode = establishment.Postcode | ||
}); | ||
} | ||
else | ||
{ | ||
existingEstablishment.LaCode = establishment.LaCode; | ||
existingEstablishment.LaName = establishment.LaName; | ||
existingEstablishment.EstablishmentName = establishment.EstablishmentName; | ||
existingEstablishment.EstablishmentTypeCode = establishment.EstablishmentTypeCode; | ||
existingEstablishment.EstablishmentTypeName = establishment.EstablishmentTypeName; | ||
existingEstablishment.EstablishmentTypeGroupCode = establishment.EstablishmentTypeGroupCode; | ||
existingEstablishment.EstablishmentTypeGroupName = establishment.EstablishmentTypeGroupName; | ||
existingEstablishment.EstablishmentStatusCode = establishment.EstablishmentStatusCode; | ||
existingEstablishment.EstablishmentStatusName = establishment.EstablishmentStatusName; | ||
existingEstablishment.Street = establishment.Street; | ||
existingEstablishment.Locality = establishment.Locality; | ||
existingEstablishment.Address3 = establishment.Address3; | ||
existingEstablishment.Town = establishment.Town; | ||
existingEstablishment.County = establishment.County; | ||
existingEstablishment.Postcode = establishment.Postcode; | ||
} | ||
|
||
if (++i % 2000 == 0) | ||
{ | ||
await dbContext.SaveChangesAsync(cancellationToken); | ||
} | ||
} | ||
|
||
if (dbContext.ChangeTracker.HasChanges()) | ||
{ | ||
await dbContext.SaveChangesAsync(cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
.../TeachingRecordSystem.Core/Services/WorkforceData/UpdatedPersonEmploymentEstablishment.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace TeachingRecordSystem.Core.Services.WorkforceData; | ||
|
||
public record UpdatedPersonEmploymentEstablishment | ||
{ | ||
public required Guid PersonEmploymentId { get; init; } | ||
public required Guid PersonId { get; init; } | ||
public required Guid CurrentEstablishmentId { get; init; } | ||
public required DateOnly StartDate { get; init; } | ||
public required DateOnly? EndDate { get; init; } | ||
public required EmploymentType EmploymentType { get; init; } | ||
public required Guid EstablishmentId { get; init; } | ||
} |
Oops, something went wrong.