Skip to content

Commit

Permalink
Add a job to sync all MQs (#1059)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad authored Jan 5, 2024
1 parent 4901662 commit 8043957
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
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

0 comments on commit 8043957

Please sign in to comment.