diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Queries/CreateTrnRequestTaskQuery.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Dqt/Queries/CreateTrnRequestTaskQuery.cs index 58bc496e1f..8f9eb59d21 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 0000000000..7060946bc9 --- /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 5da0902cd5..c3bdb6bb93 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 }; 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 0000000000..9abecd67a5 --- /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 0231582b8e..96f36d9ed2 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 8026054347..8650e34c9a 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; });