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

Add a job to sync all MQs #1059

Merged
merged 1 commit into from
Jan 5, 2024
Merged
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 @@ -80,8 +80,13 @@ public static IHostApplicationBuilder AddBackgroundJobs(this IHostApplicationBui

recurringJobManager.RemoveIfExists("MopUpQtsAwardeesJob");

recurringJobManager.AddOrUpdate<SyncAllContactsFromCrmJob>(
nameof(SyncAllContactsFromCrmJob),
recurringJobManager.AddOrUpdate<SyncAllPersonsFromCrmJob>(
nameof(SyncAllPersonsFromCrmJob),
job => job.Execute(CancellationToken.None),
Cron.Never);

recurringJobManager.AddOrUpdate<SyncAllMqsFromCrmJob>(
nameof(SyncAllMqsFromCrmJob),
job => job.Execute(CancellationToken.None),
Cron.Never);

Expand Down
Original file line number Diff line number Diff line change
@@ -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<TrsDataSyncServiceOptions> _syncOptionsAccessor;

public SyncAllMqsFromCrmJob(
ICrmServiceClientProvider crmServiceClientProvider,
TrsDataSyncHelper trsDataSyncHelper,
IOptions<TrsDataSyncServiceOptions> 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<dfeta_qualification>()).ToArray(),
ignoreInvalid: _syncOptionsAccessor.Value.IgnoreInvalidData,
cancellationToken);

if (result.MoreRecords)
{
query.PageInfo.PageNumber++;
query.PageInfo.PagingCookie = result.PagingCookie;
}
else
{
break;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<TrsDataSyncServiceOptions> _syncOptionsAccessor;

public SyncAllContactsFromCrmJob(
public SyncAllPersonsFromCrmJob(
ICrmServiceClientProvider crmServiceClientProvider,
TrsDataSyncHelper trsDataSyncHelper,
IOptions<TrsDataSyncServiceOptions> syncOptionsAccessor)
Expand All @@ -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)
Expand Down
Loading