11using OneOf ;
22using System . Security . Claims ;
3+ using Altinn . Correspondence . Application . InitializeCorrespondences ;
34using Altinn . Correspondence . Application . PublishCorrespondence ;
5+ using Altinn . Correspondence . Core . Models . Enums ;
6+ using Altinn . Correspondence . Core . Repositories ;
47using Hangfire ;
58using Microsoft . Extensions . Logging ;
69
710namespace Altinn . Correspondence . Application . ManualRetryNotPublishedCorrespondences ;
811
912public class ManualRetryNotPublishedCorrespondencesHandler (
1013 IBackgroundJobClient backgroundJobClient ,
14+ ICorrespondenceRepository correspondenceRepository ,
1115 ILogger < ManualRetryNotPublishedCorrespondencesHandler > logger ) : IHandler < ManualRetryNotPublishedCorrespondencesRequest , ManualRetryNotPublishedCorrespondencesResponse >
1216
1317{
@@ -17,8 +21,29 @@ public async Task<OneOf<ManualRetryNotPublishedCorrespondencesResponse, Error>>
1721 foreach ( var correspondenceId in request . CorrespondenceIds )
1822 {
1923 logger . LogInformation ( "Retrying publish for correspondence {CorrespondenceId}" , correspondenceId ) ;
20- var jobId = backgroundJobClient . Enqueue < PublishCorrespondenceHandler > ( handler => handler . Process ( correspondenceId , null , CancellationToken . None ) ) ;
21- response . RetriedCorrespondenceIds [ correspondenceId ] = $ "Enqueued (jobId: { jobId } )";
24+
25+ var correspondence = await correspondenceRepository . GetCorrespondenceById ( correspondenceId , false , false , false , cancellationToken ) ;
26+ if ( correspondence is null )
27+ {
28+ response . RetriedCorrespondenceIds [ correspondenceId ] = "Not found" ;
29+ continue ;
30+ }
31+
32+ var hasDialogportenReference = correspondence . ExternalReferences
33+ . Any ( r => r . ReferenceType == ReferenceType . DialogportenDialogId ) ;
34+
35+ string jobId ;
36+ if ( ! hasDialogportenReference )
37+ {
38+ var dialogJobId = backgroundJobClient . Enqueue < InitializeCorrespondencesHandler > ( h => h . CreateDialogportenDialog ( correspondenceId ) ) ;
39+ jobId = backgroundJobClient . ContinueJobWith < PublishCorrespondenceHandler > ( dialogJobId , handler => handler . Process ( correspondenceId , null , CancellationToken . None ) ) ;
40+ response . RetriedCorrespondenceIds [ correspondenceId ] = $ "Enqueued create dialog then publish (jobId: { jobId } )";
41+ }
42+ else
43+ {
44+ jobId = backgroundJobClient . Enqueue < PublishCorrespondenceHandler > ( handler => handler . Process ( correspondenceId , null , CancellationToken . None ) ) ;
45+ response . RetriedCorrespondenceIds [ correspondenceId ] = $ "Enqueued (jobId: { jobId } )";
46+ }
2247 }
2348 return response ;
2449 }
0 commit comments