diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/HostApplicationBuilderExtensions.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/HostApplicationBuilderExtensions.cs index 2979af0c3..abeaac137 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/HostApplicationBuilderExtensions.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/HostApplicationBuilderExtensions.cs @@ -80,8 +80,13 @@ public static IHostApplicationBuilder AddBackgroundJobs(this IHostApplicationBui recurringJobManager.RemoveIfExists("MopUpQtsAwardeesJob"); - recurringJobManager.AddOrUpdate( - nameof(SyncAllContactsFromCrmJob), + recurringJobManager.AddOrUpdate( + nameof(SyncAllPersonsFromCrmJob), + job => job.Execute(CancellationToken.None), + Cron.Never); + + recurringJobManager.AddOrUpdate( + nameof(SyncAllMqsFromCrmJob), job => job.Execute(CancellationToken.None), Cron.Never); diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/SyncAllMqsFromCrmJob.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/SyncAllMqsFromCrmJob.cs new file mode 100644 index 000000000..99e56fb77 --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/SyncAllMqsFromCrmJob.cs @@ -0,0 +1,76 @@ +using Microsoft.Extensions.Options; +using Microsoft.Xrm.Sdk.Query; +using TeachingRecordSystem.Core.Dqt; +using TeachingRecordSystem.Core.Services.TrsDataSync; + +namespace TeachingRecordSystem.Core.Jobs; + +public class SyncAllMqsFromCrmJob +{ + private readonly ICrmServiceClientProvider _crmServiceClientProvider; + private readonly TrsDataSyncHelper _trsDataSyncHelper; + private readonly IOptions _syncOptionsAccessor; + + public SyncAllMqsFromCrmJob( + ICrmServiceClientProvider crmServiceClientProvider, + TrsDataSyncHelper trsDataSyncHelper, + IOptions syncOptionsAccessor) + { + _crmServiceClientProvider = crmServiceClientProvider; + _trsDataSyncHelper = trsDataSyncHelper; + _syncOptionsAccessor = syncOptionsAccessor; + } + + public async Task Execute(CancellationToken cancellationToken) + { + const int pageSize = 1000; + + var serviceClient = _crmServiceClientProvider.GetClient(TrsDataSyncService.CrmClientName); + var columns = new ColumnSet(TrsDataSyncHelper.GetEntityInfoForModelType(TrsDataSyncHelper.ModelTypes.MandatoryQualification).AttributeNames); + + var filter = new FilterExpression(LogicalOperator.And) + { + Conditions = + { + new ConditionExpression(dfeta_qualification.Fields.dfeta_Type, ConditionOperator.Equal, (int)dfeta_qualification_dfeta_Type.MandatoryQualification) + } + }; + + var query = new QueryExpression(dfeta_qualification.EntityLogicalName) + { + ColumnSet = columns, + Criteria = filter, + Orders = + { + new OrderExpression(dfeta_qualification.Fields.CreatedOn, OrderType.Ascending) + }, + PageInfo = new PagingInfo() + { + Count = pageSize, + PageNumber = 1 + } + }; + + while (true) + { + cancellationToken.ThrowIfCancellationRequested(); + + var result = await serviceClient.RetrieveMultipleAsync(query); + + await _trsDataSyncHelper.SyncMandatoryQualifications( + result.Entities.Select(e => e.ToEntity()).ToArray(), + ignoreInvalid: _syncOptionsAccessor.Value.IgnoreInvalidData, + cancellationToken); + + if (result.MoreRecords) + { + query.PageInfo.PageNumber++; + query.PageInfo.PagingCookie = result.PagingCookie; + } + else + { + break; + } + } + } +} diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/SyncAllContactsFromCrmJob.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/SyncAllPersonsFromCrmJob.cs similarity index 94% rename from TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/SyncAllContactsFromCrmJob.cs rename to TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/SyncAllPersonsFromCrmJob.cs index 750e0e409..899b2446e 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/SyncAllContactsFromCrmJob.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/SyncAllPersonsFromCrmJob.cs @@ -5,13 +5,13 @@ namespace TeachingRecordSystem.Core.Jobs; -public class SyncAllContactsFromCrmJob +public class SyncAllPersonsFromCrmJob { private readonly ICrmServiceClientProvider _crmServiceClientProvider; private readonly TrsDataSyncHelper _trsDataSyncHelper; private readonly IOptions _syncOptionsAccessor; - public SyncAllContactsFromCrmJob( + public SyncAllPersonsFromCrmJob( ICrmServiceClientProvider crmServiceClientProvider, TrsDataSyncHelper trsDataSyncHelper, IOptions syncOptionsAccessor) @@ -26,7 +26,7 @@ public async Task Execute(CancellationToken cancellationToken) const int pageSize = 1000; var serviceClient = _crmServiceClientProvider.GetClient(TrsDataSyncService.CrmClientName); - var columns = new ColumnSet(TrsDataSyncHelper.GetEntityInfoForModelType(Contact.EntityLogicalName).AttributeNames); + var columns = new ColumnSet(TrsDataSyncHelper.GetEntityInfoForModelType(TrsDataSyncHelper.ModelTypes.Person).AttributeNames); // Ensure this is kept in sync with the predicate in TrsDataSyncHelper.SyncContacts var filter = new FilterExpression(LogicalOperator.And)