Skip to content

Commit

Permalink
Extend attachment deletion job to cover TRN request tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
gunndabad committed Aug 21, 2024
1 parent 39f7249 commit 4744c0c
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace TeachingRecordSystem.Core.Dqt.Queries;

public record CreateTrnRequestTaskQuery : ICrmQuery<Guid>
{
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; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Microsoft.Xrm.Sdk.Query;

namespace TeachingRecordSystem.Core.Dqt.Queries;

public record GetNonOpenTaskAnnotationsQuery(string[] Subjects, DateTime ModifiedBefore, ColumnSet ColumnSet) : ICrmQuery<Annotation[]>;
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public async Task<Guid> 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
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GetNonOpenTaskAnnotationsQuery, Annotation[]>
{
public async Task<Annotation[]> 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<object>().ToArray());
queryExpression.LinkEntities.Add(taskLink);

var request = new RetrieveMultipleRequest()
{
Query = queryExpression
};

var response = await organizationService.RetrieveMultipleAsync(queryExpression);

return response.Entities.Select(e => e.ToEntity<Annotation>()).ToArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 * * *";

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ public static IHostApplicationBuilder AddBackgroundJobs(this IHostApplicationBui
job => job.ExecuteAsync(CancellationToken.None),
Cron.Never);

recurringJobManager.AddOrUpdate<DeleteOldIncidentAttachmentsJob>(
nameof(DeleteOldIncidentAttachmentsJob),
recurringJobManager.AddOrUpdate<DeleteOldAttachmentsJob>(
nameof(DeleteOldAttachmentsJob),
job => job.Execute(CancellationToken.None),
DeleteOldIncidentAttachmentsJob.JobSchedule);
DeleteOldAttachmentsJob.JobSchedule);

return Task.CompletedTask;
});
Expand Down

0 comments on commit 4744c0c

Please sign in to comment.