Skip to content

Commit

Permalink
fix: or-1759 possible solution for naamswijziging afdeling
Browse files Browse the repository at this point in the history
  • Loading branch information
koenmetsu committed Nov 14, 2023
1 parent 3902a7b commit 7c25c4b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,7 @@ public async Task Handle(EventEnvelope<VerenigingMetRechtspersoonlijkheidWerdGer

public async Task Handle(EventEnvelope<NaamWerdGewijzigd> message)
{
await _elasticRepository.UpdateAsync(
message.Data.VCode,
new VerenigingZoekDocument
{
Naam = message.Data.Naam,
}
);
await _elasticRepository.UpdateNaamInRelaties(
new VerenigingZoekDocument
{
VCode = message.Data.VCode,
Naam = message.Data.Naam,
}
);
await _elasticRepository.WijzigNaamAfdeling(message.VCode, message.Data.Naam);
}

public async Task Handle(EventEnvelope<RoepnaamWerdGewijzigd> message)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace AssociationRegistry.Public.ProjectionHost.Projections.Search;

using Elasticsearch.Net;
using Nest;
using Schema.Search;
using Vereniging;

public class ElasticRepository : IElasticRepository
{
Expand Down Expand Up @@ -128,20 +130,20 @@ public async Task UpdateNaamInRelaties(VerenigingZoekDocument documentToUpdate)
.Nested(n => n
.Path(document => document.Relaties)
.Query(nq => nq
.Nested(n => n
.Path(doc => doc.Relaties.First()
.AndereVereniging)
.Query(nq2 => nq2
.Term(m => m
.Field(
doc => doc
.Relaties
.First()
.AndereVereniging
.VCode)
.Value(
documentToUpdate
.VCode)))))
.Nested(n => n
.Path(doc => doc.Relaties.First()
.AndereVereniging)
.Query(nq2 => nq2
.Term(m => m
.Field(
doc => doc
.Relaties
.First()
.AndereVereniging
.VCode)
.Value(
documentToUpdate
.VCode)))))
)
));

Expand Down Expand Up @@ -172,4 +174,42 @@ public async Task UpdateNaamInRelaties(VerenigingZoekDocument documentToUpdate)
if (!response.IsValid)
throw new IndexDocumentFailed(response.DebugInformation);
}

public async Task WijzigNaamAfdeling(string vCode, string nieuweNaam)
{
var afdeling = _elasticClient.Get<VerenigingZoekDocument>(vCode);

if (afdeling.Source.Type.Code == Verenigingstype.Afdeling.Code)
{
var bulkResponse = await _elasticClient.BulkAsync(b => b
// Update the Association's name and perform version check
.Update<VerenigingZoekDocument>(u => u
.Id(vCode)
.IfSequenceNumber(afdeling.SequenceNumber)
.IfPrimaryTerm(afdeling.PrimaryTerm)
.Doc(new VerenigingZoekDocument
{ Naam = nieuweNaam }))
// Update the Mother Association's relation's name and perform version check
.Update<VerenigingZoekDocument>(u => u
.Id(afdeling.Source.Relaties.First().AndereVereniging.VCode)
.ScriptedUpsert()
.Script(s => s
.Source(
"for (relatie in ctx._source.relaties) { if (relatie.andereVereniging.vCode == params.associationId) { relatie.andereVereniging.naam = params.newName; } }")
.Params(p => p
.Add(key: "newName", nieuweNaam)
.Add(key: "associationId", vCode))))
.Refresh(Refresh.True));
}
else
{
await UpdateAsync(
vCode,
new VerenigingZoekDocument
{
Naam = nieuweNaam,
}
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ Task IndexAsync<TDocument>(TDocument document)
Task Remove(string id);
Task AppendRelatie(string id, Relatie relatie);
Task UpdateNaamInRelaties(VerenigingZoekDocument documentToUpdate);
Task WijzigNaamAfdeling(string vCode, string nieuweNaam);
}

0 comments on commit 7c25c4b

Please sign in to comment.