99using Microsoft . Extensions . Logging ;
1010using OneGround . ZGW . Common . Constants ;
1111using OneGround . ZGW . Common . Handlers ;
12- using OneGround . ZGW . Common . ServiceAgent . Extensions ;
1312using OneGround . ZGW . Common . Web . Authorization ;
1413using OneGround . ZGW . Common . Web . Services ;
1514using OneGround . ZGW . Common . Web . Services . AuditTrail ;
1615using OneGround . ZGW . Common . Web . Services . UriServices ;
1716using OneGround . ZGW . Zaken . DataModel ;
18- using OneGround . ZGW . Zaken . ServiceAgent . v1 ;
1917using OneGround . ZGW . Zaken . Web . Authorization ;
2018using OneGround . ZGW . Zaken . Web . Notificaties ;
2119
@@ -24,15 +22,13 @@ namespace OneGround.ZGW.Zaken.Web.Handlers.v1;
2422class DeleteZaakCommandHandler : ZakenBaseHandler < DeleteZaakCommandHandler > , IRequestHandler < DeleteZaakCommand , CommandResult >
2523{
2624 private readonly ZrcDbContext _context ;
27- private readonly IZakenServiceAgent _zakenServiceAgent ;
2825 private readonly IAuditTrailFactory _auditTrailFactory ;
2926
3027 public DeleteZaakCommandHandler (
3128 ILogger < DeleteZaakCommandHandler > logger ,
3229 IConfiguration configuration ,
3330 ZrcDbContext context ,
3431 IEntityUriService uriService ,
35- IZakenServiceAgent zakenServiceAgent ,
3632 IAuditTrailFactory auditTrailFactory ,
3733 IAuthorizationContextAccessor authorizationContextAccessor ,
3834 INotificatieService notificatieService ,
@@ -41,7 +37,6 @@ IZaakKenmerkenResolver zaakKenmerkenResolver
4137 : base ( logger , configuration , authorizationContextAccessor , uriService , notificatieService , zaakKenmerkenResolver )
4238 {
4339 _context = context ;
44- _zakenServiceAgent = zakenServiceAgent ;
4540 _auditTrailFactory = auditTrailFactory ;
4641 }
4742
@@ -55,12 +50,6 @@ public async Task<CommandResult> Handle(DeleteZaakCommand request, CancellationT
5550 return new CommandResult ( CommandStatus . NotFound ) ;
5651 }
5752
58- // Synchroniseren relaties met informatieobjecten (zrc-005)
59- foreach ( var zaak in zakenAndDeelZaken )
60- {
61- await DeleteAndSyncZaakInformatieObjectenAsync ( zaak , cancellationToken ) ;
62- }
63-
6453 foreach ( var zaak in zakenAndDeelZaken )
6554 {
6655 DeleteZaak ( zaak ) ; // Including audittrail with all details!
@@ -74,9 +63,11 @@ public async Task<CommandResult> Handle(DeleteZaakCommand request, CancellationT
7463 // Note: After successfull commit
7564 foreach ( var zaak in zakenAndDeelZaken )
7665 {
66+ await SendInformationObjectNotificationAsync ( zaak , cancellationToken ) ;
7767 await SendNotificationAsync ( Actie . destroy , zaak , cancellationToken ) ;
7868 }
7969 }
70+
8071 return new CommandResult ( CommandStatus . OK ) ;
8172 }
8273
@@ -118,28 +109,20 @@ private CommandResult DeleteZaak(Zaak zaak)
118109 return new CommandResult ( CommandStatus . Forbidden ) ;
119110 }
120111
112+ RemoveZaakInformatieObjecten ( zaak ) ;
121113 RemoveZaak ( zaak ) ;
122114 RemoveAudit ( zaak ) ;
123115
124116 return new CommandResult ( CommandStatus . OK ) ;
125117 }
126118
127- private async Task DeleteAndSyncZaakInformatieObjectenAsync ( Zaak zaak , CancellationToken cancellationToken )
119+ private void RemoveZaakInformatieObjecten ( Zaak zaak )
128120 {
129- // Note: Before deleting the zaak, delete all related zaakinformatieobjecten from ZRC via the ZakenServiceAgent!
130- // Doing so triggers the synchronization with DRC (the DocumentListener notificatie-receiver deletes the mirrored relation-ships)
131- foreach ( var zaakInformatieObject in zaak . ZaakInformatieObjecten )
132- {
133- _logger . LogDebug ( "Deleting and synchronizing ZaakInformatieObject {Id}...." , zaakInformatieObject . Id ) ;
121+ _logger . LogDebug ( "Deleting ZaakInformatieObjecten from zaak {Id}...." , zaak . Id ) ;
134122
135- var result = await _zakenServiceAgent . DeleteZaakInformatieObjectByIdAsync ( zaakInformatieObject . Id ) ;
136- if ( ! result . Success )
137- {
138- var errors = result . GetErrorsFromResponse ( ) ;
123+ var zaakInformatieObjecten = _context . ZaakInformatieObjecten . Where ( a => a . ZaakId == zaak . Id ) ;
139124
140- _logger . LogError ( "Failed to delete zaakinformatieobject {zaakInformatieObjectUrl}. {errors}" , zaakInformatieObject . Url , errors ) ;
141- }
142- }
125+ _context . ZaakInformatieObjecten . RemoveRange ( zaakInformatieObjecten ) ;
143126 }
144127
145128 private void RemoveZaak ( Zaak zaak )
@@ -158,6 +141,16 @@ private void RemoveAudit(Zaak zaak)
158141 _context . AuditTrailRegels . RemoveRange ( zaakAuditTrails ) ;
159142 }
160143
144+ private async Task SendInformationObjectNotificationAsync ( Zaak zaak , CancellationToken cancellationToken )
145+ {
146+ const string zaakInformatieObjectKenmerken = "zaakinformatieobject.informatieobject" ;
147+ foreach ( var zaakInformatieObject in zaak . ZaakInformatieObjecten )
148+ {
149+ var extraKenmerken = new Dictionary < string , string > { { zaakInformatieObjectKenmerken , zaakInformatieObject . InformatieObject } } ;
150+ await SendNotificationAsync ( Actie . destroy , zaakInformatieObject , extraKenmerken , cancellationToken ) ;
151+ }
152+ }
153+
161154 private static AuditTrailOptions AuditTrailOptions => new AuditTrailOptions { Bron = ServiceRoleName . ZRC , Resource = "zaak" } ;
162155}
163156
0 commit comments