Skip to content

Commit 9ecf9b0

Browse files
authored
Deletion fix when deleting Besluiten or Zaken. Will not audit informationobjecten but notify for DRC that they were deleted (#117)
FUND-2136 Deletion fix when deleting Besluiten or Zaken. Will not audit informationobjecten but notify for DRC that they were deleted
1 parent 842953b commit 9ecf9b0

File tree

2 files changed

+34
-46
lines changed

2 files changed

+34
-46
lines changed

src/OneGround.ZGW.Besluiten.Web/Handlers/v1/DeleteBesluitCommandHandler.cs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -7,12 +8,10 @@
78
using Microsoft.Extensions.Configuration;
89
using Microsoft.Extensions.Logging;
910
using OneGround.ZGW.Besluiten.DataModel;
10-
using OneGround.ZGW.Besluiten.ServiceAgent.v1;
1111
using OneGround.ZGW.Besluiten.Web.Authorization;
1212
using OneGround.ZGW.Besluiten.Web.Notificaties;
1313
using OneGround.ZGW.Common.Constants;
1414
using OneGround.ZGW.Common.Handlers;
15-
using OneGround.ZGW.Common.ServiceAgent.Extensions;
1615
using OneGround.ZGW.Common.Web.Authorization;
1716
using OneGround.ZGW.Common.Web.Services;
1817
using OneGround.ZGW.Common.Web.Services.AuditTrail;
@@ -26,7 +25,6 @@ class DeleteBesluitCommandHandler : BesluitenBaseHandler<DeleteBesluitCommandHan
2625
private readonly BrcDbContext _context;
2726
private readonly IAuditTrailFactory _auditTrailFactory;
2827
private readonly IZakenServiceAgent _zakenServiceAgent;
29-
private readonly IBesluitenServiceAgent _besluitenServiceAgent;
3028

3129
public DeleteBesluitCommandHandler(
3230
ILogger<DeleteBesluitCommandHandler> logger,
@@ -36,7 +34,6 @@ public DeleteBesluitCommandHandler(
3634
IAuditTrailFactory auditTrailFactory,
3735
IEntityUriService uriService,
3836
IZakenServiceAgent zakenServiceAgent,
39-
IBesluitenServiceAgent besluitenServiceAgent,
4037
IAuthorizationContextAccessor authorizationContextAccessor,
4138
IBesluitKenmerkenResolver besluitKenmerkenResolver
4239
)
@@ -45,7 +42,6 @@ IBesluitKenmerkenResolver besluitKenmerkenResolver
4542
_context = context;
4643
_auditTrailFactory = auditTrailFactory;
4744
_zakenServiceAgent = zakenServiceAgent;
48-
_besluitenServiceAgent = besluitenServiceAgent;
4945
}
5046

5147
public async Task<CommandResult> Handle(DeleteBesluitCommand request, CancellationToken cancellationToken)
@@ -71,8 +67,7 @@ public async Task<CommandResult> Handle(DeleteBesluitCommand request, Cancellati
7167

7268
using (var audittrail = _auditTrailFactory.Create(AuditTrailOptions))
7369
{
74-
// Note: This also implies: Vernietigen van besluiten (brc-008)
75-
await DeleteAndSyncBesluitInformatieObjectenAsync(besluit, cancellationToken);
70+
RemoveBesluitInformatieObject(besluit);
7671

7772
_logger.LogDebug("Deleting Besluit {Id}....", besluit.Id);
7873

@@ -95,29 +90,29 @@ public async Task<CommandResult> Handle(DeleteBesluitCommand request, Cancellati
9590
await _zakenServiceAgent.DeleteZaakBesluitByUrlAsync(besluit.ZaakBesluitUrl);
9691
}
9792

93+
await SendInformationObjectNotificationAsync(besluit, cancellationToken);
9894
await SendNotificationAsync(Actie.destroy, besluit, cancellationToken);
9995

10096
return new CommandResult(CommandStatus.OK);
10197
}
10298

103-
private async Task DeleteAndSyncBesluitInformatieObjectenAsync(Besluit besluit, CancellationToken cancellationToken)
99+
private void RemoveBesluitInformatieObject(Besluit besluit)
104100
{
105-
// Note: Before deleting the besluit, delete all related besluitinformatieobjecten from BRC via the BesluitenServiceAgent!
106-
// Doing so triggers the synchronization with DRC (the DocumentListener notificatie-receiver deletes the mirrored relation-ships)
101+
_logger.LogDebug("Deleting {BesluitInformatieObject} from besluit {Id}....", nameof(BesluitInformatieObject), besluit.Id);
107102
foreach (var besluitInformatieObject in besluit.BesluitInformatieObjecten)
108103
{
109-
_logger.LogDebug("Deleting and synchronizing BesluitInformatieObject {Id}....", besluitInformatieObject.Id);
110-
111-
var result = await _besluitenServiceAgent.DeleteBesluitInformatieObjectByIdAsync(besluitInformatieObject.Id);
112-
if (!result.Success)
113-
{
114-
var errors = result.GetErrorsFromResponse();
115-
_logger.LogError(
116-
"Failed to delete besluitinformatieobject {besluitInformatieObjectUrl}. {errors}",
117-
besluitInformatieObject.Url,
118-
errors
119-
);
120-
}
104+
_context.BesluitInformatieObjecten.Remove(besluitInformatieObject);
105+
}
106+
}
107+
108+
private async Task SendInformationObjectNotificationAsync(Besluit besluit, CancellationToken cancellationToken)
109+
{
110+
const string besluitinformatieobjectKenmerken = "besluitinformatieobject.informatieobject";
111+
foreach (var besluitInformatieObject in besluit.BesluitInformatieObjecten)
112+
{
113+
var extraKenmerken = new Dictionary<string, string> { { besluitinformatieobjectKenmerken, besluitInformatieObject.InformatieObject } };
114+
115+
await SendNotificationAsync(Actie.destroy, besluitInformatieObject, extraKenmerken, cancellationToken);
121116
}
122117
}
123118

src/OneGround.ZGW.Zaken.Web/Handlers/v1/DeleteZaakCommandHandler.cs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
using Microsoft.Extensions.Logging;
1010
using OneGround.ZGW.Common.Constants;
1111
using OneGround.ZGW.Common.Handlers;
12-
using OneGround.ZGW.Common.ServiceAgent.Extensions;
1312
using OneGround.ZGW.Common.Web.Authorization;
1413
using OneGround.ZGW.Common.Web.Services;
1514
using OneGround.ZGW.Common.Web.Services.AuditTrail;
1615
using OneGround.ZGW.Common.Web.Services.UriServices;
1716
using OneGround.ZGW.Zaken.DataModel;
18-
using OneGround.ZGW.Zaken.ServiceAgent.v1;
1917
using OneGround.ZGW.Zaken.Web.Authorization;
2018
using OneGround.ZGW.Zaken.Web.Notificaties;
2119

@@ -24,15 +22,13 @@ namespace OneGround.ZGW.Zaken.Web.Handlers.v1;
2422
class 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

Comments
 (0)