From 4744c0c6c522fc14c0233c63005b68c661dfb480 Mon Sep 17 00:00:00 2001 From: James Gunn Date: Thu, 4 Jul 2024 11:23:02 +0100 Subject: [PATCH] Extend attachment deletion job to cover TRN request tasks --- .../Dqt/Queries/CreateTrnRequestTaskQuery.cs | 2 + .../Queries/GetNonOpenTaskAnnotationsQuery.cs | 5 +++ .../CreateTrnRequestTaskHandler.cs | 2 +- .../GetNonOpenTaskAnnotationsHandler.cs | 38 +++++++++++++++++++ .../Jobs/DeleteOldIncidentAttachmentsJob.cs | 15 +++++--- .../Jobs/HostApplicationBuilderExtensions.cs | 6 +-- 6 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Queries/GetNonOpenTaskAnnotationsQuery.cs create mode 100644 TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/GetNonOpenTaskAnnotationsHandler.cs diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Queries/CreateTrnRequestTaskQuery.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Queries/CreateTrnRequestTaskQuery.cs index 04702d25b..e3dcb2137 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Queries/CreateTrnRequestTaskQuery.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Queries/CreateTrnRequestTaskQuery.cs @@ -2,6 +2,8 @@ namespace TeachingRecordSystem.Core.Dqt.Queries; public record CreateTrnRequestTaskQuery : ICrmQuery { + public const string TaskSubject = "Notification for TRA Support Team - TRN request"; + public required string Description { get; init; } public required string EvidenceFileName { get; init; } public required Stream EvidenceFileContent { get; init; } diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Queries/GetNonOpenTaskAnnotationsQuery.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Queries/GetNonOpenTaskAnnotationsQuery.cs new file mode 100644 index 000000000..7060946bc --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Queries/GetNonOpenTaskAnnotationsQuery.cs @@ -0,0 +1,5 @@ +using Microsoft.Xrm.Sdk.Query; + +namespace TeachingRecordSystem.Core.Dqt.Queries; + +public record GetNonOpenTaskAnnotationsQuery(string[] Subjects, DateTime ModifiedBefore, ColumnSet ColumnSet) : ICrmQuery; diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/CreateTrnRequestTaskHandler.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/CreateTrnRequestTaskHandler.cs index f1fd50735..f28757a94 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/CreateTrnRequestTaskHandler.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/CreateTrnRequestTaskHandler.cs @@ -11,7 +11,7 @@ public async Task Execute(CreateTrnRequestTaskQuery query, IOrganizationSe var crmTask = new CrmTask() { Id = Guid.NewGuid(), - Subject = "Notification for TRA Support Team - TRN request", + Subject = CreateTrnRequestTaskQuery.TaskSubject, Description = query.Description, dfeta_EmailAddress = query.EmailAddress }; diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/GetNonOpenTaskAnnotationsHandler.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/GetNonOpenTaskAnnotationsHandler.cs new file mode 100644 index 000000000..9abecd67a --- /dev/null +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/QueryHandlers/GetNonOpenTaskAnnotationsHandler.cs @@ -0,0 +1,38 @@ +using Microsoft.PowerPlatform.Dataverse.Client; +using Microsoft.Xrm.Sdk.Messages; +using Microsoft.Xrm.Sdk.Query; +using TeachingRecordSystem.Core.Dqt.Queries; + +namespace TeachingRecordSystem.Core.Dqt.QueryHandlers; + +public class GetNonOpenTaskAnnotationsHandler : ICrmQueryHandler +{ + public async Task Execute(GetNonOpenTaskAnnotationsQuery query, IOrganizationServiceAsync organizationService) + { + var queryExpression = new QueryExpression() + { + EntityName = Annotation.EntityLogicalName, + ColumnSet = query.ColumnSet + }; + + var taskLink = new LinkEntity( + Annotation.EntityLogicalName, + CrmTask.EntityLogicalName, + Annotation.Fields.ObjectId, + CrmTask.PrimaryIdAttribute, + JoinOperator.Inner); + taskLink.LinkCriteria.AddCondition(CrmTask.Fields.StateCode, ConditionOperator.NotEqual, (int)TaskState.Open); + taskLink.LinkCriteria.AddCondition(CrmTask.Fields.ModifiedOn, ConditionOperator.LessThan, query.ModifiedBefore); + taskLink.LinkCriteria.AddCondition(CrmTask.Fields.Subject, ConditionOperator.In, query.Subjects.Cast().ToArray()); + queryExpression.LinkEntities.Add(taskLink); + + var request = new RetrieveMultipleRequest() + { + Query = queryExpression + }; + + var response = await organizationService.RetrieveMultipleAsync(queryExpression); + + return response.Entities.Select(e => e.ToEntity()).ToArray(); + } +} diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/DeleteOldIncidentAttachmentsJob.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/DeleteOldIncidentAttachmentsJob.cs index 0231582b8..96f36d9ed 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/DeleteOldIncidentAttachmentsJob.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/DeleteOldIncidentAttachmentsJob.cs @@ -3,7 +3,7 @@ namespace TeachingRecordSystem.Core.Jobs; -public class DeleteOldIncidentAttachmentsJob(ICrmQueryDispatcher crmQueryDispatcher, ReferenceDataCache referenceDataCache, IClock clock) +public class DeleteOldAttachmentsJob(ICrmQueryDispatcher crmQueryDispatcher, ReferenceDataCache referenceDataCache, IClock clock) { public const string JobSchedule = "0 3 * * *"; @@ -16,19 +16,24 @@ public async Task Execute(CancellationToken cancellationToken) var modifiedBefore = clock.UtcNow.Subtract(_modifiedBeforeWindow); - var annotations = await crmQueryDispatcher.ExecuteQuery( + var incidentAnnotations = await crmQueryDispatcher.ExecuteQuery( new GetResolvedIncidentAnnotationsQuery(SubjectIds: [changeDateOfBirthSubject.Id, changeNameSubject.Id], modifiedBefore, ColumnSet: new())); - foreach (var annotation in annotations) + var taskAnnotations = await crmQueryDispatcher.ExecuteQuery( + new GetNonOpenTaskAnnotationsQuery(Subjects: [CreateTrnRequestTaskQuery.TaskSubject], modifiedBefore, ColumnSet: new())); + + var annotationIds = incidentAnnotations.Select(i => i.AnnotationId!.Value).Concat(taskAnnotations.Select(i => i.AnnotationId!.Value)); + + foreach (var annotationId in annotationIds) { cancellationToken.ThrowIfCancellationRequested(); await crmQueryDispatcher.ExecuteQuery( new DeleteAnnotationQuery( - annotation.Id, + annotationId, Event: EventInfo.Create(new DqtAnnotationDeletedEvent() { - AnnotationId = annotation.Id, + AnnotationId = annotationId, CreatedUtc = clock.UtcNow, EventId = Guid.NewGuid(), RaisedBy = DataStore.Postgres.Models.SystemUser.SystemUserId diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/HostApplicationBuilderExtensions.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/HostApplicationBuilderExtensions.cs index 802605434..8650e34c9 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/HostApplicationBuilderExtensions.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/HostApplicationBuilderExtensions.cs @@ -128,10 +128,10 @@ public static IHostApplicationBuilder AddBackgroundJobs(this IHostApplicationBui job => job.ExecuteAsync(CancellationToken.None), Cron.Never); - recurringJobManager.AddOrUpdate( - nameof(DeleteOldIncidentAttachmentsJob), + recurringJobManager.AddOrUpdate( + nameof(DeleteOldAttachmentsJob), job => job.Execute(CancellationToken.None), - DeleteOldIncidentAttachmentsJob.JobSchedule); + DeleteOldAttachmentsJob.JobSchedule); return Task.CompletedTask; });